Can a schema parameter's 'required' attribute be overridden?
See original GitHub issueI have a common object defined in schemas
referenced in a couple of endpoints. However, the specific set of required fields differ between the references. For example, one endpoint requires properties foo
and bar
, while another endpoint requires properties foo
and qux
.
Is it possible to override the required
list when referencing a schema? Is there another fairly DRY way to accomplish this? (The only workaround I can think of is inlining the schema everywhere, which leads to duplication and possibility of inconsistencies.)
If it’s not possible, could this be implemented?
Issue Analytics
- State:
- Created 5 years ago
- Reactions:6
- Comments:6 (2 by maintainers)
Top Results From Across the Web
Is it possible to override the "required" attribute of a referenced ...
As of OpenAPI 3.1, you can only override the description of a referenced parameter, but not other attributes ( required , name and...
Read more >Describing Parameters - Swagger
To describe a parameter, you specify its name , location ( in ), data type (defined by either schema or content ) and...
Read more >Overriding Metadata Generated by a Transformation
In the Schema section, you can override the metadata attributes for one object only. The Schema section in this sample sfdcDigest transformation contains ......
Read more >RequiredAttribute Class (System.ComponentModel ...
The following example uses the RequiredAttribute attribute to override the database schema rule that allows a data field to be empty.
Read more >Product data specification - Google Merchant Center Help
You can use these attributes to organize your advertising campaigns in Google Ads and to override Google's automatic product categorization in specific ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Yes, since
foo
is required in both cases, have"required": ["foo"]
in your re-usable schema undercomponents
, and then useallOf
to reference the re-usable schema and also add"required": ["bar"]
or"required": ["qux"]
or whatever.https://tools.ietf.org/html/draft-wright-json-schema-validation-01#section-6.26
@emvau the schemas in an
allOf
are each evaluated separately, and must all pass. There is no “overriding” here, you just add constraints in each branch of theallOf
. In practical terms forrequired
, this just means that you can’t useallOf
to remove a requirement.As for nesting, the 2nd schema under
allOf
in your example reads as:"vehicleOffers"
property which is an array, then…"vehicles"
property which is itself an array, then…"vehicles"
array is an object, then it must have a property named"options"
The reason for all of the ifs is that
properties
anditems
do not force the instance to be of type object or array, respectively. They apply only if the instance is of the appropriate type. To force the types throughout, you need to settype
(or maybe it is already set in theoffer.json
schema, in which case you do not need to re-set it in the other schema. This is type-related behavior covered in the JSON Schema specification.Note that requiring
"options"
at the deepest level does not cause any higher level properties to be required. You would need to setrequired
at each level to do that (and presumably"minItems": 1
on arrays if you want to ensure there is always at least one vehicle in a vehicle offer).