Commit 54240242 authored by Jaap Karan Singh's avatar Jaap Karan Singh

Added speed type and fixed bug with custom types

parent 69dc6f64
node_modules
{
"node": true,
"curly": true,
"latedef": true,
"quotmark": true,
"undef": true,
"unused": true,
"trailing": true
}
{
"name": "js_jsonschema-validator",
"version": "0.1.0",
"version": "0.1.1",
"description": "jsonschema validator with custom types and attributes",
"main": "index.js",
"directories": {
"test": "test"
},
"scripts": {
"test": "./node_modules/.bin/mocha --bail"
"test": "./node_modules/.bin/mocha --bail --reporter spec"
},
"author": "Jaap Karan Singh <jksdua@gmail.com>",
"repository": {
"type": "git",
"url": "https://gitlab.com/jksdua__common/jsonschema-validator"
},
"dependencies": {
"js-quantities": "^1.4.1",
"jshint": "^2.5.2",
"jsonschema": "~0.4.0",
"lodash": "~2.4.1"
"lodash": "~2.4.1",
"precommit-hook": "^1.0.2"
},
"devDependencies": {
"chai": "^1.9.1",
......
......@@ -9,14 +9,8 @@ Usage
### Installation
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.
```json
{
"dependencies": {
"js_jsonschema-validator": "git+ssh://git@gitlab.com:jksdua__common/jsonschema-validator.git#v0.1.0"
}
}
```bash
$ npm install --save git+ssh://git@gitlab.com:jksdua__common/jsonschema-validator.git#v0.1.1
```
```js
......@@ -36,6 +30,10 @@ validator.validate('123456789012345678901234', { type: 'objectId' });
Changelog
---------
### v0.1.1 (17 July 2014)
- Added `speed` type
- Fixed bug where custom types were not getting picked up
### v0.1.0 (24 June 2014)
- Changed namespace to `js` from `ht`
......
/* jshint node:true */
'use strict';
var _ = require('lodash');
......@@ -11,12 +9,10 @@ var customAttributes = require(__dirname + '/attributes');
function Validator() {
BasicValidator.apply(this, arguments);
_.extend(this.types, customTypes);
_.extend(this.attributes, customAttributes);
}
var proto = Validator.prototype = Object.create(BasicValidator.prototype);
proto.types = Object.create(proto.types);
_.extend(proto.types, customTypes);
Validator.prototype = Object.create(BasicValidator.prototype);
module.exports = Validator;
\ No newline at end of file
......@@ -3,11 +3,20 @@
'use strict';
var _ = require('lodash');
var Quantity = require('js-quantities');
exports.error = function testError(er) {
return (er instanceof Error);
};
exports.speed = function testSpeed(sp) {
try {
return ('speed' === (new Quantity(sp)).kind());
} catch(e) {
return false;
}
};
exports.regexp = function testRegexp(re) {
return (re instanceof RegExp);
};
......
......@@ -14,26 +14,58 @@ var collection = db.get('collection');
describe('#types', function() {
describe('#regexp', function() {
it('should not work', function() {
var res = validator.validate('abc', { type: 'regexp' });
expect(res.valid).to.be.false; // jshint ignore:line
});
it('should work', function() {
var res = validator.validate(/abc/, { type: 'regexp' });
expect(res.valid).to.be.true;
expect(res.valid).to.be.true; // jshint ignore:line
});
});
describe('#objectid', function() {
it('should not work for an invalid id', function() {
var res = validator.validate('1234', { type: 'objectId' });
expect(res.valid).to.be.false; // jshint ignore:line
});
it('should work for string representation', function() {
var res = validator.validate('123456789012', { type: 'objectId' });
expect(res.valid).to.be.true;
expect(res.valid).to.be.true; // jshint ignore:line
});
it('should work for hex representation', function() {
var res = validator.validate('123456789012345678901234', { type: 'objectId' });
expect(res.valid).to.be.true;
expect(res.valid).to.be.true; // jshint ignore:line
});
it('should work for objectid instance', function() {
var res = validator.validate(collection.id(), { type: 'objectId' });
expect(res.valid).to.be.true;
expect(res.valid).to.be.true; // jshint ignore:line
});
});
describe('#speed', function() {
it('should not work for non string', function() {
var res = validator.validate(1234, { type: 'speed' });
expect(res.valid).to.be.false; // jshint ignore:line
});
it('should not work for a string that is not a quantity', function() {
var res = validator.validate('abc', { type: 'speed' });
expect(res.valid).to.be.false; // jshint ignore:line
});
it('should not work for a string that is of the wrong type', function() {
var res = validator.validate('1 metre', { type: 'speed' });
expect(res.valid).to.be.false; // jshint ignore:line
});
it('should work for the correct quantity type', function() {
var res = validator.validate('1 kph', { type: 'speed' });
expect(res.valid).to.be.true; // jshint ignore:line
});
});
});
......
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