1.0.7 • Published 5 years ago

awesome-form-validator v1.0.7

Weekly downloads
10
License
MIT
Repository
github
Last release
5 years ago

awesome-form-validator

An awesome form validator

Home to use

// via import
import Validator, {rules} from 'awesome-form-validator';
//via require
const {default:Validator, rules} = require('awesome-form-validator');


const dataForm = {
  username:"test", 
  password:'', 
  retype: 'test',
  info:{
    age:10
  },
  gender:'F',
};
const validationConfig = {
  username: [
    {
      ...rules.MIN_LENGTH,
      value: 6,
      message: 'invalid, lenth less than {{number}} charactors'
    }
  ],
  password: [
    rules.REQUIRED,
    {
      ...rules.LENGTH_BETWEEN,
      value: [6, 20],
    }
  ],
  retype:[
    rules.REQUIRED,
    {
      ...rules.FUNCTION,
      value:(value, rule, form)=>{
        return value === form.password
      },
      message: 'retype error'
    }
  ],
  info: {
    age: [
      {
        ...rules.MIN,
        value: 18
      }
    ]
  },
  gender:[
    {
      ...rules.CONTAINED,
      value: ['F','M']
    }
  ]
}

const {result, messages} = Validator.validate(dataForm, validationConfig);
//  result => false
//  messages => {
//    username: ['invalid, lenth less than 6 charactors'],
//    password: ['required', length btween 6 to 20 required],
//    retype: ['retype error']
//    info: {
//      age:['more than 18 required']
//    }
//  }
  //validate array
const dataForm = [1,5]
const validationConfig = [
  {
    ...rules.MIN_LENGTH,
    value:3
  },
  [{
    ...rules.MIN,
    value:3
  },{
    ...rules.MAX,
    value:3
  }] //array to validate array element
]
const {result, messages} = Validator.validate(dataForm, validationConfig);
// [ 
//  'more than 3 chars required',
//  [ 
//    [ 'more than 3 required' ], 
//    [ 'less than 3 required' ] 
//  ] 
// ]

API

Validator, validator instance

Validator.validate(form, rules), execute validate

return {Object}, like:

{result: true|false, messages: {
  usename: ['some message']
}}

form, {Object}

rules, {Object}, format like:

 {
  username: [
     rule
  ]
 }

rule format like

{
  type: 'min',
  value: 100,
  message:'must greater than {{number}}',
}

Validator.setMessages(messages), override validate message.

default,

  {
    required: 'required',
    minlength: 'more than {{number}} chars required',
    maxlength: 'less than {{number}} chars required',
    length_btween: 'length btween {{min}} to {{max}} required',
    max: 'less than {{number}} required',
    min: 'more than {{number}} required',
    number: 'number required',
    number_btween: 'number btween {{min}} to {{max}} required',
    array: 'array required',
    pattern: 'format not matched'
  }

{{number}}, {{min}}, {{max}} will be replaced when output message, when rule.value is array, the value will replace {{min}} and {{max}}, otherwise, {{number}}

rules

typedefault valuedestructuring from ruledescription
requiredREQUIREDstring or number required
minlength0MIN_LENGTHstring, number or array min length
maxlength0MAX_LENGTHstring, number or array max length
length_btween0, falseLENGTH_BWTWEENstring, number or array length between range
maxfalseMAXmax number
minfalseMINmin number
numberNUMBERis number?
number_btweenfalse, falseNUMBER_BETWEENnumber between range
arrayARRAYis array?
contained[]CONTAINEDvalue is contained?, value can be array or non-array
patternfalsePATTERNpattern matched
function()=>trueFUNCTIONuser defined validate function
1.0.7

5 years ago

1.0.6

5 years ago

1.0.5

5 years ago

1.0.4

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago