Skip to content

feat: add support mega feature for multi-lang #743

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/workflows/continuous-deployment-workflow.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ jobs:
with:
registry-url: https://registry.npmjs.org
- run: npm ci --ignore-scripts
- run: npm run i18n:extract
- run: npm run i18n:fix
- run: npm run prettier:check
- run: npm run lint:check
- run: npm run test:ci
Expand All @@ -20,6 +22,7 @@ jobs:
- run: npm run build:cjs
- run: npm run build:umd
- run: npm run build:types
- run: cp -r i18n build
- run: cp LICENSE build/LICENSE
- run: cp README.md build/README.md
- run: jq 'del(.devDependencies) | del(.scripts)' package.json > build/package.json
Expand Down
5 changes: 5 additions & 0 deletions .github/workflows/continuous-integration-workflow.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ jobs:
- uses: actions/checkout@v1
- uses: actions/setup-node@v1
- run: npm ci --ignore-scripts
- run: npm run i18n:extract
- run: npm run i18n:fix
- run: npm run prettier:check
- run: npm run lint:check
tests:
Expand All @@ -24,6 +26,7 @@ jobs:
with:
node-version: ${{ matrix.node-version }}
- run: npm ci --ignore-scripts
- run: npm run i18n:extract
- run: npm run test:ci
- run: npm install codecov -g
if: ${{ matrix.node-version == '14.x' }}
Expand All @@ -36,6 +39,8 @@ jobs:
- uses: actions/checkout@v1
- uses: actions/setup-node@v1
- run: npm ci --ignore-scripts
- run: npm run i18n:extract
- run: npm run i18n:fix
- run: npm run build:es2015
- run: npm run build:esm5
- run: npm run build:cjs
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,6 @@ temp
# Files for playing around locally
playground.ts
playground.js

# Buided json dictionaries of i18n
i18n/**/*.json
181 changes: 181 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
[![codecov](https://codecov.io/gh/typestack/class-validator/branch/develop/graph/badge.svg)](https://codecov.io/gh/typestack/class-validator)
[![npm version](https://badge.fury.io/js/class-validator.svg)](https://badge.fury.io/js/class-validator)
[![install size](https://packagephobia.now.sh/badge?p=class-validator)](https://packagephobia.now.sh/result?p=class-validator)
[![Crowdin](https://badges.crowdin.net/class-validator/localized.svg)](https://crowdin.com/project/class-validator)

Allows use of decorator and non-decorator based validation.
Internally uses [validator.js][1] to perform validation.
Expand Down Expand Up @@ -37,6 +38,7 @@ Class-validator works on both browser and node.js platforms.
- [Validation decorators](#validation-decorators)
- [Defining validation schema without decorators](#defining-validation-schema-without-decorators)
- [Validating plain objects](#validating-plain-objects)
- [Basic support i18n](#basic-support-i18n)
- [Samples](#samples)
- [Extensions](#extensions)
- [Release notes](#release-notes)
Expand Down Expand Up @@ -989,6 +991,185 @@ Here is an example of using it:

Due to nature of the decorators, the validated object has to be instantiated using `new Class()` syntax. If you have your class defined using class-validator decorators and you want to validate plain JS object (literal object or returned by JSON.parse), you need to transform it to the class instance via using [class-transformer](https://github.com/pleerock/class-transformer)).

## Basic support i18n

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/

Basic set custom messages

```typescript
import { IsOptional, Equals, Validator } from 'class-validator';

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

With override

```typescript
import { IsOptional, Equals, Validator, setClassValidatorMessages } from 'class-validator';

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';

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';

@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';

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';

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

## Samples

Take a look on samples in [./sample](https://github.com/pleerock/class-validator/tree/master/sample) for more examples of
Expand Down
3 changes: 3 additions & 0 deletions crowdin.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
files:
- source: '*.pot'
translation: /%original_path%/%two_letters_code%.po
Loading