Task is to validate payload in nest.js
app before managing one.
Each payload can contain tag
objects (1 - 11).
Every tag object
can have only one property and value (property determined by request)
Tag objects should be validated:
- property should be a string with any characters accept
:
and size in 1-255 - value should be a string with size in 1-255
Task looks like simple one. But I have no idea how to validate dynamically constructed properties in Tag
objects.
The DTOs are (validation configured using class-validator
):
import {
ArrayMaxSize,
ArrayMinSize,
IsArray,
IsDefined,
IsNotEmpty,
IsObject,
IsString,
Matches,
MinLength,
ValidateNested
} from 'class-validator';
export class Payload {
...
@IsArray()
@ArrayMinSize(1)
@ArrayMaxSize(11)
@ValidateType(() => Tag)
@ValidateNested()
@ApiProperty()
tags: Tag[];
}
To make Tag
flexible (because unknown property name) it made like Map
extension
export class Tag extends Map<string, string>{
}
or single field object
export class Tag {
[key: string]: string;
}
How to manage required validation for each Tag
?
(Regexp that excludes input with :
is /^[^:]+$/
and should be applied for key
)