Skip to content

Add "enforceOrder" keyword to objects #1073

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
git-n-pissed opened this issue Feb 1, 2021 · 6 comments
Closed

Add "enforceOrder" keyword to objects #1073

git-n-pissed opened this issue Feb 1, 2021 · 6 comments

Comments

@git-n-pissed
Copy link

Currently the JSON Schema does not have provisions to enforce the order of fields in an object. This is a big problem when the JSON in question is source controlled and generated by disparate tools. The contents of two JSON files may be identical, but the ordering may be different which can cause massive diffs in Git.

There are many other cases where the order of fields should also be preserved. One such instance is when JSON is used to define the memory structure of an embedded device.

XSD supports this via the "sequence" element:
https://www.w3.org/TR/2004/REC-xmlschema-0-20041028/#DefnDeclars

@gregsdennis
Copy link
Member

gregsdennis commented Feb 1, 2021

This has been requested here.

Also, JSON itself doesn't define sequencing for object keys, only for arrays, though some interpreters do support it. Having such a keyword could not be supported in all languages.

Note: the issue I linked is now the repo where we hold new keyword requests.

Mildly related to this issue.

@LikeLakers2
Copy link

LikeLakers2 commented Feb 1, 2021

I'd have to disagree with this sort of keyword. From what I can tell, neither JSON generators nor parsers are consistent in whether they preserve the order of key-value pairs on an object -- let alone if they even expose the order in the first place. Even the JSON RFC mentions this:

JSON parsing libraries have been observed to differ as to whether or
not they make the ordering of object members visible to calling
software.

If you need to keep things in order, I would recommend the use of an array. To my knowledge, most parsers/generators will preserve the order of a JSON array, which should support your use case. Alternatively, if they must be objects, add a field to your objects that denotes the order or priority that you would like things to be parsed in.

@git-n-pissed
Copy link
Author

I'd have to disagree with this sort of keyword. From what I can tell, neither JSON generators nor parsers are consistent in whether they preserve the order of key-value pairs on an object -- let alone if they even expose the order in the first place. Even the JSON RFC mentions this:

JSON parsing libraries have been observed to differ as to whether or
not they make the ordering of object members visible to calling
software.

This is why I brought up XSD and its solution to the same problem. C14N specifies that order doesn't matter:

Note that two documents may have differing canonical forms yet still be equivalent in a given context based on application-specific equivalence rules for which no generalized XML specification could account.

Yet the problem was prevalent enough that 4 years before C14N it had already been addressed by the XSD schema. JSON and XML are just different ways of representing arrays and objects, so the problems experienced by one will be experienced by the other.

If you need to keep things in order, I would recommend the use of an array. To my knowledge, most parsers/generators will preserve the order of a JSON array, which should support your use case. Alternatively, if they must be objects, add a field to your objects that denotes the order or priority that you would like things to be parsed in.

That may sound easy, but when a structure effects many users or has been engrained into a large organization for decades, it just isn't an option :(

@gregsdennis
Copy link
Member

Please add any more comments to the issues I linked. This is a duplicate, so I'm closing it.

@awwright
Copy link
Member

awwright commented Feb 2, 2021

There's one difference with json-schema-org/json-schema-vocabularies#7 — this issue seems to validate the order, instead of being for UI/metadata purposes.

A validation keyword enforcing the order is not going to happen. Since JSON defines objects as unordered, the ordering of keywords is a linting issue. See https://github.com/json-schema-org/json-schema-spec/wiki/Scope-of-JSON-Schema-Validation for more information.

One such instance is when JSON is used to define the memory structure of an embedded device.

This is a good use case for vocab#7 and I would strongly encourage you add this point over there.

@Relequestual
Copy link
Member

JSON Schema works in the JSON space, with the context of JSON. It does not work in the context of files which DO have objects with keys in some sort of order.

A validation keyword enforcing the order is not going to happen.

It simply cannot. JSON has no notion of object order, and so neither libraries which validate JSON.
If you want to validate the order of text in a file which happens to be JSON, you'll have to use another tool or write your own extension which only works in the context of a file, parsing the JSON yourself.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants