https://github.com/unlight/class-validator-flat-formatter
Convert array of ValidationError objects from class-validator to multiline string
https://github.com/unlight/class-validator-flat-formatter
class-validator error-formatters error-formatting validation-error
Last synced: 11 months ago
JSON representation
Convert array of ValidationError objects from class-validator to multiline string
- Host: GitHub
- URL: https://github.com/unlight/class-validator-flat-formatter
- Owner: unlight
- Created: 2021-03-25T13:12:11.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2024-10-26T19:14:25.000Z (over 1 year ago)
- Last Synced: 2024-10-26T21:26:33.636Z (over 1 year ago)
- Topics: class-validator, error-formatters, error-formatting, validation-error
- Language: TypeScript
- Homepage:
- Size: 322 KB
- Stars: 5
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
Awesome Lists containing this project
README
# class-validator-flat-formatter
Convert array of ValidationError objects from class-validator to multiline string
## Install
```sh
npm install class-validator-flat-formatter
```
## Usage
#### As string
```ts
import { validationError } from 'class-validator-flat-formatter';
const errors = await validate(user);
const message = validationError(errors, options);
// message => name: should not be empty (isNotEmpty), email: must be an email (isEmail)
```
##### Options
```ts
export interface ValidationErrorOptions {
/**
* Custom template, tokens:
* {propertyPath} - Full dotted property path (e.g user.email)
* {property} - Last piece of {propertyPath} (e.g. email)
* {constraintRule} - Constraint rule id (e.g. isEmail)
* {constraintMessage} - Constraint message (e.g. must be an email)
*/
template?: string;
/**
* Delimiter of joined several validation messages.
* Default: Comma and space (CS)
*/
delimiter?: Delimiter | string;
/**
* Period at the end of string.
* Default: false
*/
period?: boolean;
}
```
#### As array
```ts
import { validationErrorsAsArray } from 'class-validator-flat-formatter';
const errors = await validate(user);
const messages = validationErrorsAsArray(errors);
/**
messages => Array {
'name: should not be empty (isNotEmpty)',
'email: must be an email (isEmail)'
}
*/
```
## License
[MIT License](https://opensource.org/licenses/MIT) (c) 2024