-
Notifications
You must be signed in to change notification settings - Fork 58
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
Don't validate readOnly properties when writing, and writeOnly when reading #104
Comments
From what I understand, you have 2-in-1 schema, and based on some context there should be a "split". Now, the problem is The below example shows how the {
"type": "object",
"properties": {
"locked": {"type": "boolean"},
"name": {"type": "string"}
},
"required": ["locked"],
"if": {
"properties": {
"locked": {"const": true}
}
},
"then" {
"properties": {
"name": {"readOnly": true}
}
}
} readOnly and writeOnly are just some meta keywords, they are useful to generate documentation, or a simple form-builder might use that info to mark a field as read only. |
Thanks for the thorough explanation @sorinsarca! How would you suggest we tackle this issue if it cannot be fixed in the validator? By using 2 duplicate schemas that are 99% the same but with different Or by using 1 schema without any required fields and an The only solution I see without Am I overlooking another solution? |
Just to be clear: if in 99% of cases we have to detect the |
I think that would solve most cases yes. It's also an issue that people are experiencing with Spectral stoplightio/spectral#1274 Someone linked to the Swagger (OpenAPI) docs there, which state:
Of course that is documentation for Swagger/OpenAPI and not JSON schema. But looking at the OpenAPI 3.0 docs, it says:
https://swagger.io/docs/specification/data-models/data-types/ Those docs are probably outdated since I'm no expert though, just trying to find as much relevant info about this as possible. So feel free to correct me if I overlooked or misinterpreted something here. |
Given a schema like:
And data like this (for example on an incoming HTTP request, so when "writing"):
Opis\JsonSchema\Validator::validate()
will return a validation result with an error like:Is it possible to set some kind of "read" or "write" context on the validator, to ignore either writeOnly or readOnly properties in some situations?
We have two reasons to mark some read-only properties as required in our schemas:
Additionally even if we don't make any read-only property required, we also just don't want to validate their type and value if they happen to be included in a write context. Because an API client might fetch a resource, alter some (write-able) properties, and send the data back including the read-only properties which might be stale by the time that they send their data. Or they might always include a read-only property in their request by accident. Either way we ignore those properties in write contexts, so we also want to ignore them in the validation. Which seems to be a valid implementation according to:
https://json-schema.org/draft/2020-12/json-schema-validation.html#rfc.section.9.4
The text was updated successfully, but these errors were encountered: