Read Only Validator
Current implementation of read only validator requires us to pass a JSON Array of properties that are supposed to be read only. Two observations as per standard spec.
-
I couldn't find proper documentation describing readOnly as an array in the standard JSON Schema Spec. Please correct me if i am wrong here.
-
Current Implementation of the ReadOnly validator adds an error message for the readonly properties without providing the complete path to the properties that caused the error.
Please let me know your thoughts.
Here is the spec section about readOnly and it should be a boolean value. We also implement it as an array just like required so that you can provide a list of fields that is readOnly. It was in the discussion but I don't know why it wasn't included in the spec.
https://json-schema.org/draft/2019-09/json-schema-validation.html#rfc.section.9.4
Thank you @stevehu. For obeservation 2 there seems to be a problem with error message. The error message doesn't capture the exact property of the JSON causing the issue. I will raise a PR for same.
Yes. The error message is a mess and I feel grateful that the spec team finally considers addressing it in a standard way. I have closed all others but keep your issue open as you have linked to the exact section in the spec. Let's work together to gradually get it in the right direction.
Hey guys.
Are there any expectations for this to be fixed?
I currently work with two schemas, one for input (with fewer fields) and another more complete one with readOnly fields for output endpoints.
If this was working I could keep just one schema for both cases.
Hi @stevehu,
I need to use ReadOnlyValidator without arrays because OAS UIs are broken with it. I will create a PR but first I want to clarify some aspects...
In the code, there is a reference to the path "#original." to compare with the current value, but I can't find this term in the JSON schema specification or elsewhere in this component.
Although in the specification there is this question about comparing values to verify if there is a change, in practice I cannot imagine how this use would be. Perhaps in a PUT service where we would have to query the database and call the validator in a different way than today to pass the schema and 2 more objects.
In a simple way, as we run the validations programmatically, I think it could conceptually look like this:
1- When instantiating the JsonSchema, inform whether we want to load it in writing or reading mode. In this case, I would load it as reading mode when validating GETs and as writing mode when validating POSTs and PUTs, for example.
2- When validating (ReadOnlyValidator.java), in addition to correcting to use the readOnly property in each field instead of an array, ALWAYS return error if it has been configured as writing and the readOnly field was true.
What do you think? I await comments to start work.
I am not familiar with your use case, but what you have described makes sense. Could you please submit a PR with test cases? Other developers will easily understand what you are proposing when seeing the code. Thanks.
This was resolved by #681