Clone and edit this document
Runkit
Runkit
home page
user forum
new notebook
clone notebook
download notebook
support & documentation
log in
sign up
new notebook
help & feedback
clone this notebook
download this notebook
Sign In
Sign Up
Yup
node v8.17.0
version:
master
endpoint
share
tweet
const { object, string } = require('yup') const validationSchema = object({ firstName: string() .required("Name cannot be empty"), lastName: string() .required("Name cannot be empty"), location: object() .default(null) .nullable() .shape({ country: string().required(), state: string().required(), city: string().required() }) }) let contact = { firstName: 'jimmy', lastName: 'fred', // location: { // country: 'de', // state: 'sdf' // } } await validationSchema.validate(contact)
Or validate an object you already have:
let contact = { firstName: 'jimmy', lastName: 'fred', // location: { // country: 'de', // state: 'sdf' // } } await validationSchema.validate(contact)
Or if you want to do both at the same time:
await validationSchema.validate(contact)
If something is wrong it will let throw an error!
contact = { firstName: 'jimmy', lastName: 'fred' } await validationSchema.validate(contact, { abortEarly: false })
Loading…
no comments
sign in
to comment