A collection of data validation tools.
version 5 includes several breaking changes from version 4, mostly designed to improve interoperability with TS
optionalno longer acceptsnull, justundefined. This is to bring it inline with TypeScript and many other libraries' interpretation of optional values.isShapenow converts allT | undefinedproperties to optional properties in the validator schema type. This eliminates the requirement to explicitly define "optional" properties asundefined.acceptsandreturnshave now been moved fromvet/utilstovet/functions, where they should have been in the first place.
in addition, v5 includes:
- a new
isTupleutility
- vet :
object
- exactType()
Trigger a compiler error when a value is not an exact type.
- exactType()
Trigger a compiler error when a value is not an exact type.
Kind: global namespace
- vet :
object- .arrays :
object - .booleans :
object - .dates :
object - .functions :
object - .numbers :
object- .isBetween(lower, upper) ⇒
function- .exclusive ⇒
function - .inclusive ⇒
function
- .exclusive ⇒
- .isFinite(val) ⇒
- .isGreaterThan(bound) ⇒
function- .exclusive ⇒
function - .inclusive ⇒
function
- .exclusive ⇒
- .isInteger(val) ⇒
- .isLessThan(bound) ⇒
function- .exclusive ⇒
function - .inclusive ⇒
function
- .exclusive ⇒
- .isNegative(val) ⇒
- .isNonZero(val) ⇒
- .isNumber(val) ⇒
- .isPositive(val) ⇒
- .isZero(val) ⇒
- .isBetween(lower, upper) ⇒
- .objects :
object - .strings :
object - .utils :
object - .equals(eq) ⇒
- .exists(val) ⇒
- .isAllOf(...eq) ⇒
- .isAny(val) ⇒
- .isNoneOf(...eq) ⇒
- .isNot(validator) ⇒
- .isNotNull(val) ⇒
- .isNotNullOrUndefined(val) ⇒
- .isNotUndefined(val) ⇒
- .isNull(val) ⇒
- .isNullOrUndefined(val) ⇒
- .isOneOf(...eq) ⇒
- .isUndefined(val) ⇒
- .nullable(validator) ⇒
- .optional(validator) ⇒
- .arrays :
Kind: static namespace of vet
- .arrays :
object
Checks to see if a value is an array
Kind: static property of arrays
Returns: true if the value is an array
Params
- val - the value to check
Example
import isArray from 'vet/arrays/isArray';
isArray(null); // returns false
isArray({}); // returns false
isArray([]); // returns trueBuilds an array validator that checks the children of the array
Kind: static method of arrays
Returns: a function that returns true if the value is an array, and all of the children pass the validator
Params
- val - the validator function run against the array children
Example
import isString from 'vet/strings/isString';
import isArrayOf from 'vet/arrays/isArrayOf';
let isStringArray = isArrayOf(isString);
isStringArray(null); // returns false
isStringArray({}); // returns false
isStringArray([ 1, 2, 3 ]); // returns false
isStringArray([]); // returns true
isStringArray([ '1', '2', '3' ]); // returns trueConstructor to build an array length validator
Kind: static method of arrays
Returns: a function that returns true if the value is an array of length len
Params
- len - the length the array shouldbe
Example
import isLength from 'vet/arrays/isLength';
let isLength3 = isLength(3);
isLength3(null); // returns false
isLength3({}); // returns false
isLength3([ 1, 2 ]); // returns false
isLength3([ '1', '2', '3' ]); // returns trueKind: static namespace of vet
- .booleans :
object
Checks to see if a value is a boolean
Kind: static method of booleans
Returns: true if the value is a boolean
Params
- val - the value to check
Checks to see if a value is strictly false
Kind: static method of booleans
Returns: true if the value is strictly false
Params
- val - the value to check
Example
import isFalse from 'vet/booleans/isFalse';
isFalse(null); // returns false
isFalse(true); // returns false
isFalse(false); // returns trueChecks to see if a value is loosely false (falsy)
Kind: static method of booleans
Returns: true if the value is loosely false
Params
- val - the value to check
Example
import isFalsy from 'vet/booleans/isFalsy';
isFalse(true); // returns false
isFalsy(null); // returns true
isFalsy(false); // returns trueChecks to see if a value is strictly true
Kind: static method of booleans
Returns: true if the value is strictly true
Params
- val - the value to check
Example
import isTrue from 'vet/booleans/isTrue';
isTrue(null); // returns false
isTrue(false); // returns false
isTrue(true); // returns trueChecks to see if a value is loosely true (truthy)
Kind: static method of booleans
Returns: true if the value loosely true
Params
- val - the value to check
Example
import isTruthy from 'vet/booleans/isTruthy';
isTruthy(null); // returns false
isTruthy(false); // returns false
isTruthy({}); // returns true
isTruthy(true); // returns trueKind: static namespace of vet
- .dates :
object
Checks to see if a value is a Date
Kind: static method of dates
Returns: true if the value is a Date
Params
- val - the value to check
Example
import isDate from 'vet/dates/isDate';
isDate(null); // returns false
isDate({}); // returns false
isDate(new Date()); // returns trueChecks to see if a value is a valid Date object
Kind: static method of dates
Returns: true if the value is a valid Date object
Params
- val - the value to check
Example
import isValidDate from 'vet/dates/isValidDate';
isValidDate(null); // returns false
isValidDate({}); // returns false
isValidDate(new Date(NaN)); // returns false
isValidDate(new Date()); // returns trueKind: static namespace of vet
- .functions :
object
Wraps a function in a validator which checks its arguments, and throws an error if the arguments are bad.
Kind: static method of functions
Returns: a wrapped function that throws an error if the arguments do not pass validation
Params
- func - the function to wrap
- validator - the validator function. This gets passed the arguments as an array
- message - an optional message string to pass into the error thrown
Checks to see if a value is a function
Kind: static method of functions
Returns: true if the value is a function
Params
- val - the value to check
Example
import isFunction from 'vet/functions/isFunction';
isFunction(null); // returns false
isFunction({}); // returns false
isFunction(function (){}); // returns trueWraps a function in a validator which checks its return value, and throws an error if the return value is bad.
Kind: static method of functions
Returns: a wrapped function that throws an error if the return value doed not pass validation
Params
- func - the function to wrap
- validator - the validator function. This gets passed the return value
- message - an optional message string to pass into the error thrown
Kind: static namespace of vet
- .numbers :
object- .isBetween(lower, upper) ⇒
function- .exclusive ⇒
function - .inclusive ⇒
function
- .exclusive ⇒
- .isFinite(val) ⇒
- .isGreaterThan(bound) ⇒
function- .exclusive ⇒
function - .inclusive ⇒
function
- .exclusive ⇒
- .isInteger(val) ⇒
- .isLessThan(bound) ⇒
function- .exclusive ⇒
function - .inclusive ⇒
function
- .exclusive ⇒
- .isNegative(val) ⇒
- .isNonZero(val) ⇒
- .isNumber(val) ⇒
- .isPositive(val) ⇒
- .isZero(val) ⇒
- .isBetween(lower, upper) ⇒
construct a validator to check if a value is between two numbers
Kind: static method of numbers
Returns: function - - a validator function
Params
- lower
number- the lower boundary value to check against - upper
number- the upper boundary value to check against
- .isBetween(lower, upper) ⇒
function- .exclusive ⇒
function - .inclusive ⇒
function
- .exclusive ⇒
Kind: static property of isBetween
Returns: function - - a validator function
Params
- lower
number- the lower boundary value to check against - upper
number- the upper boundary value to check against
Kind: static property of isBetween
Returns: function - - a validator function
Params
- lower
number- the lower boundary value to check against - upper
number- the upper boundary value to check against
Checks to see if a value is a finite number
Kind: static method of numbers
Returns: true if the value is a finite number
Params
- val - the value to check
construct a validator to check if a value is greater than a number
Kind: static method of numbers
Returns: function - - a validator function
Params
- bound
number- the boundary value to check agains
- .isGreaterThan(bound) ⇒
function- .exclusive ⇒
function - .inclusive ⇒
function
- .exclusive ⇒
Kind: static property of isGreaterThan
Returns: function - - a validator function
Params
- bound
number- the boundary value to check against
Kind: static property of isGreaterThan
Returns: function - - a validator function
Params
- bound
number- the boundary value to check against
Checks to see if a value is an integer
Kind: static method of numbers
Returns: true if the value is an integer
Params
- val - the value to check
construct a validator to check if a value is less than a number
Kind: static method of numbers
Returns: function - - a validator function
Params
- bound
number- the boundary value to check agains
- .isLessThan(bound) ⇒
function- .exclusive ⇒
function - .inclusive ⇒
function
- .exclusive ⇒
Kind: static property of isLessThan
Returns: function - - a validator function
Params
- bound
number- the boundary value to check against
Kind: static property of isLessThan
Returns: function - - a validator function
Params
- bound
number- the boundary value to check against
Checks to see if a value is a negative number
Kind: static method of numbers
Returns: true if the value is a negative number
Params
- val - the value to check
Checks to see if a value is a nonzero number
Kind: static method of numbers
Returns: true if the value is a nonzero number
Params
- val - the value to check
Checks to see if a value is a number
Kind: static method of numbers
Returns: true if the value is a number
Params
- val - the value to check
Checks to see if a value is a positive number
Kind: static method of numbers
Returns: true if the value is a positive number
Params
- val - the value to check
Checks to see if a value is zero
Kind: static method of numbers
Returns: true if the value is zero
Params
- val - the value to check
Kind: static namespace of vet
- .objects :
object
Checks to see if a value is an object and inherits a prototype from a constructor function
Kind: static method of objects
Returns: a validator to check if a value inherits that prototype
Params
- con - the constructor function to check against
Checks to see if a value is an object
Kind: static method of objects
Returns: true if the value is an object
Params
- val - the value to check
Builds an object validator that checks the properties of the object NOTE: This only checks enumerable properties
Kind: static method of objects
Returns: a function that returns true if the value is an object, and all of the object properties pass the validator
Params
- validator - the validator function run against the array children
Builds a function to check an object against a schema object
A schema object consists of an object with child object, functions, and values
The schema matching process is as follows:
-
For each child in the schema object, match it against the corresponding child in the value to be checked
-
If the schema child is a function, treat it as a validator function
-
If the schema child is an object, recursively call the schema matching
-
If the schema child is anything else, check for strict equality
Kind: static method of objects
Returns: a validator function that takes in a value val, and returns true if val matches the object schema
Params
- schema - the object schema to check
Example
import isString from 'vet/strings/isString';
import isNumber from 'vet/numbers/isNumber';
import isBoolean from 'vet/booleans/isBoolean';
import isShape from 'vet/objects/isShape';
let isPerson = isShape({
name: isString,
age: isNumber,
alive: isBoolean,
});
// returns false
isPerson({});
// returns false, no 'alive' flag
isPerson({ name: 'John Doe', age: 12 });
// returns true
isPerson({ name: 'John Doe', age: 12, alive: true });Builds a function to check an object against a schema object
This function works similarly to vet/objects/isShape,
but it also checks to make sure every value in the object to check
has a corresponding validator in the schema
Kind: static method of isShape
Returns: a validator function that takes in a value val, and returns true if val matches the object schema exactly
Params
- schema - the object schema to check
Example
import isString from 'vet/strings/isString';
import isNumber from 'vet/numbers/isNumber';
import isBoolean from 'vet/booleans/isBoolean';
import isShape from 'vet/objects/isShape';
let isPerson = isShape.exact({
name: isString,
age: isNumber,
alive: isBoolean,
});
// returns false
isPerson({});
// returns false, no 'alive' flag
isPerson({ name: 'John Doe', age: 12 });
// returns false, extra property 'gender'
isPerson({ name: 'John Doe', age: 12, alive: true, gender: 'm' });
// returns true
isPerson({ name: 'John Doe', age: 12, alive: true });Builds a function to check an object against a schema object
This function works similarly to vet/objects/isShape,
but it only checks if the value is a "partial match" to the schema, i.e. properties can be undefined
Kind: static method of isShape
Returns: a validator function that takes in a value val, and returns true if val matches the object schema exactly
Params
- schema - the object schema to check
Example
import isString from 'vet/strings/isString';
import isNumber from 'vet/numbers/isNumber';
import isBoolean from 'vet/booleans/isBoolean';
import isShape from 'vet/objects/isShape';
let isPerson = isShape.pattial({
name: isString,
age: isNumber,
contact: {
email: isString,
phone: isString,
},
});
// returns true
isPerson({});
// returns true
isPerson({ name: 'John Doe', age: 12 });
// returns true, empty contact still passes
isPerson({ name: 'John Doe', age: 12, contact: { } });
// returns true, partial contact still passes
isPerson({ name: 'John Doe', age: 12, contact: { phone: '00000000' } });
// returns false, age is not a number
isPerson({ name: 'John Doe', age: '12' });Kind: static namespace of vet
- .strings :
object
Checks to see if a value is an empty string
Kind: static method of strings
Returns: true if val is an empty string
Params
- val - the value to check
Builds a function to check if a value is a string of length len
Kind: static method of strings
Returns: a function that takes in a value val, and returns true if val is a string of length len
Params
- len - the desired length of string
Checks to see if a value is a non-empty string
Kind: static method of strings
Returns: true if val is a non-empty string
Params
- val - the value to check
Checks to see if a value is probably a valid base64 string
Kind: static method of strings
Returns: true if val is probably a valid base64 string
Params
- val - the value to check
Checks to see if a value is probably a valid data URL
Kind: static method of strings
Returns: true if val is probably a valid data URL
Params
- val - the value to check
Checks to see if a value is probably a valid email
Kind: static method of strings
Returns: true if val is probably a valid email
Params
- val - the value to check
Checks to see if a value is probably a valid URL
Kind: static method of strings
Returns: true if val is probably a valid URL
Params
- val - the value to check
Checks to see if a value is a string
Kind: static method of strings
Returns: true if val is a string
Params
- val - the value to check
Builds a function that checks to see if a value matches a regular expression
Kind: static method of strings
Returns: a function that takes in a value val, and returns true if it is a string that matches regex
Params
- regex - the regular expression to check against
Kind: static namespace of vet
- .utils :
object
A utility function for building a react-compatible assertion from a Vet validator
This is useful for some libraries (like React) that expect assertion-style validation.
Kind: static property of utils
Returns: a function that returns null if the arguments pass validation, or throws an error if they do not
Params
- validator - the validator to wrap
- message - an optional message string to pass into the error
Wraps a validator, and throws an error if it returns false.
This is useful for some code that expects assertion-style validation.
Kind: static method of utils
Returns: a function that returns null if the arguments pass validation, or throws an error if they do not
Params
- validator - the validator to wrap
- message - an optional message string to pass into the error
Builds an curried equal function
Kind: static method of vet
Returns: a function that takes in one parameter val, and returns true if val === eq
Params
- eq - value to check equality against
Example
import equals from 'vet/equals';
let is3 = equals(3);
is3(null); // returns false
is3({}); // returns false
is3(3); // returns trueAlias for vet/isNotNullOrUndefined
Kind: static method of vet
Returns: true if val is not null or undefined
Params
- val - value to check
Example
import exists from 'vet/exists';
exists(null); // returns false
exists(undefined); // returns false
exists({}); // returns trueConstructs a function that checks equality against any number of arguments
Kind: static method of vet
Returns: a function that takes in a parameter val, and returns true if val is equal to any of the options in ...eq
Params
- ...eq
*- values to check equality against
Example
import isAllOf from 'vet/isAllOf';
import isNumber from 'vet/numbers/isNumber';
import isPositive from 'vet/numbers/isPositive';
let check = isAllOf(isNumber, isPositive);
check(-1); // returns false
check(1); // returns trueA default validator, that always returns true. This can be useful to spec out parameters that you don't wish to validate, but need to document for future work.
Kind: static method of vet
Returns: true
Params
- val - a value to check
Example
import isAny from 'vet/isAny';
isAny(null); // returns true
isAny(undefined); // returns true
isAny({}); // returns trueConstructs a function that checks equality against any number of arguments
Kind: static method of vet
Returns: a function that takes in a parameter val, and returns true if val is NOT equal to any of the options in ...eq
Params
- ...eq
*- values to check equality against
Example
import isNoneOf from 'vet/isNoneOf';
let check = isNoneOf(1, 2, 3);
check(1); // returns false
check(4); // returns truea function that inverts the result of a validator
Kind: static method of vet
Returns: a wrapper function that inverts the result of a validator
Params
- validator
function- validator to invert
Example
import isNot from 'vet/isNot';
import isNumber from 'vet/numbers/isNumber';
let check = isNot(isNumber);
check(1); // returns false
check(null); // returns trueA function to check for nulls
Kind: static method of vet
Returns: true if val is strictly not equal to null
Params
- val - a value to check against null
Example
import isNotNull from 'vet/isNotNull';
isNotNull(null); // returns false
isNotNull(undefined); // returns true
isNotNull({}); // returns trueA function to check for null or undefined
Kind: static method of vet
Returns: true if val is loosely not null (strictly not null or undefined)
Params
- val - a value to check against null and undefined
Example
import isNotNullOrUndefined from 'vet/isNotNullOrUndefined';
isNotNullOrUndefined(null); // returns false
isNotNullOrUndefined(undefined); // returns false
isNotNullOrUndefined({}); // returns trueA function to check for undefined
Kind: static method of vet
Returns: true if val is strictly not undefined
Params
- val - a value to check
Example
import isNotUndefined from 'vet/isNotUndefined';
isNotUndefined(undefined); // returns false
isNotUndefined(null); // returns true
isNotUndefined({}); // returns trueA function to check for null
Kind: static method of vet
Returns: true if val is strictly null
Params
- val - a value to check
Example
import isNull from 'vet/isNull';
isNull(undefined); // returns false
isNull({}); // returns false
isNull(null); // returns trueA function to check for null or undefined
Kind: static method of vet
Returns: true if val is loosely null (strictly null or undefined)
Params
- val - a value to check
Example
import isNullOrUndefined from 'vet/isNullOrUndefined';
isNullOrUndefined({}); // returns false
isNullOrUndefined(undefined); // returns true
isNullOrUndefined(null); // returns trueConstructs a function that checks equality against any number of arguments
Kind: static method of vet
Returns: a function that takes in a parameter val, and returns true if val is equal to any of the options in ...eq
Params
- ...eq
*- values to check equality against
Example
import isOneOf from 'vet/isOneOf';
let check = isOneOf(1, 2, 3);
check(4); // returns false
check(1); // returns trueA function to check for undefined
Kind: static method of vet
Returns: true if val is strictly undefined
Params
- val - a value to check
Example
import isUndefined from 'vet/isUndefined';
isUndefined({}); // returns false
isUndefined(null); // returns false
isUndefined(undefined); // returns trueA function builder to check for a value or null
Kind: static method of vet
Returns: a function that takes in a value, and returns true if the value is null, or the wrapped validator returns true
Params
- validator - a validator function
Example
import nullable from 'vet/nullable';
import isNumber from 'vet/numbers/isNumber';
let isMaybeNumber = nullable(isNumber);
isMaybeNumber(undefined); // returns false
isMaybeNumber("1"); // returns false
isMaybeNumber(1); // returns true
isMaybeNumber(null); // returns trueA function builder to optionally check a value
Kind: static method of vet
Returns: a function that takes in a value, and returns true if the value does not exist, or the validator returns true
Params
- validator - a validator function
Example
import optional from 'vet/optional';
import isNumber from 'vet/numbers/isNumber';
let isMaybeNumber = optional(isNumber);
isMaybeNumber(null); // returns false
isMaybeNumber("1"); // returns false
isMaybeNumber(1); // returns true
isMaybeNumber(undefined); // returns trueTrigger a compiler error when a value is not an exact type.
Kind: global function
Trigger a compiler error when a value is not an exact type.
Kind: global function