Validators
Options#
All validators accept as their final argument the same set of options (optional):
name#
By default, name is the name of the validator you are calling - i.e. the name for the oneOf validator is oneOf.
params#
params differ from validator to validator and are provided to the string/message function for convenience. label is the only common parameter always available for interpolation. For more information about error messages see error messages.
message#
message is a string or a function used to define the error message for this specific validator. If a string the appropriate parameters will be interpolated using ${interpolationparam}.
- ast
- functional
Validators#
Validators aren't always applicable to all schemas (min is not relevant for a boolean schema, for instance) This is accomplished via a pre-validation hook which verifies that the current ValidatorDefinition is applicable to the current schema.
Note: undefined is considered a valid value for all validators except required.
required#
required(options: ValidatorOptions)
Applicable to schemas: ALL
Most of the time undefined is not validated. This is accomplished via the pre-validation check function that determines if a validator is applicable given a schema type/value.
required does validate the value of undefined and it returns false if the value being validated is undefined or null.
In the case of an array, it also requires at least 1 element and in the case of a string it requires the string be non-empty (not "").
- ast
- functional
is#
is(value: any | Ref, options: ValidatorOptions)
Applicable to schemas: ALL
Value being validated must equal (===) the value/ref.
Note: This validator is equivalent to oneOf([value: any | Ref]), except the ValidationError type is is.
- ast
- functional
not#
not(value: any | Ref, options: ValidatorOptions)
Applicable to schemas: ALL
- ast
- functional
oneOf#
oneOf(value: Ref | Array<any | Ref>, options: ValidatorOptions)
Applicable to schemas: ALL
Value being validated must be one of the values given to oneOf OR if value is a ref then the value being validated must be one of the values in the reference.
Note: Just a reminder that undefined is considered a valid value. If you need to test for undefined use required.
- ast
- functional
notOneOf#
notOneOf(value: Array<any | Ref>, options: ValidatorOptions)
Applicable to schemas: ALL
- ast
- functional
same#
same(value: string, options: ValidatorOptions)
Applicable to schemas: ALL
Validation-sugar for is(ref(value)).
- ast
- functional
different#
different(value: string, options: ValidatorOptions)
Applicable to schemas: ALL
- ast
- functional
matches#
matches(value: string | RegExp | Array<string | RegExp>, options = { validateEmpty: boolean, ...ValidatorOptions })
Applicable to schemas: STRING
Validates that the value being validated matches the provided string/regexp. In the case of an array, the value being validated must match one of the provided string/regexp.
Empty strings always return true unless validateEmpty: true is passed as an option.
- ast
- functional
email#
email(options = { validateEmpty: boolean, ...ValidatorOptions })
Applicable to schemas: STRINGS
Validates that the value is a valid e-mail address
- ast
- functional
min#
min(value: number | Ref, options = { inclusive: boolean, ...ValidatorOptions})
Applicable to schemas: DATE, STRING, NUMBER, ARRAY
Validates a date, string, number, or array is no less than the given number or ref.
inclusive is true by default, if passed as false, then the value must be more than min
- ast
- functional
max#
max(value: number | Ref, options = { inclusive: boolean = true, ...ValidatorOptions})
Applicable to schemas: DATE, STRING, NUMBER, ARRAY
Validates a date, string, number, or array is no more than the given number or ref.
inclusive is true by default, if passed as false, then the value must be less than max.
- ast
- functional
includes#
includes(value: any | Ref, options: ValidatorOptions)
Applicable to schemas: STRING, ARRAY
Validates a string or array value includes the value (or ref):
- ast
- functional
oneOfType#
oneOfType(schemas: Schema[], options: ValidatorOptions)
Applicable to schemas: MIXED
This is a special validator (similar to joi's alternatives) that validates a value to be one of a schema type.
- ast
- functional
Combination#
There are certain utility validators that can be used to combine/alter validators in certain ways.
negate#
Inverts the logic for any validator. This validator is used internally for validator pairs like same/different, is/not and oneOf/notOneOf.
- ast
- functional
combine#
Combines multiple validators into a new validator. Used internally to create the between validator.
- ast
- functional
serial#
Essentially a "real" abortEarly:true for async validators.
- ast
- functional