Open DONTWARRYIMMAFINE opened 9 months ago
This can be resolved by changing the link as below.
exception.extensions = { errors: normalizedErrors };
But I need something custom, so I use it like below: i18n-gql-validation-exception.filter.ts
import { ArgumentsHost, Catch, ExceptionFilter } from '@nestjs/common';
import { GqlContextType } from '@nestjs/graphql';
import { I18nContext, I18nValidationException } from 'nestjs-i18n';
import { formatI18nErrors } from 'nestjs-i18n/dist/utils';
@Catch(I18nValidationException)
export class I18nGqlValidationExceptionFilter implements ExceptionFilter {
catch(exception: any, host: ArgumentsHost) {
if (host.getType<GqlContextType>() !== 'graphql') {
return exception;
}
const i18n = I18nContext.current<I18nTranslations>(host);
if (!i18n) {
return exception;
}
const errors = formatI18nErrors(exception.errors, i18n.service, { lang: i18n.lang });
const constraints = errors?.[0].constraints;
if (constraints && Object.values(constraints).length > 0) {
exception.message = Object.values(constraints)[0];
exception.extensions = { errors };
}
return exception;
}
}
main.ts
app.useGlobalPipes(new I18nValidationPipe({ transform: true }));
app.useGlobalFilters(new I18nGqlValidationExceptionFilter());
Describe the bug
main.ts
create-user.input.ts
Test input:
{ "input": { "username": "a", "password": "12345678", } }
Result with plain ValidationPipe:
Result with I18nValidationPipe:
The issue is reproducible with: "@nestjs/apollo": "^12.0.11" "@nestjs/graphql": "^12.0.11"
With old packages everything works as expected: "@nestjs/apollo": "^10.2.0", "@nestjs/graphql": "^10.2.0"
Reproduction
Use any class-validator annotation on field and try to make a mutation.
System Info
Used Package Manager
yarn
Validations