Skip to content

required properties are not respected when they are missing from properties #619

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

Closed
shuluster opened this issue May 28, 2021 · 2 comments · Fixed by #620
Closed

required properties are not respected when they are missing from properties #619

shuluster opened this issue May 28, 2021 · 2 comments · Fixed by #620

Comments

@shuluster
Copy link
Contributor

Example schema

type: object
properties: 
  email: 
    type: string
required: ["user"]

should generate a type that requires user to be present. Today it generates a type with only email being the optional argument.

This is part of spec and commonly used for when reusing an object and making optional fields required, for example

LogInReq:
  type: object
  allOf: 
    - $ref: '#/someCommonObject' 
    - type: object
       required: ["user", "password"]

LogInReq now requires user and password to be present while all other fields in someCommonObject to be their default setting.

Working on a PR.

This will change a lot of behavior for the library and curious about people's thoughts.

@drwpow
Copy link
Contributor

drwpow commented Jun 3, 2021

This is interesting. I can’t say I’ve used this before, but I don’t see a problem with adding this. Will check out the PR, thanks!

@felixschorer
Copy link

felixschorer commented Feb 24, 2022

This solution doesn't seem to work if exactOptionalPropertyTypes is disabled in the TS Config (it is disabled by default). When the option is enabled, it does work, but breaks when using mapped types.

See these two TS playgrounds.

This could be fixed by introducing an utility type Require<T, RequiredKeys extends PropertyKey>. The fix works with exactOptionalPropertyTypes disabled and enabled.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants