HTT Documentation

htt.is

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.

  • AValidatorFn

    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.

    Definition
    fun (value: any): boolean, string?
  • APredicateFn

    Function which, given a value returns true if test succeeds, false otherwise.

    Definition
    fun (value: any) boolean
  • Fnnull(any) → boolean, string?

    True iff. `value` is null.

    Parameters
    • value: any
      • some input value to test
    Returns
    • boolean
      • a boolean, true if the value was deemed valid
    • string?
      • error message if validation failed
  • Fnboolean(any) → boolean, string?

    True iff. `value` is a boolean.

    Parameters
    • value: any
      • some input value to test
    Returns
    • boolean
      • a boolean, true if the value was deemed valid
    • string?
      • error message if validation failed
  • Fnnumber(any) → boolean, string?

    True iff. `value` is a number.

    Parameters
    • value: any
      • some input value to test
    Returns
    • boolean
      • a boolean, true if the value was deemed valid
    • string?
      • error message if validation failed
  • Fnstring(any) → boolean, string?

    True iff. `value` is a string.

    Parameters
    • value: any
      • some input value to test
    Returns
    • boolean
      • a boolean, true if the value was deemed valid
    • string?
      • error message if validation failed
  • Fnuserdata(any) → boolean, string?

    True iff. `value` is a userdata object. userdata objects are raw blocks of memory created by an external language like C

    Parameters
    • value: any
      • some input value to test
    Returns
    • boolean
      • a boolean, true if the value was deemed valid
    • string?
      • error message if validation failed
  • Fnfn(any) → boolean, string?

    True iff. `value` is a function.

    Parameters
    • value: any
      • some input value to test
    Returns
    • boolean
      • a boolean, true if the value was deemed valid
    • string?
      • error message if validation failed
  • Fncallable(any) → boolean, string?

    True iff. `value` is a function.

    Parameters
    • value: any
      • some input value to test
    Returns
    • boolean
      • a boolean, true if the value was deemed valid
    • string?
      • error message if validation failed
  • Fntable(any) → boolean, string?

    True iff. `value` is a table.

    Note that tables can *also* be callable if they implement the __call metafunction

    Parameters
    • value: any
      • some input value to test
    Returns
    • boolean
      • a boolean, true if the value was deemed valid
    • string?
      • error message if validation failed
  • Fnoptional(ValidatorFn) → ValidatorFn

    Returns a function which tests input value iff. not nil

    Parameters
    • validator: ValidatorFn
      • a validate function like htt.is.null
    Returns
    • ValidatorFn
      • validator function
  • Fnpred(PredicateFn, string) → ValidatorFn

    Wraps `pred` and returns a validator function.

    Parameters
    • pred: PredicateFn
      • a predicate function (a function taking a single argument and returning a bool)
    • label: string
      • a label, used to the describe the kind of test `pred` performs, used in the error message if validation fails
    Returns
    • ValidatorFn
      • validator function
  • Fnany(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.

    Parameters
    • ...: ValidatorFn
      • one or more validator functions to test again
    Returns
    • ValidatorFn
      • validator function
  • Fnall(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.

    Parameters
    • ...: ValidatorFn
      • one or more validator functions to test again
    Returns
    • ValidatorFn
      • validator function
  • Fnlist_of(ValidatorFn) → ValidatorFn

    Wraps the provided validator, returning a validator which succeeds if provided a list where each element is valid according to `validator`.

    Parameters
    • validator: ValidatorFn
      • validator to apply to each element of the list
    Returns
    • ValidatorFn
      • validator function
  • Fntable_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.

    Parameters
    • spec: table
      • a table whose keys point to validator functions
    Returns
    • ValidatorFn
      • validator function
  • Fntable_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`.

    Parameters
    • keyValidator: ValidatorFn
      • validator to apply to each key
    • valValidator: ValidatorFn
      • validator to apply to each value
    Returns
    • ValidatorFn
      • validator function