Comments (4)
Thank you for your explanation @sinclairzx81
from typebox.
from typebox.
@Meriyemelhajoui Hi,
TypeBox currently yields only the top most error for Union-like structures (which includes Enum) but doesn't yield any internal errors of that Union. The reason for this is it tries to prevent excessive error generation for values with non-matching sub variants as these tend to produce excessively large (and sometimes duplicated) error results. Consider the following.
const A = Type.Object({ x: Type.Number(), y: Type.Number() })
const B = Type.Object({ x: Type.String(), y: Type.String() })
const C = Type.Object({ x: Type.Boolean(), y: Type.Boolean() })
const U = Type.Union([A, B, C])
// ...
const E = [...Value.Errors(U, { })] // no properties
// Current (top level only)
//
// [
// { message: 'Expected union value', path: '' }
// ]
//
// -----
//
// Yielded sub variant errors (not supported)
// [
// { message: 'Required property', path: '/x' }, // For variant A
// { message: 'Required property', path: '/y' },
// { message: 'Required property', path: '/z' },
//
// { message: 'Required property', path: '/x' }, // For variant B (duplicated)
// { message: 'Required property', path: '/y' },
// { message: 'Required property', path: '/z' },
//
// { message: 'Required property', path: '/x' }, // For variant C (duplicated)
// { message: 'Required property', path: '/y' },
// { message: 'Required property', path: '/z' },
//
// ...
// ]
Unfortunately, I don't know of a better solution to this problem that is both performant and meaningful in terms of errors presented. The generation of the top level Union error isn't ideal, but it is the simplest option I've found thus far. I'm open to community thoughts on a better error generation strategy though.
Better Errors with SetErrorFunction
While it's not possible to yield internal errors for Union, you can provide custom error messages which may be a bit more descriptive of the actual error. You can use the SetErrorFunction and DefaultErrorFunction to achieve this, documentation on this can be found at the link below.
https://github.com/sinclairzx81/typebox#error-function
import { SetErrorFunction, DefaultErrorFunction } from '@sinclair/typebox/errors'
import { Type } from '@sinclair/typebox'
import { Value } from '@sinclair/typebox/value'
// Overrides the default error function to intercept schemas with
// a errorMessage property. This enable you to assign specific
// errors to types and have them generate in error messages.
SetErrorFunction((param) => ('errorMessage' in param.schema)
? param.schema.errorMessage
: DefaultErrorFunction(param)
)
const Color = Type.Union([
Type.Literal('Red'),
Type.Literal('Blue'),
Type.Literal('Green'),
], {
errorMessage: "Expected either 'Red', 'Blue' or 'Green'"
})
// ...
const R = [...Value.Errors(Color, 'Orange')]
console.log(R) // [
// {
// type: 62,
// schema: {
// errorMessage: "Expected either 'Red', 'Blue' or 'Green'",
// anyOf: [Array],
// },
// path: '',
// value: 'Orange',
// message: "Expected either 'Red', 'Blue' or 'Green'"
// }
// ]
Hope this provides a bit of insight into the current Union/Enum error generation (as well as a viable workaround with the SetErrorFunction). I do think better error generation may be possible with the introduction of Tagged/Discriminated Unions (which TypeBox doesn't support natively, but may provide facilities for in later revisions). For now though, the above is the best it can provide under the current setup.
Hope this helps
S
from typebox.
@Meriyemelhajoui Hiya,
Might close off this issue as generating union variant errors is generally not supported (at least for now). As per example though, the SetErrorFunction will allow you to generate type specific error messages if you need a bit more detail than just the generic union error TypeBox currently generates.
If you have any questions on the above or follow up suggestions on how to improve the current TypeBox union / enum generated errors, happy to discuss this further on this thread. Will close for now though.
Cheers!
S
from typebox.
Related Issues (20)
- Max call stack size exceeded HOT 1
- Value.Convert() doesn't work with nullable record HOT 1
- Variadic Any Typed function parameters HOT 2
- How to do a cross field validation? HOT 3
- Undefined is incorrectly handled in Diff HOT 3
- QoL helper: `TypeCheck.Ensure` HOT 3
- Usage of `Type.Composite` with `Type.TemplateLiteral` HOT 3
- `Check` doesn't validate strings with enums HOT 4
- `Type.Omit` mixed with `Type.Union` is not preserving the `additionalProperties` attribute HOT 2
- Advice to compose types HOT 3
- Access to schema properties for non TObject schemas HOT 5
- Type.Optional with Type.Transform unexpected behaviour HOT 1
- ts(7056) when you try to export a big union type HOT 1
- `Value.Default` does not traverse object schema HOT 2
- Unable to use a union of literal as record key type after upgrading version HOT 2
- Unknown format 'email' after update HOT 3
- Uncaught Error: Cannot access 'Object' before initialization HOT 3
- Support for conditional optional based on other value HOT 2
- Type.Mapped does not handle optional properties the same as Typescript HOT 1
- Add changelog or create a new release for each tag
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from typebox.