Check an object against predefined schema.
Using npm:
npm i -S type-validate
Using yarn:
yarn add type-validate
Sample typings definition:
const typings = {
name: 'string',
age: 'number',
height: 'number?',
isDeveloper: 'boolean',
birthDate: 'date|number',
address: {
country: 'string?',
city: 'string',
},
posts: [{
title: 'string'
}, true],
}
- primitive types -
string
,number
,boolean
,date
, andundefined
- optional types, defined with
?
, likestring?
- union types, defined with
|
, likestring|number
- array types, defined as
[arrayItemType, isOptional]