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.validators

import validate

Everything here is re-exported by validate, so import validate is enough and the names are called as validate.*. Importing validate.validators on its own works too and reaches the same definitions.

Top-level convenience functions that create a fresh Validator instance with one rule pre-applied. Every rule exposed by the Validator class has a corresponding function here so callers never need to import Validator directly for common cases.

Every function returns a Validator, so additional rules can be chained immediately:

import validate

validate.required().string().email().max_length(254)
validate.integer().gte(0).lte(150)
validate.list().min_items(1).each(validate.string())
validate.string().starts_with('ZURI-').length(12)

Functions

value()

validate.value() -> Validator

Returns a bare Validator with no rules pre-applied. Useful when you want to compose rules manually or start with a custom rule.

Returns Validator

required()

validate.required() -> Validator

The field must be present, non-nil, and non-empty.

Returns Validator

nullable()

validate.nullable() -> Validator

A nil value passes all subsequent rules without evaluation. Combine with sometimes() for a fully optional-nullable field.

Returns Validator

sometimes()

validate.sometimes() -> Validator

Skips the entire rule chain when the field is absent from the data dictionary or its value is nil / blank. Use this to mark fields as optional while still validating them when they are present.

Returns Validator

string()

validate.string() -> Validator

The value must be a string.

Returns Validator

number()

validate.number() -> Validator

The value must be a number (integer or float).

Returns Validator

integer()

validate.integer() -> Validator

The value must be an integer (no fractional part). Also accepts string representations of integers.

Returns Validator

boolean()

validate.boolean() -> Validator

The value must be boolean-like: true, false, "true", "false", "1", "0", 1, or 0.

Returns Validator

list()

validate.list() -> Validator

The value must be a list.

Returns Validator

dict()

validate.dict() -> Validator

The value must be a dictionary.

Returns Validator

numeric()

validate.numeric() -> Validator

The value must be numeric: either a number type or a string that converts cleanly to a number.

Returns Validator

size()

validate.size(n) -> Validator

The value’s size must equal exactly n.

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

Parameters

  • n (number)

Returns Validator

min()

validate.min(min) -> Validator

The value’s size must be at least min.

Parameters

  • min (number)

Returns Validator

max()

validate.max(max) -> Validator

The value’s size must be at most max.

Parameters

  • max (number)

Returns Validator

between()

validate.between(min, max) -> Validator

The value’s size must be between min and max (inclusive).

Parameters

  • min (number)
  • max (number)

Returns Validator

length()

validate.length(n) -> Validator

The string’s character count must equal exactly n.

Parameters

  • n (number)

Returns Validator

min_length()

validate.min_length(min) -> Validator

The string must be at least min characters long.

Parameters

  • min (number)

Returns Validator

max_length()

validate.max_length(max) -> Validator

The string must be at most max characters long.

Parameters

  • max (number)

Returns Validator

length_between()

validate.length_between(min, max) -> Validator

The string’s length must be between min and max characters (inclusive).

Parameters

  • min (number)
  • max (number)

Returns Validator

gt()

validate.gt(n) -> Validator

The numeric value must be greater than n.

Parameters

  • n (number)

Returns Validator

gte()

validate.gte(n) -> Validator

The numeric value must be greater than or equal to n.

Parameters

  • n (number)

Returns Validator

lt()

validate.lt(n) -> Validator

The numeric value must be less than n.

Parameters

  • n (number)

Returns Validator

lte()

validate.lte(n) -> Validator

The numeric value must be less than or equal to n.

Parameters

  • n (number)

Returns Validator

positive()

validate.positive() -> Validator

The numeric value must be strictly positive (greater than zero).

Returns Validator

negative()

validate.negative() -> Validator

The numeric value must be strictly negative (less than zero).

Returns Validator

positive_or_zero()

validate.positive_or_zero() -> Validator

The numeric value must be zero or positive.

Returns Validator

negative_or_zero()

validate.negative_or_zero() -> Validator

The numeric value must be zero or negative.

Returns Validator

multiple_of()

validate.multiple_of(n) -> Validator

The numeric value must be a multiple of n.

Parameters

  • n (number)

Returns Validator

alpha()

validate.alpha() -> Validator

The string must contain only ASCII alphabetic characters (a-z, A-Z).

Returns Validator

alpha_num()

validate.alpha_num() -> Validator

The string must contain only ASCII alphanumeric characters.

Returns Validator

alpha_dash()

validate.alpha_dash() -> Validator

The string must contain only ASCII alphanumeric characters, hyphens, and underscores.

Returns Validator

starts_with()

validate.starts_with(prefixes) -> Validator

The string must start with one of the given prefixes.

Parameters

  • prefixes (string|list)

Returns Validator

doesnt_start_with()

validate.doesnt_start_with(prefixes) -> Validator

The string must not start with any of the given prefixes.

Parameters

  • prefixes (string|list)

Returns Validator

ends_with()

validate.ends_with(suffixes) -> Validator

The string must end with one of the given suffixes.

Parameters

  • suffixes (string|list)

Returns Validator

doesnt_end_with()

validate.doesnt_end_with(suffixes) -> Validator

The string must not end with any of the given suffixes.

Parameters

  • suffixes (string|list)

Returns Validator

contains()

validate.contains(needle) -> Validator

The string must contain the given substring.

Parameters

  • needle (string)

Returns Validator

doesnt_contain()

validate.doesnt_contain(needle) -> Validator

The string must not contain the given substring.

Parameters

  • needle (string)

Returns Validator

regex()

validate.regex(pattern) -> Validator

The string must match the given regular expression pattern.

Parameters

  • pattern (string)

Returns Validator

not_regex()

validate.not_regex(pattern) -> Validator

The string must not match the given regular expression pattern.

Parameters

  • pattern (string)

Returns Validator

lowercase()

validate.lowercase() -> Validator

The string must be entirely lowercase.

Returns Validator

uppercase()

validate.uppercase() -> Validator

The string must be entirely uppercase.

Returns Validator

not_blank()

validate.not_blank() -> Validator

The string must not consist entirely of whitespace.

Returns Validator

email()

validate.email() -> Validator

The value must be a syntactically valid email address.

Returns Validator

url()

validate.url() -> Validator

The value must be a valid HTTP or HTTPS URL.

Returns Validator

ipv4()

validate.ipv4() -> Validator

The value must be a valid IPv4 address.

Returns Validator

ipv6()

validate.ipv6() -> Validator

The value must be a valid IPv6 address.

Returns Validator

ip()

validate.ip() -> Validator

The value must be a valid IPv4 or IPv6 address.

Returns Validator

uuid()

validate.uuid() -> Validator

The value must be a valid canonical UUID (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx).

Returns Validator

json()

validate.json() -> Validator

The value must be a valid JSON string.

Returns Validator

date()

validate.date() -> Validator

The value must be a valid date string parseable by the date module.

Returns Validator

after()

validate.after(ref) -> Validator

The value must be a date string after ref.

Parameters

  • ref (string) — A date string parseable by the date module.

Returns Validator

after_or_equal()

validate.after_or_equal(ref) -> Validator

The value must be a date string after or equal to ref.

Parameters

  • ref (string)

Returns Validator

before()

validate.before(ref) -> Validator

The value must be a date string before ref.

Parameters

  • ref (string)

Returns Validator

before_or_equal()

validate.before_or_equal(ref) -> Validator

The value must be a date string before or equal to ref.

Parameters

  • ref (string)

Returns Validator

timezone()

validate.timezone() -> Validator

The value must be a valid timezone identifier recognised by the date module (e.g. "UTC", "Africa/Lagos", "America/New_York").

Returns Validator

is_in()

validate.is_in(values) -> Validator

The value must be one of the given allowed values (strict comparison).

Parameters

  • values (list)

Returns Validator

not_in()

validate.not_in(values) -> Validator

The value must not be one of the given forbidden values.

Parameters

  • values (list)

Returns Validator

in_list()

validate.in_list(values) -> Validator

Every item in the list value must be contained in values.

Parameters

  • values (list)

Returns Validator

accepted()

validate.accepted() -> Validator

The value must be one of the accepted truthy representations: true, "true", "yes", "on", "1", 1.

Returns Validator

declined()

validate.declined() -> Validator

The value must be one of the declined falsy representations: false, "false", "no", "off", "0", 0.

Returns Validator

equals()

validate.equals(expected) -> Validator

The value must strictly equal expected.

Parameters

  • expected (any)

Returns Validator

not_equals()

validate.not_equals(forbidden) -> Validator

The value must not equal forbidden.

Parameters

  • forbidden (any)

Returns Validator

same()

validate.same(other_field) -> Validator

The value must match the value of other_field in the same data dictionary. Commonly used to confirm passwords.

Parameters

  • other_field (string)

Returns Validator

different()

validate.different(other_field) -> Validator

The value must differ from the value of other_field.

Parameters

  • other_field (string)

Returns Validator

confirmed()

validate.confirmed() -> Validator

The value must match the {field_name}_confirmation sibling field.

Returns Validator

required_if()

validate.required_if(other_field, other_values) -> Validator

The field becomes required when other_field equals any value in other_values.

Parameters

  • other_field (string)
  • other_values (any|list)

Returns Validator

required_unless()

validate.required_unless(other_field, other_values) -> Validator

The field becomes required unless other_field equals any value in other_values.

Parameters

  • other_field (string)
  • other_values (any|list)

Returns Validator

required_with()

validate.required_with(fields) -> Validator

The field becomes required when any of the listed sibling fields are present and non-blank.

Parameters

  • fields (string|list)

Returns Validator

required_with_all()

validate.required_with_all(fields) -> Validator

The field becomes required when all of the listed sibling fields are present and non-blank.

Parameters

  • fields (string|list)

Returns Validator

required_without()

validate.required_without(fields) -> Validator

The field becomes required when any of the listed sibling fields are absent or blank.

Parameters

  • fields (string|list)

Returns Validator

required_without_all()

validate.required_without_all(fields) -> Validator

The field becomes required when all of the listed sibling fields are absent or blank.

Parameters

  • fields (string|list)

Returns Validator

prohibits()

validate.prohibits(fields) -> Validator

When this field is present, none of the listed sibling fields may also be present.

Parameters

  • fields (string|list)

Returns Validator

prohibited()

validate.prohibited() -> Validator

This field must be absent or nil: it is never permitted.

Returns Validator

prohibited_if()

validate.prohibited_if(other_field, other_values) -> Validator

The field must be absent when other_field equals any of other_values.

Parameters

  • other_field (string)
  • other_values (any|list)

Returns Validator

prohibited_unless()

validate.prohibited_unless(other_field, other_values) -> Validator

The field must be absent unless other_field equals any of other_values.

Parameters

  • other_field (string)
  • other_values (any|list)

Returns Validator

distinct()

validate.distinct() -> Validator

The list must not contain duplicate values.

Returns Validator

min_items()

validate.min_items(min) -> Validator

The list must have at least min items.

Parameters

  • min (number)

Returns Validator

max_items()

validate.max_items(max) -> Validator

The list must have at most max items.

Parameters

  • max (number)

Returns Validator

each()

validate.each(validator) -> Validator

Every item in the list must pass the given Validator chain.

validate.each(validate.string().email())
# Equivalent to: validate.list().each(validate.string().email())

Parameters

  • validator (Validator)

Returns Validator

is_nil()

validate.is_nil() -> Validator

The value must be nil.

Returns Validator

not_nil()

validate.not_nil() -> Validator

The value must not be nil.

Returns Validator

custom()

validate.custom(fn, message) -> Validator

Validates with an inline anonymous function. The function receives the field value and must return true to pass.

validate.custom(@(value) {
  return value.starts_with('ZURI-')
}, 'Must be a valid Zuri identifier.')

Parameters

  • fn (function) — @(value) -> bool
  • message (string) — Optional custom error message.

Returns Validator

custom_with_data()

validate.custom_with_data(fn, message) -> Validator

Validates with an inline function that also receives the full data dictionary, enabling cross-field logic without subclassing Rule.

validate.custom_with_data(@(value, data) {
  return value < data.get('max_price', 0)
}, 'Must be less than max_price.')

Parameters

  • fn (function) — @(value, data) -> bool
  • message (string) — Optional custom error message.

Returns Validator

use()

validate.use(rule_class) -> Validator

Attaches a pre-defined Rule subclass (the class itself, not an instance) directly. The class must extend Rule and accept only name as its constructor argument.

import validate { Rule }

class Palindrome < Rule {
  validate(value) {
    if !is_string(value) return false
    var s = value.lower()
    return s == s.reverse()
  }
  error() { return 'The ${self.name} field must be a palindrome.' }
}

validate.use(Palindrome).string()

Parameters

  • rule_class (class) — A class that extends Rule.

Returns Validator