Yup custom max example

node v6.17.1
version: 1.0.1
endpointsharetweet
const { object, number, test } = require('yup') const contactSchema = object({ price: number() .min(0, `Minimum tip is $0`), tips: number() .min(0, `Minimum tip is $0`) .test({ name: 'max', exclusive: false, params: { }, message: '${path} must be less than 10% of the price', test: function (value) { // You can access the price field with `this.parent`. return value <= parseFloat(this.parent.price * 0.1) }, }), })
validate an object you already have:
let ok = { price: 100, tips: 9, } let ng = { price: 100, tips: 11, } console.log(await contactSchema.isValid(ok)) console.log(await contactSchema.isValid(ng))
`ok` pass through validation without Error:
await contactSchema.validate(ok)
`ng` throws ValidationError:
await contactSchema.validate(ng)
Loading…

no comments

    sign in to comment