export const Customer = Type . Object ( {
firstName : Type . String ( ) ,
lastName : Type . String ( ) ,
age : Type . Number ( ) ,
dob : Type . Date ( ) ,
address : Type . Object ( {
streetAddress : Type . String ( ) ,
city : Type . String ( ) ,
state : Type . String ( ) ,
postalCode : Type . String ( ) ,
} ) ,
contact : Type . Array (
Type . Object ( {
type : Type . String ( { default : 'Joe' } ) ,
value : Type . String ( ) ,
} ) ,
) ,
} )
type Customer = {
firstName : string ;
lastName : string ;
age : number ;
dob : Date ;
address : {
streetAddress : string ;
city : string ;
state : string ;
postalCode : string ;
} ;
contact : {
type : string ;
value : string ;
} [ ] ;
}
{
type : 'object' ,
properties : {
firstName : {
type : 'string' ,
} ,
lastName : {
type : 'string' ,
} ,
age : {
type : 'number' ,
} ,
dob : {
type : 'object' ,
instanceOf : 'Date' ,
} ,
address : {
type : 'object' ,
properties : {
streetAddress : {
type : 'string' ,
} ,
city : {
type : 'string' ,
} ,
state : {
type : 'string' ,
} ,
postalCode : {
type : 'string' ,
} ,
} ,
required : [ 'streetAddress' , 'city' , 'state' , 'postalCode' ] ,
} ,
contact : {
type : 'array' ,
items : {
type : 'object' ,
properties : {
type : {
default : 'Joe' ,
type : 'string' ,
} ,
value : {
type : 'string' ,
} ,
} ,
required : [ 'type' , 'value' ] ,
} ,
} ,
} ,
required : [ 'firstName' , 'lastName' , 'age' , 'dob' , 'address' , 'contact' ] ,
}
Compiled Validator (highly performant)
return function check ( value ) {
return (
( typeof value === 'object' && value !== null && ! Array . isArray ( value ) ) &&
( typeof value . firstName === 'string' ) &&
( typeof value . lastName === 'string' ) &&
( typeof value . age === 'number' && ! isNaN ( value . age ) ) &&
( value . dob instanceof Date ) && ! isNaN ( value . dob . getTime ( ) ) &&
( typeof value . address === 'object' && value . address !== null && ! Array . isArray ( value . address ) ) &&
( typeof value . address . streetAddress === 'string' ) &&
( typeof value . address . city === 'string' ) &&
( typeof value . address . state === 'string' ) &&
( typeof value . address . postalCode === 'string' ) &&
( Array . isArray ( value . contact ) && value . contact . every ( value => ( ( typeof value === 'object' && value !== null && ! Array . isArray ( value ) ) && ( typeof value . type === 'string' ) && ( typeof value . value === 'string' ) ) ) )
)
}