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

Return value of type ValidationResult contains both value and error, which is inconsistent #2963

Open
StephanMeijer opened this issue Jul 5, 2023 · 1 comment
Labels
support Questions, discussions, and general support

Comments

@StephanMeijer
Copy link
Contributor

StephanMeijer commented Jul 5, 2023

Support plan

  • is this issue currently blocking your project? (yes/no): yes
  • is this issue affecting a production system? (yes/no): yes

Context

  • node version: v18.7.0
  • ts-node version: v10.9.1
  • module version with issue: joi
  • last module version without issue: unsure
  • environment (e.g. node, browser, native): node (ts-node)
  • used with (e.g. hapi application, another framework, standalone, ...): standalone / hapi application
  • any other relevant information:

Types defined as following:
image

or

    type ValidationResult<TSchema = any> = {
        error: undefined;
        warning?: ValidationError;
        value: TSchema;
    } | {
        error: ValidationError;
        warning?: ValidationError;
        value: undefined;
    }

What are you trying to achieve or the steps to reproduce?

import Joi from 'joi';

const schema = Joi.string()
        .required()
        .valid('only allowed value');

const result = schema.validate("a non-allowed value");

console.log(result);

What was the result you got?

{
  value: 'a non-allowed value',
  error: [Error [ValidationError]: "value" must be [only allowed value]] {
    _original: 'a non-allowed value',
    details: [ [Object] ]
  }
}

What result did you expect?

{
  value: undefined,
  error: [Error [ValidationError]: "value" must be [only allowed value]] {
    _original: 'a non-allowed value',
    details: [ [Object] ]
  }
}

@StephanMeijer StephanMeijer added the support Questions, discussions, and general support label Jul 5, 2023
@StephanMeijer StephanMeijer changed the title return value of type ValidationResult contains both value and error, which is inconsistent Return value of type ValidationResult contains both value and error, which is inconsistent Jul 5, 2023
StephanMeijer added a commit to StephanMeijer/joi that referenced this issue Jul 5, 2023
StephanMeijer added a commit to StephanMeijer/joi that referenced this issue Jul 5, 2023
StephanMeijer added a commit to StephanMeijer/joi that referenced this issue Jul 5, 2023
StephanMeijer added a commit to StephanMeijer/joi that referenced this issue Jul 5, 2023
Marsup added a commit that referenced this issue Aug 27, 2023
#2963 make return value of validate match type definitions
@SimonSchick
Copy link

FYI This seems to have the side effect that when destructuring the validation result value is always any, is this intentional?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
support Questions, discussions, and general support
Projects
None yet
Development

No branches or pull requests

2 participants