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

possibly undefined value whereas .default is defined #17

Closed
nicgirault opened this issue Jun 14, 2019 · 10 comments
Closed

possibly undefined value whereas .default is defined #17

nicgirault opened this issue Jun 14, 2019 · 10 comments

Comments

@nicgirault
Copy link

Hello!

Thanks so much for this project.

I think I catch an unexpected behavior:

My object definition is:

Joi.object({
  page: Joi.number().default(0),
}).required()

Then when I extract type I get this type:

Map<{
    page?: number | undefined;
} & {}>

As far as I know, page can't be undefined after joi validation. Am I right?

@TCMiranda
Copy link
Owner

TCMiranda commented Jun 14, 2019 via email

@nicgirault
Copy link
Author

I don't understand why the extracted type would be:

{
  page: number
}

Why would we allow an empty object {}?

@TCMiranda
Copy link
Owner

TCMiranda commented Jun 15, 2019 via email

@nicgirault
Copy link
Author

And don't we want Joi.validate({ }, schema) to be accepted?

@TCMiranda
Copy link
Owner

TCMiranda commented Jun 17, 2019 via email

@mrozekma
Copy link

mrozekma commented Oct 2, 2019

I think the confusion here is around whether the extracted type is supposed to model the object going into Joi.validate() or the object that comes out when validation is successful. For example:

const schema = Joi.object({
  page: Joi.number().default(0),
}).required();
const { value } = schema.validate({});

The object being passed into validate can be empty, but the one that comes out can't -- in this case it would be { page: 0 }. I would expect the extracted type to model the validated object, so:

{
    page: number;
}

@NicolasDuran
Copy link

@TCMiranda I think @mrozekma is right.
I hope this issue can be addresses, as .default(xx) is a very common pattern

@TCMiranda
Copy link
Owner

Thanks everybody, I made it required.
Just published 15.0.2, could you see if it fits?

@mantljosh
Copy link

@TCMiranda did this ever land? I'm still seeing the following:

const enabledConfigSchema = Joi.object({ enabled: Joi.boolean().default(true) })
export type EnabledConfig = Joi.extractType<typeof enabledConfigSchema>
// enabled can still be undefined, even though it has a default

@pke
Copy link

pke commented Sep 25, 2022

@TCMiranda this does indeed still not work in 15.0.8. Was the patch ever released to npm? I can't find any tags on this repo nor releases.

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

6 participants