Skip to content

Custom error message for a Currency RegExp #723

Answered by sinclairzx81
divmgl asked this question in Q&A
Discussion options

You must be logged in to vote

@divmgl Hi,

You can try the following which intercepts errors and allows you to return an errorMessage passed via schema options.

import { SetErrorFunction, DefaultErrorFunction } from '@sinclair/typebox/errors'
import { Type, Static } from '@sinclair/typebox'
import { Value } from '@sinclair/typebox/value'

// Intercepts all errors and return an "errorMessage" string if defined, otherwise default.
SetErrorFunction(param => {
  return typeof param.schema.errorMessage === 'string'
    ? param.schema.errorMessage
    : DefaultErrorFunction(param)
})

// Creates a Currency Type, sets the errorMessage
export type Currency = Static<typeof Currency>
export const Currency = Type.RegExp(/^[+-]?[0…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@divmgl
Comment options

Answer selected by divmgl
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants