Function: runInValidationLanguage()
runInValidationLanguage<
T>(language,fn):T
Run a function with a validation language: the validation errors raised in it, its asynchronous continuations included, have their messages in that language (the messages registered by registerValidationMessages()), except for the validations giving their own errors.language option. The codes without a message in the language keep the default (English) one, and so does every code of a language with no registered messages.
Type Parameters
T
T
Parameters
language
string
The language tag the messages are registered under, like fr or zh-CN.
fn
() => T
The function to run; it may be asynchronous.
Returns
T
What the function returns (its promise, for an asynchronous function).
Remarks
A valid value is validated as without a language: only an invalid value is validated a second time, in the language, for its messages.
Example
import {DTO, loadValidationMessages, registerValidationMessages, runInValidationLanguage} from 'lakutata'
const messages: Record<string, string> | undefined = await loadValidationMessages('fr')
if (messages) registerValidationMessages('fr', messages)
await runInValidationLanguage('fr', async (): Promise<void> => {
try {
DTO.validate('', DTO.String().required())
} catch (error) {
//The message is in French
console.log((error as Error).message)
}
})