Fork of class-validator with support multi-language (typescript)
Tags: validator, typescript, i18n, decorators
Published: 2020-09-12 09:21:53

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
```

## Differences from the original project:

🤘 This feature added support to replace any validation errors

:fire: Integrations with https://crowdin.com/ for manual simplified update translates :cn: :de: :ru: :us:

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

:exclamation: The translations are inside the [package](https://www.npmjs.com/package/class-validator-multi-lang), so the package size is very large, you can use the [lite version](https://www.npmjs.com/package/class-validator-multi-lang-lite) without translations and add the necessary translates ones [manually](https://github.com/EndyKaufman/class-validator-multi-lang/tree/i18n/i18n)

## Examples of usages

👶 Basic set custom messages

```typescript
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
});
```

👦 Load from file

```typescript
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
});
```

🧔 Load from 2 file :bomb: :boom:

```typescript
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
  });
});
```

🤴 With override

```typescript
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
});
```

🔢 With change property name

```typescript
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
});
```

🔢 With change target name

```typescript
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
});
```

🔢 With change arguments for validation decorator

```typescript
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 должно быть равно "тест"
});
```

🔢 With change value

```typescript
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
});
```

## Links

[class-validator-multi-lang](https://github.com/EndyKaufman/class-validator-multi-lang) - Github repository of this fork.

[class-validator](https://github.com/typestack/class-validator) - Github repository of original package.