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 B Hope this helps |
Beta Was this translation helpful? Give feedback.
-
It would be nice if
Type.Union()
supported theerrorMessage
option 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
SetErrorFunction
definition.I tried using the
Type.Unsafe
solution recommended here but it didn't work for me, and wouldn'tType.Unsafe
also cause the type to beunknown
?Beta Was this translation helpful? Give feedback.
All reactions