Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Here is a JSON instance showing the start-time and end-time for a meeting:

{
    "start time": "2015-02-19T08:00:00Z",
    "end time": "2015-02-19T09:00:00Z"
}

I can specify the structure of that instance using JSON Schema: the instance must contain an object with a "start time" property and an "end time" property and each property must be a date-time formatted string. See below for the JSON schema. But what I cannot specify is this: the meeting must start before it ends. That is, the value of "start time" must be less than the value of "end time". Some people call this data dependency a co-constraint. In the XML world there is a wonderful, simple technology for expressing co-constraints: Schematron. I am wondering if there is an equivalent technology in the JSON world? What would you use to declaratively describe the relationship between the value of "start time" and "end time"? (Note: writing code in some programming language is not what I mean by "declaratively describe the relationships". I am seeking a declarative means to describe the data dependencies that are present in JSON documents, not procedural code.)

{
    "$schema": "http://json-schema.org/draft-04/schema#",
    "definitions": {
        "meeting": {
            "type": "object",
            "properties": {
                "start time": { "type": "string", "format": "date-time"},
                "end time": { "type": "string", "format": "date-time"}
            },
            "required": [ "start time", "end time" ],
            "additionalProperties": false
        }
    },
    "$ref": "#/definitions/meeting"
}
like image 542
Roger Costello Avatar asked Feb 20 '15 12:02

Roger Costello


People also ask

What is the difference between JSON and JSON Schema?

JSON (JavaScript Object Notation) is a simple and lightweight text-based data format. JSON Schema is an IETF standard providing a format for what JSON data is required for a given application and how to interact with it.

Is there an XSD for JSON?

Since JSON isn't there yet, the best option is to use one of the XML Schema languages. We can use an XSD to generate our Java object model. We can use the Java objects to export data into XML documents. We can even use the same Java objects to export the data into JSON documents.

What is JSON Schema additionalProperties?

The additionalProperties keyword is used to control the handling of extra stuff, that is, properties whose names are not listed in the properties keyword or match any of the regular expressions in the patternProperties keyword. By default any additional properties are allowed.

Does JSON have schema?

JSON has a schema. REST services have WADL. Also there are tools like wadl2java . Old question, but worth clarifying: The JSON Schema standard includes "hyper-schemas", which specify links/actions - including HTTP method, required data (specified as JSON Schema), and expected results.

What is JSON Schema?

JSON Schema is a specification for JSON based format for defining the structure of JSON data. It was written under IETF draft which expired in 2011. JSON Schema − Describes your existing data format. Clear, human- and machine-readable documentation. Complete structural validation, useful for automated testing.

What is the best validator for JSON Schema?

There are several validators currently available for different programming languages. Currently the most complete and compliant JSON Schema validator available is JSV. Orderly (BSD); JSV; json-schema; Matic (MIT); Dojo; Persevere (modified BSD or AFL 2.0); schema.js.

What is JSON and why is it useful?

However, it has proven useful enough and simple enough that it is now used in many other contexts that don’t involve web surfing. At its heart, JSON is built on the following data structures: These types have analogs in most programming languages, though they may go by different names.

What is hypermedia ready JSON Schema?

JSON Schema is hypermedia ready, and ideal for annotating your existing JSON-based HTTP API. JSON Schema documents are identified by URIs, which can be used in HTTP Link headers, and inside JSON Schema documents to allow recursive definitions.


3 Answers

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: Meetingtimings
1 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.json
Starting Semantic Validation .........
Parsing Pattern: Meetingtimings
1 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'
like image 84
Amer Avatar answered Nov 12 '22 12:11

Amer


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.

like image 40
jasiek Avatar answered Nov 12 '22 11:11

jasiek


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

like image 39
Octavian Avatar answered Nov 12 '22 11:11

Octavian