JSON: Is there an equivalent of Schematron for JSON and JSON Schema? (That is, a JSON technology to express co-constraints) JSON: Is there an equivalent of Schematron for JSON and JSON Schema? (That is, a JSON technology to express co-constraints) json json

JSON: Is there an equivalent of Schematron for JSON and JSON Schema? (That is, a JSON technology to express co-constraints)


Yes.There is a JSON Semantic Validator based on Schematron available at:https://www.npmjs.com/package/jsontron

It implements 'schema', 'phase', 'rule', 'assert' and reporting features of Schematron.

Here is when the original example of start time and end time was run through the validator:

good_time.json file contents:

{"starttime": "2015-02-19T08:00:00Z","endtime": "2015-02-19T09:00:00Z"}

bad_time.json file contents:

{"starttime": "2015-02-19T09:00:00Z","endtime": "2015-02-19T08:00:00Z"}

Schematron Rules file meeting-times-rules.json snippet:

        "rule":[            {                      "context": "$",            "assert":[              {                 "id":"start_stop_meeting_chec",                 "test":"jp.query(contextNode, '$..starttime') < jp.query(contextNode, '$..endtime')",                 "message": "Meeting cannot end before it starts"              }            ]                       }        ]

When ran with correct example:

$jsontron\bin>node JSONValidator -i ./good_time.json -r ./meeting-times-rules.json

The output was:

Starting Semantic Validation .........Parsing Pattern: Meetingtimings1 Pattern(s) Requested. 1 Pattern(s) Processed. 0 Pattern(s)  Ignored.**** THIS INSTANCE IS SEMANTICALLY VALID ****Completed Semantic Validation .........

When ran with bad data example. The output was:

$jsontron\bin>node JSONValidator -i ./bad_time.json -r ./meeting-times-rules.jsonStarting Semantic Validation .........Parsing Pattern: Meetingtimings1 Pattern(s) Requested. 1 Pattern(s) Processed. 0 Pattern(s)  Ignored.**** THIS INSTANCE CONTAINS SEMANTIC VALIDATION ISSUES. PLEASE SEE FULL REPORT BY ENABLING DEBUG WITH -d OPTION ****Completed Semantic Validation .........

The message with debug options was:

...validation failed...message: 'Meeting cannot end before it starts'


Sadly, the answer is no. JSON Schema allows you to validate the structure, and permitted values, but there are no mechanisms for validating sets of values, a'la Schematron.

The simplest way to solve this is to have another script in the pipeline which runs these kinds of checks.


There is an implementation in Oxygen JSON Editor that allows you to validate JSON documents against Schematron.https://www.oxygenxml.com/doc/versions/22.0/ug-editor/topics/json-validating-documents-against-schema.html

The Schematron rules are expressed using XPath expressions, and the problems are reported in the JSON documents.

<!-- The 'genre' property should be none but one of the items declared in 'literatureGenres' property --><sch:rule context="genre">    <sch:let name="genre" value="text()"/>    <sch:let name="literatureGenres" value="//literatureGenres/text()"/>    <sch:assert test="normalize-space($genre) = $literatureGenres">            Wrong genre: '<sch:value-of select="$genre"/>'. See the 'literatureGenres' property for the permitted ones.    </sch:assert>    </sch:rule>

https://www.slideshare.net/nottavy/schematron-for-nonxml-languages