question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

allow both readOnly and required for a given property

See original GitHub issue

Since it landed in 34793ee1 (Added readOnly to Schema, data type clarifications, 2014-09-10), the readOnly property has the following semantics:

This means that it MAY be sent as part of a response but MUST NOT be sent as part of the request. Properties marked as readOnly being true SHOULD NOT be in the required list of the defined schema.

It seems like there’s an unambiguous interpretation for properties with both settings enabled:

This means that it MAY be sent as part of a response but MUST NOT be sent as part of the request. If a property is marked as both readOnly and true, it MUST be sent as part of a response.

That’s useful for a number of my own properties that report aggregate information (e.g. the amount of product in stock is always returned by requests but never set by requests to the product endpoints. Users update the stock count by updating the stock endpoints). It would also be useful for things like object IDs, which are currently complicated.

The downside to this interpretation would be that you’d need two separate schemas (one for requests and another for responses) if you wanted to validate with a non-Swagger-aware JSON Schema validator. Since it would be easy to generate the basic JSON Schema schemas, I don’t think that’s enough of a problem to hold this up. Folks who feel like it is would certainly be free to avoid using both readOnly and required for a given property 😉.

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Reactions:3
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

6reactions
ePaulcommented, Aug 3, 2016

I normally view the required property as more relevant to the request rather than the response.

This means an API consumer can’t be sure that what he gets from the server actually contains the required attributes?

1reaction
webroncommented, Feb 22, 2017

This has been implemented in #894. Note the wording in the spec.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using required and readonly attributes together - Stack Overflow
I'm creating an HTML5 web form. One of the inputs is for geolocation and it is filled by clicking on a button (it...
Read more >
HTML attribute: readonly - HTML: HyperText Markup Language
The Boolean readonly attribute, when present, makes the element not mutable, meaning the user can not edit the control.
Read more >
Workaround for having attributes "readonly" and "required" on ...
Workaround for having attributes "readonly" and "required" on same input field while keeping form validation. Useful if you have a JS-Callback (eg.
Read more >
readonly keyword - C# Reference - Microsoft Learn
The readonly keyword is a modifier that can be used in four contexts: ... returns a reference and writes aren't allowed to that...
Read more >
How to add readonly attribute to an input tag in JavaScript
Example 1: In this example the read-only attribute of form input text field is enabled by accessing the property.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found