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))