Validate data through composable predicate functions describing the shape of data.
You get to declaratively describe, through composable validation functions, the valid shape(s) of data, useful for constraining the data input (the model) to your templates.
Function used to determine if value conforms to some specification
A validator function returns a pair of values, a boolean which is true iff the value conforms to the specification and (optionally) a string message, describing the issue if the value does not conform.
Function which, given a value returns true if test succeeds, false otherwise.
null(any) → boolean, string?
True iff. `value` is null.
boolean(any) → boolean, string?
True iff. `value` is a boolean.
number(any) → boolean, string?
True iff. `value` is a number.
string(any) → boolean, string?
True iff. `value` is a string.
userdata(any) → boolean, string?
True iff. `value` is a userdata object. userdata objects are raw blocks of memory created by an external language like C
fn(any) → boolean, string?
True iff. `value` is a function.
callable(any) → boolean, string?
True iff. `value` is a function.
table(any) → boolean, string?
True iff. `value` is a table.
Note that tables can *also* be callable if they implement the __call metafunction
optional(ValidatorFn) → ValidatorFn
Returns a function which tests input value iff. not nil
pred(PredicateFn, string) → ValidatorFn
Wraps `pred` and returns a validator function.
any(ValidatorFn) → ValidatorFn
Wraps the provided validator functions, returning a new validator which succeeds if any of the provided validators apply to the provided value.
Read as: if any of these validator apply, the data is valid.
all(ValidatorFn) → ValidatorFn
Wraps the provided validator functions, returning a new validator which succeeds if all of the provided validators apply to the provided value.
Read as: if all of these validators apply, the data is valid.
list_of(ValidatorFn) → ValidatorFn
Wraps the provided validator, returning a validator which succeeds if provided a list where each element is valid according to `validator`.
table_with(table) → ValidatorFn
Returns a validator which takes an input value and returns true if value is a table where for each entry in `spec`, there is a value which conforms to the corresponding validator
Read as: I want to validate associative tables, I don't care about *all* their entries, but for some of the keys, I want to associate validator functions to constrain valid values. An input value (a table) is valid if all entries constrained by a validator function in `spec` succeed their validaton.
table_of(ValidatorFn, ValidatorFn) → ValidatorFn
Returns a validator which succeeds if the input is a table whose keys all succeed when tested against `keyValidator` and whose values all succeed when tested against `valValidator`.