Skip to content

Commit

Permalink
#11 NPM package shouldn't include any code that requires JSV
Browse files Browse the repository at this point in the history
  • Loading branch information
arekinath committed Oct 15, 2015
1 parent 16a0692 commit 0f2ad65
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 41 deletions.
8 changes: 8 additions & 0 deletions .npmignore
@@ -0,0 +1,8 @@
/node_modules
/test
/lib/jsprim-jsv.js
/jsl.node.conf
/Makefile*
/.gitignore
/.gitmodules
/.npmignore
48 changes: 48 additions & 0 deletions lib/jsprim-jsv.js
@@ -0,0 +1,48 @@
/*
* lib/jsprim-jsv.js: extras for testing performance vs JSV
*/

var mod_assert = require('assert');
var mod_jsv; /* lazy-loaded because it may not be here */

module.exports = {
validateJsonObjectJSV: validateJsonObjectJSV
};

function validateJsonObjectJSV(schema, input)
{
if (!mod_jsv)
mod_jsv = require('JSV');

var env = mod_jsv.JSV.createEnvironment();
var report = env.validate(input, schema);

if (report.errors.length === 0)
return (null);

/* Currently, we only do anything useful with the first error. */
mod_assert.ok(report.errors.length > 0);
var error = report.errors[0];

/* The failed property is given by a URI with an irrelevant prefix. */
var propname = error['uri'].substr(error['uri'].indexOf('#') + 2);
var reason;

/*
* Some of the default error messages are pretty arcane, so we define
* new ones here.
*/
switch (error['attribute']) {
case 'type':
reason = 'expected ' + error['details'];
break;
default:
reason = error['message'].toLowerCase();
break;
}

var message = reason + ': "' + propname + '"';
var rv = new Error(message);
rv.jsv_details = error;
return (rv);
}
41 changes: 0 additions & 41 deletions lib/jsprim.js
Expand Up @@ -9,8 +9,6 @@ var mod_extsprintf = require('extsprintf');
var mod_verror = require('verror');
var mod_jsonschema = require('json-schema');

var mod_jsv; /* lazy-loaded because it may not be here */

/*
* Public interface
*/
Expand All @@ -23,7 +21,6 @@ exports.flattenObject = flattenObject;
exports.flattenIter = flattenIter;
exports.validateJsonObject = validateJsonObjectJS;
exports.validateJsonObjectJS = validateJsonObjectJS;
exports.validateJsonObjectJSV = validateJsonObjectJSV;
exports.randElt = randElt;
exports.extraProperties = extraProperties;
exports.mergeObjects = mergeObjects;
Expand Down Expand Up @@ -272,44 +269,6 @@ function parseDateTime(str)
}
}

function validateJsonObjectJSV(schema, input)
{
if (!mod_jsv)
mod_jsv = require('JSV');

var env = mod_jsv.JSV.createEnvironment();
var report = env.validate(input, schema);

if (report.errors.length === 0)
return (null);

/* Currently, we only do anything useful with the first error. */
mod_assert.ok(report.errors.length > 0);
var error = report.errors[0];

/* The failed property is given by a URI with an irrelevant prefix. */
var propname = error['uri'].substr(error['uri'].indexOf('#') + 2);
var reason;

/*
* Some of the default error messages are pretty arcane, so we define
* new ones here.
*/
switch (error['attribute']) {
case 'type':
reason = 'expected ' + error['details'];
break;
default:
reason = error['message'].toLowerCase();
break;
}

var message = reason + ': "' + propname + '"';
var rv = new Error(message);
rv.jsv_details = error;
return (rv);
}

function validateJsonObjectJS(schema, input)
{
var report = mod_jsonschema.validate(input, schema);
Expand Down
2 changes: 2 additions & 0 deletions test/validate-bench.js
Expand Up @@ -16,6 +16,7 @@

var sprintf = require('extsprintf').sprintf;
var jsprim = require('../lib/jsprim');
var jsprimjsv = require('../lib/jsprim-jsv');

/* BEGIN JSSTYLED */
var schema = {
Expand Down Expand Up @@ -150,6 +151,7 @@ var template = {
var start = Date.now();
var count = 1000;
var validate = jsprim.validateJsonObjectJS;
/* Or use: var validate = jsprimjsv.validateJsonObjectJSV; */
var i;

for (i = 0; i < count; i++)
Expand Down

0 comments on commit 0f2ad65

Please sign in to comment.