Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#11 NPM package shouldn't include any code that requires JSV
- Loading branch information
Showing
4 changed files
with
58 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
/node_modules | ||
/test | ||
/lib/jsprim-jsv.js | ||
/jsl.node.conf | ||
/Makefile* | ||
/.gitignore | ||
/.gitmodules | ||
/.npmignore |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters