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 examples
node v0.12.18
endpoint
share
tweet
Define your schema by combining yup schema objects and use them to parse objects
var yup = require('yup') var contactSchema = yup.object({ name: yup.string() .required(), age: yup.number() .required() .positive() .integer(), email: yup.string() .email(), website: yup.string() .url(), createdOn: yup.date() .default(() => new Date()) }) contactSchema.cast({ name: 'jimmy', age: '24', createdOn: '2014-09-23T19:25:25Z' })
Or validate an object you already have.
let contact = { name: 'jimmy', age: 24, email: 'jdog@cool.biz' } await contactSchema.isValid()
Or if you want to do both at the same time:
await contactSchema.validate(contact)
If something is wrong it will let throw an error!
contact = { name: 'jimmy', email: 'jdog' } await contactSchema.validate(contact, { abortEarly: false })
Loading…
no comments
sign in
to comment