validar example

node v12.22.12
version: 2.0.0
endpointsharetweet
const { validate, validation} = require("validar") const validator = require('validator') //object to validate const testObj = { name: 'Mike', email:'test@example.com', location:{ city:'Sydney' } } const stringLengthTest = (value, field, path, objectUnderTest) => { return typeof value === 'string' && value.length > 5 } //assemble the validation object const validationObject = { name: validation(stringLengthTest), email: validation((value)=>{ return validator.isEmail(value) // use validator tests directly }), location:{ city:validation((value)=>false), // fail this one location:validation({ // this one is not present on the test object test:(value)=>true, missingMessage:'%path not present, please select country', required:false }) } } const result = validate(validationObject,testObj)
Loading…

no comments

    sign in to comment