Hi,
is there a way to get the initially declared Metadata on a decorator ?,
For example with the MaxLength decorator. MaxLength(255, { message: "somemessage" }) ,
i am looking for a way to retrive that 255 .
Thanks alot!
I'm also interested in this, I'm trying to generate forms with required fields using the @IsDefined decorator metadata.
You have to use internal API to get this kind of data. Check a code and how internally class-validator works/store data you need.
Hey again,
I spent a bit of time looking at this today and the best I could come up with is the following:
import { getFromContainer, MetadataStorage } from 'class-validator';
const validatorStorage: MetadataStorage = getFromContainer(MetadataStorage);
const createUserDtoValidationMetadata = validatorStorage.getTargetValidationMetadatas(CreateUserDto, null, null);
console.log(validatorStorage.groupByPropertyName(createUserDtoValidationMetadata));
For reference here is what the CreateUserDto looks like:
export class CreateUserDto {
@IsString({
message: 'Name must be a text value'
})
name: string;
@IsString({
message: 'Surname must be a text value'
})
surname: string;
@IsString()
@IsOptional()
password?: string;
@IsEmail()
email: string;
@IsString()
type: UserType;
@IsOptional()
@IsString()
cellphoneNumber?: string;
}
Most helpful comment
I'm also interested in this, I'm trying to generate forms with required fields using the @IsDefined decorator metadata.