-
-
Notifications
You must be signed in to change notification settings - Fork 309
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
Comments
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. |
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:
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. |
This is why I brought up XSD and its solution to the same problem. C14N specifies that order doesn't matter:
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.
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 :( |
Please add any more comments to the issues I linked. This is a duplicate, so I'm closing it. |
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.
This is a good use case for vocab#7 and I would strongly encourage you add this point over there. |
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.
It simply cannot. JSON has no notion of object order, and so neither libraries which validate JSON. |
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
The text was updated successfully, but these errors were encountered: