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

fix value type on validation result #3022

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

mastery-benjamin-palko
Copy link

@mastery-benjamin-palko mastery-benjamin-palko commented Mar 7, 2024

The value property returned from .validate is always any, which forces casting to the expected type.

The value should always be known (TSchema) and the error property will be optional, check if error is falsey to determine if value has been properly validated.

Example:

const { value, error } = <schema>.validate(<obj>);

if (error) {
  console.error(error);
  return;
}

// do stuff...

Related to #2677

@Marsup
Copy link
Collaborator

Marsup commented Mar 8, 2024

The previous type was valid, if you have an error, you can have anything as a value.

@mastery-benjamin-palko
Copy link
Author

The previous type was valid, if you have an error, you can have anything as a value.

The issue is that value is always cast as an any, there is no situation where checking for error would return value to its TSchema type.

For example...

const result = <schema>.validate(<obj>);
// result.value is _any_
if (result.error === undefined) {
  // result.value is still _any_
}

Ideally, checking for error being undefined should return value to its proper type.

From VS Code...
image

@Marsup
Copy link
Collaborator

Marsup commented Mar 9, 2024

I think your schema is not typed, maybe you thought that joi auto-inferred the type but that's not currently the case, hopefully soon.

@mastery-benjamin-palko
Copy link
Author

I think your schema is not typed, maybe you thought that joi auto-inferred the type but that's not currently the case, hopefully soon.

My schema is typed.

image

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 this pull request may close these issues.

2 participants