Skip to content

Internationalization

Currently @hiperf/validate out of the box include only english language but you can define your own translations.

Example

js
import { validate } from '@hiperf/validate';

const locales = {
	es: {
		isEmail: '\"%v\" no es un correo electrónico válido'
	}
};
const schema = {
	email: {
		isEmail: true
	}
};
const data = {
	email: 'd@w.joe@example.com'
};
const { isValid, errors } = validate(schema, data, { lang: 'es', locales });
// isValid = false
// errors = ["d@w.joe@example.com" no es un correo electrónico válido']

Variables inside messages

You can use special variables inside custom locales.

%e - expected value

This variable is used inside some build in validators like min or minLength.

%v - current value

This variable is used inside most build in validators.

Example

js
const schema = {
	myValidator: 42
};
const locales = {
	en: {
		myValidator: 'Ultimate answer to everything is "%e", your answer is "%v"'
	}
};