-
-
Notifications
You must be signed in to change notification settings - Fork 10
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
Properties order #7
Comments
Reopening, as this was not being tracked anywhere else. |
Correct me if I'm wrong, but since the schema is defined with (Personally, I would do |
Only slightly related to the current subject, but can someone recommend a tool, preferably js or python that can be used to sort schema definition files? As they grow fast and they are edited by numerous people I find quite hard to keep them in a relative normalized way. A JSON sorted would be really welcomed here, especially if it can be configured to sort some properties on top of others, like So far I found a prettier plugin that does basic sorting at https://www.npmjs.com/package/prettier-plugin-sort-json -- sadly title and description end-up at the weird positions. |
@ssbarnea It's typical these days for properties to be listed by insert order (including in JSON.stringify), maybe just recreate the object in your preferred sort order. Does that make sense? |
@awwright The idea is to declare the sort-order at schema level, so validators, editors, reformatters could make use of it. I kinda like the simplicity of If inheriting from parent is not possible we should at least allow to configure implicit value at document/schema level, for the same reasons (to avoid repetition). Does anyone know some ordering that could not be achieved with the proposed format? Or if they know another format which would make easier to be consumed by those that would implement it? |
@ssbarnea are you looking for the schema to validate that the properties are in the right order? That's not something that can be guaranteed, especially interoperably. The parsers in some languages aren't even deterministic in property order from reading the same file multiple times. Secondly, we don't have an idea of keyword inheritence in the way you're describing (flowing down from the parent). However, subschemas can be extracted into a |
In addition to or in lieu of a "properties": [
["bar", { "type": "boolean" }],
["foo", { "type": "string" }]
], A A "properties": [
["bar", { "type": "boolean" }],
["foo", { "type": "string" }]
],
"if": { /* condition schema */ },
"then": {
"properties": [
["baz", { "type": "number" }],
"bar",
"foo"
]
} A UI derived from the schema wouldn't show a The full list of properties could even not be required to be specified in the conditional schema. As long as one other property is defined for a point of reference then the insertion point can be determined. "properties": [
["bar", { "type": "boolean" }],
["foo", { "type": "string" }]
],
"if": { /* condition schema */ },
"then": {
"properties": [
["baz", { "type": "number" }],
"bar",
// foo is omitted
// baz clearly goes before bar
]
} There would need to be clear rules defined for merged schemas as to what order takes precedence over order defined elsewhere, etc. That goes for a |
I am personally against over-complicating that feature, shortly I find the simple list of strings being more than enough to enable tools to determine how to sort. Property type should not be among criterias, if someone wants to determine prefered sorting using this they can do it while listing the fields names. Keep in mind that this extension works like "prefered-sort-order", it does not act as a validation. Schema can still be valid regardless the order of the keys. The main use case is for editors and I suspect especially for those using YAML files. Here is two practical examples: package.json"propertiesOrder": [
"name",
"version",
".*",
"scripts"
] ansible tasksThat example is bit simplified as in reality I do expect that list to grow much longer, this being likely one of the most complex sorting logics. "propertiesOrder": [
"name", # always first!
".*",
"action",
"args",
"with_.*",
"loop",
"loop_.*",
"when",
"tags", # tags are usually last
"block", # ensure nothing goes after block as it often causes indentation bugs
] simple alphabetical sortingIf someone wants to just use alphabetical sorting: "propertiesOrder": [".*"] |
Yes, a An advantage here is that in the newer drafts, the default behavior for unrecognized keywords is to treat the as simple annotations, so you wouldn't need to write custom code to support it (if you can use an implementation that collects annotations and implements the new default behavior, anyway). @mfulton26 we try to avoid changing existing keywords without very good reason. On reason that has been considered "very good" is to split keywords with two different syntaxes for different behaviors, so changing In particular, since an assertion cannot validate field order due to the limitations of JSON's data model, a keyword that tries to impart ordering while also having assertion behavior ( |
thank you for the explanation @handrews; very helpful! my ideas around a I appreciate all the points raised here and I too find a |
It sounds like you intend this to be in support of tooling, such as a linter. If that's the case, I suggest that this be prepared in a separate vocabulary, not added to the core or validation spec. |
This is a tricky question, touching on the overall philosophy/purpose of JSON Schema.. I do think that describing properties order would fit into the current declared goal:
Not the validation aspect, but the annotation aspect, as @handrews comments above:
We're currently using We'll likely start using |
Defining this in a separate vocabulary does follow the philosophy/purpose of JSON Schema.
Then just use the annotation in your form generation. Of course, it'll likely need to be |
Oo this is good info, thanks! We'll use the |
And HTTP standard has moved away from x- prefix: https://datatracker.ietf.org/doc/html/rfc6648 |
For anyone who's interested in such a solution, I've found https://pypi.org/project/jschon-sort/ which sorts yaml/json according to a json schema. |
UI generation tools would benefit, I think, from having a way to specify the order of properties.
For simple cases, perhaps
properties
could be optionally defined as an array of objects where at a minimum each object has aname
property:Alternatively a new keyword could be introduced,
propertiesOrder
:This keyword could also support patterns:
Any properties not mentioned in a
propertiesOrder
array (and are not matched by any patterns if supported) would go after any ordered properties and would not have a deterministic order.The text was updated successfully, but these errors were encountered: