Peoplealizing problems
On simplest instance a test setting yields correct or not the case with regards to the in the event that the have a look at introduced. When it comes to a weak sample, yup usually place a beneficial ValidationError with your (or the standard) content regarding shot. ValidationErrors in addition to have a count of most other metadata regarding the try, and it’s term, just what arguments (or no) it had been titled having, therefore the way to the latest failing profession in the example of a good nested recognition.
const buy = object( no: number().needed(). sku: sequence().test( name: 'is-sku', skipAbsent: true, test(well worth, ctx) if (!value.startsWith('s-')) return ctx.createError( message: 'SKU shed proper prefix' >) > if (!value.endsWith('-42a')) return ctx.createError( message: 'SKU missing right suffix' >) > if (value.duration ten) return ctx.createError( message: 'SKU isn't the correct length' >) > return true > >) >) order.confirm( no: 1234, sku: 's-1a45-14a' >)
Composition and you can Reuse
Schema try immutable, per strategy label production a separate outline target. Recycle and you may solution all of them up to in place of concern about mutating an alternative particularly.
const electiveString = string().optional(); const laid outString = optionalString.defined(); const value = vague; optionalString.isValid(value); // true definedString.isValid(value); // not the case
TypeScript consolidation
transfer * as yup off 'yup'; const personSchema = yup.object( firstName: yup.string().defined(), nickname: yup.string().default('').nullable(), sex: yup .combined() .oneOf(['male', 'female', 'other'] as const) .defined(), email address address: yup.string().nullable().email(), birthTime: yup.date().nullable().