DEV Community

ILshat Khamitov
ILshat Khamitov

Posted on

Fork of class-validator with support multi-language (typescript)

Allows use of decorator and non-decorator based validation. Internally uses validator.js to perform validation. Class-validator works on both browser and node.js platforms.

Installation

npm install class-validator-multi-lang --save
Enter fullscreen mode Exit fullscreen mode

Differences from the original project:

🤘 This feature added support to replace any validation errors

🔥 Integrations with https://crowdin.com/ for manual simplified update translates 🇨🇳 🇩🇪 🇷🇺 🇺🇸

🌎 Translations created with the machine 🤖, if you found the mistake 🐛 please add a new version of translate and write a comment in the right panel in https://crowdin.com/project/class-validator 😎

❗ The translations are inside the package, so the package size is very large, you can use the lite version without translations and add the necessary translates ones manually

Examples of usages

👶 Basic set custom messages

import { IsOptional, Equals, validator } from 'class-validator-multi-lang';

class MyClass {
  @IsOptional()
  @Equals('test')
  title: string = 'bad_value';
}

const RU_I18N_MESSAGES = {
  '$property must be equal to $constraint1': '$property должно быть равно $constraint1',
};

const model = new MyClass();

validator.validate(model, messages: RU_I18N_MESSAGES).then(errors => {
  console.log(errors[0].constraints);
  // out: title должно быть равно test
});
Enter fullscreen mode Exit fullscreen mode

👦 Load from file

import { IsOptional, Equals, validator } from 'class-validator-multi-lang';
import { readFileSync } from 'fs';
import { resolve } from 'path';

class MyClass {
  @IsOptional()
  @Equals('test')
  title: string = 'bad_value';
}

const RU_I18N_MESSAGES = JSON.parse(readFileSync(resolve(__dirname, './node_modules/class-validator-multi-lang/i18n/ru.json')).toString());

const model = new MyClass();

validator.validate(model, messages: RU_I18N_MESSAGES).then(errors => {
  console.log(errors[0].constraints);
  // out: title должен быть равен test
});
Enter fullscreen mode Exit fullscreen mode

🧔 Load from 2 file 💣 💥

import { IsOptional, Equals, validator } from 'class-validator-multi-lang';
import { readFileSync } from 'fs';
import { resolve } from 'path';

class MyClass {
  @IsOptional()
  @Equals('test')
  title: string = 'bad_value';
}

const RU_I18N_MESSAGES = JSON.parse(
  readFileSync(resolve(__dirname, './node_modules/class-validator-multi-lang/i18n/ru.json')).toString()
);

const FR_I18N_MESSAGES = JSON.parse(
  readFileSync(resolve(__dirname, './node_modules/class-validator-multi-lang/i18n/fr.json')).toString()
);

const model = new MyClass();

validator.validate(model, { messages: RU_I18N_MESSAGES }).then(errors => {
  console.log(errors[0].constraints);
  // out: title должен быть равен test

  validator.validate(model, { messages: FR_I18N_MESSAGES }).then(errors => {
    console.log(errors[0].constraints);
    // out: title doit être égal à test
  });
});
Enter fullscreen mode Exit fullscreen mode

🤴 With override

import { IsOptional, Equals, validator, setClassValidatorMessages } from 'class-validator-multi-lang';

class MyClass {
  @IsOptional()
  @Equals('test')
  title: string = 'bad_value';
}

setClassValidatorMessages({
  '$property must be equal to $constraint1': '$property должно быть равно $constraint1',
});

const model = new MyClass();

validator.validate(model).then(errors => {
  console.log(errors[0].constraints);
  // out: title должно быть равно test
});
Enter fullscreen mode Exit fullscreen mode

🔢 With change property name

import { IsOptional, Equals, ClassPropertyTitle, validator } from 'class-validator-multi-lang';

class MyClass {
  @IsOptional()
  @Equals('test')
  @ClassPropertyTitle('property "title"')
  title: string = 'bad_value';
}

const RU_I18N_MESSAGES = {
  '$property must be equal to $constraint1': '$property должно быть равно $constraint1',
};
const RU_I18N_TITLES = {
  'property "title"': 'поле "заголовок"',
};

const model = new MyClass();

validator.validate(model, { messages: RU_I18N_MESSAGES, titles: RU_I18N_TITLES }).then(errors => {
  console.log(errors[0].constraints);
  // out: поле "заголовок" должно быть равно test
});
Enter fullscreen mode Exit fullscreen mode

🔢 With change target name

import { IsOptional, Equals, ClassPropertyTitle, validator } from 'class-validator-multi-lang';

@ClassTitle('object "MyClass"')
class MyClass {
  @IsOptional()
  @Equals('test')
  title: string = 'bad_value';
}

const RU_I18N_MESSAGES = {
  '$property must be equal to $constraint1': '$property в $target должно быть равно $constraint1',
};
const RU_I18N_TITLES = {
  'object "MyClass"': 'объекте "МойКласс"',
};

const model = new MyClass();

validator.validate(model, { messages: RU_I18N_MESSAGES, titles: RU_I18N_TITLES }).then(errors => {
  console.log(errors[0].constraints);
  // out: title в объекте "МойКласс" должно быть равно test
});
Enter fullscreen mode Exit fullscreen mode

🔢 With change arguments for validation decorator

import { IsOptional, Equals, validator } from 'class-validator-multi-lang';

class MyClass {
  @IsOptional()
  @Equals('test')
  title: string = 'bad_value';
}

const RU_I18N_MESSAGES = {
  '$property must be equal to $constraint1': '$property должно быть равно $constraint1',
};
const RU_I18N_TITLES = {
  test: '"тест"',
};

const model = new MyClass();

validator.validate(model, { messages: RU_I18N_MESSAGES, titles: RU_I18N_TITLES }).then(errors => {
  console.log(errors[0].constraints);
  // out: title должно быть равно "тест"
});
Enter fullscreen mode Exit fullscreen mode

🔢 With change value

import { IsOptional, Equals, validator } from 'class-validator-multi-lang';

class MyClass {
  @IsOptional()
  @Equals('test')
  title: string = 'bad_value';
}

const RU_I18N_MESSAGES = {
  '$property must be equal to $constraint1': '$property равно $value, а должно быть равно $constraint1',
};
const RU_I18N_TITLES = {
  bad_value: '"плохое_значение"',
};

const model = new MyClass();

validator.validate(model, { messages: RU_I18N_MESSAGES, titles: RU_I18N_TITLES }).then(errors => {
  console.log(errors[0].constraints);
  // out: title равно "плохое_значение", а должно быть равно test
});
Enter fullscreen mode Exit fullscreen mode

Links

class-validator-multi-lang - Github repository of this fork.

class-validator - Github repository of original package.

Top comments (0)