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

Added quantity types backed by js-quantities

parent f75b6acf
{
"name": "jsonschema-extra",
"version": "1.1.1",
"version": "1.2.0",
"description": "Extends jsonschema validator with some common custom types and attributes",
"main": "index.js",
"directories": {
......@@ -19,6 +19,7 @@
],
"author": "Jaap Karan Singh <jksdua@gmail.com>",
"dependencies": {
"js-quantities": "^1.5.0",
"lodash.isplainobject": "^2.4.1"
},
"devDependencies": {
......
......@@ -117,6 +117,61 @@ validator.validate(new ObjectID(), { type: 'objectId' });
validator.validate({ a: 'a', b: 'b' }, { type: 'plainObject' });
```
### js-quantities types
Quantity types supported by `js-quantities`
- qty.acceleration
- qty.activity
- qty.angle
- qty.angular_velocity
- qty.area
- qty.capacitance
- qty.charge
- qty.conductance
- qty.currency
- qty.current
- qty.energy
- qty.force
- qty.frequency
- qty.illuminance
- qty.inductance
- qty.length
- qty.luminous_power
- qty.magnetism
- qty.magnetism
- qty.magnetism
- qty.mass
- qty.mass_concentration
- qty.memory
- qty.molar_concentration
- qty.potential
- qty.power
- qty.pressure
- qty.radiation
- qty.resistance
- qty.speed
- qty.substance
- qty.temperature
- qty.time
- qty.unitless
- qty.viscosity
- qty.viscosity
- qty.volume
```js
validator.validate('1 meter', { type: 'qty.length' });
validator.validate('45.3 seconds', { type: 'qty.time' });
// can also validate Qty instances
var Qty = require('js-quantities');
var qty = Qty.parse('2 meters');
validator.validate(qty, { type: 'qty.length' });
```
## Testing
......@@ -135,7 +190,10 @@ $ npm test
## Changelog
### v1.1.1 (19 Sep 2014)
### v1.2.0 (22 Dec 2014)
- Added quantity types backed by [js-quantities](https://www.npmjs.com/package/js-quantities)
### v1.1.1 (19 Dec 2014)
- Minor doc updates
### v1.1.0 (18 Sep 2014)
......
......@@ -14,6 +14,11 @@ describe('#jsonschema-extra', function() {
attributes: require(__dirname + '/attributes')
};
function startsWith(string, searchString, position) {
position = position || 0;
return string.lastIndexOf(searchString, position) === position;
}
var assertions = {
types: {
error: [
......@@ -95,6 +100,16 @@ describe('#jsonschema-extra', function() {
instance: { a: { a: 1 } },
pass: true
}
],
'qty.speed': [
{
instance: 123,
pass: false
},
{
instance: '123 kph',
pass: true
}
]
},
attributes: {
......@@ -166,10 +181,12 @@ describe('#jsonschema-extra', function() {
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
);
if (!startsWith(name, 'qty.')) {
assert(
name in assertions[category],
'Missing assertions for ' + category + '.' + name
);
}
});
});
});
......
......@@ -2,6 +2,7 @@
'use strict';
var Qty = require('js-quantities');
var isPlainObject = require('lodash.isplainobject');
exports.error = function testError(er) {
......@@ -32,4 +33,20 @@ exports.objectId = function testObjectId(id) {
exports.plainObject = function testPlainObject(ob) {
return isPlainObject(ob);
};
\ No newline at end of file
};
// quantities
var QTY_PREFIX = 'qty.';
function isKind(kind) {
return function(qty) {
qty = (qty instanceof Qty ? qty : Qty.parse('' + qty));
return (qty && kind === qty.kind());
};
}
Qty.getKinds().forEach(function(kind) {
exports[QTY_PREFIX + kind] = isKind(kind);
});
\ 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