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

feature: add parameter type map for decorators #2502

Open
leandroluk opened this issue Jul 3, 2024 · 0 comments
Open

feature: add parameter type map for decorators #2502

leandroluk opened this issue Jul 3, 2024 · 0 comments
Labels
flag: needs discussion Issues which needs discussion before implementation. type: feature Issues related to new features.

Comments

@leandroluk
Copy link

leandroluk commented Jul 3, 2024

Description

Hello, I'm using the lib to validate some projects and I have an interesting feature to centralize validation settings for certain fields. See:

import * as classValidator from 'class-validator';
import {Field, InputType} from '@nestjs/graphql';
import {IsEmail, IsNotEmpty, IsString, MinLength} from 'class-validator';

export namespace ClassValidator {
  type AllKeys = keyof typeof classValidator
  type Func = (...args: any) => any
  type DecoratorKeys = {[Key in AllKeys]: typeof classValidator[Key] extends Func ? Key : never;}[AllKeys];
  export type ParameterMap = {[Key in DecoratorKeys]: Parameters<typeof classValidator[Key]>;};
  export type FieldMap = Record<string, Partial<ParameterMap>>
}

const fields: ClassValidator.FieldMap = {
  email: {
    IsNotEmpty: [{message: "'email' cannot be empty."}],
    IsEmail: [{}, {message: "'email' is invalid."}],
  },
  password: {
    IsNotEmpty: [{message: "'password' cannot be empty."}],
    IsString: [{message: "'password' must be a string."}],
    MinLength: [8, {message: "'password' must have at least 8 characters."}],
  },
  token: {
    IsNotEmpty: [{message: "'token' cannot be empty."}],
  },
  code: {
    IsNotEmpty: [{message: "'code' cannot be empty."}],
  },
};

With this i can share the same field configuration between decorators and maybe it can be useful to more people.

Proposed solution

Add the namespace ClassValidator or something like that in default library types

@leandroluk leandroluk added flag: needs discussion Issues which needs discussion before implementation. type: feature Issues related to new features. labels Jul 3, 2024
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

1 participant