Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add skipIf predicate option to decorator options #1455

Open
aoi-umi opened this issue Dec 19, 2021 · 3 comments
Open

feat: add skipIf predicate option to decorator options #1455

aoi-umi opened this issue Dec 19, 2021 · 3 comments
Labels
flag: needs discussion Issues which needs discussion before implementation. type: feature Issues related to new features.

Comments

@aoi-umi
Copy link

aoi-umi commented Dec 19, 2021

Description

like this
#740

export class ContentSave {
  @IsDefined()
  @MinLength(5)
  title: string;

  @Type()
  submit?: boolean;
}

when submit is true, i want to skip IsDefined , but not minLength
for example:
{title: "111111111", submit: true} => valid
{title: undefined, submit: false} => valid
{title: "1", submit: false} => invalid(MinLength)

Proposed solution

add skipIf on the ValidationOptions

export class ContentSave {
  @IsDefined({
    skipIf: (data) => data.submit !== true
  })
  @MinLength(5)
  title: string;

  @Type()
  submit?: boolean;
}
@aoi-umi aoi-umi added flag: needs discussion Issues which needs discussion before implementation. type: feature Issues related to new features. labels Dec 19, 2021
@ruscon
Copy link
Contributor

ruscon commented Dec 24, 2021

import type { ValidationOptions } from 'class-validator';
import { ValidateIf } from 'class-validator';

/**
 * Checks (in case of condition passed) if the value is missing and if so, ignores all validators.
 *
 * @example
 * @IsOptionalIf((o) => o && o?.source === 'instore', { always: true })
 * email: string
 */
export function IsOptionalIf(condition: (object: any, value: any) => boolean, validationOptions?: ValidationOptions): PropertyDecorator {
    return ValidateIf((object: Record<string, unknown>, value: string): boolean => {
        return !condition(object, value) ? true : value !== null && value !== undefined;
    }, validationOptions);
}

@aoi-umi
Copy link
Author

aoi-umi commented Dec 29, 2021

import type { ValidationOptions } from 'class-validator';
import { ValidateIf } from 'class-validator';

/**
 * Checks (in case of condition passed) if the value is missing and if so, ignores all validators.
 *
 * @example
 * @IsOptionalIf((o) => o && o?.source === 'instore', { always: true })
 * email: string
 */
export function IsOptionalIf(condition: (object: any, value: any) => boolean, validationOptions?: ValidationOptions): PropertyDecorator {
    return ValidateIf((object: Record<string, unknown>, value: string): boolean => {
        return !condition(object, value) ? true : value !== null && value !== undefined;
    }, validationOptions);
}

i just want to ignore one validator not all

@NoNameProvided NoNameProvided changed the title skip validator by option feat: add skipIf predicate option to decorator options Dec 15, 2022
@braaar
Copy link
Member

braaar commented Dec 15, 2023

I believe this is the best proposal for improving validator conditionality. I think #1579 needs some work to resolve this (see my comments there), but this solution is my preferred approach to making specific decorators conditional.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
flag: needs discussion Issues which needs discussion before implementation. type: feature Issues related to new features.
Development

No branches or pull requests

3 participants