Skip to content

Function: registerValidationMessages() ​

registerValidationMessages(language, messages): void

Register the validation error messages of a language, by error code (any.required, string.min). The messages are merged into the ones already registered for the language, replacing those with the same code. A validation in the language (run in runInValidationLanguage(), or given the errors.language option) uses them; the codes without a message in the language keep the default (English) one.

Parameters ​

language ​

string

The language tag, like fr or zh-CN, matched exactly (case-sensitive) by the validations.

messages ​

Record<string, string>

The message templates by error code, like {'any.required': '{{#label}} est requis'}: {{#label}}, {{#limit}}… are replaced by the context of the error (see defaultValidationMessages() for the codes and their variables).

Returns ​

void

Throws ​

When a message is not a string, or its template is invalid.

Remarks ​

The templates are compiled once, when registered: register the languages when the application starts.

Example ​

typescript
import {DTO, registerValidationMessages, runInValidationLanguage} from 'lakutata'

registerValidationMessages('fr', {
    'any.required': '{{#label}} est requis',
    'string.min': '{{#label}} doit contenir au moins {{#limit}} caractères'
})

runInValidationLanguage('fr', (): void => {
    //Throws: "name" est requis
    DTO.validate({}, DTO.Object({name: DTO.String().min(2).required()}))
})

Released under the MIT License.