Skip to content
Snippets Groups Projects
Commit 5a2f2dc4 authored by L4168's avatar L4168
Browse files

customvalidation in one file, added rolevalidator

parent 3eafdd7f
No related branches found
No related tags found
4 merge requests!59Development to master,!31Development,!25Dto service,!24Faction task edit
import {ValidatorConstraint, ValidatorConstraintInterface, ValidationArguments} from "class-validator";
import { Logger } from "@nestjs/common";
// validates array length
@ValidatorConstraint({ name: "arrayLength", async: true })
export class ArrayLength implements ValidatorConstraintInterface {
validate(array: string[], args: ValidationArguments) {
Logger.log(array.length)
return array.length > args.constraints[0] && array.length < args.constraints[1]; // for async validations you must return a Promise<boolean> here
}
defaultMessage(args: ValidationArguments) { // here you can provide default error message if validation failed
return "Please input all passwords";
}
}
\ No newline at end of file
......@@ -17,3 +17,16 @@ export class Uuid implements ValidatorConstraintInterface {
return 'Not valid uuid';
}
}
// checks if role is valid
@ValidatorConstraint({ name: 'roleValidation', async: true })
export class RoleValidation implements ValidatorConstraintInterface {
validate(role: string, args: ValidationArguments) {
const validRoles = ['admin', 'soldier', 'factionleader'];
return validRoles.includes(role);
}
defaultMessage(args: ValidationArguments) {
return 'Not valid uuid';
}
}
......@@ -7,8 +7,8 @@ import {
Equals,
} from 'class-validator';
import { FactionEntity } from '../game/faction.entity';
import { Uuid } from '../shared/uuid.validation';
import { GameEntity } from 'src/game/game.entity';
import { GameEntity } from '../game/game.entity';
import { Uuid } from '../shared/custom-validation';
export class CreateTaskDTO {
@IsString()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment