-
-
Notifications
You must be signed in to change notification settings - Fork 296
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
v6 validation: propertyNames #70
Comments
I'm a bit confused about this proposal. I don't see the |
@erosb it's all there. The second and the third examples have this keyword. It's value is the schema E.g. in this object: {
"192.168.0.1": { "interval": "15s" },
"8.8.8.8": { "interval": "86400s" }
}
I hope it makes sense. |
Ohh sorry I'm blind. |
I had a property that did exactly this in one of my software applications, for the purpose of UI generation. Any property that deals with strings would be acceptable here... "minLength", "maxLength", and maybe even "links" and "readOnly" and similar hyper-schema things. |
OK so "propertyNames" validates names, but does not impose any schema on the names that validate against propertyNames? The schemas all still need to be supplied in one of "properties", "patternProperties", or "additionalProperties"? What if I want entries keyed by IPv4, IPv6, and hostname to have slightly different schemas? |
+1 |
This is not quite correct, in that there is no order to the validation against all of the matching schemas. If a property name is both present in |
@handrews I don't think it's right at all. How you can collect string and object in the allOf? Makes no logical sense. Plus that's not what happening anyway, the validators will be happy if I put the object instead of string. So it's ONLY what pattern property defines. |
@dmikov The only point I was tying to make was that the validation is order-independent, just like with an {
"type": "object",
"properties": {
"foo": {"type": "number"},
"fee": {"type": "string", "pattern": "^z"},
},
"patternProperties": {
"^f": {"type": "string", "pattern": "bar$"}
}
}```
Then if the instance has a property `foo` then validation of foo's value will always fail, because this is _effectively equivalent to_ validating foo's value against the following:
```JSON
{
"allOf": [
{"type": "number"},
{"type": "string", "pattern": "bar$"}
]
} which is an impossible schema because no value can be both a number and a string. On the other hand, if the instance has a property "fee", then it would effectively be the same as validating fee's value against: {
"allOf": [
{"type": "string", "pattern": "^z"},
{"type": "string", "pattern": "bar$"},
]
} So an instance of Does that make more sense? |
I am aware of how it currently acts (wasn't aware that it's both, but the fact that it didn't use static definition if regex matched was pretty obvious), that's why I put it here as another reason to have propertyNames. Current implementation of dictionary of objects in json schema is just to prone to errors and awkward now. That's what I was saying. |
@dmikov properties/patternProperties behaviour is another issue that you've submitted.
I thought about it and it's very easy to construct the keyword that would do it. I don't think we should cater for this use case for two reasons:
Mixing different things is fine when the keys are static, your data is essentially like a db "record". If you treat your object as a map where both keys and values are data it's generally a bad idea to mix something else in the same object. For the same reason, I am not that concerned about properties/patternProperties issue - I don't remember ever using multiple patterns in the same object, leave alone mixing patternProperties with properties. A much better design is to have a separate object with a single pattern (or schema in case of propertyNames) for the keys. But each to its own I guess... |
@dmikov The question is why would you have such data structure? It seems a better design to have this variable key as a value for another static property - i.e. have 7 static properties instead of 5 static properties and 1 pattern property. It's easier to both validate and to process. |
This proposal has my support =] |
@epoberezkin while I don't think JSON Schema should be all that opinionated about data design, at this point I'm on board with this proposal as it stands. I've got pull request #111 outstanding that impacts how all of the object validation keywords are described so you might want to wait until that is resolved before doing a pull request for this. |
#111 has been merged |
If we want to support more complex rules about schemas for keys leading to different data validation schemas we can extend properties keyword to accept array like: {
"properties": [
{ "name": { "$ref": "schema1" }, "value": { "$ref": "schema2" } }
]
} It would mean that if the property name is valid according to the schema1, the value of the property must be valid according to schema2. But I won't push it :) |
I've had a few people on irc the last week or so ask how to do this exact thing. I directed them to pattern properties. Looks like consensus is we should do this. Awaiting RP. |
@Relequestual do what :)? propertyNames or extend properties as above? or both? |
@epoberezkin not speaking for @Relequestual but since your extension of properties was a last-minute comment and you said "but I won't push it", I'm going to go out on a limb and say he meant "propertyNames". That's the subject of this issue, anyway. If you want to pursue extending "properties", I suggest a separate issue to keep things clear. |
@epoberezkin @Relequestual et al., Does #172 seem to have the features you're looking for? |
@epoberezkin it's been over a month, I'm assuming you would have said something by now if you thought this needed anything more than PR #172. Obviously please re-open if we missed something. |
@handrews thank you. I have some ideas :) but it's another issue and after draft 6 is published. This is good as is indeed. |
Definition
The value of this keyword is a JSON-schema. The keyword only applies to objects. If the instance is not an object, validation succeeds. For the instance to be valid all property names of the object should be valid according to this schema. Possible variant: only apply it to property names not explicitly mentioned in
properties
keyword - there are pros and cons for it.Motivation
Generally the current spec is more suited for the data stored in values with static keys. The only exception to that is
patternProperties
.There are two disadvantages of patternProperties:
propertyNames
addresses both issues. So instead of having:you can have:
or even
You can see how using
propertyNames
allows to have the schema for property names that is:We use this keyword a lot, it is defined as a custom keyword for Ajv. It seems to be so useful and simple that maybe it's worth considering adding it to the spec.
The text was updated successfully, but these errors were encountered: