Flexible and composable runtime type assertion for Javascript. Syntax inspired by prop-types. Supports:
- ES6 data structures
- inner elements in data structures
- nested assertions
- inverse assertions
- custom validators
Quick Examples
// takor.oneOfconst isNumOrStr = takor // true // false // takor.shape basicconst checkShape = takorshape key1: takor // takor.shape with nested assertionsconst checkNestedElement = takorshape key1: takorshape key2: takor // supports literal number or stringconst isValidDogBreed = takor // true // false // custom validator(s)const lessThanTen = el < 10const greaterThanThree = el > 3const goodNumberRange = takor// compose existing validator into anotherconst validNumRangeOrStr = takor // true // true // false // false // false // takor.mapOfconst validMap = takor // takor.notconst nonNullOrArray = takornot // true // false /** * Practical example: * Checks if the api response has a data key that is an array of objects * that have the keys id, phoneNumber, and name, where * the phone number is also checked if it is a valid phone number */const isValidPhoneNumber = { return /^[+]*[(]{0,1}[0-9]{1,4}[)]{0,1}[-\s\./0-9]*$/}const isExpectedApiResponse = takorshape status: Number message: String data: takor
Table of Contents
- Quick Examples
- Available Matchers
- Validators
- Inverse Validators
- types
static method | type | description |
---|---|---|
allOf | ListOfMatchers | Passed in validators must meet every criteria |
arrayOf | ListOfMatchers | Asserts the element is an array with specific types stated |
is | SingleMatcher | Performs an assertion on a single value |
mapOf | ListOfTupleMatchers | Asserts inner elemnts of an es6 Map |
oneOf | ListOfMatchers | At least one validator must match |
setOf | ListOfMatchers | Asserts inner elements of a Set |
shape | ShapeOfMatchers | Asserts a specific structure of a pojo (plain old javascript object) |
not.arrayOf | ListOfMatchers | The converse arrayOf |
not.is | SingleMatcher | The converse is |
not.mapOf | ListOfTupleMatchers | The converse mapOf |
not.oneOf | ListOfMatchers | The converse oneOf |
not.setOf | ListOfMatchers | The converse setOf |
not.shape | ShapeOfMatchers | The converse shape |
Available Matchers
Description
Out-of-the-box type validators. See examples for usage.
Number
(Constructor)String
(Constructor)Set
(Constructor)Map
(Constructor)Boolean
(Constructor)Array
(Constructor)Function
(Constructor)null
undefined
NaN
true
false
any
type: IAny
Desc
Not a validator. A function that is true
for any value
Notes
Intended use is as an argument for validators.
Examples
takor // true const assertKeysOnly = takorshape data: takorshape entry: takorany
falsey
type: IFalsey
Desc
Not a validator. It is a raw function that checks for any falsey
value type
Notes
More information on falsey
can be found on MDN
Intended use is as an argument for validators.
Examples
takor // truetakor // false const falseyOnly = takor // false // true
pojo
type: IPojo
Desc
Not a validator. It is a raw function that if a value is a pojo. Use shape to specify a pojo shape
Notes
Intended use is as an argument for validators
Examples
takor // truetakor // false // any object or array as long as top level key is `data`const isPayload = takorshape data: takor
truthy
type: ITruthy
Desc
Not a validator. It is a function that checks if the value is truthy
.
Notes
More information on truthy
can be found on MDN
Intended use is as an argument for validators.
Examples
takor // truetakor // false const truthiesOnly = takor // true // false
allOf
type: ListOfMatchers
Desc
Passed in validators must meet every criteria
Notes
- Contradictory validators will result in
false
Examples
const isPopulated = arrlength > 0const populatedStringArr = takor // true // false // false // contradictory types. impossible to meet criteriaconst impossibleCheck = takor // false // false
arrayOf
type: ListOfMatchers
Desc
Asserts the element is an array with specific types stated
Notes
Examples
const assert = takor //false const checkInnerOobj = takor // true const goodShapeNumOrStr = takor // false const emptyArr = takor // true // false
is
type: SingleMatcher
Desc
Performs an assertion on a single value
Notes
This is a less flexible version of typeof. takor.is
only accepts a single validator, while typeof accepts an arbitrary number.
Examples
const isStr = takor // true // false // custom validatortakor // true // false
mapOf
type: ListOfTupleMatchers
Desc
Asserts inner elemnts of an es6 Map
Notes
mapOf
takes an arbitrary number of tuples, correlating to [key, value]
assertions
Examples
const allNumbers = takor // inner assertion inside mapOfconst valueMapShape = takorshape key: takorshape key2: takor const checkMapValue = takor)
not.arrayOf
type: ListOfMatchers
Desc
The converse arrayOf
Notes
Any non-Array
value will be true
Examples
const checkArray = takornot // false // true // true
not.is
type: SingleMatcher
Desc
The converse is
Notes
Examples
const checkStrLen = takornot // false // true
not.mapOf
type: ListOfTupleMatchers
Desc
The converse mapOf
Notes
non-Map
values will always be true
Examples
const notAllNumbers = takornot
not.oneOf
type: ListOfMatchers
Desc
The converse oneOf
Notes
Examples
const keyIsNotNull = takorshape key: takornot
not.setOf
type: ListOfMatchers
Desc
The converse setOf
Notes
Examples
const notStrs = takornot // true // false
not.shape
type: ShapeOfMatchers
Desc
The converse shape
Notes
Examples
const notEmpty = takornotshape{} // true // false
oneOf
type: ListOfMatchers
Desc
At least one validator must match
Notes
Examples
const numOrStr = takor // true // false // check inner elements of an arrayconst dogOrCat = takor // true // false
setOf
type: ListOfMatchers
Desc
Asserts inner elements of a Set
Notes
Examples
const allNums = takor // true const strOrNums = takor // true // false
shape
type: ShapeOfMatchers
Desc
Asserts a specific structure of a pojo (plain old javascript object)
Notes
shape
checks only top level keys.- Nest another
shape
to assert deeper - Does not check for superfluous keys. Only ensures types of defined keys
Examples
const assert = takorshape key: String // true const assertNested = takorshape key: takorshape key: takorshape key1: String const assertApiPayload = takorshape data: takor
types
// Custom validators are any function that accepts an input value and returns a boolean