Ensuring a outline matches a preexisting type
Customizing errors
On greatry case an examinuteation setting productivity correct or zerot true according to when the the have a look at enacted. Regarding a deep failing shot, yup usually put a great ValidationError along with your (or even the standard) content for which try. ValidationErrors in addition to incorporate a lot of most other metadata regarding the test, plus it is title, exactly what arguments (or no) it was titled having, and the path to the latest weak industry in the case of an effective nested recognition.
const order = object( no: number().necessary(). sku: sequence().test( name: 'is-sku', skipAbsent: true, test(really worth, ctx) if (!value.startsWith('s-')) return ctx.createError( message: 'SKU lost correct prefix' >) > if (!value.endsWith('-42a')) return ctx.createError( message: 'SKU forgotten best suffix' >) > if (value.length ten) return ctx.createError( message: 'SKU isn't the best length' >) > return true > >) >) order.examine( no: 1234, sku: 's-1a45-14a' >)
Composition and Recycle
Outline is actually immutable, each strategy call productivity yet another schema object. Recycle and solution them as much as rather than fear of mutating a unique like.
const electiveString = string().optional(); const laid outString = optionalString.defined(); const value = vague; optionalString.isValid(value); // real definedString. (mer …)