draft
Give your object and models schemas
--
install
nodejs
$ npm install draft --save
component
$ component install jwerle/draft
bower
$ bower install draft
browser
usage
Creating a model with draft is as simple as passing it a schema descriptor
var User = Post = var werle = name: 'werle' email: 'joseph@werle.io' post = owner: werle content: "I like draft :)"
api
draft(descriptor, options)
Creates a schema form a descriptor and returns a model constructor
example
var Post = ;
--
Schema(descriptor, options)
Schema constructor
descriptor
- An object defining a descriptor for the schema instanceoptions
- An object of options:strict
- Strict mode. Model from schema will be frozen from schema updates after instantiation. (Default:true
)
example
var schema = name: String email: String;
.add(key, descriptor)
Adds an object to the schema tree
key
- A key used to identify the property in the schemadescriptor
- An object descriptor or constructor function
example
schema;
.static(name, func)
Creates a static function on the schema's model constructor
name
- The name of the static functionfunc
- The actual function
example
var siteSchema = name : String url : String; siteSchema; // create Site modelvar Site = siteSchema; var sites = Site; sites0name; // 'google'sites0url; // 'http://google.com'sites1name; // 'github'sites1url; // 'https://github.com'
.createModel()
Creates a constructor from the defined schema
example
var schema = name : String email : String; var User = schema;var user = name: 'werle' email: 'joseph@werle.io' ;
.new(data)
Accepts an object of data and passes it to the Model constructor from the Schema instance
data
- An object of data to pass to the schema instance model constructor
example
var schema = name : String albums : Album fans : Fan; var bob = schema;
using schema descriptors
A schema descriptor is a key to type descriptor object. The key for each property on the object represents a possible key on a model instance created from the schema instance.
A simple example of a schema who defines a model which accepts an object that defines a single property name
of the type string
name : String
A more advanced example would be to define the descriptor object for the property:
name: type: String
--
Tree(descriptor, options)
Tree constructor. Creates an object tree for a schema. This is used for aggregating types
A tree instance is intended to be used with a schema
descriptor
- A schema descriptor used to define the tree.options
- An object of options. If present andarray
is set to true then an array is returned who's prototype is an instance of the tree .
example
var tree = name: String; // tree.name is an instance of draft.Type who's constructor is a String constructortreename; // { Constructor: [Function: String] }
.add(parent, key, descriptor)
Adds a key to the tree on a given parent tree. Defaults to 'this' as the parent if one is not provided.
parent
- The parent tree object in which the descriptor is added to by the provided key. Defaults to the tree instance caller.key
- The key of the item in the tree to adddescriptor
- The object descriptor for the key being added to the tree
example
var tree = domain: String; tree;treesubdomains; // {} (Tree instance)tree;treesubdomainsprimary; // { Constructor: [Function: String] }tree;treesubdomainssecondary; // { Constructor: [Function: String] }treesubdomains;treesubdomainscdn; // { Constructor: [Function: String] }tree;treehost; // { Constructor: [Function: String] }
--
Type(Constructor, descriptor)
Type constructor. Creates a Type used in a Tree instance for a Schema instance. It is meant to provide methods for validation and coercion.
Constructor
- A valid type constructor who will NEVER be invoked with thenew
operatordescriptor
- A valid schema constructor
example
var stringType = String;stringType; // '123' var numberType = Number;numberType; // 123 var booleanType = Boolean;booleanType; // truebooleanType; // false
Creating a custom type
var customType = { return { return value; } { return value; } { return ; } { return ; } ;}; +customType; // 9+customType; // 1customType; // 'joe'
.toString
Returns a string representation of a Type instance
example
someType; // '[object Type]'
.valueOf
Returns the valueOf return value from the original constructor on the Type instance
var someType; // some value
.get(value)
Default getter that coerces a value. This method that can be implemented by the descriptor. Defaults to .coerce()
var stringType = StringstringType; // '12345'
.set(value)
Default setter that coerces a value. This method that can be implemented by the descriptor. Defaults to .coerce()
var stringType = StringstringType; // '12345'
.validate(input)
Validates a defined type. It performs instance of checks on values that are not primitive. Primitive inputs are validated with a 'typeof' check
input
- Mixed input to validate against the type
example
var numberType = Number numberType; // falsenumberType; // falsenumberType; // true
.coerce(input)
Coerces a given input with the set Constructor type
input
- Input to coerce to a type
example
var booleanType = Boolean booleanType; // truebooleanType; // truebooleanType; // falsebooleanType; // falsebooleanType; // false
--
Model(data, schema)
Base constructor for all created Model instances. Usually a Model constructor is created from a schema, but passing a schema to a Model constructor works too.
data
- An object of data is validated against the schema used to create the Modelschema
- An instance of a schema.
example
var schema = name: String email: String; var user = name: 'werle' email: 'joseph@werle.io' schema;
.refresh()
Refreshes the state of the model based on its schema
.set()
Sets data on the model based on the schema
.toObject()
Returns a plain object representation of the model
.toJSON()
Called with JSON.stringify
.toString()
Returns a string representation of a Model instance
.valueOf()
Returns a value representation of a Model instance
--
example
Define a schema for an object with types. Strict mode default
var draft = var schema = name : String profile : email : String age : Number ;
Create a model constructor from the schema defaulting to strict mode
var User = schema;
Instantiate the model passing in an object. In strict mode all properties on an object must be defined in the schema that was used to create it
var user = name: 'werle' profile: email: 'joseph@werle.io' somePropertyNotInSchema: 'someValue';
Only values in the schema were set on the object
username // werleuserprofileemail // joseph@werle.iousersomePropertyNotInSchema // undefined
todo
- write more tests
- document more
license
MIT