question: validate based on different type in model
See original GitHub issueHi Im working with TypeORM and class validation, and I would like to provide different to field based on other field value. So to explain, here is how my DTO model looks like this:
import { IsString, IsOptional, IsNumber, IsEnum, IsObject, IsBoolean, ValidateNested } from 'class-validator';
export enum AttributeTypes {
DATE = 'DATE',
TIME = 'TIME',
NUMBERS = 'NUMBERS',
}
export class BaseValidation {
@IsOptional()
@IsBoolean()
required: boolean;
}
export class modelCreate {
@IsOptional()
@IsNumber()
id: number;
@IsOptional()
@IsString()
label: string;
@IsOptional()
@IsEnum(AttributeTypes)
type: AttributeTypes;
@IsOptional()
@IsObject()
@ValidateNested()
validation: BaseValidation;
}
he problem here is that I have this field: validation in modelCreate, & that field is an object and can have multiple properties & can look like this in db:
validation: {
required: true,
text: 2
}
or it can look like this
validation: {
required: false,
number: 1,
maxNumber: 10
}
and that would depend on type property of modelCreate, because if type is ‘TIME’, I would like to have validation for this:
BaseValidation {
@IsBoolean()
required: true,
@IsString()
text: 2
}
and if type is ‘NUMBERS’, I would like to have validation like this
BaseValidation {
@IsBoolean()
required: boolean,
@IsNumber()
number: number,
@IsNumber()
maxNumber: number
}
o the question is how would I toogle different classes in validation field based on type field value in class validator, and is that even possible ?
Issue Analytics
- State:
- Created 3 years ago
- Comments:7 (3 by maintainers)
Top Results From Across the Web
typescript - class validator - validate based on different type
So the question is how would I toogle different classes in validation field based on type field value in class validator, and is...
Read more >Model Validation and Testing: A Step-by-Step Guide | Built In
Compute statistical values comparing the model results to the validation data. Calculate the model results to the data points in the testing ...
Read more >How to Validate your Model & Data and Easily Avoid Common ...
Building good and stable ML models is hard. The wide variety of challenges in the process includes biases when building or splitting the ......
Read more >4 Questions to Ask When Determining Model Validation Scope
Key Considerations for Model Validation · Question 1: What about the model has changed since the last full-scope validation? · Question 2: How ......
Read more >Question Types and Validation - ProForma
Checkboxes. Validation: Response required. Required choice: An option users are required to chose. Minimum and Maximum number of choices: Limits how many ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Yes, but type its working like that if type is property inside of validation object, but for me the type should be on modelCreate 😃, nevermind this doesnt concerne class validator, @vlapo thank you ver much , closing issue.
You can use groups - https://github.com/typestack/class-validator#validation-groups - example https://github.com/typestack/class-validator/issues/278#issuecomment-604675151
Or create different classes for all types. E.g.
NumbersValidation
,TimeValidation
,DateValidation