From a1c2914725c04e421995462ea5a059f7aedc2a4b Mon Sep 17 00:00:00 2001 From: Rubin Bhandari Date: Mon, 25 Sep 2023 19:04:07 +0545 Subject: [PATCH] feat: typesafe comparision decorator --- .eslintrc.cjs | 90 +++++++++---------- .../validation/is-after.validator.ts | 14 +-- .../validation/is-equal-to.validator.ts | 6 +- .../validation/is-greater-than.validator.ts | 4 +- tsconfig.json | 1 - 5 files changed, 53 insertions(+), 62 deletions(-) diff --git a/.eslintrc.cjs b/.eslintrc.cjs index 4c3f575f..d83ae1c6 100644 --- a/.eslintrc.cjs +++ b/.eslintrc.cjs @@ -1,52 +1,52 @@ // @ts-check // eslint-disable-next-line @typescript-eslint/no-var-requires, @typescript-eslint/no-require-imports -const {defineConfig} = require("eslint-define-config"); +const { defineConfig } = require("eslint-define-config"); module.exports = defineConfig({ - parser: "@typescript-eslint/parser", - parserOptions: { - project: "tsconfig.json", + parser: "@typescript-eslint/parser", + parserOptions: { + project: "tsconfig.json", + }, + ignorePatterns: ["migrations", "src/generated", "**/*.spec.ts", "**/*.e2e.ts"], // optimize this + extends: ["@rubiin/eslint-config-ts"], + root: true, + settings: { + "import/resolver": { + typescript: { + alwaysTryTypes: true, + project: "./tsconfig.json", + }, }, - ignorePatterns: ["migrations", "src/generated", "**/*.spec.ts", "**/*.e2e.ts"], // optimize this - extends: ["@rubiin/eslint-config-ts"], - root: true, - settings: { - "import/resolver": { - typescript: { - alwaysTryTypes: true, - project: "./tsconfig.json", - }, - }, - }, - rules: { - "unicorn/prefer-module": "off", - "@typescript-eslint/no-floating-promises": "off", - "no-useless-constructor": "off", // optimize this - "@typescript-eslint/require-await": "off", // optimize this - "@typescript-eslint/no-unsafe-assignment": "off", // optimize this - "@typescript-eslint/no-unsafe-member-access": "off", // optimize this - "unicorn/prefer-top-level-await": "off", - "max-nested-callbacks": "off", // rxjs is nested - "@typescript-eslint/no-misused-promises": [ - "error", - { - checksVoidReturn: false, - }, - ], - "unicorn/prevent-abbreviations": [ - "error", - { - ignore: [ - "\\.e2e*", - "\\.spec*", - "\\.decorator*", - "\\*idx*", - ], - allowList: { - ProcessEnv: true, - UUIDParam: true, - }, - }, + }, + rules: { + "unicorn/prefer-module": "off", + "@typescript-eslint/no-floating-promises": "off", + "no-useless-constructor": "off", // optimize this + "@typescript-eslint/require-await": "off", // optimize this + "@typescript-eslint/no-unsafe-assignment": "off", // optimize this + "@typescript-eslint/no-unsafe-member-access": "off", // optimize this + "unicorn/prefer-top-level-await": "off", + "max-nested-callbacks": "off", // rxjs is nested + "@typescript-eslint/no-misused-promises": [ + "error", + { + checksVoidReturn: false, + }, + ], + "unicorn/prevent-abbreviations": [ + "error", + { + ignore: [ + "\\.e2e*", + "\\.spec*", + "\\.decorator*", + "\\*idx*", ], - }, + allowList: { + ProcessEnv: true, + UUIDParam: true, + }, + }, + ], + }, }); diff --git a/src/common/decorators/validation/is-after.validator.ts b/src/common/decorators/validation/is-after.validator.ts index b66c2b4b..7cf4640d 100644 --- a/src/common/decorators/validation/is-after.validator.ts +++ b/src/common/decorators/validation/is-after.validator.ts @@ -26,8 +26,8 @@ class IsAfterConstraint implements ValidatorConstraintInterface { } } -export const IsAfter = ( - property: string, +export const IsAfterField = ( + property: keyof T, validationOptions?: ValidationOptions, ): PropertyDecorator => { return function (object: Record, propertyName: string | symbol) { @@ -42,13 +42,3 @@ export const IsAfter = ( }; -// // add typesafe property string - -// export const IsAfterField = (property: keyof T, validationOptions?: ValidationOptions) => { -// return applyDecorators( -// IsNotEmpty({ -// message: validationI18nMessage("validation.isNotEmpty"), -// }), -// IsAfter(String(property), validationOptions), -// ); -// }; diff --git a/src/common/decorators/validation/is-equal-to.validator.ts b/src/common/decorators/validation/is-equal-to.validator.ts index 27855888..bd018e5b 100644 --- a/src/common/decorators/validation/is-equal-to.validator.ts +++ b/src/common/decorators/validation/is-equal-to.validator.ts @@ -25,8 +25,8 @@ class IsEqualToConstraint implements ValidatorConstraintInterface { } } -export const IsEqualToField = ( - property: string, +export const IsEqualToField = ( + property: keyof T, validationOptions?: ValidationOptions, ): PropertyDecorator => { return function (object: Record, propertyName: string | symbol) { @@ -39,3 +39,5 @@ export const IsEqualToField = ( }); }; }; + + diff --git a/src/common/decorators/validation/is-greater-than.validator.ts b/src/common/decorators/validation/is-greater-than.validator.ts index 7a5b6fba..6f4ae8a0 100644 --- a/src/common/decorators/validation/is-greater-than.validator.ts +++ b/src/common/decorators/validation/is-greater-than.validator.ts @@ -25,8 +25,8 @@ class IsGreaterThanConstraint implements ValidatorConstraintInterface { } } -export const IsGreaterThan = ( - property: string, +export const IsGreaterThan = ( + property: keyof T, validationOptions?: ValidationOptions, ): PropertyDecorator => { return function (object: Record, propertyName: string | symbol) { diff --git a/tsconfig.json b/tsconfig.json index 95bb0e86..fab0c12b 100755 --- a/tsconfig.json +++ b/tsconfig.json @@ -3,7 +3,6 @@ "compilerOptions": { "baseUrl": "src", "outDir": "dist", - "esModuleInterop": true, "paths": { "@lib/*": [ "./lib/*"