Skip to content
On this page

New Feature 1.4.0

Custom Function

Through this way, you can fully customize your validator to create more complicated and more personal validation rules
  • An model property: custom: Function(value, key, config, data) (after Version 1.4.0)
    • To give validator a custom function to validate field, the function need a return value that must be Boolean type (true or false). false means that the value fail to pass the validation, true means the opposite.
    • custom function tools: contains some built-in functions that you can use in the custom function

Usage

  • In this option when you define a data model, you should give a function that must contain a return value which is type of Boolean.
  • When your custom function return true, it means this value can pass the validation. When it returns false, it means you reject this value.
  • param:
    • value: the value of the field
    • key: the key of the value, you figure out which field you are validating
    • config: you can get the completed config of the data model
    • data: you can get the whole data that you are validating
  • custom function tools: you can use this to get an object containing some method you can use to make some change on the value of the data or something else, it will be more and more method constantly added in.
    • alter(newValue: Any)
      • you can use this method to change the value that you are validating
    • alterConfig(field: String, value: Any)
      • you can use this method to change any option in the model config that you are validating
    • ignore()
      • if you call this function in custom function, all the validation procedure will be ignored including the validation of type. And that means you don't need to specific any option except the custom function in data model if you decide to use this method. All the work need to be done by your custom function.
    • ParamValidator
      • you can access all the method provided by ParamValidator
js
const peopleModel = {
    name:{
        type: String,
        custom: function(name) {
            const newName = name.toUpperCase()
            this.alter(newName)
            if (newName === 'MATCH') {
                this.alterData('age', 20)
            }
            return true;
        }
    },
    age:{
        type: Number,
        // if number is not an even then it can't pass the validation
        custom(age) {
            if (age % 2 !== 0) return false
            return true
        }
    }
}

const validator = new ParamValidator(peopleModel)

const person = {
    name:'Match',
    age:18
}
console.log( validator.check(person) )// true

Built-in Model

Q: What is Built-in Model?

ParamValidator provided some Built-in Models, it can help you create data model faster and more conveniently. Built-in Model is essentially a config of data model, which is defined by the ParamValidator author. And the number of Built-in Model will be expended more and more in the future.

After Version 1.4.0

Example of usage:

js
const ParamValidator = require('param-validator.js')
const {UserName, Password, Email, Phone, Integer} = ParamValidator
const userModel = {
    username: UserName,
    password: Password,
    email: Email,
    phone: Phone,
    age: Integer
}
const userValidator = new ParamValidator(userModel)
const user1 = {
    username: 'Match_OvO',
    password: 'Match_12345',
    email: '1033085048@qq.com',
    phone: '12345678901',
    age: 20
}
console.log(userValidator.test(user1)) //true

List of Built-in Models:

  • Email: String
  • Url: String
  • IP: String
  • ChineseWord: String
  • Base64: String
  • Phone: String
  • Html: String
  • IntString: String
  • FloatString: String
  • UserName: String
  • Password: String
  • Integer: Number
  • Float: Number
  • Odd: Number
  • Even: Number

RegexpLibrary

Usage:

js
const {regexpLibrary} = require('param-validator.js')
{
    email: /^\w{3,}(\.\w+)*@[A-z0-9]+(\.[A-z]{2,5}){1,2}$/,
    url: /[a-zA-z]+:\/\/[^\s]*/,
    IP: /((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)/,
    ChineseWord: /[\u4e00-\u9fa5]/,
    base64: /[\/]?([\da-zA-Z]+[\/+]+)*[\da-zA-Z]+([+=]{1,2}|[\/])?/,
    phone: /^\d{11}$/,
    html: /^<([a-z]+)([^<]+)*(?:>(.*)<\/\1>|\s+\/>)$/,
    intString: /^[0-9]+$/,
    floatString: /^[0-9]+\.[0-9]+$/,
    userName: /^[a-zA-Z0-9_]{4,16}$/,
    password: /^(?=.*[a-zA-Z])(?=.*[0-9])(?=.*[!@#$%^&*()_+]).{8,}$/
}