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"
}
Roger Costello
  • 3,007
  • 1
  • 22
  • 43
  • 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 ValidatorBuddy](http://www.xml-buddy.com) – Clemens Feb 24 '15 at 14:03

4 Answers4

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'
Amer
  • 61
  • 1
  • 4
  • The *jsontron* syntax seems something ugly... Perhaps in the future [jsontron development](https://github.com/amer-ali/jsontron) can use better path-syntax by [JSON Pointer](https://tools.ietf.org/html/rfc6901#section-5), and betther general syntax by modern Javascript [destructuring assignments](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment). It need some compatibility with [json-schema](http://json-schema.org), as in XML they schema and schematron are complementar ones. – Peter Krauss Dec 31 '18 at 13:42
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.

jasiek
  • 51
  • 2
  • 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"*. – Peter Krauss Dec 31 '18 at 13:47
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

Octavian
  • 416
  • 2
  • 4
-3

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

Liosan
  • 7,520
  • 2
  • 17
  • 16