hapijs / hapijs/joi

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

Open
#2,963 1 comment 0 reactions 0 assignees View on GitHub
support
Dominant language
JavaScript
Stars
21.2k
Forks
1.5k
Avg merge
4h 57m
Merged PRs (30d)
14

Description

#### 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](https://github.com/hapijs/joi/assets/3169264/a73773b0-3a20-4e2d-ac4f-b1ec71688f7f)

or

```ts
type ValidationResult = {
error: undefined;
warning?: ValidationError;
value: TSchema;
} | {
error: ValidationError;
warning?: ValidationError;
value: undefined;
}
```

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

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

```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.