A streamlined form validation library , base on proxy .
$ yarn add proy
$ npm install proy
import { proy } from 'proy'
const descriptor = {
name: {
type: 'string',
required: true,
validator: (rule, val) => val === 'kanno',
},
}
const basic = proy(descriptor)
basic.validate({ name: 'XeryYue' }, (err, fields) => {
if (err) {
console.log(err)
}
})Pass in the object to be verified . If you want to catch the error, you can also pass in a callback function
type ValidateCallBack = (err: [] | CallbackErrors[], fields: Record<string, any>) => void
interface Validate {
(source:Record<string,any>,callback?ValidateCallBack):Proy
}-
source: The object to validate (required). -
callback: A callback function to invoke when validation completes .Thecallbackwill return a sync function -
err: A error list
-
fields: A list of successfully verified data
descriptor: entry your validate rule
interface Rule {
type: 'string' | 'number' | 'boolean' | 'array' | 'object';
required?: boolean;
message?: string;
validate?: (val: any) => boolean;
}typeSpecify the type to be verifiedrequiredIs it requiredmessageCustom error message , if you don't set this field , will use default error messagevalidateCustom check
producewill interrupt the chain call.producewill return successfully verified data , If un verifeid will return empty object .