Skip to main content

Available Validation Rules

accepted

The field under validation must be "yes", "on", 1, or true. This is useful for validating "Terms of Service" acceptance or similar fields.

accepted_if:anotherfield,value,...

The field under validation must be "yes", "on", 1, or true if another field under validation is equal to a specified value. This is useful for validating "Terms of Service" acceptance or similar fields.

after:date

The field under validation must be a value after a given date. The dates will be converted to a Javascript Date instance.
    start_date: 'required|date|after:2021-12-11'

Instead of passing a date string, you may specify another field to compare against the date:

    finish_date: 'required|date|after:start_date'

after_or_equal:date

The field under validation must be a value after or equal to the given date. For more information, see the after rule.

alpha

The field under validation must be entirely alphabetic characters.

alpha_dash

The field under validation must have alpha-numeric characters, as well as dashes and underscores.

alpha_num

The field under validation must be entirely alpha-numeric characters.

array

The field under validation must be a Javascript array.

array_unique

The field under validation must be a Javascript array with unique values.

bail

Stop running validation rules for the field after the first validation failure.

While the bail rule will only stop validating a specific field when it encounters a validation failure, the stopOnFirstFailure method will inform the validator that it should stop validating all attributes once a single validation failure has occurred.

    if (validator.stopOnFirstFailure().validate()) {
// ...
}

before:date

The field under validation must be a value preceding the given date. The dates will be converted to a Javascript Date instance. In addition, like the after rule, the name of another field under validation may be supplied as the value of date.

before_or_equal:date

The field under validation must be a value preceding or equal the given date. The dates will be converted to a Javascript Date instance. In addition, like the after rule, the name of another field under validation may be supplied as the value of date.

between:min,max

The field under validation must have a size between the given min and max. Strings, numercis, arrays, and objects are evaluated in the same fashion as the size rule.

boolean

The field under validation must be able to be cast as boolean. Accepted input are true, false, 1,  0, "1", "0".

If you want the attribute to be strictly a boolean, you can use the boolean rule in combination with the strict rule.

    value: 'strict|boolean'

confirmed

The field under validation must have a matching field of {field}_confirmation or {field}Confirmation. For example, if the field under validation is password, a matching password_confirmation or passwordConfirmation field must be present in the input.

date

The field under validation must be a valid date. It can be either a string or an instance of Date.

date_equals:date

The field under validation must be equal to the given date. The dates will be converted to a Javascript Date instance.

declined

The field under validation must be "no", "off", 0, or false.

declined_if:anotherfield,value,...

The field under validation must be "no", "off", 0, or false if another field under validation is equal to a specified value.

different:field

The field under validation must have a different value than field.

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 a length between the given min and max.

email

The field under validation must be formatted as an email address.

ends_with:foo,bar

The field under validation must end with one of the given values.

gt:field

The field under validation must be greater than the given field. The two fields must be of the same type. Strings, numerics, arrays, and objects are evaluated using the same conventions as the size rule.

gte:field

The field under validation must be greater than or equal to the given field. The two fields must be of the same type. Strings, numerics, arrays, and objects are evaluated using the same conventions as the size rule.

in:foo,bar,...

The field under validation must be included in the given list of values

    value: 'in:first,second'

Since this rule often requires you to implode and array, the ruleIn method may be used to fluently construct the rule.

    import { make, ruleIn } from 'simple-body-validator';
    const { make, ruleIn } = require('simple-body-validator');
    const validator = make(data, {
zones: [
required,
ruleIn(['first-zone', 'second-zone']);
]
});

When the in rule is combined with the array rule, each value in the input array must be present within the list of values provided to the in rule. In the following example, the LAS airport code in the input array is invalid since it is not contained in the list of airports provided to the in rule.

    const { make, ruleIn } = require('simple-body-validator');

const data = {
airports: ['NYC', 'LIT']
};

const validator = make(data, {
airports: [
'required',
'array',
],
'airports.*': ruleIn(['NYC', 'LIT']),
})

integer

The field under validation must be an integer. This validation rule does not verify that the input is of type number, so an integer in a string is also considered as a valid integer. If you need to validate the the input is being sent as number, you can combine the integer rule with the strict rule.

    value: 'required|strict|integer'

json

The field under validation must be a valid JSON string.

lt:field

The field under validation must bess less tha the given field. The two fields must be of the same type. Strings, numerics, arrays, and objects are evaluated using the same conventions as the size rule.

lte:field

The field under validation must be less than or equal to the given field. The two fields must be of the same type. Strings, numerics, arrays, and objects are evaluated using the same conventions as the size rule.

max:value

The field under validation must be less than or equal to a maximum value. Strings, numerics, arrays, and objects are evaluated using the same conventions as the size rule.

min:value

The field under validation must be greater than or equal to a minimum value. Strings, numerics, arrays, and objects are evaluated using the same conventions as the size rule.

not_in:foo,bar,...

The field under validation must not be included in the given list of values.

    value: 'not_in:foo,bar'

The ruleNotIn method may be used to fluently construct the rule.

    const { make, ruleNotIn} = require('simple-body-validator');

const validator = make(data, {
toppings: [
'required',
ruleNotIn(['sprinkles', 'cherries']);
],
});

not_regex

The field under validation must not match the given regular expression. The not_regex rule can only be used through the notRegex method.

    import { notRegex } from 'simple-body-validator';
    const { notRegex } = require('simple-body-validator');

The parameter passed to the notRegex method must be an instance of RegExp

    const { make, notRegex } = require('simple-body-validator');

const validator = make()
.setData({
title: 'not Regex',
})
.setRules({
title: [
'required',
notRegex(/[0-9]/),
// or notRegex(new RegExp('[0-9]'))
],
});

nullable

The field under validation may be null
    publish_at: 'nullable|date',

In this example, we are specifying that the publish_at field may be either null or a valid date representation. If the nullable modifier is not added to the rule definition, the validator would consider null an invalid date.

numeric

The field under validation must be numeric. This validation rule does not verify that the input is of type number, so a number in a string is also considered as numeric. If you need to validate that the input is of type number, you can combine the numeric rule with the strict rule.

    value: 'required|strict|numeric'

object

The field under validation must be a valid Javascript object.

present

The field under validation must be present in the input data but can be empty.

regex

The field under validation must match the given regular expression. The regex rule can only be used through the regex method.

    import { regex } from 'simple-body-validator';
    const { regex } = require('simple-body-validator');

The parameter passed to the notRegex method must be an instance of RegExp

    const { make, regex } = require('simple-body-validator');

const validator = make()
.setData({
title: 'Regex',
})
.setRules({
title: [
'required',
regex(/^[^\d]+$/),
// or regex(new RegExp('^[^\\d]+$'))
],
});

required

The field under validation must be present in the input data and not empty. A field is considered empty if one of the following conditions are true:

  • The value is null or the type of the value is undefined.
  • The value is an empty string.
  • The value is an empty array.
  • The value is an empty object.

required_if:anotherfield,value,...

The field under validation must be present and not empty if the other field is equal to any value. If you would to construct a more complex condition for the required_if rule, you may use the requiredIf method.

    import { requiredIf } from 'simple-body-validator';
    const { requiredIf }  = require('simple-body-validator');

This method accepts a boolean or a closure. When passed a closure, the closure should return true or false to indicate if the field under validation is required.

    const { make, requiredIf }  = require('simple-body-validator');

validator = make(data, {
role_id: requiredIf(user.isAdmin()),
});

validator = make(data, {
role_id: requiredIf(() => user.isAdmin()),
});

required_unless:anotherfield,value,...

The field under validation must be present and not empty unless the other field is equal to any value. This also means that the other field must be present in the request data unless value is null. If value is null (required_unless:name,null), the field under validation will be required unless the comparison field is null or the comparison field is missing from the request data.

required_with:foo,bar,...

The field under validation must be present and not empty only if any of the other specified fields are present and not empty.

required_with_all:foo,bar,...

The field under validation must be present and not empty only if all of the other specified fields are present and not empty.

required_without:foo,bar,...

The field under validation must be present and not empty only when any of the other specified fields are empty or not present.

required_without_all:foo,bar,...

The field under validation must be present and not empty only when all of the other specified fields are empty or not present.

same:field

The given field must match the field under validation.

size:value

The field under validation must have a size matching the given value. For string data, value corresponds to the number of characters. For numeric data, value corresponds to a given integer value (If the number is inside a string and you still want to consider it as number you must also add the numeric or integer rule). For an array, size corresponds to the length of the array. For an object, size corresponds to the length of the object keys.


// Validate that a string is exactly 12 characters long
title: 'size:12';

// Validate that a provided integer equals 10
seats: 'integer|size:10';

// Validate that an array has exactly 5 elements
tags: 'array|size:5';

// Validate than an object has exactly 5 elemnts
zones: 'object|size:5';

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 validator = make(data, {
email: 'sometimes|required|email',
});

In the example above, the email field will only be validated if it is present in the data object.

starts_with:foo,bar...

The field under validation must start with one of the given values.

string

The field under validation must be a string. If you would like the field to also be null, you should assign the nullable rule to the field.

url

The field under validation must be a valid URL.