Skip to content

feat: make IsOptional conditional #689

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

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from

Conversation

ruscon
Copy link
Contributor

@ruscon ruscon commented Aug 10, 2020

Description

This is a modified copy of the old PR #196.

Checklist

  • the pull request title describes what this PR does (not a vague title like Update index.md)
  • the pull request targets the default branch of the repository (develop)
  • the code follows the established code style of the repository
    • npm run prettier:check passes
    • npm run lint:check passes
  • tests are added for the changes I made (if any source code was modified)
  • documentation added or updated
  • I have run the project locally and verified that there are no errors

Fixes

fixes #651

derekslarson
derekslarson previously approved these changes Nov 1, 2020
@yitzchak-ben-ezra-ecoplant

This is stuck for too much time.. can save a lot effort of writing custom validators which not always work..

@aqemi
Copy link

aqemi commented Jun 30, 2022

Please merge this pr

@yitzchak-ben-ezra-ecoplant

this repo is not maintained, I ended up copying the relevant code to my project
it worked great

@NoNameProvided
Copy link
Member

Please rebase to review.

Copy link
Member

@braaar braaar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice feature! I think we need to determine if we want this to co-exist with the general validation condition argument that has been proposed (for all validators) in #1579

I'm not sure, myself.

@IsOptional(item => item.age === 34)

versus

@IsOptional({ skip: item => item.age === 34 })

@@ -800,7 +821,7 @@ isBoolean(value);
| ------------------------------------------------| ----------- |
| **Common validation decorators** | |
| `@IsDefined(value: any)` | Checks if value is defined (!== undefined, !== null). This is the only decorator that ignores skipMissingProperties option. |
| `@IsOptional()` | Checks if given value is empty (=== null, === undefined) and if so, ignores all the validators on the property. |
| `@IsOptional(condition?: function)` | Checks if given value is empty (=== null, === undefined) and if so, ignores all the validators on the property. |
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps an explanation of the condition argument would be appreciated here?

@Clashsoft
Copy link
Contributor

What is the difference to ValidateIf?

@yitzchak-ben-ezra-ecoplant

@Clashsoft this is the difference:
ValidateIf - ignore all validations if condition is met
IsOptional - allows a field not to be specified when condition is met. but if the field is specified (even if condition is met!): all validations will be checked!!

@joaofranciscoguarda
Copy link

Please, aprove this PR! It's must needed

@armansujoyan
Copy link

@NoNameProvided can we merge this one? It is very useful validator and people use different workarounds to use it. I am sure community will be thankful for this one 🙌

@braaar
Copy link
Member

braaar commented Dec 15, 2023

See my recent comment in #1721

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

[Feature] @IsOptionaIf()
9 participants