Replies: 1 comment 1 reply
-
|
@EvHaus Hi, TypeBox doesn't actually have a concept of This said, TypeBox doesn't prohibit end users from defining an The reference issue you linked was from a while back, here is an updated version. import { SetErrorFunction, DefaultErrorFunction } from '@sinclair/typebox/errors'
import { Value } from '@sinclair/typebox/value'
import { Type } from '@sinclair/typebox'
SetErrorFunction((parameter) => parameter.schema.errorMessage ?? DefaultErrorFunction(parameter))
const T = Type.Union([
Type.Literal('A'),
Type.Literal('B')
], {
errorMessage: 'Expected either A or B'
})
console.log(Value.Errors(T, 'C').First()) // Expected either A or BHope this helps |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
It would be nice if
Type.Union()supported theerrorMessageoption like other types. This would allow for something more user-friendly, like:Instead of the current generic "Expected union value" message that is returned.
At the moment I'm working around this with a custom
SetErrorFunctiondefinition.I tried using the
Type.Unsafesolution recommended here but it didn't work for me, and wouldn'tType.Unsafealso cause the type to beunknown?Beta Was this translation helpful? Give feedback.
All reactions