vet
v6.1.0
Published
A JS/TS library for data validation
Downloads
123
Readme
vet
A collection of data validation tools.
major changes in vet 5
version 5 includes several breaking changes from version 4, mostly designed to improve interoperability with TS
optional
no longer acceptsnull
, justundefined
. This is to bring it inline with TypeScript and many other libraries' interpretation of optional values.isShape
now converts allT | undefined
properties to optional properties in the validator schema type. This eliminates the requirement to explicitly define "optional" properties asundefined
.accepts
andreturns
have now been moved fromvet/utils
tovet/functions
, where they should have been in the first place.
in addition, v5 includes:
- a new
isTuple
utility
API
Objects
Functions
vet : object
Kind: global namespace
- vet : object
- .arrays : object
- .booleans : object
- .dates : object
- .functions : object
- .numbers : object
- .isBetween(lower, upper) ⇒ function
- .exclusive ⇒ function
- .inclusive ⇒ function
- .isFinite(val) ⇒
- .isGreaterThan(bound) ⇒ function
- .exclusive ⇒ function
- .inclusive ⇒ function
- .isInteger(val) ⇒
- .isLessThan(bound) ⇒ function
- .exclusive ⇒ function
- .inclusive ⇒ function
- .isNegative(val) ⇒
- .isNonZero(val) ⇒
- .isNumber(val) ⇒
- .isPositive(val) ⇒
- .isZero(val) ⇒
- .isBetween(lower, upper) ⇒ function
- .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) ⇒
vet.arrays : object
Kind: static namespace of vet
- .arrays : object
arrays.isArray ⇒
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
let isArray from 'vet/arrays/isArray');
isArray(null); // returns false
isArray({}); // returns false
isArray([]); // returns true
arrays.isArrayOf(val) ⇒
Builds 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
let isString from 'vet/strings/isString');
let 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 true
arrays.isLength(len) ⇒
Constructor 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
let 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 true
vet.booleans : object
Kind: static namespace of vet
- .booleans : object
booleans.isBoolean(val) ⇒
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
booleans.isFalse(val) ⇒
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
let isFalse from 'vet/booleans/isFalse');
isFalse(null); // returns false
isFalse(true); // returns false
isFalse(false); // returns true
booleans.isFalsy(val) ⇒
Checks 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
let isFalsy from 'vet/booleans/isFalsy');
isFalse(true); // returns false
isFalsy(null); // returns true
isFalsy(false); // returns true
booleans.isTrue(val) ⇒
Checks 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
let isTrue from 'vet/booleans/isTrue');
isTrue(null); // returns false
isTrue(false); // returns false
isTrue(true); // returns true
booleans.isTruthy(val) ⇒
Checks 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
let isTruthy from 'vet/booleans/isTruthy');
isTruthy(null); // returns false
isTruthy(false); // returns false
isTruthy({}); // returns true
isTruthy(true); // returns true
vet.dates : object
Kind: static namespace of vet
- .dates : object
dates.isDate(val) ⇒
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
let isDate from 'vet/dates/isDate');
isDate(null); // returns false
isDate({}); // returns false
isDate(new Date()); // returns true
dates.isValidDate(val) ⇒
Checks 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
let isValidDate from 'vet/dates/isValidDate');
isValidDate(null); // returns false
isValidDate({}); // returns false
isValidDate(new Date(NaN)); // returns false
isValidDate(new Date()); // returns true
vet.functions : object
Kind: static namespace of vet
- .functions : object
functions.accepts(func, validator, message) ⇒
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
functions.isFunction(val) ⇒
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
let isFunction from 'vet/functions/isFunction');
isFunction(null); // returns false
isFunction({}); // returns false
isFunction(function (){}); // returns true
functions.returns(func, validator, message) ⇒
Wraps 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
vet.numbers : object
Kind: static namespace of vet
- .numbers : object
- .isBetween(lower, upper) ⇒ function
- .exclusive ⇒ function
- .inclusive ⇒ function
- .isFinite(val) ⇒
- .isGreaterThan(bound) ⇒ function
- .exclusive ⇒ function
- .inclusive ⇒ function
- .isInteger(val) ⇒
- .isLessThan(bound) ⇒ function
- .exclusive ⇒ function
- .inclusive ⇒ function
- .isNegative(val) ⇒
- .isNonZero(val) ⇒
- .isNumber(val) ⇒
- .isPositive(val) ⇒
- .isZero(val) ⇒
- .isBetween(lower, upper) ⇒ function
numbers.isBetween(lower, upper) ⇒ function
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
isBetween.exclusive ⇒ function
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
isBetween.inclusive ⇒ function
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
numbers.isFinite(val) ⇒
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
numbers.isGreaterThan(bound) ⇒ function
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
isGreaterThan.exclusive ⇒ function
Kind: static property of isGreaterThan
Returns: function - - a validator function
Params
- bound number - the boundary value to check against
isGreaterThan.inclusive ⇒ function
Kind: static property of isGreaterThan
Returns: function - - a validator function
Params
- bound number - the boundary value to check against
numbers.isInteger(val) ⇒
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
numbers.isLessThan(bound) ⇒ function
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
isLessThan.exclusive ⇒ function
Kind: static property of isLessThan
Returns: function - - a validator function
Params
- bound number - the boundary value to check against
isLessThan.inclusive ⇒ function
Kind: static property of isLessThan
Returns: function - - a validator function
Params
- bound number - the boundary value to check against
numbers.isNegative(val) ⇒
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
numbers.isNonZero(val) ⇒
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
numbers.isNumber(val) ⇒
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
numbers.isPositive(val) ⇒
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
numbers.isZero(val) ⇒
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
vet.objects : object
Kind: static namespace of vet
- .objects : object
objects.isInstanceOf(con) ⇒
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
objects.isObject(val) ⇒
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
objects.isObjectOf(validator) ⇒
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
objects.isShape(schema) ⇒
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
let isString from 'vet/strings/isString');
let isNumber from 'vet/numbers/isNumber');
let isBoolean from 'vet/booleans/isBoolean');
let 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 });
isShape.isShape.exact(schema) ⇒
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
let isString from 'vet/strings/isString');
let isNumber from 'vet/numbers/isNumber');
let isBoolean from 'vet/booleans/isBoolean');
let 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 });
isShape.isShape.partial(schema) ⇒
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
let isString from 'vet/strings/isString');
let isNumber from 'vet/numbers/isNumber');
let isBoolean from 'vet/booleans/isBoolean');
let 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' });
vet.strings : object
Kind: static namespace of vet
- .strings : object
strings.isEmpty(val) ⇒
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
strings.isLength(len) ⇒
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
strings.isNotEmpty(val) ⇒
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
strings.isProbablyBase64(val) ⇒
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
strings.isProbablyDataURL(val) ⇒
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
strings.isProbablyEmail(val) ⇒
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
strings.isProbablyURL(val) ⇒
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
strings.isString(val) ⇒
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
strings.matches(regex) ⇒
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
vet.utils : object
Kind: static namespace of vet
- .utils : object
utils.assertReact ⇒
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
utils.assert(validator, message) ⇒
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
vet.equals(eq) ⇒
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
let equals from 'vet/equals');
let is3 = equals(3);
is3(null); // returns false
is3({}); // returns false
is3(3); // returns true
vet.exists(val) ⇒
Alias for vet/isNotNullOrUndefined
Kind: static method of vet
Returns: true if val is not null or undefined
Params
- val - value to check
Example
let exists from 'vet/exists');
exists(null); // returns false
exists(undefined); // returns false
exists({}); // returns true
vet.isAllOf(...eq) ⇒
Constructs 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
let isAllOf from 'vet/isAllOf');
let isNumber from 'vet/numbers/isNumber');
let isPositive from 'vet/numbers/isPositive');
let check = isAllOf(isNumber, isPositive);
check(-1); // returns false
check(1); // returns true
vet.isAny(val) ⇒
A 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 true
vet.isNoneOf(...eq) ⇒
Constructs 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
let isNoneOf from 'vet/isNoneOf');
let check = isNoneOf(1, 2, 3);
check(1); // returns false
check(4); // returns true
vet.isNot(validator) ⇒
a 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
let isNot from 'vet/isNot');
let isNumber from 'vet/numbers/isNumber');
let check = isNot(isNumber);
check(1); // returns false
check(null); // returns true
vet.isNotNull(val) ⇒
A 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
let isNotNull from 'vet/isNotNull');
isNotNull(null); // returns false
isNotNull(undefined); // returns true
isNotNull({}); // returns true
vet.isNotNullOrUndefined(val) ⇒
A 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
let isNotNullOrUndefined from 'vet/isNotNullOrUndefined');
isNotNullOrUndefined(null); // returns false
isNotNullOrUndefined(undefined); // returns false
isNotNullOrUndefined({}); // returns true
vet.isNotUndefined(val) ⇒
A 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
let isNotUndefined from 'vet/isNotUndefined');
isNotUndefined(undefined); // returns false
isNotUndefined(null); // returns true
isNotUndefined({}); // returns true
vet.isNull(val) ⇒
A function to check for null
Kind: static method of vet
Returns: true if val is strictly null
Params
- val - a value to check
Example
let isNull from 'vet/isNull');
isNull(undefined); // returns false
isNull({}); // returns false
isNull(null); // returns true
vet.isNullOrUndefined(val) ⇒
A 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
let isNullOrUndefined from 'vet/isNullOrUndefined');
isNullOrUndefined({}); // returns false
isNullOrUndefined(undefined); // returns true
isNullOrUndefined(null); // returns true
vet.isOneOf(...eq) ⇒
Constructs 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
let isOneOf from 'vet/isOneOf');
let check = isOneOf(1, 2, 3);
check(4); // returns false
check(1); // returns true
vet.isUndefined(val) ⇒
A function to check for undefined
Kind: static method of vet
Returns: true if val is strictly undefined
Params
- val - a value to check
Example
let isUndefined from 'vet/isUndefined');
isUndefined({}); // returns false
isUndefined(null); // returns false
isUndefined(undefined); // returns true
vet.nullable(validator) ⇒
A 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
let nullable from 'vet/nullable');
let isNumber from 'vet/numbers/isNumber');
let isMaybeNumber = nullable(isNumber);
isMaybeNumber(undefined); // returns false
isMaybeNumber("1"); // returns false
isMaybeNumber(1); // returns true
isMaybeNumber(null); // returns true
vet.optional(validator) ⇒
A 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
let optional from 'vet/optional');
let isNumber from 'vet/numbers/isNumber');
let isMaybeNumber = optional(isNumber);
isMaybeNumber(null); // returns false
isMaybeNumber("1"); // returns false
isMaybeNumber(1); // returns true
isMaybeNumber(undefined); // returns true
exactType()
Trigger a compiler error when a value is not an exact type.
Kind: global function
exactType()
Trigger a compiler error when a value is not an exact type.
Kind: global function