Skip to content
On this page

New Feature 2.0.0

Validation Mode

You can provide a validation mode when you are defining a new validator. With different validation mode, validator will perform different behaviour.

  • option value: "strict" | "sloppy"
  • default value: "strict"
js
const strictValidator = new ParamValidator({}, 'strict')
const sloppyValidator = new ParamValidator({}, 'sloppy')

Strict Mode

input value: "strict"

Strict Mode is the default validation mode, it has a more strict rule for validation. You can simply understand as a behaviour that is more like traditional object-oriented language, such as Java, C++. In Javascript, all the objects are base on a root class(Object). And Javascript's realization of many behaviours in traditional object-oriented language is designed as prototype chain.

In "strict" mode, ParamValidator was designed to cut the prototype chain for more precise type-judgement. For example, if there is a "class Person{}". Although its instance: person is an object, validator will not match it for an Object.

js
class Person{}

const validatorObject = new ParamValidator({
    someone: Object
})
const validatorPerson = new ParamValidator({
    someone: Person
}, "sloppy")

const person = new Person()
validatorObject.test({someone: person}) // expected return: false
validatorObject.test({someone: person}) // expected return: true

Moreover, if a "class Student extends Person{}" is subclass of Person, it will not be matched for Person either.

js
class Person{}
class Student extends Person{}

const validatorPerson = new ParamValidator({
    someone: Person
})
const validatorStudent = new ParamValidator({
    someone: Student
}, "sloppy")

const stu = new Student()
validatorPerson.test({someone: stu}) // expected return: false
validatorStudent.test({someone: stu}) // expected return: true

In validation of Number, as normal NaN is treated as a Number, but in "strict" mode, NaN will not be matched as Number.

js
const strictValidator = new ParamValidator({
    num: Number
})
const sloppyValidator = new ParamValidator({
    num: Number
}, "sloppy")

strictValidator.test({num: NaN}) // expected return: false
sloppyValidator.test({num: NaN}) // expected return: true

In validation of Function, AsyncFunction will not be matched as Function

js
const { AsyncFunction } = requrie('param-validator.js')
const strictValidator = new ParamValidator({
    fun: Function
})
const sloppyValidator = new ParamValidator({
    fun: AsyncFunction
}, "sloppy")

strictValidator.test({fun: ()=>{}}) // expected return: false
sloppyValidator.test({fun: async function() {}}) // expected return: true

Sloppy Mode

input value: "sloppy"

Sloppy mode has a more flexible regulation for validation. Such as the children Class/Function will be matched by its father; NaN is treated as a Number. But Array, null, undefined are still be matched separately.

js
const { AsyncFunction } = require('param-validator.js')
class Person{}
class Student extends Person{}

const validatorPerson = new ParamValidator({
    someone: Person,
    num: Number,
    fun: Function,
    obj: Object
})

const stu = new Student()
validatorStudent.test({
    someone: stu,
    num: NaN,
    fun: AsyncFunction,
    obj: stu
}) // expected return: true

That means you can validate the params in a custom class or js-engine's object, which we don't suggest you to do that. But if you do need to, you can set type as Object and use sloppy mode or set enableClimb as true.

TIP

In sloppy mode, validator will automatically use global deep clone algorithm, which has a high performance cost. Because js-engine's object is complicated and contains a lot of native code or property, clone algorithm is hard to ensure to do a perfect clone with the different Javascript engine's feature in the future. And validation procedure may change the property in object when you set some config, such as default value, we can not ensure it will not affect the origin function of the object. If you do need that, we suggest you use some library like lodash to do preprocessing before validation.

Extend Custom Config in Built-in Model

  • BuiltInModel.prototype.extend(ModelConfig:Object)
    • ModelConfig: Object If you want to customize the built-in models, for example, you want to set a field as Email, but this field is not necessary needed. You can use method: extend(), to provide your own config. This method returns a new Built-in Model and your config will cover the pre-existed config.
js
const {Email, Integer}  = require('param-validator.js')
const validator = new ParamValidator({
    email: Email.extend({required: false}),
    age: Integer.extend({int: false})
})
console.log(validator.test({
    age: 18.2
})) // true

Code Hint in IDE

We use JS DOC to implement code hint in the ide to improve the developer's writing experience. More details in: Code Hint