Skip to content

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

Open
@StephanMeijer

Description

@StephanMeijer

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] ]
  }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    supportQuestions, discussions, and general support

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions