4

I'd like to know whether there are any JavaScript code libraries which allow walking through a JSON Schema and JSON data file in conjunction such that one could grab type/schema information while walking through a JSON data file (without it being limited to whole-document validation).

Although such a tool might allow piecemeal validation, it would not need to be used for validation but would simply provide JSON Schema type information corresponding to any point in a JSON data file (and possibly vice versa as well).

My interest in this would be as the core for the following uses for arbitrary JSON paired to a JSON schema file:

  • a default display of a JSON file similar to JHTML, but JSON Schema-aware (e.g., displaying not only arrays as lists, strings within a bordered area, etc., but also URLs as links, HTML within an iframe preview, etc.)
  • type-aware editing of a JSON file (similar to JSONEditor) or a subset/joins of JSON files
  • type-aware (and hierarchy-aware) default search options for arbitrary JSON (e.g., exposing a date range UI when detecting date types--for refining one's search to branches of a data file which included date types of a certain range)
  • enhancing JSONPath to be schema-aware (e.g., allowing for a single path expression to find all URL instances in a file)
2

It seems like I met the same problem some time ago and also did not found a solution. So I've created my own tool to iterate over JSON schema and object. You may take a look at it here

It allows to write your own processors the following way:

var s = require('schemasaurus');

var it = s.newIterator({...your schema... }, function () {
   return {
     //matches all string fields (accroding to schema)
     "[type=string]": function (schema, object, ctx) {
        console.log("string field [" + ctx.path.join('.') + "] = " + object");
     }
   }
});

it({...your object...});

Please check github repo for more examples.

Hope that helps.

  • This looks supremely awesome. Thank you for creating this and sharing. Eager to see whether a generic JSON Reference library can be applied for the remote references and whether your work could handle (or be made to handle) lazy loading of the schema or input JSON. I'm wondering also about whether the test suite checks for infinite recursion. – Brett Zamir Apr 18 '15 at 23:57
  • I am still not clear though whether your library allows for the piecemeal iteration, i.e., walking through the input and its resulting schema object and stopping or continuing validation (or whatever one is doing). I guess I'll take a closer look and see if it can be adapted if not. – Brett Zamir Apr 19 '15 at 0:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.