hapijs / hapijs/joi

Allow custom value type for ValidationResult

Open
#2,677 2 comments 7 reactions 0 assignees View on GitHub
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): no
* *is this issue affecting a production system?* (yes/no): no

#### Context

* *node version*: 15.3.0
* *module version*: 17.4.2
* *environment* (e.g. node, browser, native): node
* *used with* (e.g. hapi application, another framework, standalone, ...): Next.JS
* *any other relevant information*: pretty much just using it to validate incoming API data

#### What problem are you trying to solve?

I'm currently writing a project in Typescript and am using Joi to validate incoming API data. What I would like to be able to do is cast the data to the correct type when I use the `validate` function. It just seems clunky to me to have to validate some data then cast it to the correct type afterwards.

For example:

```ts
type RequestData = {
message: string
}

const schema = Joi.object({
message: Joi.string(),
})

const handler = (req, res) => {
const { value } = schema.validate(req.body)
const realValue: RequestData = value

// ... use realValue
}
```

I think it would be much nicer to just be able to do the following:

```ts
const handler = (req, res) => {
const { value } = schema.validate(req.body)

// ... use value
}
```

#### Do you have a new or modified API suggestion to solve the problem?

The Typescript definitions for `ValidationResult` and `validate` could be changed to include a generic type for the output. (Maybe even `ObjectSchema` could override `validate` to automatically use `TSchema`)

`ValidationResult` could be changed to:

```ts
interface ValidationResult {
error?: ValidationError;
warning?: ValidationError;
value: TValue;
}
```

Then `AnySchema` could be changed to:

```ts
interface AnySchema extends SchemaInternals {
// ...

validate(value: any, options?: ValidationOptions): ValidationResult;

// ...
}
```

All the different `Schema` interfaces could also be modified to return the correct type, however that might introduce increased burden in terms of maintaining.

Additionally, a separate interface could be created that only includes the `validate` function, and uses a generic type as the `TValue` type. From there, all the separate `Schema` interfaces could extend it, using the correct type, but again, that might come at the cost of increased burden.

All of this is a relatively simple change, and I would have gone straight to a pull request, but I wanted to make sure this is something that actually *should* be added.

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.