Skip to content

add typescript generic type to functions to ensure type check#2075

Open
amir-khoshbakht wants to merge 419 commits intomasterfrom
develop
Open

add typescript generic type to functions to ensure type check#2075
amir-khoshbakht wants to merge 419 commits intomasterfrom
develop

Conversation

@amir-khoshbakht
Copy link

Description

as you can see there is a union type which consists of two strings "aaa" and "bbb".
I need to get sure that class property is one of allowed values. This is done in three ways in the following section.
The first method: does not have the necessary efficiency and even though there is no "bad" option in the type, it does not show an error

the second method: In this case, all the options should be available in the type ((unionType)), and this case is desirable

import { IsEnum} from 'class-validator';

type unionType = 'aaa' | 'bbb';

const as = <T>(option: T) => option;

class ForTest{
  // method 1
  @IsEnum(['aaa', 'bad'] as unionType[])
  test: string;

  // method 2
  // typescript dosnt allow "bad" string which is not available in type
  @IsEnum(as<unionType[]>(['aaa', 'bad']))
  test2: string;
}

References

the expected method :

// typescript will show error for "bad" value
@IsEnum<unionType[]>(['aaa', 'bad'])
test3: string;

If the generic type is added to these functions, the correctness of the type can be ensured directly without defining the secondary function.

note: function "as" is custom function above
image

typestack-ci
typestack-ci previously approved these changes May 5, 2023
@typestack-ci
Copy link

@dependabot squash and merge

typestack-ci
typestack-ci previously approved these changes May 5, 2023
@typestack-ci
Copy link

@dependabot squash and merge

@typestack-ci
Copy link

@dependabot squash and merge

typestack-ci
typestack-ci previously approved these changes May 8, 2023
typestack-ci
typestack-ci previously approved these changes May 8, 2023
@typestack-ci
Copy link

@dependabot squash and merge

typestack-ci
typestack-ci previously approved these changes May 8, 2023
@typestack-ci
Copy link

@dependabot squash and merge

typestack-ci
typestack-ci previously approved these changes May 9, 2023
@typestack-ci
Copy link

@dependabot squash and merge

typestack-ci
typestack-ci previously approved these changes May 9, 2023
@typestack-ci
Copy link

@dependabot squash and merge

typestack-ci
typestack-ci previously approved these changes May 9, 2023
@typestack-ci
Copy link

@dependabot squash and merge

typestack-ci
typestack-ci previously approved these changes May 10, 2023
@typestack-ci
Copy link

@dependabot squash and merge

typestack-ci
typestack-ci previously approved these changes May 10, 2023
@typestack-ci
Copy link

@dependabot squash and merge

@typestack-ci
Copy link

@dependabot squash and merge

typestack-ci
typestack-ci previously approved these changes Jun 16, 2024
@typestack-ci
Copy link

@dependabot squash and merge

haiweilian and others added 10 commits November 12, 2024 16:25
* fix: fill base64 options correctly

pass the constraint to the isBase64 function
add unit test IsBase64 options

* refactor: pass options by name

---------

Co-authored-by: Brage Sekse Aarset <brage.aarset@gmail.com>
* fix: update version in package-lock.json

* fix: pass options to isBase64 correctly
* update validator to 13.15.20

* update
typestack-ci
typestack-ci previously approved these changes Nov 24, 2025
@typestack-ci
Copy link

@dependabot squash and merge

typestack-ci
typestack-ci previously approved these changes Dec 2, 2025
@typestack-ci
Copy link

@dependabot squash and merge

braaar and others added 11 commits February 25, 2026 09:31
* docs: add changelog for 0.14.4

* build: bump version to 0.14.4

---------

Co-authored-by: Viktor István Plézer <vplezer@wellapp.com>
* Create IsISO31661Numeric decorator

* Add export for IsISO31661Numeric decorator

* Add tests for IsISO31661Numeric validation decorator

* Document `IsISO31661Numeric` decorator in README

---------

Co-authored-by: Brage Sekse Aarset <brage.aarset@gmail.com>
* feat: `validateIf` for validation options

* refactor: format code with Prettier

* update

* update

* Update README.md

* fix: format readme

* set spec tsconfig sourceMap=true

* add test case for isValidationOptions

* run lint:fix prettier:fix

* Rename variable validateIf to shouldValidate

---------

Co-authored-by: Attila Oláh <NoNameProvided@users.noreply.github.com>
Co-authored-by: Brage Sekse Aarset <brage.aarset@gmail.com>
Co-authored-by: Brage Sekse Aarset <brage@bjerk.io>
…ll (#2647)

* fix: update UUID validation to support versions 1-8, nil, max, and all

* feat: add loose UUID validation and update documentation

* feat: enhance isUUID function to support array of versions and add corresponding tests

* test: update loose UUID validation tests with new valid and invalid cases

* Enhance @IsUUID description for version support

Updated the @IsUUID description to include support for an array of versions.

---------

Co-authored-by: Brage Sekse Aarset <brage.aarset@gmail.com>
* Create isISO6391.ts

* Document `@IsIso6391()` in `README.md`

* Add unit tests

* Add `isISO6391` to exported decorators

---------

Co-authored-by: Brage Sekse Aarset <brage.aarset@gmail.com>
Co-authored-by: Brage Sekse Aarset <brage.aarset@gmail.com>
Co-authored-by: Brage Sekse Aarset <brage.aarset@gmail.com>
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.