yup examples

node v0.12.18
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 })

no comments

    sign in to comment