Built in Validators
Super validator only supplies a few basic validators. You can take a look into making your own or using addons for more validators.
If you have suggestions for more validators please create a issue here.
#
RequiredMakes a value required. This will fail if the value is equal to null or undefined.
import { required } from 'super-validator'
required()// Orrequired('Custom Error Message')
#
ParamsName | Type | Description |
---|---|---|
customMessage | string, undefined | Custom message to override any validation errors. |
#
MinIf the value to validate is a number it will check to make sure it is equal to or greater than x. If the value is a string it will ensure the length of the string is at least x characters long.
#
ParamsName | Type | Description |
---|---|---|
minValue | number | The minimum value if the input is a number or the minimum length if a string is the value |
customMessage | string, undefined | Custom message to override any validation errors. |
import { min } from 'super-validator'
min(5)// Ormin(5, 'Custom Error Message')
#
Notes- If the value is undefined or null this will not return a error. You should use required as well as this if you want to check if its not included.
#
MaxIf the value to validate is a number it will check to make sure it is less than or equal to x. If the value is a string it will ensure the length of the string is not longer than x.
#
ParamsName | Type | Description |
---|---|---|
maxValue | number | The maximum value the input can be if the value is a number or the maximum length if a string is the value |
customMessage | string, undefined | Custom message to override any validation errors. |
import { max } from 'super-validator'
max(5)// Ormax(5, 'Custom Error Message')
#
Notes- If the value is undefined or null this will not return a error. You should use required as well as this if you want to check if its not included.
#
TypeOfIf the value to validate is a number it will check to make sure it is less than or equal to x. If the value is a string it will ensure the length of the string is not longer than x.
#
ParamsName | Type | Description |
---|---|---|
type | string | The type the value should be. If the value has a type of anything else it will return a error. |
customMessage | string, undefined | Custom message to override any validation errors. |
import { typeOf } from 'super-validator'
typeOf('string')// OrtypeOf('string', 'Custom Error Message')
#
EqualsChecks if the value is equal to another this can be used with both strict and non strict checks.
#
ParamsName | Type | Description | Default |
---|---|---|---|
toMatch | unknown | The object to match to the value provided. | |
strict | boolean | If we should use strict equals or not. (This should be true in most cases.) | true |
customMessage | string, undefined | Custom message to override any validation errors. |
import { equal } from 'super-validator'
equal(true)// Orequal('test')// Orequal(6)// Orequal(null, false)// OrtypeOf('test', true, 'Custom Error Message')