Commit 3ca099e3 authored by Jaap Karan Singh's avatar Jaap Karan Singh

Revert "Changed public api"

This reverts commit 66472b3a.
parent 66472b3a
......@@ -2,26 +2,6 @@
'use strict';
var assert = require('assert');
console.info('[DANGER] Overriding jsonschema module manually. It will be replaced once the module is updated. Pull request submitted - https://github.com/tdegrunt/jsonschema/pull/62');
var types = require(__dirname + '/types');
var attributes = require(__dirname + '/attributes');
function extend(a, b) {
Object.keys(b).forEach(function(prop) {
a[prop] = b[prop];
});
return a;
}
module.exports = function(validator) {
assert(validator && validator.attributes && validator.types, 'Missing / invalid validator');
validator.types = Object.create(validator.types);
extend(validator.types, types);
validator.attributes = Object.create(validator.attributes);
extend(validator.attributes, attributes);
return validator;
};
\ No newline at end of file
module.exports = require(__dirname + '/src');
\ No newline at end of file
{
"name": "jsonschema-extra",
"version": "1.0.0",
"description": "Extends jsonschema with custom types and attributes",
"name": "js_jsonschema-validator",
"version": "0.1.0",
"description": "jsonschema validator with custom types and attributes",
"main": "index.js",
"directories": {
"test": "test"
},
"scripts": {
"test": "./node_modules/.bin/mocha --bail --harmony --reporter spec"
"test": "./node_modules/.bin/mocha --bail"
},
"author": "Jaap Karan Singh <jksdua@gmail.com>",
"dependencies": {
"lodash.isplainobject": "^2.4.1"
"jsonschema": "~0.4.0",
"lodash": "~2.4.1"
},
"devDependencies": {
"jsonschema": "^1.0.0",
"mongodb": "^1.4.16"
"chai": "^1.9.1",
"mocha": "^1.20.1",
"monk": "^0.9.0"
}
}
jsonschema EXTRAAA!!!
JSON Schema Validator
=====================
**Extends jsonschema validator with some common custom types and attributes**
jsonschema validator with custom types and attributes
Usage
......@@ -9,18 +9,18 @@ Usage
### Installation
```bash
npm install jsonschema-extra --save
```
Install the module as a git npm module. For example, to install `v0.1.0` add the following to `package.json`. **Note:** It is important to use `js_jsonschema-validator` as the dependency name.
### Example
```json
{
"dependencies": {
"js_jsonschema-validator": "git+ssh://git@gitlab.com:jksdua__common/jsonschema-validator.git#v0.1.0"
}
}
```
```js
var jsonschema = require('jsonschema');
var extend = require('jsonschema-extra');
var validator = new (jsonschema.Validator)();
extend(validator);
var validator = require('js_jsonschema-validator').validator;
// regexp
validator.validate(/abc/, { type: 'regexp' });
......@@ -28,69 +28,14 @@ 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' });
// objectid
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`
......
/* jshint node:true */
'use strict';
var _ = require('lodash');
var jsonschema = require('jsonschema');
var customTypes = require(__dirname + '/types');
var BasicValidator = jsonschema.Validator;
var customAttributes = require(__dirname + '/attributes');
function Validator() {
BasicValidator.apply(this, arguments);
_.extend(this.attributes, customAttributes);
}
var proto = Validator.prototype = Object.create(BasicValidator.prototype);
proto.types = Object.create(proto.types);
_.extend(proto.types, customTypes);
module.exports = Validator;
\ No newline at end of file
......@@ -6,4 +6,14 @@ exports.validate = function customValidate(instance, schema, opt, ctx) {
if (instance || schema.required) {
return schema.validate(instance, schema, opt, ctx);
}
};
exports.conditionalEnum = function conditionalEnum(instance, schema, options, ctx) {
var res;
if (instance) {
schema.enum = schema.conditionalEnum;
res = this.attributes.enum(instance, schema, options, ctx);
delete schema.enum/* = void 0 */;
}
return res;
};
\ No newline at end of file
/* jshint node:true */
'use strict';
var jsonschema = require('jsonschema');
exports.ValidationError = jsonschema.ValidationError;
exports.SchemaError = jsonschema.SchemaError;
exports.Validator = require(__dirname + '/Class.Validator');
exports.validator = new exports.Validator();
exports.custom = {
attributes: require(__dirname + '/attributes'),
types: require(__dirname + '/types')
};
\ No newline at end of file
......@@ -2,7 +2,7 @@
'use strict';
var isPlainObject = require('lodash.isPlainObject');
var _ = require('lodash');
exports.error = function testError(er) {
return (er instanceof Error);
......@@ -13,11 +13,7 @@ exports.regexp = function testRegexp(re) {
};
exports.function = function testFunction(fn) {
return ('function' === typeof fn);
};
exports.generatorFunction = function testGeneratorFunction(fn) {
return ('function' === typeof fn && 'GeneratorFunction' === fn.constructor.name);
return (fn instanceof Function);
};
exports.objectId = function testObjectId(id) {
......@@ -31,5 +27,5 @@ exports.objectId = function testObjectId(id) {
};
exports.plainObject = function testPlainObject(ob) {
return isPlainObject(ob);
return _.isPlainObject(ob);
};
\ No newline at end of file
......@@ -3,178 +3,39 @@
'use strict';
describe('#jsonschema-extra', function() {
var assert = require('assert');
var ObjectID = require('mongodb').ObjectID;
var monk = require('monk');
var chai = require('chai');
var expect = chai.expect;
var validator = new (require('jsonschema').Validator)();
require(__dirname)(validator);
var validator = require(__dirname + '/src').validator;
var extras = {
types: require(__dirname + '/types'),
attributes: require(__dirname + '/attributes')
};
var db = monk('localhost/test');
var collection = db.get('collection');
var assertions = {
types: {
error: [
{
instance: 123,
pass: false
},
{
instance: new Error(),
pass: true
}
],
regexp: [
{
instance: 'abc',
pass: false
},
{
instance: /a/,
pass: true
},
{
instance: new RegExp('a', 'i'),
pass: true
}
],
function: [
{
instance: new Function(), // jshint ignore:line
pass: true
},
{
instance: eval('function a() {}'), // jshint ignore:line
pass: true
},
{
instance: function() {},
pass: true
},
{
instance: function *() {}, // jshint ignore:line
pass: true
}
],
generatorFunction: [
{
instance: function() {},
pass: false
},
{
instance: function *() {}, // jshint ignore:line
pass: true
}
],
objectId: [
{
instance: 123,
pass: false
},
{
instance: '123456789012345678901234',
pass: true
},
{
instance: new ObjectID('123456789012345678901234'),
pass: true
},
{
instance: new ObjectID(),
pass: true
}
],
plainObject: [
{
instance: 123,
pass: false
},
{
instance: new String('abc'), // jshint ignore:line
pass: false
},
{
instance: new ObjectID('123456789012345678901234'),
pass: false
},
{
instance: { a: 1 },
pass: true
},
{
instance: { a: { a: 1 } },
pass: true
}
]
},
attributes: {
validate: [
{
instance: 1,
schema: {
validate: function() {
return 'is not valid';
}
},
pass: false
},
{
instance: 2,
schema: {
validate: function() {
return;
}
},
pass: true
}
]
}
};
it('should test everything', function() {
Object.keys(extras).forEach(function(category) {
Object.keys(extras[category]).forEach(function(name) {
assert(
name in assertions[category],
'Missing assertions for ' + category + '.' + name
);
});
describe('#types', function() {
describe('#regexp', function() {
it('should work', function() {
var res = validator.validate(/abc/, { type: 'regexp' });
expect(res.valid).to.be.true;
});
});
describe('#types', function() {
Object.keys(assertions.types).forEach(function(type) {
var category = assertions.types[type];
var schema = { type: type };
describe('#' + type, function() {
category.forEach(function(assertion) {
it('should ' + (assertion.pass ? '' : 'not ') + 'work for ' + assertion.instance, function() {
var res = validator.validate(assertion.instance, schema);
assert.equal(res.valid, assertion.pass, res.errors);
});
});
});
describe('#objectid', function() {
it('should work for string representation', function() {
var res = validator.validate('123456789012', { type: 'objectId' });
expect(res.valid).to.be.true;
});
});
describe('#attributes', function() {
Object.keys(assertions.attributes).forEach(function(attribute) {
var category = assertions.attributes[attribute];
describe('#' + attribute, function() {
category.forEach(function(assertion) {
it('should ' + (assertion.pass ? '' : 'not ') + 'work for ' + assertion.instance, function() {
var res = validator.validate(assertion.instance, assertion.schema);
assert.equal(res.valid, assertion.pass, res.errors);
});
});
});
it('should work for hex representation', function() {
var res = validator.validate('123456789012345678901234', { type: 'objectId' });
expect(res.valid).to.be.true;
});
it('should work for objectid instance', function() {
var res = validator.validate(collection.id(), { type: 'objectId' });
expect(res.valid).to.be.true;
});
});
});
\ No newline at end of file
});
describe('#attributes', function() {});
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment