question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

question: validate based on different type in model

See original GitHub issue

Hi 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:closed
  • Created 3 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
HunterJS-bitcommented, Mar 30, 2020

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.

1reaction
vlapocommented, Mar 27, 2020

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

export class modelCreate {
    @IsOptional()
    @IsNumber()
    id: number;

    @IsOptional()
    @IsString()
    label: string;

    @IsOptional()
    @IsEnum(AttributeTypes)
    type: AttributeTypes;

    @IsOptional()
    @IsObject()
    @ValidateNested()
    validation: TimeValidation | NumbersValidation | DateValidation;
}
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found