jsonschema EXTRAAA!!! ===================== **Extends jsonschema validator with some common custom types and attributes** Usage ----- ### Installation ```bash npm install jsonschema-extra --save ``` ### Example ```js var jsonschema = require('jsonschema'); var extend = require('jsonschema-extra'); var validator = new (jsonschema.Validator)(); extend(validator); // regexp validator.validate(/abc/, { type: 'regexp' }); // error validator.validate(new Error(), { type: 'error' }); // mongodb objectid validator.validate('123456789012345678901234', { type: 'objectId' }); ``` ### Details #### Attributes See `jsonschema` documentation for more detailed documentation on custom attributes. ##### validate Custom validator when `jsonschema` just isn't what you are after. This one is my absolute favourite! It comes in handy whenever you are trying to do something complex. > One of the features of jsonschema is that you can share it across client and server which is exactly what I do! However, if your schema contains a validate attribute it will no longer be valid json. This is not a big issue if you are ok for client side code to not contain all your validation logic if schema is sent from the server. ```js var schema = { validate: function(instance, schema, options) { // do your crazy validation here // return an error string if not valid return 'is not a valid instance'; } }; ``` #### Types Supported types: - error (instanceof Error) - regexp (instanceof RegExp) - function (Function - also works for Generator Function) - generatorFunction (ES6 Generator Function) - objectId (MongoDb objectId) - plainObject (calls _.isPlainObject) ```js validator.validate(new Error(), { type: 'error' }); validator.validate(/regexp/, { type: 'regexp' }); validator.validate(function() {}, { type: 'function' }); validator.validate(function *() {}, { type: 'generatorFunction' }); validator.validate('123456789012345678901234', { type: 'objectId' }); validator.validate(new ObjectID(), { type: 'objectId' }); validator.validate({ a: 'a', b: 'b' }, { type: 'plainObject' }); ``` Changelog --------- ### v1.0.0 (27 Sep 2014) - Changed public API - Name of module changed to jsonschema-extra - Removed conditionalEnum custom attribute since this has been fixed in the latest versions of jsonschema ### v0.1.0 (24 June 2014) - Changed namespace to `js` from `ht` ### v0.0.2 (24 June 2014) - Removed dependency on `mongoskin` - Added some tests - Removed shrinkwrap ### v0.0.1 (24 June 2014) - Initial commit