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

4

10

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"
}
Dynamiter answered 20/2, 2015 at 12:32 Comment(1)
Not exactly what you are looking for but you could convert the JSON data first to XML and then apply any Schematron rules on it afterwards? A tool that supports both would be XML ValidatorBuddyPajamas
H
6

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'
Headmistress answered 16/10, 2018 at 17:14 Comment(1)
The jsontron syntax seems something ugly... Perhaps in the future jsontron development can use better path-syntax by JSON Pointer, and betther general syntax by modern Javascript destructuring assignments. It need some compatibility with json-schema, as in XML they schema and schematron are complementar ones.Epenthesis
K
1

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.

Kushner answered 17/8, 2016 at 12:52 Comment(1)
The answer is "yes", check @Amer's jsontron (!)... Well, seems a good starting point and can evolve to standard or reference-implementation. Today need some community-tests to confirm that is a "mechanism for validating sets of values, a'la Schematron".Epenthesis
K
1

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

Kenwee answered 21/2, 2020 at 7:17 Comment(0)
L
-3

The json-schema.org website lists quite a few implementations.

Lamellar answered 29/5, 2015 at 10:40 Comment(1)
Nothing on that page is relevant to the question about schematron.Hudnall

© 2022 - 2024 — McMap. All rights reserved.