Reputation: 261
export class MyDto extends Base{
@ApiModelProperty()
@Expose()
@MyCustomModifier()
readonly code: string = "";
MyCustomModifier(){
// modify
code = someUpdateOnCode()
}
Can we do something like this, so we can update dto properties
Upvotes: 1
Views: 1047
Reputation: 866
@Injectable()
export class JoiValidationPipe implements PipeTransform {
constructor(private readonly schema) {}
transform(value: any, metadata: ArgumentMetadata) {
const { error } = this.schema.validate(value);
if (error) {
console.log(error, 'error');
throw new BadRequestException(error.message);
}
// some changing value.code = someUpdateOnCode()
return value;
}
}
and use your pipe like this
import * as Joi from '@hapi/joi';
@Put('')
@UsePipes(
new JoiValidationPipe(
Joi.object().keys({
code: Joi.string()
.min(3)
.max(250)
.allow('')
.optional()
)
})
async someControler(){}
Upvotes: 1