Validatorjs
The validator-js library makes data validation in JavaScript very easy in both the browser and Node.js.
Install / Use
/learn @chantouchsek/ValidatorjsREADME
ValidatorJs
The ValidatorJs library makes data validation in JavaScript very easy in both the browser and Node.js. This library was forked from ValidatorJs to re-write in typescript to add more rules and features.
Why use ValidatorJs?
- Works in both the browser and Node.
- Readable and declarative validation rules.
- Error messages with multilingual support.
- CommonJS/Browserify support.
- ES6 support.
- Re-written in Typescript
Installation
Using pnpm
pnpm add @chantouchsek/validatorjs
Using npm
npm install @chantouchsek/validatorjs
Using yarn
yarn add @chantouchsek/validatorjs
Basic Usage
import { Validator } from '@chantouchsek/validatorjs'
const validation = new Validator(data, rules, options)
data {Object} - The data you want to validate
rules {Object} - Validation rules
options {Object} - Optional custom options to return
- Options
- locale?: string | Optional passing locale from config
- confirmedReverse?: boolean | Optional showing error message on confirmation field instead of password
- customMessages?: Record<string, any> | Optional custom error messages to return
- customAttributes?: Record<string, any> | Optional custom attribute name to return
- defaultAttributeName?: Record<LangTypes, string> | Optional replace all :attribute property with languages provided
Example 1 - Passing Validation
const data = {
age: 28,
email: 'johndoe@gmail.com',
name: 'John',
}
const rules = {
age: 'min:18',
email: 'required|email',
name: 'required',
}
const validation = new Validator(data, rules)
validation.passes() // true
validation.fails() // false
To apply validation rules to the data object, use the same object key names for the rules object.
Example 2 - Failing Validation
const validation = new Validator(
{
email: 'not an email address.com',
name: 'D',
},
{
email: 'required|email',
name: 'size:3',
},
)
validation.fails() // true
validation.passes() // false
// Error messages
validation.errors.first('email') // 'The email format is invalid.'
validation.errors.get('email') // returns an array of all email error messages
Example 3 - With Default Attribute Name
const validation = new Validator(
{
email: 'not an email address.com',
name: 'D',
},
{
email: 'required|email',
name: 'size:3',
},
{
defaultAttributeName: {
en: '',
ja: 'この項目',
},
}
)
validation.fails() // true
validation.passes() // false
// Error messages
validation.errors.first('email') // 'The field format is invalid.'
validation.errors.first('email') // 'この項目は正しいメールアドレスを入力してください。'
validation.errors.get('email') // returns an array of all email error messages
Nested Rules
Nested objects can also be validated. There are two ways to declare validation rules for nested objects. The first way is to declare the validation rules with a corresponding nested object structure that reflects the data. The second way is to declare validation rules with flattened key names. For example, to validate the following data:
const data = {
bio: {
age: 28,
education: {
primary: 'Elementary School',
secondary: 'Secondary School',
},
},
name: 'John',
}
We could declare our validation rules as follows:
const nested = {
bio: {
age: 'min:18',
education: {
primary: 'string',
secondary: 'string',
},
},
name: 'required',
}
// OR
const flattened = {
'bio.age': 'min:18',
'bio.education.primary': 'string',
'bio.education.secondary': 'string',
'name': 'required',
}
WildCards Rules
WildCards can also be validated.
const data = {
users: [
{
bio: {
age: 28,
education: {
primary: 'Elementary School',
secondary: 'Secondary School',
},
},
name: 'John',
},
],
}
We could declare our validation rules as follows:
const rules = {
'users.*.bio.age': 'min:18',
'users.*.bio.education.primary': 'string',
'users.*.bio.education.secondary': 'string',
'users.*.name': 'required',
}
Available Rules
Validation rules do not have an implicit 'required'. If a field is undefined or an empty string, it will pass validation. If you want a validation to fail for undefined or '', use the required rule.
accepted
The field under validation must be yes, on, 1 or true. This is useful for validating "Terms of Service" acceptance.
after:date
The field under validation must be after the given date.
after_or_equal:date
The field under validation must be after or equal to the given field
alpha
The field under validation must be entirely alphabetic characters.
alpha_dash
The field under validation may have alphanumeric characters, as well as dashes and underscores.
alpha_num
The field under validation must be entirely alphanumeric characters.
array
The field under validation must be an array.
before:date
The field under validation must be before the given date.
before_or_equal:date
The field under validation must be before or equal to the given date.
between:min,max
The field under validation must have a size between the given min and max. Strings, numerics, and files are evaluated in the same fashion as the size rule.
boolean
The field under validation must be a boolean value of the form true, false, 0, 1, 'true', 'false', '0'
, '1',
confirmed
The field under validation must have a matching field of foo_confirmation. For example, if the field under validation is password, a matching password_confirmation field must be present in the input.
date
The field under validation must be a valid date format which is acceptable by Javascript's Date object.
digits:value
The field under validation must be numeric and must have an exact length of value.
digits_between:min,max
The field under validation must be numeric and must have length between given min and max.
different:attribute
The given field must be different from the field under validation.
The field under validation must be formatted as an e-mail address.
hex
The field under validation should be a hexadecimal format. Useful in combination with other rules, like hex|size:6 for
hex color code validation.
in:foo,bar,...
The field under validation must be included in the given list of values. The field can be an array or string.
integer
The field under validation must have an integer value.
max:value
Validate that an attribute is no greater than a given size
Note: Maximum checks are inclusive.
Example 1 - Max validation
const rules = {
phone: 'required|digits|max:11',
}
const input = {
phone: '01234567890',
}
// passes: true
Example 2 - Max validation
const rules = {
phone: 'integer|max:16',
}
const input = {
phone: '18',
}
// passes: false
min:value
Validate that an attribute is at least a given size.
Note: Minimum checks are inclusive.
Example 1 - Min validation
const rules = {
phone: 'required|digits|min:11',
}
const input = {
phone: '01234567890',
}
// passes: true
Example 2 - Min validation
const rules = {
phone: 'integer|min:11',
}
const input = {
phone: '18',
}
// passes: false
not_in:foo,bar,...
The field under validation must not be included in the given list of values.
numeric
Validate that an attribute is numeric. The string representation of a number will pass.
const rules = {
amount: 'numeric|digits:5',
}
present
The field under validation must be present in the input data but can be empty.
required
Checks if the length of the String representation of the value is >
required_if:another_field,value
The field under validation must be present and not empty if the another field is equal to any value.
required_unless:another_field,value
The field under validation must be present and not empty unless the another field is equal to any value.
required_with:foo,bar,...
The field under validation must be present and not empty only if any of the other specified fields are present.
required_with_all:foo,bar,...
The field under validation must be present and not empty only if all the other specified fields are present.
required_without:foo,bar,...
The field under validation must be present and not empty only when any of the other specified fields are not present.
required_without_all:foo,bar,...
The field under validation must be present and not empty only when all the other specified fields are not present.
sometimes
In some situations, you may wish to run validation checks against a field only if that field is present in the data being validated. To quickly accomplish this, add the sometimes rule to your rule list:
const rules = {
email: 'sometimes|required|email',
}
In the example above, the email field
Related Skills
node-connect
346.8kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
107.6kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
346.8kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
qqbot-media
346.8kQQBot 富媒体收发能力。使用 <qqmedia> 标签,系统根据文件扩展名自动识别类型(图片/语音/视频/文件)。
