async validar example

node v12.22.12
version: 1.0.0
endpointsharetweet
const { validateAsync, validation} = require('validar') //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(()=> Promise.resolve(true)), email: validation((value)=>{ return true }), 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 }) } } validateAsync(validationObject,testObj).then(result =>{ console.log(result) }) .catch(error=>{ console.log('Some validator has thrown an error') })
Loading…

no comments

    sign in to comment