Open ako-v opened 7 months ago
Hello.
In your custom validator, you can utilize snippet similar to this one:
import { getMetadataStorage, MetadataStorage } from 'class-validator';
// ... Some other code
const dataType = CreateAdminDTO; // you put your data type here (one you are validating). I think you can use ValidationError.target for this.
const metadataStorage: MetadataStorage = getMetadataStorage();
const metas = metadataStorage.getTargetValidationMetadatas(dataType, null, false, false);
console.log(metas);
// Result is like:
// [
// ValidationMetadata {
// groups: [],
// each: false,
// context: undefined,
// type: 'customValidation',
// name: 'minLength',
// target: [class CreateAdminDTO],
// propertyName: 'username',
// constraints: [ 4 ], => validator was @minLength(4)
// constraintCls: [class CustomConstraint],
// validationTypeOptions: undefined
// },
// ... other metadatas
// ]
I am trying to customize messages in nestjs response, I am using decorators in DTOs, and want to get min constraint I pass to
@Min
(this is just an example). My DTO is something like this:in nestjs I am getting the validation error that comes from class-validator, but I get only "validation.min" as the constraint:
in the console.log(errors), I get something like this:
As you see I get
validation.min
(the message) I passed as the constraint, the question is how can I get the value (here is 1) I passed to@Min
?