UNPKG

ink-docstrap

Version:

[![NPM](https://nodei.co/npm/ink-docstrap.png?downloads=true)](https://nodei.co/npm/ink-docstrap/)

589 lines (479 loc) 15.7 kB
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>DocStrap Source: documents/schema.js</title> <!--[if lt IE 9]> <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script> <![endif]--> <link type="text/css" rel="stylesheet" href="styles/sunlight.default.css"> <link type="text/css" rel="stylesheet" href="styles/site.cerulean.css"> </head> <body> <div class="container-fluid"> <div class="navbar navbar-fixed-top navbar-inverse"> <div class="navbar-inner"> <a class="brand" href="index.html">DocStrap</a> <ul class="nav"> <li class="dropdown"> <a href="modules.list.html" class="dropdown-toggle" data-toggle="dropdown">Modules<b class="caret"></b></a> <ul class="dropdown-menu "> <li> <a href="module-base.html">base</a> </li> <li> <a href="chains_.html">base/chains</a> </li> <li> <a href="binder.html">documents/binder</a> </li> <li> <a href="model_.html">documents/model</a> </li> <li> <a href="probe.html">documents/probe</a> </li> <li> <a href="schema_.html">documents/schema</a> </li> <li> <a href="collector.html">ink/collector</a> </li> <li> <a href="bussable_.html">mixins/bussable</a> </li> <li> <a href="signalable_.html">mixins/signalable</a> </li> <li> <a href="format.html">strings/format</a> </li> <li> <a href="logger.html">utils/logger</a> </li> </ul> </li> <li class="dropdown"> <a href="classes.list.html" class="dropdown-toggle" data-toggle="dropdown">Classes<b class="caret"></b></a> <ul class="dropdown-menu "> <li> <a href="base.html">base</a> </li> <li> <a href="chains.html">base/chains</a> </li> <li> <a href="model.html">documents/model</a> </li> <li> <a href="probe.queryOperators.html">queryOperators</a> </li> <li> <a href="probe.updateOperators.html">updateOperators</a> </li> <li> <a href="collector-ACollector.html">ACollector</a> </li> <li> <a href="collector-CollectorBase_.html">CollectorBase</a> </li> <li> <a href="collector-OCollector.html">OCollector</a> </li> <li> <a href="signalable-Signal.html">Signal</a> </li> <li> <a href="logger.Logger.html">Logger</a> </li> </ul> </li> <li class="dropdown"> <a href="mixins.list.html" class="dropdown-toggle" data-toggle="dropdown">Mixins<b class="caret"></b></a> <ul class="dropdown-menu "> <li> <a href="schema.html">documents/schema</a> </li> <li> <a href="bussable.html">mixins/bussable</a> </li> <li> <a href="signalable.html">mixins/signalable</a> </li> </ul> </li> <li class="dropdown"> <a href="tutorials.list.html" class="dropdown-toggle" data-toggle="dropdown">Tutorials<b class="caret"></b></a> <ul class="dropdown-menu "> <li> <a href="tutorial-Teeth.html">Brush Teeth</a> </li> <li> <a href="tutorial-Car.html">Drive Car</a> </li> <li> <a href="tutorial-Test.html">Fence Test</a> </li> </ul> </li> <li class="dropdown"> <a href="global.html" class="dropdown-toggle" data-toggle="dropdown">Global<b class="caret"></b></a> <ul class="dropdown-menu "> <li> <a href="global.html#utils/logger">utils/logger</a> </li> </ul> </li> </ul> </div> </div> <div class="row-fluid"> <div class="span12"> <div id="main"> <h1 class="page-title">Source: documents/schema.js</h1> <section> <article> <pre class="sunlight-highlight-javascript linenums">"use strict"; /** * @fileOverview Enables a schema and validation feature set to your document or other object. * @module documents/schema * @requires base * @requires jjv * @require lodash */ var sys = require( "lodash" ); var Validator = require( "jjv" ); var Base = require( "../base" ); /** * The validator mixin provides access to the features of the JSON validation system * @exports documents/schema * @mixin */ var Schema = Base.compose( [Base], /** @lends documents/schema# */{ constructor : function () { /** * The schema that defines the validation rules. This should probably be defined at the prototype for each * object or model classification. It can be an anonymous schema defined right here, or this can be * registered schema names to use, or just a single name * * @type {object} * @memberOf documents/schema# * @name schema */ /** * If you want to register multiple schemas, use this property instead * * @type {object} * @memberOf documents/schema# * @name schemas */ /** * The validation environment * @private * @type {jjv} */ var env = new Validator(); /** * The default name of the scheman when you use anonymous schemas. You can define this at the prototype for classified * schemas. The can also * * @type {string|function():{string}} * @memberOf documents/schema# * @name _defaultSchemaName */ this._defaultSchemaName = sys.result( this, "_defaultSchemaName" ) || sys.uniqueId( "schema" ); /** * The options to pass to the validator when it runs * @type {object|function():{object}} * @name validationOptions * @memberOf documents/schema# */ this.validationOptions = sys.defaults( {}, sys.result( this, 'validationOptions' ), {checkRequired : true} ); /** * Validate an object against the schema * @returns {object?} * @method * @name validate * @memberOf documents/schema# * @param {object=} record The record to validate * @param {string|object=} schemaName The name of a previously registered schema * @param {object=} options Options to pass to the validator * @example * // This supports these signatures: * * instance.validate(record, schemaName, options); * * * instance.validate(); // this, this._defaultSchemaName, this.validationOptions * instance.validate(record); // record, this._defaultSchemaName, this.validationOptions * instance.validate(schemaName); //this, schemaName, this.validationOptions * instance.validate(record, schemaName); //record, schemaName, this.validationOptions * instance.validate(schemaName, options); //this, schemaName, this.validationOptions */ this.validate = function ( record, schemaName, options ) { if ( arguments.length === 0 ) { record = this; schemaName = this._defaultSchemaName; options = this.validationOptions; } else { if ( sys.isString( record ) ) { schemaName = record; record = this; } if ( sys.isEmpty( options ) ) { options = this.validationOptions; } } return env.validate( schemaName, record, options ); }; /** * Initialize the schema collection by registering the with the handler. You can call this at any time and as often as you like. It will be called once * by the constructor on any instance schemas * @method * @name registerSchemas * @memberOf documents/schema# * @param {hash} schemas A hash of schemas where the key is the name of the schema */ this.registerSchemas = function ( schemas ) { var schema = sys.result( this, "schema" ); var schemas = schemas || sys.result( this, "schemas" ); if ( !sys.isEmpty( schema ) ) { env.addSchema( this._defaultSchemaName, schema ); } if ( !sys.isEmpty( schemas ) ) { sys.each( schemas, function ( val, key ) { env.addSchema( val, key ); } ); } }; /** * Extracts only the elements of the object that are defined in the schema * @memberOf documents/schema# * @name extract * @param {object=} record The record to extract from * @param {string=} schema The name of the schema to attach * @method */ this.extract = function ( record, schema ) { if ( arguments.length === 0 ) { record = this; schema = this._defaultSchemaName; } if ( sys.isString( record ) ) { schema = record; record = this; } }; /** * Create a type to be used in your schemas to define new validators * @memberOf documents/schema# * @name addType * @method * @param {string} name The name of the type * @param {function(object)} operation What to do with the type. * @param {object} operation.value The value to validation * @returns {boolean} */ this.addType = env.addType; /** * It is also possible to add support for additional string formats through the addFormat function. * @memberOf documents/schema# * @name addFormat * @method * @param {string} name The name of the formatter * @param {function(object)} formatter How to format it * @param {object} formatter.value The value to format * @returns {boolean} */ this.addFormat = env.addFormat; /** * It is possible to add support for custom checks (i.e., minItems, maxItems, minLength, maxLength, etc.) through the addCheck function * @memberOf documents/schema# * @name addCheck * @method * @param {string} name The name of the check * @param {function(...object)} formatter Perform the check * @param {object} formatter.value The value to check followed by any parameters from the schema * @returns {boolean} */ this.addCheck = env.addCheck; /** * Custom coercion rules * * @memberOf documents/schema# * @name addTypeCoercion * @method * @param {string} name The name of the coercion * @param {function(object)} coercer Perform the coercion * @param {object} coercer.value The value to coerce * @returns {boolean} */ this.addTypeCoercion = env.addTypeCoercion; /** * Get a registered schema by name * @param {string=} schemaName * @returns {object?} * @memberOf documents/schema# * @name getSchema * @method */ this.getSchema = function ( schemaName ) { if ( sys.isEmpty( schemaName ) || !sys.isString() ) { schemaName = this._defaultSchemaName; } return env.schema[schemaName]; } }, /** * This method will create a new object that contains only the fields and no methods or other artifacts. This is useful * for creating objects to pass over the wire or save in a table. This is not deeply copied, so changes made to the * extracted object will be represented in this class for reference objects. * * @param {string=} schema The schema name to use * @param {object=} src The object to extract fields from * @return {object} Data-only version of the class instance. */ extract : function ( schemaName, src ) { if ( sys.isObject( schemaName ) ) { src = schema; schemaName = this._defaultSchemaName; } if ( sys.isEmpty( src ) ) { src = this; } if ( sys.isFunction( src.toJSON ) ) { src = src.toJSON(); } var schema = this.getSchema( schemaName ) || {}; var newobj = {}; sys.each( schema.properties, function ( prop, propname ) { if ( prop.properties &amp;&amp; !sys.isUndefined( src[ propname ] ) ) { newobj[ propname ] = this.extract( prop, src[propname] ); } else if ( !sys.isUndefined( src[ propname ] ) ) { newobj[ propname ] = src[ propname ]; } }, this ); return newobj; }, /** * Builds a default document based on the schema. What this does is create a document from schema and for each property * that has a default value or is required, the resultant object will contain that property. It is useful for extending * values from some source that may be incomplete, like options or some such. * @param {json-schema} schema A schema to use to create the default document * @returns {object?} * @name defaultDoc * @memberOf documents/schema# * @method */ defaultDoc : function ( schemaName ) { if ( sys.isEmpty( schemaName ) ) { schemaName = this._defaultSchemaName; } var newdoc = {}; var schema; if ( sys.isObject( schemaName ) ) { schema = schemaName; } else { schema = this.getSchema( schemaName ) || {}; } sys.each( schema.properties, function ( val, key ) { var def = val[ "default" ]; // keyword and all that if ( val.type === "object" &amp;&amp; !sys.isEmpty( val.properties ) ) { newdoc[ key ] = this.defaultDoc( val ); } else { if ( sys.isFunction( def ) || sys.isBoolean( def ) || sys.isNumber( def ) || !sys.isEmpty( def ) ) { if ( sys.isFunction( def ) ) { newdoc[ key ] = def( schema ); } else { newdoc[ key ] = def; } } else if ( val.required ) { if ( val.type === 'string' ) { newdoc[ key ] = null; } else if ( val.type === 'object' ) { newdoc[ key ] = {}; } else if ( val.type === 'array' ) { newdoc[ key ] = []; } else { newdoc[ key ] = null; } } } }, this ); return newdoc; } } ); module.exports = Schema; </pre> </article> </section> </div> <div class="clearfix"></div> <footer> <span class="copyright"> DocStrap Copyright © 2012-2013 The contributors to the JSDoc3 and DocStrap projects. </span> <br /> <span class="jsdoc-message"> Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.3.0-alpha5</a> on Sun Jun 1st 2014 using the <a href="https://github.com/terryweiss/docstrap">DocStrap template</a>. </span> </footer> </div> <br clear="both"> </div> </div> <!--<script src="scripts/sunlight.js"></script>--> <script src="scripts/docstrap.lib.js"></script> <script src="scripts/bootstrap-dropdown.js"></script> <script src="scripts/toc.js"></script> <script> $( function () { $( "#toc" ).toc( { anchorName : function ( i, heading, prefix ) { return $( heading ).attr( "id" ) || ( prefix + i ); }, selectors : "h1,h2,h3,h4", showAndHide : false, scrollTo : 60 } ); $( "#toc>ul" ).addClass( "nav nav-pills nav-stacked" ); $( "#main span[id^='toc']" ).addClass( "toc-shim" ); // $( ".tutorial-section pre, .readme-section pre" ).addClass( "sunlight-highlight-javascript" ).addClass( "linenums" ); $( ".tutorial-section pre, .readme-section pre" ).each( function () { var $this = $( this ); var example = $this.find("code" ); exampleText = example.html(); var lang = /{@lang (.*?)}/.exec( exampleText ); if ( lang && lang[1] ) { exampleText = exampleText.replace( lang[0], "" ); example.html(exampleText); lang = lang[1]; } else { lang = "javascript"; } if ( lang ) { $this .addClass( "sunlight-highlight-" + lang ) .addClass( "linenums" ) .html( example.html() ); } } ); Sunlight.highlightAll( { lineNumbers : true, showMenu:true, enableDoclinks:true } ); } ); </script> <!--Google Analytics--> <!--Navigation and Symbol Display--> </body> </html>