Built-In Functions

All built in functions have built in error messages.

If you think this list should be expanded, feel free to submit a feature request!

Validation Functions

The below functions should be used like:

let schema = {
    key: Prudence.isInteger
}
Prudence.isInteger

Checks whether a value is a safe integer or not.

Note

This is an alias for Number.isSafeInteger.

Prudence.isPositiveInteger

Checks whether a value is a positive integer or not. 0 is regarded as positive.

Prudence.isPositiveNonZeroInteger

Checks whether a value is a positive, non-zero integer or not.

Validation Function Generators

The below functions should be used as follows:

let schema = {
    key: Prudence.isBoundedInteger(1, 10)
}

This is because these are functions that return validation functions, rather than simply being validation functions.

The arguments they expect are detailed in their below definitions.

Prudence.isIn(...values)

Returns a function that checks whether the value given is inside the list of arguments.

Arguments
  • ...values – Rest Parameter: the values to check against.

Returns

Validation Function

Example usage:

let schema = {
    fruit: Prudence.isIn("apple", "banana"),
    alternatively: Prudence.isIn(["apple", "banana"])
}

Note

You can also pass an array as a first and only argument, and it will automatically be expanded.

Prudence.isBoundedInteger(lower, upper)

Returns a function that checks whether a value is an integer and between the two arguments.

This is inclusive on both ends.

Arguments
  • lower (integer) – The number the object’s value must be greater than or equal to.

  • upper (integer) – The number the object’s value must be less than or equal to.

Returns

Validation Function

Prudence.isBoundedString(lower, upper)

Returns a function that checks whether a value is a string and its length is between the two arguments.

This is inclusive on both ends.

Arguments
  • lower (integer) – The number the string’s length must be greater than or equal to.

  • upper (integer) – The number the string’s length must be less than or equal to.

Returns

Validation Function

Prudence.regex(regex)

Returns a function that checks whether the input is a string and matches the given regex.

This exists because regex.test coerces input to a string, even when thats not what we want.

Arguments
  • regex (regex) – The number the string’s length must be greater than or equal to.

Returns

Validation Function

Warning

The below functions exist because the <, <=, > and >= operators in JS are not strict (think ==) and convert non-numeric input into numbers, when that’s almost certainly not what you’d want.

Prudence.gt(num)

Returns a function that checks whether a value is a number and greater than the argument.

Arguments
  • num (number) – The number the object’s value must be greater than.

Returns

Validation Function

Prudence.gte(num)

Returns a function that checks whether a value is a number and greater than or equal to the argument.

Arguments
  • num (number) – The number the object’s value must be less than or equal to.

Returns

Validation Function

Prudence.lt(num)

Returns a function that checks whether a value is a number and less than the argument.

Arguments
  • num (number) – The number the object’s value must be greater than.

Returns

Validation Function

Prudence.lte(num)

Returns a function that checks whether a value is a number and less than or equal to the argument.

Arguments
  • num (number) – The number the object’s value must be less than or equal to.

Returns

Validation Function

Prudence.gtInt(num)

Returns a function that checks whether a value is an integer and greater than the argument.

Arguments
  • num (number) – The number the object’s value must be greater than.

Returns

Validation Function

Prudence.gteInt(num)

Returns a function that checks whether a value is an integer and greater than or equal to the argument.

Arguments
  • num (number) – The number the object’s value must be less than or equal to.

Returns

Validation Function

Prudence.ltInt(num)

Returns a function that checks whether a value is an integer and less than the argument.

Arguments
  • num (number) – The number the object’s value must be greater than.

Returns

Validation Function

Prudence.lteInt(num)

Returns a function that checks whether a value is an integer and less than or equal to the argument.

Arguments
  • num (number) – The number the object’s value must be less than or equal to.

Returns

Validation Function