Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Localization #214

Merged
merged 12 commits into from
Aug 1, 2022
Merged

Localization #214

merged 12 commits into from
Aug 1, 2022

Conversation

sinclairzx81
Copy link
Owner

This PR adds error codes for each kind of validation error. These codes can be used to remap validation errors to specific locales. This functionality should allow for future i18n support in TypeBox.

The following remaps errors to French. Note that dependent properties can be obtained via the schema passed on each error.

import { ValueErrorType } from '@sinclair/typebox/error'

const errors = [...C.Errors({...})].map(error => {
  switch(error.type) {
    case ValueErrorType.ArrayMinItems: 
      return { ...error, message: `Attendu au moins ${error.schema.minItems} éléments`}
    case ValueErrorType.Boolean: 
      return { ...error, message: 'Booléen attendu' }
    case ValueErrorType.String: 
      return { ...error, message: 'Chaîne attendue' }
    case ValueErrorType.Number: 
      return { ...error, message: 'Nombre attendu' }
    // ... 
    default: 
      return error
})

@sinclairzx81 sinclairzx81 merged commit 57f8f55 into master Aug 1, 2022
@sinclairzx81 sinclairzx81 deleted the i18n branch August 1, 2022 10:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant