MichalLytek / MichalLytek/type-graphql
Feature Request: Add ValidateOptions to Field decorator
- Dominant language
- TypeScript
- Stars
- 8.1k
- Forks
- 672
- PR merge metrics
- No merged PRs in 30d
Description
**Is your feature request related to a problem?**
When using `type-graphql` with class-level validation, I need to override or disable validation on a per-field basis. Currently, `FieldOptions` only extends `AdvancedOptions` (which covers deprecation, naming, complexity, and description), but it does not include `ValidateOptions`. This means `validate` and `validateFn` cannot be configured at the field level, only at the class/resolver level via `@ObjectType` or `@Resolver`.
**Solution that I like**
Extend `FieldOptions` to also include `ValidateOptions`:
```ts
// Current
export type FieldOptions = AdvancedOptions & {
simple?: boolean;
};
// Proposed
export type FieldOptions = AdvancedOptions & ValidateOptions & {
simple?: boolean;
};
```
This would allow per-field validation control, for example:
```ts
@ObjectType()
class Comment {
@Field(() => String, { validate: false })
rawHtml: string;
@Field(() => String, { validateFn: customValidator })
body: string;
}
```
**Alternatives that I considered**
- **Class-level `validate: false` + manual field validation**, works but defeats the purpose of declarative validation and requires imperative boilerplate inside resolvers.
- **Custom middleware on the field**, possible but verbose; requires a separate `@UseMiddleware` decorator per field, which pollutes the type definition and splits validation config across multiple decorators.
- **Wrapping the type in a new `@ObjectType`**, over-engineered for what is essentially a per-field config concern.
**Additional context**
`ValidateOptions` is already defined in `decorators/types.d.ts` and used in `@Arg` and `@Args` decorators, so the plumbing already exists. Adding it to `FieldOptions` is a consistent extension of an existing pattern within the library.
Related types for reference:
- [`FieldOptions`](decorators/Field.d.ts), currently `AdvancedOptions & { simple?: boolean }`
- [`ValidateOptions`](decorators/types.d.ts#L29-L32),`{ validate?: ValidateSettings; validateFn?: ValidatorFn }`
Contributor guide
Research direction
Start with decorators/Field.d.ts to inspect the current FieldOptions definition, then compare it with ValidateOptions in decorators/types.d.ts and its use in the @Arg and @Args decorators. Done means field options expose the requested validation settings consistently with the existing decorator types; verify the resulting TypeScript declarations and field-level configuration.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- graphql, typescript
- Domain
- api
- Issue type
- Feature
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Quiet
- Clarity
- Clearly specified
- Newbie friendliness
- 78/100