{"id":15497149,"url":"https://github.com/jwerle/draft","last_synced_at":"2025-04-22T21:32:32.123Z","repository":{"id":8302820,"uuid":"9846202","full_name":"jwerle/draft","owner":"jwerle","description":"Give your objects and models schemas","archived":false,"fork":false,"pushed_at":"2014-07-09T20:48:13.000Z","size":351,"stargazers_count":10,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-13T06:16:32.325Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jwerle.png","metadata":{"files":{"readme":"README.md","changelog":"History.md","contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2013-05-03T22:49:55.000Z","updated_at":"2020-10-23T19:29:43.000Z","dependencies_parsed_at":"2022-08-07T02:01:05.690Z","dependency_job_id":null,"html_url":"https://github.com/jwerle/draft","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jwerle%2Fdraft","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jwerle%2Fdraft/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jwerle%2Fdraft/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jwerle%2Fdraft/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jwerle","download_url":"https://codeload.github.com/jwerle/draft/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250328478,"owners_count":21412627,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-02T08:31:02.772Z","updated_at":"2025-04-22T21:32:32.079Z","avatar_url":"https://github.com/jwerle.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"draft \n=====\n\n[![draft](http://www.inkity.com/shirtdesigner/prints/clipArt1/S6881113.png)]()\n\nGive your object and models schemas\n\n[![Build Status](https://travis-ci.org/jwerle/draft.png?branch=master)](https://travis-ci.org/jwerle/draft)\n[![browser support](https://ci.testling.com/jwerle/draft.png)](https://ci.testling.com/jwerle/draft)\n\n--\n\n## install\n\n*nodejs*\n\n```sh\n$ npm install draft --save\n```\n\n*component*\n\n```sh\n$ component install jwerle/draft\n```\n\n*bower*\n\n```sh\n$ bower install draft\n```\n\n*browser*\n\n```html\n\u003cscript type=\"text/javascript\" src=\"https://raw.github.com/jwerle/draft/master/draft.js\"\u003e\u003c/script\u003e\n```\n\n## usage\n\nCreating a model with draft is as simple as passing it a schema descriptor\n\n```js\nvar User = draft({ name: String, email: String })\n  , Post = draft({ owner: Object, content: String })\n\nvar werle = new User({ name: 'werle', email: 'joseph@werle.io' })\n  , post  = new Post({ owner: werle, content: \"I like draft :)\"})\n```\n\n## api\n\n### draft(descriptor, options)\n\nCreates a schema form a descriptor and returns a model constructor\n\n*example*\n\n```js\nvar Post = draft({\n  owner    : User,\n  comments : [Comment],\n  content  : String,\n  created  : Date,\n  updated  : Date\n});\n```\n--\n\n### Schema(descriptor, options)\n\nSchema constructor\n\n*  `descriptor` - An object defining a descriptor for the schema instance\n*  `options` - An object of options:\n  * `strict` - Strict mode. Model from schema will be frozen from schema updates after instantiation. (Default: `true`)\n\n*example*\n\n```js\nvar schema = new draft.Schema({\n  name: String,\n  email: String\n});\n```\n\n#### .add(key, descriptor)\n\nAdds an object to the schema tree\n\n* `key` - A key used to identify the property in the schema\n* `descriptor` - An object descriptor or constructor function \n\n*example*\n\n```js\nschema.add({ age: Number });\n```\n\n#### .static(name, func)\n\nCreates a static function on the schema's model constructor\n\n* `name` - The name of the static function\n* `func` - The actual function\n\n*example*\n\n```js\nvar siteSchema = new draft.Schema({\n  name : String,\n  url  : String\n});\n\nsiteSchema.static('createSites', function createSites (map) {\n  return Object.keys(map).map(function (site) {\n    return new this({ name: site, url: map[site] });\n  }, this);\n});\n\n// create Site model\nvar Site = siteSchema.createModel();\n\nvar sites = Site.createSites({\n  'google'  : \"http://google.com\",\n  'github'  : \"https://github.com\",\n  'twitter' : \"https://twitter.com\",\n});\n\nsites[0].name; // 'google'\nsites[0].url; // 'http://google.com'\nsites[1].name; // 'github'\nsites[1].url; // 'https://github.com'\n```\n\n#### .createModel()\n\nCreates a constructor from the defined schema\n\n*example*\n\n```js\nvar schema = new draft.Schema({\n  name  : String,\n  email : String\n});\n\nvar User = schema.createModel();\nvar user = new User({ name: 'werle', email: 'joseph@werle.io' });\n```\n\n#### .new(data)\n\nAccepts an object of data and passes it to the Model constructor from the Schema instance\n\n* `data` - An object of data to pass to the schema instance model constructor\n\n*example*\n\n```js\nvar schema = new draft.Schema({\n  name   : String,\n  albums : [Album],\n  fans   : [Fan]\n});\n\nvar bob = schema.new({\n  name: 'bob',\n  albums: [new Album({ name: \"Sun Is Shining\" }), new Album({ name: \"Roots of a Legend\" }) ],\n  fans: [sally, frank, joe]\n});\n```\n\n#### using schema descriptors\n\nA schema descriptor is a key to type descriptor object. \nThe key for each property on the object represents a possible key on a model instance created from the schema instance.\n\nA simple example of a schema who defines a model which accepts an object that defines a single property `name` of the type `string`\n\n```js\nnew draft.Schema({ name : String })\n```\n\nA more advanced example would be to define the descriptor object for the property:\n\n```js\nnew draft.Schema({\n  name: { type: String }\n})\n```\n\n--\n\n### Tree(descriptor, options)\n\nTree constructor.\nCreates an object tree for a schema. This is used for aggregating types\n\n**A tree instance is intended to be used with a schema**\n\n* `descriptor` - A schema descriptor used to define the tree.\n* `options` - An object of options. If present and `array` is set to true then an array is returned who's prototype is an instance of the tree .\n\n*example*\n\n```js\nvar tree = new draft.Tree({\n  name: String\n});\n\n// tree.name is an instance of draft.Type who's constructor is a String constructor\ntree.name; // { Constructor: [Function: String] }\n```\n\n#### .add(parent, key, descriptor)\n\nAdds a key to the tree on a given parent tree. \nDefaults to 'this' as the parent if one is not provided.\n\n* `parent` - The parent tree object in which the descriptor is added to by the provided key. Defaults to the tree instance caller.\n* `key` - The key of the item in the tree to add\n* `descriptor` - The object descriptor for the key being added to the tree\n\n*example*\n\n```js\nvar tree = new draft.Tree({\n  domain: String\n});\n\ntree.add('subdomains', {});\ntree.subdomains; // {} (Tree instance)\ntree.add(tree.subdomains, 'primary', String);\ntree.subdomains.primary; // { Constructor: [Function: String] }\ntree.add(tree.subdomains, 'secondary', String);\ntree.subdomains.secondary; // { Constructor: [Function: String] }\ntree.subdomains.add('cdn', String);\ntree.subdomains.cdn; // { Constructor: [Function: String] }\ntree.add('host', String);\ntree.host; // { Constructor: [Function: String] }\n```\n--\n\n### Type(Constructor, descriptor)\n\nType constructor. Creates a Type used in a Tree instance for a Schema instance. \nIt is meant to provide methods for validation and coercion.\n\n* `Constructor` - A valid type constructor who will *NEVER* be invoked with the `new` operator\n* `descriptor` - A valid schema constructor\n\n*example*\n\n```js\nvar stringType = new draft.Type(String);\nstringType.coerce(123); // '123'\n\nvar numberType = new draft.Type(Number);\nnumberType.coerce('123'); // 123\n\nvar booleanType = new draft.Type(Boolean);\nbooleanType.coerce(1); // true\nbooleanType.coerce(0); // false\n```\n\nCreating a custom type\n\n```js\nvar customType = new draft.Type(function CustomType (value) {\n  return {\n    toString: function () {\n      return value.toString();\n    },\n\n    valueOf: function () {\n      return value;\n    },\n\n    add: function (n) {\n      return CustomType(value + n);\n    },\n\n    sub: function (n) {\n      return CustomType(value - n);\n    }\n  };\n});\n\n+customType.coerce(4).add(5); // 9\n+customType.coerce(10).sub(9); // 1\ncustomType.coerce('j').add('o').add('e').toString(); // 'joe'\n```\n\n#### .toString\n\nReturns a string representation of a Type instance\n\n*example*\n\n```js\nsomeType.toString(); // '[object Type]'\n```\n\n#### .valueOf\n\nReturns the valueOf return value from the original constructor on the Type instance\n\n```js\nvar someType.valueOf(); // some value\n```\n\n#### .get(value)\nDefault getter that coerces a value. This method that can be implemented by the descriptor. Defaults to `.coerce()`\n\n```js\nvar stringType = new draft.Type(String)\nstringType.get(12345); // '12345'\n```\n\n#### .set(value)\n\nDefault setter that coerces a value. This method that can be implemented by the descriptor. Defaults to `.coerce()`\n\n```js\nvar stringType = new draft.Type(String)\nstringType.set(12345); // '12345'\n```\n\n#### .validate(input)\n\nValidates a defined type. It performs instance of checks on values that are not primitive. Primitive inputs are validated with a 'typeof' check\n\n* `input` - Mixed input to validate against the type\n\n*example*\n\n```js\nvar numberType = new draft.Type(Number)\n\nnumberType.validate('123'); // false\nnumberType.validate(true); // false\nnumberType.validate(123); // true\n```\n\n#### .coerce(input)\n\nCoerces a given input with the set Constructor type\n\n* `input` - Input to coerce to a type\n\n*example*\n\n```js\nvar booleanType = new draft.Type(Boolean)\n\nbooleanType.coerce(1); // true\nbooleanType.coerce(123); // true\nbooleanType.coerce(0); // false\nbooleanType.coerce(null); // false\nbooleanType.coerce(); // false\n``` \n\n--\n\n### Model(data, schema)\n\nBase 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.\n\n* `data` - An object of data is validated against the schema used to create the Model\n* `schema` - An instance of a schema.\n\n*example*\n\n```js\nvar schema = new draft.Schema({\n  name: String,\n  email: String\n});\n\nvar user = new draft.Model({ name: 'werle', email: 'joseph@werle.io' }, schema);\n```\n\n#### .refresh()\n\nRefreshes the state of the model based on its schema\n\n#### .set()\n\nSets data on the model based on the schema\n\n#### .toObject()\n\nReturns a plain object representation of the model\n\n#### .toJSON()\n\nCalled with JSON.stringify\n\n#### .toString()\n\nReturns a string representation of a Model instance\n\n#### .valueOf()\n\nReturns a value representation of a Model instance\n\n--\n\n## example\n\nDefine a schema for an object with types. Strict mode default\n\n```js\nvar draft = require('draft')\nvar schema = new draft.Schema({\n  name : String,\n  profile : {\n    email : String,\n    age : Number\n  }\n});\n```\n\nCreate a model constructor from the schema defaulting to strict mode\n\n```js\nvar User = schema.createModel();\n```\n\nInstantiate 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\n\n```js\nvar user = new User({\n  name: 'werle',\n  profile: { email: 'joseph@werle.io' },\n  somePropertyNotInSchema: 'someValue'\n});\n\n```\n\nOnly values in the schema were set on the object\n\n```js\nuser.name // werle\nuser.profile.email // joseph@werle.io\nuser.somePropertyNotInSchema // undefined\n```\n\n## todo\n\n* write more tests\n* document more\n\n## license\n\nMIT\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjwerle%2Fdraft","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjwerle%2Fdraft","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjwerle%2Fdraft/lists"}