Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

validate.rules

import validate.rules

validate does not re-export this module, so it is reached only by importing it directly.

All built-in validation rule implementations. Rules are plain classes that extend Rule. They are not intended to be constructed directly; the Validator fluent builder and Schema handle construction and injection automatically.

Classes

Required

class validate.rules.Required < Rule

Fails when the field is absent, nil, an empty string, or an empty list.

Required.validate()

validate.rules.Required.validate(value)

Required.error()

validate.rules.Required.error()

Nullable

class validate.rules.Nullable < Rule

Passes when the field is absent or nil. Use this to mark a field as optional while still attaching other rules that run only when a value is present. Pair with sometimes on the Validator.

Nullable.validate()

validate.rules.Nullable.validate(value)

Nullable.error()

validate.rules.Nullable.error()

StringRule

class validate.rules.StringRule < Rule

Passes only when the value is a string.

StringRule.validate()

validate.rules.StringRule.validate(value)

StringRule.error()

validate.rules.StringRule.error()

NumberRule

class validate.rules.NumberRule < Rule

Passes only when the value is a number (integer or float).

NumberRule.validate()

validate.rules.NumberRule.validate(value)

NumberRule.error()

validate.rules.NumberRule.error()

IntegerRule

class validate.rules.IntegerRule < Rule

Passes only when the value is an integer (no fractional part).

IntegerRule.validate()

validate.rules.IntegerRule.validate(value)

IntegerRule.error()

validate.rules.IntegerRule.error()

BooleanRule

class validate.rules.BooleanRule < Rule

Passes only when the value is a boolean (true or false). Also accepts the strings "true", "false", "1", "0", and the integers 1 and 0.

BooleanRule.validate()

validate.rules.BooleanRule.validate(value)

BooleanRule.error()

validate.rules.BooleanRule.error()

ListRule

class validate.rules.ListRule < Rule

Passes only when the value is a list.

ListRule.validate()

validate.rules.ListRule.validate(value)

ListRule.error()

validate.rules.ListRule.error()

DictRule

class validate.rules.DictRule < Rule

Passes only when the value is a dictionary.

DictRule.validate()

validate.rules.DictRule.validate(value)

DictRule.error()

validate.rules.DictRule.error()

NumericRule

class validate.rules.NumericRule < Rule

Passes only when the value is numeric (a number, or a string that can be losslessly converted to a number).

NumericRule.validate()

validate.rules.NumericRule.validate(value)

NumericRule.error()

validate.rules.NumericRule.error()

Size

class validate.rules.Size < Rule

Passes when the value’s size equals n.

Size is measured as: character count for strings, item count for lists and dicts, and numeric value for numbers.

Constructor

validate.rules.Size(name, n)

Size.validate()

validate.rules.Size.validate(value)

Size.error()

validate.rules.Size.error()

Min

class validate.rules.Min < Rule

Passes when the value’s size is at least min.

Constructor

validate.rules.Min(name, min)

Min.validate()

validate.rules.Min.validate(value)

Min.error()

validate.rules.Min.error()

Max

class validate.rules.Max < Rule

Passes when the value’s size is at most max.

Constructor

validate.rules.Max(name, max)

Max.validate()

validate.rules.Max.validate(value)

Max.error()

validate.rules.Max.error()

Between

class validate.rules.Between < Rule

Passes when the value’s size is between min and max (inclusive).

Constructor

validate.rules.Between(name, min, max)

Between.validate()

validate.rules.Between.validate(value)

Between.error()

validate.rules.Between.error()

Length

class validate.rules.Length < Rule

Passes when a string’s length is exactly n characters.

Constructor

validate.rules.Length(name, n)

Length.validate()

validate.rules.Length.validate(value)

Length.error()

validate.rules.Length.error()

MinLength

class validate.rules.MinLength < Rule

Passes when a string’s length is at least min characters.

Constructor

validate.rules.MinLength(name, min)

MinLength.validate()

validate.rules.MinLength.validate(value)

MinLength.error()

validate.rules.MinLength.error()

MaxLength

class validate.rules.MaxLength < Rule

Passes when a string’s length is at most max characters.

Constructor

validate.rules.MaxLength(name, max)

MaxLength.validate()

validate.rules.MaxLength.validate(value)

MaxLength.error()

validate.rules.MaxLength.error()

LengthBetween

class validate.rules.LengthBetween < Rule

Passes when a string’s length is between min and max characters (inclusive).

Constructor

validate.rules.LengthBetween(name, min, max)

LengthBetween.validate()

validate.rules.LengthBetween.validate(value)

LengthBetween.error()

validate.rules.LengthBetween.error()

Gt

class validate.rules.Gt < Rule

Passes when the numeric value is greater than n.

Constructor

validate.rules.Gt(name, n)

Gt.validate()

validate.rules.Gt.validate(value)

Gt.error()

validate.rules.Gt.error()

Gte

class validate.rules.Gte < Rule

Passes when the numeric value is greater than or equal to n.

Constructor

validate.rules.Gte(name, n)

Gte.validate()

validate.rules.Gte.validate(value)

Gte.error()

validate.rules.Gte.error()

Lt

class validate.rules.Lt < Rule

Passes when the numeric value is less than n.

Constructor

validate.rules.Lt(name, n)

Lt.validate()

validate.rules.Lt.validate(value)

Lt.error()

validate.rules.Lt.error()

Lte

class validate.rules.Lte < Rule

Passes when the numeric value is less than or equal to n.

Constructor

validate.rules.Lte(name, n)

Lte.validate()

validate.rules.Lte.validate(value)

Lte.error()

validate.rules.Lte.error()

Positive

class validate.rules.Positive < Rule

Passes when the numeric value is positive (strictly greater than zero).

Positive.validate()

validate.rules.Positive.validate(value)

Positive.error()

validate.rules.Positive.error()

Negative

class validate.rules.Negative < Rule

Passes when the numeric value is negative (strictly less than zero).

Negative.validate()

validate.rules.Negative.validate(value)

Negative.error()

validate.rules.Negative.error()

PositiveOrZero

class validate.rules.PositiveOrZero < Rule

Passes when the numeric value is positive or zero.

PositiveOrZero.validate()

validate.rules.PositiveOrZero.validate(value)

PositiveOrZero.error()

validate.rules.PositiveOrZero.error()

NegativeOrZero

class validate.rules.NegativeOrZero < Rule

Passes when the numeric value is negative or zero.

NegativeOrZero.validate()

validate.rules.NegativeOrZero.validate(value)

NegativeOrZero.error()

validate.rules.NegativeOrZero.error()

MultipleOf

class validate.rules.MultipleOf < Rule

Passes when the numeric value is a multiple of n.

Constructor

validate.rules.MultipleOf(name, n)

MultipleOf.validate()

validate.rules.MultipleOf.validate(value)

MultipleOf.error()

validate.rules.MultipleOf.error()

Alpha

class validate.rules.Alpha < Rule

Passes when the string contains only ASCII alphabetic characters (a-z, A-Z).

Alpha.validate()

validate.rules.Alpha.validate(value)

Alpha.error()

validate.rules.Alpha.error()

AlphaNum

class validate.rules.AlphaNum < Rule

Passes when the string contains only ASCII alphanumeric characters.

AlphaNum.validate()

validate.rules.AlphaNum.validate(value)

AlphaNum.error()

validate.rules.AlphaNum.error()

AlphaDash

class validate.rules.AlphaDash < Rule

Passes when the string contains only ASCII alphanumeric characters, hyphens (-), and underscores (_).

AlphaDash.validate()

validate.rules.AlphaDash.validate(value)

AlphaDash.error()

validate.rules.AlphaDash.error()

StartsWith

class validate.rules.StartsWith < Rule

Passes when the string starts with one of the given prefixes.

Constructor

validate.rules.StartsWith(name, prefixes)

StartsWith.validate()

validate.rules.StartsWith.validate(value)

StartsWith.error()

validate.rules.StartsWith.error()

DoesntStartWith

class validate.rules.DoesntStartWith < Rule

Passes when the string does not start with any of the given prefixes.

Constructor

validate.rules.DoesntStartWith(name, prefixes)

DoesntStartWith.validate()

validate.rules.DoesntStartWith.validate(value)

DoesntStartWith.error()

validate.rules.DoesntStartWith.error()

EndsWith

class validate.rules.EndsWith < Rule

Passes when the string ends with one of the given suffixes.

Constructor

validate.rules.EndsWith(name, suffixes)

EndsWith.validate()

validate.rules.EndsWith.validate(value)

EndsWith.error()

validate.rules.EndsWith.error()

DoesntEndWith

class validate.rules.DoesntEndWith < Rule

Passes when the string does not end with any of the given suffixes.

Constructor

validate.rules.DoesntEndWith(name, suffixes)

DoesntEndWith.validate()

validate.rules.DoesntEndWith.validate(value)

DoesntEndWith.error()

validate.rules.DoesntEndWith.error()

Contains

class validate.rules.Contains < Rule

Passes when the string contains the given substring.

Constructor

validate.rules.Contains(name, needle)

Contains.validate()

validate.rules.Contains.validate(value)

Contains.error()

validate.rules.Contains.error()

DoesntContain

class validate.rules.DoesntContain < Rule

Passes when the string does not contain the given substring.

Constructor

validate.rules.DoesntContain(name, needle)

DoesntContain.validate()

validate.rules.DoesntContain.validate(value)

DoesntContain.error()

validate.rules.DoesntContain.error()

Regex

class validate.rules.Regex < Rule

Passes when the string value matches the given regular expression.

Constructor

validate.rules.Regex(name, pattern)

Regex.validate()

validate.rules.Regex.validate(value)

Regex.error()

validate.rules.Regex.error()

NotRegex

class validate.rules.NotRegex < Rule

Passes when the string value does not match the given regular expression.

Constructor

validate.rules.NotRegex(name, pattern)

NotRegex.validate()

validate.rules.NotRegex.validate(value)

NotRegex.error()

validate.rules.NotRegex.error()

Lowercase

class validate.rules.Lowercase < Rule

Passes when the string value contains only lowercase characters.

Lowercase.validate()

validate.rules.Lowercase.validate(value)

Lowercase.error()

validate.rules.Lowercase.error()

Uppercase

class validate.rules.Uppercase < Rule

Passes when the string value contains only uppercase characters.

Uppercase.validate()

validate.rules.Uppercase.validate(value)

Uppercase.error()

validate.rules.Uppercase.error()

NotBlank

class validate.rules.NotBlank < Rule

Passes when the value does not consist solely of whitespace. Use on string fields to prevent inputs like " ".

NotBlank.validate()

validate.rules.NotBlank.validate(value)

NotBlank.error()

validate.rules.NotBlank.error()

Email

class validate.rules.Email < Rule

Passes when the value is a syntactically valid email address.

Validates the structure local@domain.tld per a practical subset of RFC 5321. Does not perform DNS MX lookup.

Email.validate()

validate.rules.Email.validate(value)

Email.error()

validate.rules.Email.error()

Url

class validate.rules.Url < Rule

Passes when the value is a syntactically valid HTTP or HTTPS URL.

Url.validate()

validate.rules.Url.validate(value)

Url.error()

validate.rules.Url.error()

Ipv4

class validate.rules.Ipv4 < Rule

Passes when the value is a valid IPv4 address.

Ipv4.validate()

validate.rules.Ipv4.validate(value)

Ipv4.error()

validate.rules.Ipv4.error()

Ipv6

class validate.rules.Ipv6 < Rule

Passes when the value is a valid IPv6 address.

Ipv6.validate()

validate.rules.Ipv6.validate(value)

Ipv6.error()

validate.rules.Ipv6.error()

Ip

class validate.rules.Ip < Rule

Passes when the value is a valid IPv4 or IPv6 address.

Ip.validate()

validate.rules.Ip.validate(value)

Ip.error()

validate.rules.Ip.error()

Uuid

class validate.rules.Uuid < Rule

Passes when the value is a valid canonical UUID (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx).

Uuid.validate()

validate.rules.Uuid.validate(value)

Uuid.error()

validate.rules.Uuid.error()

Json

class validate.rules.Json < Rule

Passes when the value is a valid JSON string.

Json.validate()

validate.rules.Json.validate(value)

Json.error()

validate.rules.Json.error()

DateRule

class validate.rules.DateRule < Rule

Passes when the value is a valid date string parseable by the date module (e.g. "2024-06-15", "2024-06-15T12:00:00Z").

DateRule.validate()

validate.rules.DateRule.validate(value)

DateRule.error()

validate.rules.DateRule.error()

After

class validate.rules.After < Rule

Passes when the value is a date string after the given reference date.

Both the value and the reference must be parseable by the date module.

Constructor

validate.rules.After(name, ref)

After.validate()

validate.rules.After.validate(value)

After.error()

validate.rules.After.error()

AfterOrEqual

class validate.rules.AfterOrEqual < Rule

Passes when the value is a date string after or equal to the given reference date.

Constructor

validate.rules.AfterOrEqual(name, ref)

AfterOrEqual.validate()

validate.rules.AfterOrEqual.validate(value)

AfterOrEqual.error()

validate.rules.AfterOrEqual.error()

Before

class validate.rules.Before < Rule

Passes when the value is a date string before the given reference date.

Constructor

validate.rules.Before(name, ref)

Before.validate()

validate.rules.Before.validate(value)

Before.error()

validate.rules.Before.error()

BeforeOrEqual

class validate.rules.BeforeOrEqual < Rule

Passes when the value is a date string before or equal to the given reference date.

Constructor

validate.rules.BeforeOrEqual(name, ref)

BeforeOrEqual.validate()

validate.rules.BeforeOrEqual.validate(value)

BeforeOrEqual.error()

validate.rules.BeforeOrEqual.error()

Timezone

class validate.rules.Timezone < Rule

Passes when the value is a real IANA timezone identifier (e.g. "UTC", "Africa/Lagos", "America/New_York"), checked against the full timezone database date.is_valid_timezone() uses.

Timezone.validate()

validate.rules.Timezone.validate(value)

Timezone.error()

validate.rules.Timezone.error()

In

class validate.rules.In < Rule

Passes when the value is contained in the given list of allowed values. Comparison is strict (type-sensitive).

Constructor

validate.rules.In(name, values)

In.validate()

validate.rules.In.validate(value)

In.error()

validate.rules.In.error()

NotIn

class validate.rules.NotIn < Rule

Passes when the value is not contained in the given list of values.

Constructor

validate.rules.NotIn(name, values)

NotIn.validate()

validate.rules.NotIn.validate(value)

NotIn.error()

validate.rules.NotIn.error()

InList

class validate.rules.InList < Rule

Passes when every item in the value list is contained in the allowed list. The field value must be a list.

Constructor

validate.rules.InList(name, values)

InList.validate()

validate.rules.InList.validate(value)

InList.error()

validate.rules.InList.error()

Accepted

class validate.rules.Accepted < Rule

Passes when the value equals one of the specified accepted values: true, "true", "yes", "on", "1", or 1.

Accepted.validate()

validate.rules.Accepted.validate(value)

Accepted.error()

validate.rules.Accepted.error()

Declined

class validate.rules.Declined < Rule

Passes when the value equals one of the specified declined values: false, "false", "no", "off", "0", or 0.

Declined.validate()

validate.rules.Declined.validate(value)

Declined.error()

validate.rules.Declined.error()

Equals

class validate.rules.Equals < Rule

Passes when the field value is strictly equal to the given expected value.

Constructor

validate.rules.Equals(name, expected)

Equals.validate()

validate.rules.Equals.validate(value)

Equals.error()

validate.rules.Equals.error()

NotEquals

class validate.rules.NotEquals < Rule

Passes when the field value is not equal to the given forbidden value.

Constructor

validate.rules.NotEquals(name, forbidden)

NotEquals.validate()

validate.rules.NotEquals.validate(value)

NotEquals.error()

validate.rules.NotEquals.error()

Same

class validate.rules.Same < Rule

Passes when the field’s value matches the value of another field in the same data dictionary. Commonly used for password confirmation.

Requires needs_data() == true so that the Schema injects _data.

Example
import validate

var schema = validate.schema({
  password:              validate.string().min_length(8),
  password_confirmation: validate.string().same('password'),
})

Constructor

validate.rules.Same(name, other_field)

Same.needs_data()

validate.rules.Same.needs_data()

Same.validate()

validate.rules.Same.validate(value)

Same.error()

validate.rules.Same.error()

Different

class validate.rules.Different < Rule

Passes when the field’s value does not match the value of another field.

Constructor

validate.rules.Different(name, other_field)

Different.needs_data()

validate.rules.Different.needs_data()

Different.validate()

validate.rules.Different.validate(value)

Different.error()

validate.rules.Different.error()

Confirmed

class validate.rules.Confirmed < Rule

Passes when the field’s value matches {name}_confirmation in the data.

This is the canonical Zuri spelling of Laravel’s confirmed rule.

Confirmed.needs_data()

validate.rules.Confirmed.needs_data()

Confirmed.validate()

validate.rules.Confirmed.validate(value)

Confirmed.error()

validate.rules.Confirmed.error()

RequiredIf

class validate.rules.RequiredIf < Rule

Passes when this field is present and non-nil only if another field in the data satisfies a given condition. If the other field equals any of the given values, this field becomes required.

Example
var rules = validate.schema({
  # "shipping_address" is required when "has_delivery" is true
  shipping_address: validate.string().required_if('has_delivery', true),
})

Constructor

validate.rules.RequiredIf(name, other_field, other_values)

RequiredIf.needs_data()

validate.rules.RequiredIf.needs_data()

RequiredIf.validate()

validate.rules.RequiredIf.validate(value)

RequiredIf.error()

validate.rules.RequiredIf.error()

RequiredUnless

class validate.rules.RequiredUnless < Rule

Passes when this field is present and non-nil unless another field equals any of the given values.

Constructor

validate.rules.RequiredUnless(name, other_field, other_values)

RequiredUnless.needs_data()

validate.rules.RequiredUnless.needs_data()

RequiredUnless.validate()

validate.rules.RequiredUnless.validate(value)

RequiredUnless.error()

validate.rules.RequiredUnless.error()

RequiredWith

class validate.rules.RequiredWith < Rule

Passes when this field is present and non-nil if any of the listed sibling fields are also present and non-nil.

Constructor

validate.rules.RequiredWith(name, fields)

RequiredWith.needs_data()

validate.rules.RequiredWith.needs_data()

RequiredWith.validate()

validate.rules.RequiredWith.validate(value)

RequiredWith.error()

validate.rules.RequiredWith.error()

RequiredWithAll

class validate.rules.RequiredWithAll < Rule

Passes when this field is present and non-nil if all of the listed sibling fields are present and non-nil.

Constructor

validate.rules.RequiredWithAll(name, fields)

RequiredWithAll.needs_data()

validate.rules.RequiredWithAll.needs_data()

RequiredWithAll.validate()

validate.rules.RequiredWithAll.validate(value)

RequiredWithAll.error()

validate.rules.RequiredWithAll.error()

RequiredWithout

class validate.rules.RequiredWithout < Rule

Passes when this field is present and non-nil if any of the listed sibling fields are absent or nil.

Constructor

validate.rules.RequiredWithout(name, fields)

RequiredWithout.needs_data()

validate.rules.RequiredWithout.needs_data()

RequiredWithout.validate()

validate.rules.RequiredWithout.validate(value)

RequiredWithout.error()

validate.rules.RequiredWithout.error()

RequiredWithoutAll

class validate.rules.RequiredWithoutAll < Rule

Passes when this field is present and non-nil if all of the listed sibling fields are absent or nil.

Constructor

validate.rules.RequiredWithoutAll(name, fields)

RequiredWithoutAll.needs_data()

validate.rules.RequiredWithoutAll.needs_data()

RequiredWithoutAll.validate()

validate.rules.RequiredWithoutAll.validate(value)

RequiredWithoutAll.error()

validate.rules.RequiredWithoutAll.error()

Prohibits

class validate.rules.Prohibits < Rule

Passes when both this field and the listed sibling fields are either all present (non-nil) or all absent (nil / blank).

Constructor

validate.rules.Prohibits(name, fields)

Prohibits.needs_data()

validate.rules.Prohibits.needs_data()

Prohibits.validate()

validate.rules.Prohibits.validate(value)

Prohibits.error()

validate.rules.Prohibits.error()

Prohibited

class validate.rules.Prohibited < Rule

Passes when this field is absent or nil. Useful to explicitly forbid a field from being submitted.

Prohibited.validate()

validate.rules.Prohibited.validate(value)

Prohibited.error()

validate.rules.Prohibited.error()

ProhibitedIf

class validate.rules.ProhibitedIf < Rule

Passes when this field is absent or nil if another field equals one of the given values.

Constructor

validate.rules.ProhibitedIf(name, other_field, other_values)

ProhibitedIf.needs_data()

validate.rules.ProhibitedIf.needs_data()

ProhibitedIf.validate()

validate.rules.ProhibitedIf.validate(value)

ProhibitedIf.error()

validate.rules.ProhibitedIf.error()

ProhibitedUnless

class validate.rules.ProhibitedUnless < Rule

Passes when this field is absent or nil unless another field equals one of the given values.

Constructor

validate.rules.ProhibitedUnless(name, other_field, other_values)

ProhibitedUnless.needs_data()

validate.rules.ProhibitedUnless.needs_data()

ProhibitedUnless.validate()

validate.rules.ProhibitedUnless.validate(value)

ProhibitedUnless.error()

validate.rules.ProhibitedUnless.error()

Distinct

class validate.rules.Distinct < Rule

Passes when the list field contains no duplicate values.

Distinct.validate()

validate.rules.Distinct.validate(value)

Distinct.error()

validate.rules.Distinct.error()

MinItems

class validate.rules.MinItems < Rule

Passes when the list field has at least min items.

Constructor

validate.rules.MinItems(name, min)

MinItems.validate()

validate.rules.MinItems.validate(value)

MinItems.error()

validate.rules.MinItems.error()

MaxItems

class validate.rules.MaxItems < Rule

Passes when the list field has at most max items.

Constructor

validate.rules.MaxItems(name, max)

MaxItems.validate()

validate.rules.MaxItems.validate(value)

MaxItems.error()

validate.rules.MaxItems.error()

Each

class validate.rules.Each < Rule

Passes when every item in the list satisfies the given Validator chain.

Example
import validate

var schema = validate.schema({
  tags: validate.list().each(validate.string().max_length(32)),
})

Constructor

validate.rules.Each(name, validator)

Each.validate()

validate.rules.Each.validate(value)

Each.error()

validate.rules.Each.error()

Nil

class validate.rules.Nil < Rule

Passes when the value is nil. Useful for asserting a field is absent.

Nil.validate()

validate.rules.Nil.validate(value)

Nil.error()

validate.rules.Nil.error()

NotNil

class validate.rules.NotNil < Rule

Passes when the value is not nil.

NotNil.validate()

validate.rules.NotNil.validate(value)

NotNil.error()

validate.rules.NotNil.error()

CustomRule

class validate.rules.CustomRule < Rule

Custom rule backed by a caller-supplied function.

Used internally by Validator.custom(). The function receives the field value and must return true to pass.

Constructor

validate.rules.CustomRule(name, fn, message)

CustomRule.validate()

validate.rules.CustomRule.validate(value)

CustomRule.error()

validate.rules.CustomRule.error()

CustomRuleWithData

class validate.rules.CustomRuleWithData < Rule

Custom rule that also receives the full data dictionary.

Used internally by Validator.custom_with_data(). The function receives (value, data) and must return true to pass.

Constructor

validate.rules.CustomRuleWithData(name, fn, message)

CustomRuleWithData.needs_data()

validate.rules.CustomRuleWithData.needs_data()

CustomRuleWithData.validate()

validate.rules.CustomRuleWithData.validate(value)

CustomRuleWithData.error()

validate.rules.CustomRuleWithData.error()