fastest-validator

The fastest JS validator library for NodeJS

MIT License

Downloads
278.5K
Stars
1.4K
Committers
48

Bot releases are hidden (Show)

fastest-validator - v1.4.0

Published by icebob over 4 years ago

New custom function signature

Thanks for @erfanium, in this version there is a new signature of custom check functions.
In this new function you should always return the value. It means you can change the value, thus you can also sanitize the input value.

Old custom function:

const v = new Validator({});

const schema = {
    weight: {
        type: "custom",
        minWeight: 10,
        check(value, schema) {
            return (value < schema.minWeight)
                ? [{ type: "weightMin", expected: schema.minWeight, actual: value }]
                : true;
        }
    }
};

New custom function:

const v = new Validator({
    useNewCustomCheckerFunction: true, // using new version
});

const schema = {
    name: { type: "string", min: 3, max: 255 },
    weight: {
        type: "custom",
        minWeight: 10,
        check(value, errors, schema) {
            if (value < minWeight) errors.push({ type: "weightMin", expected: schema.minWeight, actual: value });
            if (value > 100) value = 100
            return value
        }
    }
};

Please note: the old version will be removed in the version 2.0.0!

The signature is used in custom function of built-in rules.

const v = new Validator({
    useNewCustomCheckerFunction: true // using new version
});

const schema = {
    phone: { type: "string", length: 15, custom(v, errors) => {
        if (!v.startWith("+")) errors.push({ type: "phoneNumber" })
        return v.replace(/[^\d+]/g, ""); // Sanitize: remove all special chars except numbers
    } }	
};
fastest-validator - v1.3.0

Published by icebob over 4 years ago

Changes

  • Add new class rule to check the instance of value #126
  • Updated typescript definitions #127 #129
  • Fix deep-extend function to detect objects better. #128
  • Add hex check to string rule #132
fastest-validator - v1.2.0

Published by icebob over 4 years ago

Changes

fastest-validator - v1.1.0

Published by icebob over 4 years ago

Changes

fastest-validator - v1.0.2

Published by icebob over 4 years ago

Changes

  • Fix string with a pattern where regular expression contains a double quote #111 by @FranzZemen
fastest-validator - v1.0.1

Published by icebob over 4 years ago

Changes

  • fix missing field property in custom rules #109
fastest-validator - v1.0.0

Published by icebob almost 5 years ago

Changes

  • add unique validation in array rule #104
fastest-validator - v1.0.0-beta4

Published by icebob almost 5 years ago

Changes

  • fix array rule return value issue (again).
fastest-validator - v1.0.0-beta3

Published by icebob almost 5 years ago

Changes

  • fix optional multi rule.
fastest-validator - v1.0.0-beta2

Published by icebob almost 5 years ago

Changes

  • fix array rule return value issue.
fastest-validator - v1.0.0-beta1

Published by icebob almost 5 years ago

The full library has been rewritten. It uses code generators in order to be much faster.

Breaking changes

This new version contains several breaking changes.

Rule logic changed

The rule codes have been rewritten to code generator functions. Therefore if you use custom validators, you should rewrite them after upgrading.

Convert values

The number, boolean and date rules have a convert: true property. In the previous version it doesn't modify the value in the checked object, just converted the value to the rules. In the version 1.0 this property converts the values in the checked object, as well.

New

Sanitizations

The sanitization function is implemented. There are several rules which contains sanitizers. Please note, the sanitizers change the original checked object values.

Rule Property Description
boolean convert Convert the value to a boolean.
number convert Convert the value to a number.
date convert Convert the value to a date.
string trim Trim the value.
string trimLeft Left trim the value.
string trimRight Right trim the value.
string lowercase Lowercase the value.
string uppercase Uppercase the value.
string localeLowercase Lowercase the value with String.toLocaleLowerCase.
string localeUppercase Uppercase the value with String.toLocaleUpperCase.
string padStart Left padding the value.
string padEnd Right padding the value.
string convert Convert the value to a string.
email normalize Trim & lowercase the value.
forbidden remove Remove the forbidden field.
object strict: "remove" Remove additional properties in the object.
* default Use this default value if the value is null or undefined.

Root element validation

Basically the validator expects that you want to validate a Javascript object. If you want others, you can define the root level schema, as well. In this case set the $$root: true property.

Example to validate a string variable instead of object

const schema = {
    $$root: true,
    type: "string", 
    min: 3, 
    max: 6
};

v.validate("John", schema); // Valid
v.validate("Al", schema); // Fail, too short.

Enhanced shorthand types

You can use string-based shorthand validation definitions in the schema with properties.

{
    password: "string|min:6",
    age: "number|optional|integer|positive|min:0|max:99",

    retry: ["number|integer|min:0", "boolean"] // multiple types
}

Other changes

New equal rule

It checks the value equal (==) to a static value or another property. The strict property uses === to check values.

Example with static value:

const schema = {
    agreeTerms: { type: "equal", value: true, strict: true } // strict means `===`
}

v.validate({ agreeTerms: true }, schema); // Valid
v.validate({ agreeTerms: false }, schema); // Fail

Example with other field:

const schema = {
    password: { type: "string", min: 6 },
    confirmPassword: { type: "equal", field: "password" }
}

v.validate({ password: "123456", confirmPassword: "123456" }, schema); // Valid
v.validate({ password: "123456", confirmPassword: "pass1234" }, schema); // Fail

properties in object rule

You can use the properties property besides the props property in the object rule.

fastest-validator - v0.6.19

Published by icebob almost 5 years ago

Changes

  • update typescript definitions.
  • add "actual" variable into string messages.
fastest-validator - v0.6.18

Published by icebob almost 5 years ago

Changes

  • add mac and luhn rules by @intech;
  • update dev dependencies.
  • fix custom rule custom messages issue. #83
fastest-validator - v0.6.17

Published by icebob over 5 years ago

Changes

  • fix typescript definitions.
  • fix strict property compilation.
fastest-validator - v0.6.16

Published by icebob over 5 years ago

Changes

  • fix typescript definitions.
fastest-validator - v0.6.15

Published by icebob over 5 years ago

Changes

  • fix uuid rule. #60
  • fix typescript definitions. #59
fastest-validator - v0.6.14

Published by icebob over 5 years ago

Changes

fastest-validator - v0.6.13

Published by icebob over 5 years ago

Changes

  • add error message for url rule.
  • add numeric attribute to string rule.
  • add alpha, alphanum & alphadash attributes to string rule.
  • add index.d.ts file.
  • fix multiple validator with different messages issue.
fastest-validator - v0.6.12

Published by icebob almost 6 years ago

Changes

  • support recursive schemas by @andersnm
  • fix irregular object property names
fastest-validator - v0.6.11

Published by icebob almost 6 years ago

Changes