Give your object and models schemas
--
nodejs
$ npm install draft --savecomponent
$ component install jwerle/draftbower
$ bower install draftbrowser
<scripttype="text/javascript" src="https://raw.github.com/jwerle/draft/master/draft.js"></script>Creating a model with draft is as simple as passing it a schema descriptor
varUser=draft({name: String,email: String}),Post=draft({owner: Object,content: String})varwerle=newUser({name: 'werle',email: 'joseph@werle.io'}),post=newPost({owner: werle,content: "I like draft :)"})Creates a schema form a descriptor and returns a model constructor
example
varPost=draft({owner : User,comments : [Comment],content : String,created : Date,updated : Date});--
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
varschema=newdraft.Schema({name: String,email: String});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.add({age: Number});Creates a static function on the schema's model constructor
name- The name of the static functionfunc- The actual function
example
varsiteSchema=newdraft.Schema({name : String,url : String});siteSchema.static('createSites',functioncreateSites(map){returnObject.keys(map).map(function(site){returnnewthis({name: site,url: map[site]});},this);});// create Site modelvarSite=siteSchema.createModel();varsites=Site.createSites({'google' : "http://google.com",'github' : "https://github.com",'twitter' : "https://twitter.com",});sites[0].name;// 'google'sites[0].url;// 'http://google.com'sites[1].name;// 'github'sites[1].url;// 'https://github.com'Creates a constructor from the defined schema
example
varschema=newdraft.Schema({name : String,email : String});varUser=schema.createModel();varuser=newUser({name: 'werle',email: 'joseph@werle.io'});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
varschema=newdraft.Schema({name : String,albums : [Album],fans : [Fan]});varbob=schema.new({name: 'bob',albums: [newAlbum({name: "Sun Is Shining"}),newAlbum({name: "Roots of a Legend"})],fans: [sally,frank,joe]});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
newdraft.Schema({name : String})A more advanced example would be to define the descriptor object for the property:
newdraft.Schema({name: {type: String}})--
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 andarrayis set to true then an array is returned who's prototype is an instance of the tree .
example
vartree=newdraft.Tree({name: String});// tree.name is an instance of draft.Type who's constructor is a String constructortree.name;// { Constructor: [Function: String] }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
vartree=newdraft.Tree({domain: String});tree.add('subdomains',{});tree.subdomains;// {} (Tree instance)tree.add(tree.subdomains,'primary',String);tree.subdomains.primary;// { Constructor: [Function: String] }tree.add(tree.subdomains,'secondary',String);tree.subdomains.secondary;// { Constructor: [Function: String] }tree.subdomains.add('cdn',String);tree.subdomains.cdn;// { Constructor: [Function: String] }tree.add('host',String);tree.host;// { Constructor: [Function: String] }--
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 thenewoperatordescriptor- A valid schema constructor
example
varstringType=newdraft.Type(String);stringType.coerce(123);// '123'varnumberType=newdraft.Type(Number);numberType.coerce('123');// 123varbooleanType=newdraft.Type(Boolean);booleanType.coerce(1);// truebooleanType.coerce(0);// falseCreating a custom type
varcustomType=newdraft.Type(functionCustomType(value){return{toString: function(){returnvalue.toString();},valueOf: function(){returnvalue;},add: function(n){returnCustomType(value+n);},sub: function(n){returnCustomType(value-n);}};});+customType.coerce(4).add(5);// 9+customType.coerce(10).sub(9);// 1customType.coerce('j').add('o').add('e').toString();// 'joe'Returns a string representation of a Type instance
example
someType.toString();// '[object Type]'Returns the valueOf return value from the original constructor on the Type instance
varsomeType.valueOf();// some valueDefault getter that coerces a value. This method that can be implemented by the descriptor. Defaults to .coerce()
varstringType=newdraft.Type(String)stringType.get(12345);// '12345'Default setter that coerces a value. This method that can be implemented by the descriptor. Defaults to .coerce()
varstringType=newdraft.Type(String)stringType.set(12345);// '12345'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
varnumberType=newdraft.Type(Number)numberType.validate('123');// falsenumberType.validate(true);// falsenumberType.validate(123);// trueCoerces a given input with the set Constructor type
input- Input to coerce to a type
example
varbooleanType=newdraft.Type(Boolean)booleanType.coerce(1);// truebooleanType.coerce(123);// truebooleanType.coerce(0);// falsebooleanType.coerce(null);// falsebooleanType.coerce();// false--
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
varschema=newdraft.Schema({name: String,email: String});varuser=newdraft.Model({name: 'werle',email: 'joseph@werle.io'},schema);Refreshes the state of the model based on its schema
Sets data on the model based on the schema
Returns a plain object representation of the model
Called with JSON.stringify
Returns a string representation of a Model instance
Returns a value representation of a Model instance
--
Define a schema for an object with types. Strict mode default
vardraft=require('draft')varschema=newdraft.Schema({name : String,profile : {email : String,age : Number}});Create a model constructor from the schema defaulting to strict mode
varUser=schema.createModel();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
varuser=newUser({name: 'werle',profile: {email: 'joseph@werle.io'},somePropertyNotInSchema: 'someValue'});Only values in the schema were set on the object
user.name// werleuser.profile.email// joseph@werle.iouser.somePropertyNotInSchema// undefined- write more tests
- document more
MIT


