Not sure why context would be optional in validation results
- 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*: v12.18.0
* *module version*: 17.4.0
* *environment* (e.g. node, browser, native): node
* *used with* (e.g. hapi application, another framework, standalone, ...): hapi application
* *any other relevant information*: using with Typescript 4.1.3
#### How can we help?
In the ValidationErrorItem type, the context is marked as optional.
In the Context interface, most of the elements are also marked as optional.
I don't quite understand why; if Joi's failed validation for an item there would be a reason, so it would show up in the context. Why is it marked optional...?
I'm dealing with it at the moment with ! to tell tsc it will be defined regardless of the type definition, but I'd like to understand if that's going to blow up on me at some point. :-)
```ts
interface ValidationErrorItem {
message: string;
path: Array;
type: string;
context?: Context;
}
interface Context {
[key: string]: any;
key?: string;
label?: string;
value?: any;
}
```
```ts
for (const detail of err.details) {
errors[detail.context.key] = detail.message;
}
```
```
src/sites.ts:45:16 - error TS2532: Object is possibly 'undefined'.
45 errors[detail.context.key] = detail.message;
~~~~~~~~~~~~~~
src/sites.ts:45:16 - error TS2538: Type 'undefined' cannot be used as an index type.
45 errors[detail.context.key] = detail.message;
~~~~~~~~~~~~~~~~~~
[16:16:33] Found 2 errors. Watching for file changes.
```
Thanks
Paul
Contributor guide
Assessment
This issue has not been assessed yet.