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.

Required and Default

See original GitHub issue

Hi 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:closed
  • Created 5 years ago
  • Comments:12 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
handrewscommented, Apr 14, 2018

@hkosova default and required 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) and default. They have distinct purposes, although implementations MAY treat the default as an additional example (or as the only example if examples 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 and examples) 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.

0reactions
oralokancommented, Dec 13, 2022

A quick wrap-up.

I have come to agree with the OpenAPI documentation that states that required and default 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, the default value will never be used. If the default 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 and default together was due to code generation (in my case, generating TypeScript types). Setting required 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.

Read more comments on GitHub >

github_iconTop 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 >

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