@rsuite/validation v1.5.0
@rsuite/validation
Validate like a boss.
npm install -S @rsuite/validationFeatures
- Rich built-in validation rules called with a single expression.
- Auto-detects objects and arrays.
- Easy to extend.
rsuite/schema-typedas data validation.
Usage
Validate data
import { Validator } from "@rsuite/validation";
const checkResult = Validator.check(data, {
name: "required",
email: "required|email",
age: "number|between:18,30",
});Create SchemaModel for <Form> component
import { Validator } from "@rsuite/validation";
const model = Validator.SchemaModel({
name: "required",
email: "required|email",
age: "number|between:18,30",
});Validate array items and object properties
import { Validator } from "@rsuite/validation";
const validator = Validator.make({
"pet.name": "required", // Auto-detects `pet` as an object of shape { name: string }
"luckyNumbers.*": "number", // Auto-detects `luckyNumbers` as an array, whose items should be numbers
"cars.*.price": "number|min:1000000000", // Auto-detects `cars` as an array of objects of shape { price: number }
});On the other hand, if your field name contains a literal period, you can explicitly prevent this from being interpreted as "dot" syntax by escaping the period with a backslash:
import { Validator } from "@rsuite/validation";
const validator = Validator.make({
"v1\\.0": "required",
"object\\.withDot.property": "required", // { 'object.withDot' : { property }}
"anotherObject.withDot\\.inProperty": "required", // { anotherObject: { 'withDot.inProperty' }}
});Custom error messages
You can override error message for any rule, or for any rule on any field specifically.
import { Validator } from "@rsuite/validation";
const validator = Validator.make(
{
name: "required",
email: "required|email",
age: "required|number|between:18,30",
},
{
required: "You should not omit {field}!",
"email.required"(field) {
return "Email is a must.";
},
"age.between": "Age should be between {min} and {max}",
fields: {
email: "Email address",
},
}
);Learn more in Error messages.
Custom rules
import { Validator } from "@rsuite/validation";
const validator = Validator.make({
name: [
"required",
{
check(field, value, data) {
return value === "Tom";
},
errorMessage: "Only whose {field} is Tom shall pass.",
},
],
});Learn more in Custom validation rules.
API
Validator
import { Validator } from '@rsuite/validationstatic make(rules: object, messages?: object): ValidatorCreate a validator from given rules and custom message if necessary.
static message(messages: object): voidDefine custom error messages for all Validators.
static SchemaModel(rules: object, messages?: object): SchemaEquivalent toValidator.make(rules, messages).getSchemaModel(),static check(data: any, rules: object): CheckResultEquivalent to
Validator.make(rules).check()getSchemaModel(): SchemaReturn the
SchemaModelinstance inside theValidator.check(data: any): CheckResultEquivalent tovalidator.getSchemaModel().check()
Error messages
@rsuite/validation is packed with predefined messages for every built-in rules.
You can define your own messages for Validator, either per rule or per rule per field.
const validator = Validator.make(
{
name: "required",
email: "required|email",
age: "required|number|between:18,30",
},
{
required: customMessage1,
"age.between": customMessage2,
}
);A custom message should be either a string or a ErrorMessageFormatter.
interface ErrorMessageFormatter {
(field: string, placeholderValues?: any): string;
}If you want to customize how your field names are displayed in the error messages (by default is capitalized), set them in field property of the message bag.
const validator = Validator.make(
{
name: "required",
email: "required|email",
age: "required|number|between:18,30",
},
{
required: "Please enter {field}",
fields: {
email: "Email address",
},
}
);Defined messages globally
Use Validator.messages() to register message bag globally.
Validator.messages({
required: "Please enter {field}.",
});Placeholders
You can use placeholders (marked with brackets) in your error message string. Note that you can only use them when your error message is a string, not a formatter function.
const messages = {
required: "{field} is required.",
min: "{field} must be no smaller than {value}.",
};Each rule has different placeholders available, while {field} is available across all rules, representing the field name declared in your rules object.
Most placeholders have the same name as the rule signature.
Here is a full list of built-in rules that have placeholders in messages.
| Rule signature | Placeholders |
|---|---|
size:value | {value} |
max:value | {value} |
min:value | {value} |
between:min,max | {min}, {max} |
gt:other | {other}, {value} |
gte:other | {other}, {value} |
lt:other | {other}, {value} |
lte:other | {other}, {value} |
same:other | {other} |
different:other | {other} |
in:value1,value2... | {values} |
notIn:value1,value2... | {values} |
unique:by? | {by} |
regex:pattern | {pattern} |
after:date | {date} |
afterOrEqual:date | {date} |
before:date | {date} |
beforeOrEqual:date | {date} |
endsWith:value1,value2... | {values} |
startsWith:value1,value2... | {values} |
If you use ErrorMessageFormatter, placeholders values are passed in as an object to its second argument.
Available rules
Type rules
@rsuite/validation has 6 supported types.
Some rules are effective among all these types, some are only effective under specific types.
stringEquivalent to
StringType(). Most of time you can omit this becausestringis the default type.numberEquivalent toNumberType().arrayEquivalent to
ArrayType().dateEquivalent to
DateType().objectEquivalent to
ObjectType().booleanEquivalent to
BooleanType().
Validation rules
requiredfor all types The field under validation must be present and have a non-empty value. ApplyisRequired()from@rsuite/schema-typed.size:valueforstring,number,arrayThe field under validation must have a 'size' of
value, where size is:string's length.number's value.array's length.
max:valueforstring,number,arrayThe field under validation must have a 'size' no larger than
value. Seesizerule for more about 'size'.min:valueforstring,number,arrayThe field under validation must have a 'size' no smaller than
value. Seesizerule for more about 'size'.between:min,maxforstring,number,arrayThe field under validation must have a 'size' between
minandmax. Seesizerule for more about 'size'.same:otherfor all typesThe field under validation must have the same value with
otherfield.different:otherfor all typesThe field under validation must have the different value from
otherfield.lt:otherforstring,number,arrayThe field under validation must have a 'size' less than the
otherfield. Seesizerule for more about 'size'.lte:otherforstring,number,arrayThe field under validation must have a 'size' less than or equal the
otherfield. Seesizerule for more about 'size'.gt:otherforstring,number,arrayThe field under validation must have a 'size' greater than the
otherfield. Seesizerule for more about 'size'.gte:otherforstring,number,arrayThe field under validation must have a 'size' greater than or equal the
otherfield. Seesizerule for more about 'size'.in:value1,value2...forstring,numberThe field under validation must be included in the given list of values.
notIn:value1,value2...forstring,numberThe field under validation must not be included in the given list of values.
emailforstringThe field under validation must be formatted as an e-mail address.
urlforstringThe field under validation must be a valid URL.
unique:by?forarrayThe array under validation must not have any duplicate items. If
byis provided for array of objects, duplication is checked by object property.integerfornumberThe field under validation must be an integer.
regex:patternforstring,numberThe field under validation must match the given regular expression.
Note: When using the
regexpatterns, it may be necessary to specify rules in an array instead of using pipe delimiters, especially if the regular expression contains a pipe character.after:datefordateThe field under validation must be a value after a given date. The dates will be passed into
new Date(date).afterOrEqual:datefordateThe field under validation must be a value after or equal to the given date. The dates will be passed into
new Date(date).before:datefordateThe field under validation must be a value preceding the given date. The dates will be passed into
new Date(date).beforeOrEqual:datefordateThe field under validation must be a value preceding or equal to the given date. The dates will be passed into
new Date(date).endsWith:value1,value2...forstringThe field under validation must end with one of the given values.
startsWith:value1,value2...forstringThe field under validation must start with one of the given values.
Custom validation rules
In addition to built-in validation rules, you can also define you own validation rules.
Validator.make({
name: ["required", customRule1, customRule2],
email: ["required", "email", customRule3],
});A custom rule must implement Rule interface.
interface Rule {
/**
* Check whether the value passes this rule
*/
check(field: string, value: any, data: any): boolean;
/**
* Message to show when this rule fails.
*/
errorMessage: string | ErrorMessageFormatter;
}@rsuite/schema-typed API coverage
The table below shows @rsuite/schema-typed API and their equivalent rules in @rsuite/validation.
Note that equivalent means equivalent, which is, implementation of the rule is calling the according API or copies its underlying implementation.
Those APIs that don't have an equivalent rule for now (marked as -) can still be achieved using Custom validation rules.
- Common
| API | Rule |
|---|---|
.isRequired() | required |
.addRule() | Custom validation rules |
StringType()
| API | Rule |
|---|---|
StringType() | string |
.isEmail() | email |
.isURL() | url |
.isOneOf(items) | in:value1,value2... |
.containsLetter() | regex:[a-zA-Z] |
.containsUppercaseLetter() | regex:[A-Z] |
.containsLowercaseLetter() | regex:[a-z] |
.containsLetterOnly() | regex:^[a-zA-Z]+$ |
.containsNumber() | regex:[0-9] |
.pattern(regExp) | regex:pattern |
.rangeLength(minLength, maxLength) | between:min,max |
.minLength(minLength) | min:value |
.maxLength(maxLength) | max:value |
NumberType()
| API | Rule |
|---|---|
NumberType() | number |
.isInteger() | integer |
.isOneOf(items) | in:value1,value2... |
.pattern(regExp) | regex:pattern |
.range(min, max) | between:min,max |
.min(min) | min:value |
.max(max) | max:value |
ArrayType()
| API | Rule |
|---|---|
ArrayType() | array or auto-detect from field expression |
.rangeLength(minLength, maxLength) | between:min,max |
.minLength(minLength) | min:value |
.maxLength(maxLength) | max:value |
.unrepeatable() | unique |
.of(type) | Wildcard field expression |
DateType()
| API | Rule |
|---|---|
DateType() | date |
.range(min,max) | afterOrEqual:min | beforeOrEqual:max |
.min(min) | afterOrEqual:date |
.max(max) | beforeOrEqual:date |
ObjectType()
| API | Rule |
|---|---|
ObjectType() | object or auto-detect from field expression |
.shape(type) | Auto-detect from field expression |
BooleanType()
| API | Rule |
|---|---|
BooleanType() | boolean |
Philosophy
@rsuite/validationsupposes default type for a field to bestring, respecting HTML<input>s' value definition, so you can get rid of callingStringType()every time.@rsuite/validationnormalizes error messages for rules, so you don't need to declare error messages for every call of a rule, but only when you need to customize them.@rsuite/validationextracts common rules across different types likemin,max, etc. so you remember them easily.
Prior Art
- Inspired by Laravel Validation.
- Implemented with
rsuite/schema-typed.
License
MIT Licensed.