const yup = require('yup')
const contactSchema = yup.object({
name: yup.string()
.required(),
nrOfApples: yup
.number()
.min(0)
.max(999)
.nullable(true)
// checking self-equality works for NaN, transforming it to null
.transform((_, val) => val ? Number(val) : null)
// .transform((_, val) => val === val ? val : null)
})
const string = await contactSchema.isValid({
name: 'jimmy',
nrOfApples: 'test',
})
const number = await contactSchema.isValid({
name: 'jimmy',
nrOfApples: 24,
})
const nan = await contactSchema.isValid({
name: 'jimmy',
nrOfApples: NaN,
})
const undef = await contactSchema.isValid({
name: 'jimmy',
nrOfApples: undefined
})
const nul = await contactSchema.isValid({
name: 'jimmy',
nrOfApples: null
})
console.log({ string, number, nan, undef, nul })