forked from zaach/jsonlint
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add web and programmatic interfaces to JSON Schema validation
* Introduce a new module "jsonlint/lib/validator". * Add schema support on the web page with the on-line validator.
- Loading branch information
Showing
11 changed files
with
220 additions
and
76 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 |
---|---|---|
|
@@ -2,3 +2,4 @@ jsonlint*.js | |
test/fails | ||
test/passes | ||
test/recursive | ||
web/*.min.* |
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 |
---|---|---|
|
@@ -2,3 +2,4 @@ | |
coverage | ||
node_modules | ||
jsonlint*.js | ||
web/*.min.* |
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
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,56 @@ | ||
var validator = (function () { | ||
var jsonlint, Ajv, requireDraft | ||
if (typeof window !== 'undefined') { | ||
jsonlint = window.jsonlint | ||
Ajv = window.Ajv | ||
requireDraft = function (environment) { | ||
return window.drafts[environment] | ||
} | ||
} else { | ||
jsonlint = require('./jsonlint') | ||
Ajv = require('ajv') | ||
requireDraft = function (environment) { | ||
return require('ajv/lib/refs/' + environment + '.json') | ||
} | ||
} | ||
|
||
function compile (schema, environment) { | ||
var ajv | ||
if (!environment) { | ||
ajv = new Ajv({ schemaId: 'auto' }) | ||
ajv.addMetaSchema(requireDraft('json-schema-draft-04')) | ||
ajv.addMetaSchema(requireDraft('json-schema-draft-06')) | ||
} else if (environment === 'json-schema-draft-07') { | ||
ajv = new Ajv() | ||
} else if (environment === 'json-schema-draft-06') { | ||
ajv = new Ajv() | ||
ajv.addMetaSchema(requireDraft('json-schema-draft-06')) | ||
} else if (environment === 'json-schema-draft-04') { | ||
ajv = new Ajv({ schemaId: 'id' }) | ||
ajv.addMetaSchema(requireDraft('json-schema-draft-04')) | ||
} else { | ||
throw new Error('Unsupported environment for the JSON schema validation: "' + | ||
environment + '".') | ||
} | ||
var validate | ||
try { | ||
schema = jsonlint.parse(schema) | ||
validate = ajv.compile(schema) | ||
} catch (error) { | ||
throw new Error('Compiling the JSON schema failed.\n' + error.message) | ||
} | ||
return function (data) { | ||
var result = validate(data) | ||
if (!result) { | ||
var message = ajv.errorsText(validate.errors) | ||
throw new Error(message) | ||
} | ||
} | ||
} | ||
|
||
return { compile: compile } | ||
}()) | ||
|
||
if (typeof require !== 'undefined' && typeof exports !== 'undefined') { | ||
exports.compile = validator.compile | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
var fs = require('fs') | ||
var path = require('path') | ||
|
||
var environments = [ | ||
'json-schema-draft-04', | ||
'json-schema-draft-06', | ||
'json-schema-draft-07' | ||
] | ||
var drafts = environments.map(function (environment) { | ||
var draftFile = path.join(__dirname, '../node_modules/ajv/lib/refs/' + environment + '.json') | ||
var draftSource = fs.readFileSync(draftFile) | ||
return 'exports["' + environment + '"]=' + draftSource | ||
}) | ||
var source = 'var drafts=(function(){var require=true,module=false,exports={};\n' + | ||
drafts.join('\n') + | ||
'\nreturn exports})()' | ||
|
||
console.log(source) |
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,11 @@ | ||
var fs = require('fs') | ||
|
||
var scriptFile = process.argv[2] | ||
var exportName = process.argv[3] | ||
|
||
var source = 'var ' + exportName + | ||
'=(function(){var require=true,module=false,exports={};\n' + | ||
fs.readFileSync(scriptFile, 'utf8') + | ||
'\nreturn exports})()' | ||
|
||
console.log(source) |
This file was deleted.
Oops, something went wrong.
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
Oops, something went wrong.