object-type

0.1.0 • Public • Published

object-type npm

Build Status Code Climate Test Coverage downloads license

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.

Usage

//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' } ]

API

See abstract-type.

TODO

License

MIT

Package Sidebar

Install

npm i object-type

Weekly Downloads

1

Version

0.1.0

License

MIT

Last publish

Collaborators

  • riceball