Skip to content
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

Optional fields with default values are not considered as required if treatDefaultedOptionalAsRequired is set to true #490

Open
tyio opened this issue Nov 20, 2024 · 1 comment

Comments

@tyio
Copy link

tyio commented Nov 20, 2024

Describe the bug
It appears that the current implementation of the treatDefaultedOptionalAsRequired option in src/parse (Line 70) does not align with the documented behavior.

To Reproduce
Steps to reproduce the behavior:

// for the schema 
export const Schema = Joi.object({
  foo: Joi.number()
    .integer()
    .positive()
    .default(60000)
    .optional(),
});

// and configuration options
await convertFromDirectory({
      treatDefaultedOptionalAsRequired: true,
      ...
});

Expected behavior
The treatDefaultedOptionalAsRequired option should mark fields as required.

export interface Schema {
  foo: 60000 | number;
}

Actual behavior
Fields are still marked as optional.

export interface Schema {
  foo?: 60000 | number;
}

Additional context
The implementation can be updated:

if (
  presence === 'required' ||
  (settings.treatDefaultedOptionalAsRequired && presence === 'optional' && value !== undefined)
) {
  required = true;
}
@mrjono1
Copy link
Owner

mrjono1 commented Dec 5, 2024

Hi
If you are able to raise a PR im happy to merge it

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

No branches or pull requests

2 participants