Transform & Validate
Usage
import { Definition, Property, Thunk, Id, ObjectId } from 'dryerjs';
import { MaxLength } from 'class-validator';
import { Transform } from 'class-transformer';
@Definition()
export class Tag {
@Id()
id: ObjectId;
@Thunk(MaxLength(100), { scopes: 'input' })
@Thunk(Transform(({ value }) => value.trim()))
@Thunk(Transform(({ value }) => value.toLowerCase()), { scopes: 'output' })
@Property()
name: string;
}
DryerJS follows the approach of NestJS for transforming and validating data,
utilizing class-validator
and class-transformer
internally.
💡
Using @Transform
directly on the property will NOT work
Scope
You can use the scopes option with the @Thunk decorator to define the specific scopes to which the decorator should be applied.
The available scopes include input
, create
, update
, output
, or all
.
If you do not specify any scopes, the decorator will be applied to all
scopes.
export type ThunkScope = 'all' | 'create' | 'update' | 'input' | 'output';