hapijs / joi

The most powerful data validation library for JS
Other
20.95k stars 1.51k forks source link

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

Open StephanMeijer opened 1 year ago

StephanMeijer commented 1 year ago

Support plan

Context

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] ]
  }
}
SimonSchick commented 7 months ago

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