Skip to content

Commit 35624ea

Browse files
author
hirsch88
committed
Add validotor command
1 parent 86bb29b commit 35624ea

File tree

3 files changed

+38
-0
lines changed

3 files changed

+38
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@ All the templates for the commands are located in `src/console/templates`.
186186
* `npm run console make:request <file>` - Generates a basic request.
187187
* `npm run console make:listener <file>` - Generates a basic listener.
188188
* `npm run console make:exception <file>` - Generates a basic exception.
189+
* `npm run console make:validator <file>` - Generates a custom validator.
189190
* `npm run console update:targets <file>` - Reads all the API files and generate a new `constants/Targets.ts` file out of it.
190191

191192
**Example**

src/console/MakeValidatorCommand.ts

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* MakeValidatorCommand
3+
* -------------------------------------
4+
*
5+
*/
6+
import { AbstractMakeCommand } from './lib/AbstractMakeCommand';
7+
8+
9+
export class MakeValidatorCommand extends AbstractMakeCommand {
10+
11+
public static command = 'make:validator';
12+
public static description = 'Generate new validator';
13+
14+
public type = 'Validator';
15+
public suffix = 'Validator';
16+
public template = 'validator.hbs';
17+
public target = 'api/validators';
18+
public updateTargets = false;
19+
20+
}

src/console/templates/validator.hbs

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { ValidatorConstraint, ValidatorConstraintInterface, ValidationArguments } from 'class-validator';
2+
3+
4+
@ValidatorConstraint({ name: '{{name.normal}}', async: false })
5+
export class {{name.capitalize}}Validator implements ValidatorConstraintInterface {
6+
7+
public validate(text: string, args: ValidationArguments): boolean {
8+
// Place your validation here
9+
return true;
10+
}
11+
12+
public defaultMessage(args: ValidationArguments): string {
13+
// Error message if the validation fails
14+
return 'Incorrect value';
15+
}
16+
17+
}

0 commit comments

Comments
 (0)