Required and Default
See original GitHub issueHi all, i need a clarification about using Required and Default properties.
Consider this definition took from OpenAPI specs “Models with Polymorphism Support” example:
type: object
properties:
huntingSkill:
type: string
description: The measured skill for hunting
default: lazy
enum:
- clueless
- lazy
- adventurous
- aggressive
required:
- huntingSkill
If huntingSkill
property required, i’m expected to provide a value or get a validation error, so default
property is useless. Otherwise, if is supposed to use default value if not provided, it’s required
property to be useless.
Can you help clarify?
Thank you!
Issue Analytics
- State:
- Created 5 years ago
- Comments:12 (7 by maintainers)
Top Results From Across the Web
Mongoose model required and with default - Stack Overflow
I created mongoose model with a field named "phoneNumber": ... phoneNumber: { type: 'String', required: true, default ...
Read more >Defining required fields and default values - Documentation
Required fields and default field values are project-specific. Required fields must be populated for all instances of actions. For example, the Found by...
Read more >Mongoose v6.8.1: Defaults
Your schemas can define default values for certain paths. ... Note: Mongoose only applies a default if the value of the path is...
Read more >Required Field and Default Value function in Database ... - IBM
When a user changes a database column from not required to required, and that column has existing instances of null data, Database Configuration ......
Read more >Ruby Required, Default and Optional Parameters - Medium
Default Parameters; Optional Parameters. Required parameters. These are parameters that have to be provided with an argument or ...
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
@hkosova
default
andrequired
together is definitely odd. I was pointing out that potential use case because it’s come up in the JSON Schema spec repo. I doubt it’s common.Modern JSON Schema has both
examples
(note the plural) anddefault
. They have distinct purposes, although implementations MAY treat thedefault
as an additional example (or as the only example ifexamples
is not present).examples
is “here are some things you can do, some of which may be unusual which is why they are being called out”default
is generally “here is the thing that happens if you don’t do anything”All annotation keywords (including
default
andexamples
) simply provide information to applications without mandating whether or how it must be used. This is why the initial UI form value use case is valid- it’s basically an indirect form of writing it into the instance.A quick wrap-up.
I have come to agree with the OpenAPI documentation that states that
required
anddefault
do not make sense together.required
states that the data submitted by the user must include the property, otherwise it is invalid. So for valid data, thedefault
value will never be used. If thedefault
value was to be applied prior to validation, well in that case the user was not really required to include the property in the first place.The main reason I wanted to use
required
anddefault
together was due to code generation (in my case, generating TypeScript types). Settingrequired
ensures that the property is a required property of the generated type.However, being required in use submitted data and being required in the generated type are two separate things.
Specifically, if the JSON schema includes a
default
value, then deserialized objects will always include a value for that property: either the user-submitted value or the default value.As such, I think the correct solution is to ensure that the generated type includes the property as a required property either if it is marked as
required
in the JSON Schema, or it has a default value.Basically, this has to be implemented in the code-generation library.
Having said that, it would be great if OpenAPI specifies how code-generation should work so that we can have compliant code-generation libraries.