The allow middleware is used to considere some values as valid if provided.
For example, if your schema is a string schema but you want to allow null, just set the allow property with the null value.
Step: pre: this middleware run before other validations
const { validateSchema } = require('jzor')
const schema = {
//...
allow: [ null, 1, 'Terry Bogard' ]
}
const value = null
const result = validateSchema(schema, value) // valid| parameter | type | required | default | description |
|---|---|---|---|---|
| allow | array of any | no | [] | The items that will be considered as valid in schema. |
The reject middleware is used to considere some values as invalid if provided.
For example, if your schema is a string schema but you want to not allow acb, just set the reject property with the abc value.
Step: pre: this middleware run before other validations
const { validateSchema } = require('jzor')
const schema = {
//...
reject: [ 'abc' ]
}
const value = 'abc'
const result = validateSchema(schema, value) // invalid| parameter | type | required | default | description |
|---|---|---|---|---|
| reject | array of any | no | [] | The items that will be considered as invalid in schema. |
The throw middleware is used to throw custom errors instead of run the normal validation.
If a schema with a throw property fails, instead of record the validation, it will throw the error.
const { validateSchema } = require('jzor')
const schema = {
//...
throw: new Error('My custom error!')
}
const value = 'abc'
const result = validateSchema(schema, value) // will throw the new Error('My custom error!')