The Object type can hold the attributes of the object.
- Object Type Attributes:
attributes
(object): this object's attribute list.strict
(boolean): whether treat this object as strict mode. defaults to false.- the object can only have attributes in the attributes list if strict mode is enabled.
//register the string and number types:
require('string-type')
require('number-type')
var ObjectType = require('object-type')
var Obj = ObjectType({
strict: true, //the strict mode, defaults to false.
attributes: //define the object attributes
s: 'string',
n: {
type:'number',
required: true
}
})
//=<type "Object": "attributes":{"s":"String","n":{"required":true,"type":"Number"}},"strict":true>
var value = Obj.create({s:'123', n:33})
//=<type "Object": "attributes":{"s":"String","n":{"required":true,"type":"Number"}},"strict":true,"value":{"s":"123","n":33}>
value = Obj.create({s:'123'})
//=TypeError: "[object Object]" is an invalid Object
console.log(Obj.errors)
//=[ { name: 'n', message: 'is required' } ]
Obj.isValid(s:123)
//=false
console.log(Obj.errors)
//[ { name: 's', message: 'is invalid' },
// { name: 'n', message: 'is required' } ]
See abstract-type.
MIT