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

Implement AllowIf decorator #86

Open
wants to merge 140 commits into
base: master
Choose a base branch
from
Open

Implement AllowIf decorator #86

wants to merge 140 commits into from

Conversation

chambber
Copy link

PR Checklist

Please check if your PR fulfills the following requirements:

PR Type

What kind of change does this PR introduce?

  • Bugfix
  • Feature
  • Code style update (formatting, local variables)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • CI related changes
  • Other... Please describe:

What is the current behavior?

There is currently no way to define whether a property is conditionally whitelisted, for example:

enum TypesCancel = {
  USER: 'user',
  STOCKOUT: 'stockout',
  OTHERS: 'others',
}

class CancelDTO {
  @IsEnum(TypesCancel)
  @IsNotEmpty() 
  type: TypesCancel;

  // if the type === TypesCancel.OTHERS, reason is required, but if not, must not sent
  @Optional() // with optional, it still receives even if the type !== TypesCancel.OTHERS
  @IsString()
  reason?: string;
}

const cancel = new CancelDTO();
cancel.type = TypesCancel.USER;
cancel.reason = 'reason';

validate(cancel).then(errors => {
  // cancel.reason is defined
});

Issue Number: typestack#1489

What is the new behavior?

enum TypesCancel = {
  USER: 'user',
  STOCKOUT: 'stockout',
  OTHERS: 'others',
}

class CancelDTO {
  @IsEnum(TypesCancel)
  @IsNotEmpty() 
  type: TypesCancel;

  @AllowIf(cancel => cancel.type === TypesCancel.OTHERS) // with AllowIf, the reason was accept if type === TypesCancel.OTHERS
  @IsString()
  reason?: string;
}

const cancel = new CancelDTO();
cancel.type = TypesCancel.USER;
cancel.reason = 'reason';

validate(cancel).then(errors => {
  // errors = reason should not exist
  // cancel.reason is not defined
});

Does this PR introduce a breaking change?

  • Yes
  • No

kamilmysliwiec and others added 30 commits October 28, 2021 09:37
chore(deps): update dependency @types/node to v14.17.32
…esolve-11.x

chore(deps): update dependency @rollup/plugin-node-resolve to v11.2.1
chore(deps): update dependency lint-staged to v10.5.4
chore(deps): update dependency husky to v4.3.8
chore(deps): update dependency eslint to v7.32.0
chore(deps): update dependency rollup to v2.58.3
…24.x

chore(deps): update dependency eslint-plugin-jest to v24.7.0
chore(deps): update dependency ts-jest to v26.5.6
kamilmysliwiec and others added 24 commits February 14, 2022 14:05
chore(deps): update dependency rollup to v2.67.2
chore(deps): update dependency ts-node to v10.5.0
chore(deps): update dependency lint-staged to v12.3.4
…25.x

chore(deps): update dependency eslint-plugin-jest to v25.7.0
…onorepo

chore(deps): update typescript-eslint monorepo to v5.11.0
@chambber
Copy link
Author

Hey @kamilmysliwiec, it's my first contribution to an open-source project, if you need to change something in PR, just say so!

@kasir-barati
Copy link

kasir-barati commented May 10, 2022

@chambber @kamilmysliwiec why this feature is not merged?

BTW we have this one too: https://stackoverflow.com/a/70128576/8784518

@kamilmysliwiec kamilmysliwiec force-pushed the master branch 2 times, most recently from 7b454dd to 5f0d424 Compare June 20, 2023 07:03
@burriedu2
Copy link

I would like to use this feature. Is there any chance it will end up in nestJs?

@burriedu2 burriedu2 mentioned this pull request Aug 16, 2023
12 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants