-
-
Notifications
You must be signed in to change notification settings - Fork 311
Should "readOnly" (and "writeOnly") be an array like "required"? #364
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
I just thought of a pretty compelling reason to move Validation schemas fundamentally describe a type, in the form of an acceptable set of values. This is part of why Making |
@geemus this would affect PRMD if you were to move it to (or add support for) draft-07. |
This exact thing occurred to me recently as well while writing hyper-schemas. I think making However, there is one use case I would loose. I sometimes use |
This use case occurred to me, but I think in most cases it is better handled at the link level (including "self" links) with Recommending a hyper-schema approach while we're moving I've also considered the unusual but possible case of making some but not all array elements read-only. This would mostly be fore the tuple form of I still like this idea, but there are definitely some odd corner cases to smooth out. |
@handrews sounds reasonable, thanks for the heads up. |
See also PR #424 which delves more into the nature of annotation keywords. It might help us decide which way to go on this (although I'm still waffling back and forth myself for |
I don't think I have the energy left to consider this in draft-07, and assuming the concept of applicability in #424 is accepted, I don't think it's as much of a concern. Bumping it out to the "future" milestone and lowering the priority unless someone wants to champion this. |
Having spent a lot more time thinking about annotations and how they work over the last several months, I've decided against changing these, so I'm going to close this (feel free to comment if you have a compelling reason that I should reconsider and it can of course be re-opened). Reasons to drop this idea:
So I think it's best to drop this. |
I've stumbled upon a rather practical issue. We are trying to do:
where
Do you mean this?
Thanks! |
Jiří Brabec a écrit :
I've stumbled upon a rather practical issue. We are trying to do:
|{ $ref: '#/definitions/Foo', readOnly: true } |
where |readOnly| will get stripped when the pointer is resolved.
This is not specific to "readOnly", this is because extra properties in
an object schema containing a "$ref" are ignored. See
https://tools.ietf.org/html/draft-handrews-json-schema-00#section-8
|
What would be the recommended way to handle readOnly with allOf? We would like to make status field in the below given data readOnly for POST requests. Right now, we're creating post specific schemas like given below. A readOnly array can handle it in a better way. Example:
|
@jugaadi I don't see
|
@karenetheridge Sorry for not being more clear. The example is how Im solving it now i.e by modeling request and response
|
Well technically, you could do this:
The problem with moving |
Or... how would we differentiate between an array being read-only vs the items within it being read-only? |
(I feel like I commented on this elsewhere but I can't find it...) The array form of "required" was only necessary because it needs to be accessible to the validator when said property does not exist. For "readOnly", the only time it's going to be accessed is when the property exists: If a user creates the property while preparing it for submission, they would immediately be able to see that it's read-only (from the generated annotation). I would think this ought to be sufficient: // ...
"user-post": {
"$ref": "#/user",
"type": "object",
"properties": {
"status": {"readOnly": true},
}
} But maybe this is impractical, or there's another use for "readOnly" where this logic isn't applicable. Is this the case? |
C# (and likely other languages) have read-only types built in. https://devblogs.microsoft.com/premier-developer/the-in-modifier-and-the-readonly-structs-in-c/ https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/record If you're using schema to model types, this can be important to have outside of properties. |
What about keywords like "type"? Does the same logic apply? If you're using this to model types, then what you're doing is a static analysis of the entire schema, so you can still see "X property is going to be a readonly string, max 20 characters". |
@awwright In the above case, is it safe to assume that the rest of the constraints/vocabularies that are applicable for status field would be inherited from |
@jugaadi Yes, all the keywords will be used to validate the property. I suggest, if in doubt, run a few experiments to get a feel for how it works. |
Thanks @awwright @gregsdennis @karenetheridge Skipping the fields instead of depending on annotations seem to be a better solution for my problem since it does not depend on the owning authority's implementation choice to ignore or reject. Any choice would have a bearing on our API versioning strategy. |
In the discussions for "deprecated", @awwright brought up that rather than make it a boolean in subschemas, it probably makes more sense to follow the pattern of
"required"
and make it an array or object alongside of"properties"
. Similar to #117 ("patternRequired"
) we would need an analogous"patternReadOnly"
(and"additionalReadOnly"
?)Despite the likely need to add separate pattern/additional support, it feels like we should have a consistent design pattern of either specifying these things at the object level, or at the subschema level. Having worked with object level
"required"
for several years, I feel that it is the better approach.The text was updated successfully, but these errors were encountered: