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

feat(common): adding the fieldname to validation errors #12403

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions packages/common/pipes/parse-array.pipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import { HttpErrorByCode } from '../utils/http-error-by-code.util';
import { isNil, isUndefined, isString } from '../utils/shared.utils';
import { ValidationPipe, ValidationPipeOptions } from './validation.pipe';

const VALIDATION_ERROR_MESSAGE = 'Validation failed (parsable array expected)';
const DEFAULT_ARRAY_SEPARATOR = ',';

/**
Expand Down Expand Up @@ -62,21 +61,27 @@ export class ParseArrayPipe implements PipeTransform {
*/
async transform(value: any, metadata: ArgumentMetadata): Promise<any> {
if (!value && !this.options.optional) {
throw this.exceptionFactory(VALIDATION_ERROR_MESSAGE);
throw this.exceptionFactory(
this.getValidationErrorMessage(metadata.data),
);
} else if (isNil(value) && this.options.optional) {
return value;
}

if (!Array.isArray(value)) {
if (!isString(value)) {
throw this.exceptionFactory(VALIDATION_ERROR_MESSAGE);
throw this.exceptionFactory(
this.getValidationErrorMessage(metadata.data),
);
} else {
try {
value = value
.trim()
.split(this.options.separator || DEFAULT_ARRAY_SEPARATOR);
} catch {
throw this.exceptionFactory(VALIDATION_ERROR_MESSAGE);
throw this.exceptionFactory(
this.getValidationErrorMessage(metadata.data),
);
}
}
}
Expand Down Expand Up @@ -135,6 +140,10 @@ export class ParseArrayPipe implements PipeTransform {
return value;
}

protected getValidationErrorMessage(field: string) {
return `Validation failed (parsable array expected in "${field}")`;
}

protected isExpectedTypePrimitive(): boolean {
return [Boolean, Number, String].includes(this.options.items as any);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/common/pipes/parse-bool.pipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export class ParseBoolPipe
return false;
}
throw this.exceptionFactory(
'Validation failed (boolean string is expected)',
`Validation failed (boolean string is expected in "${metadata.data}")`,
);
}

Expand Down
2 changes: 1 addition & 1 deletion packages/common/pipes/parse-enum.pipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export class ParseEnumPipe<T = any> implements PipeTransform<T> {
}
if (!this.isEnum(value)) {
throw this.exceptionFactory(
'Validation failed (enum string is expected)',
`Validation failed (enum string is expected in "${metadata.data}")`,
);
}
return value;
Expand Down
2 changes: 1 addition & 1 deletion packages/common/pipes/parse-float.pipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export class ParseFloatPipe implements PipeTransform<string> {
}
if (!this.isNumeric(value)) {
throw this.exceptionFactory(
'Validation failed (numeric string is expected)',
`Validation failed (numeric string is expected in "${metadata.data}")`,
);
}
return parseFloat(value);
Expand Down
2 changes: 1 addition & 1 deletion packages/common/pipes/parse-int.pipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export class ParseIntPipe implements PipeTransform<string> {
}
if (!this.isNumeric(value)) {
throw this.exceptionFactory(
'Validation failed (numeric string is expected)',
`Validation failed (numeric string is expected in "${metadata.data}")`,
);
}
return parseInt(value, 10);
Expand Down
2 changes: 1 addition & 1 deletion packages/common/pipes/parse-uuid.pipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export class ParseUUIDPipe implements PipeTransform<string> {
throw this.exceptionFactory(
`Validation failed (uuid${
this.version ? ` v ${this.version}` : ''
} is expected)`,
} is expected in "${metadata.data}")`,
);
}
return value;
Expand Down
5 changes: 5 additions & 0 deletions packages/common/test/pipes/parse-array.pipe.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ describe('ParseArrayPipe', () => {
beforeEach(() => {
target = new ParseArrayPipe();
});
it('should mention the field name in the error', async () => {
return expect(
target.transform(true, { data: 'foo' } as ArgumentMetadata),
).to.be.rejectedWith(/.*Validation failed.*"foo".*/);
});
it('should throw an exception (boolean)', async () => {
return expect(
target.transform(true, {} as ArgumentMetadata),
Expand Down
11 changes: 9 additions & 2 deletions packages/common/test/pipes/parse-bool.pipe.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { expect } from 'chai';
import { ArgumentMetadata } from '../../interfaces';
import { ParseBoolPipe } from '../../pipes/parse-bool.pipe';
import { BadRequestException } from '../../exceptions';

describe('ParseBoolPipe', () => {
let target: ParseBoolPipe;
Expand All @@ -27,8 +28,14 @@ describe('ParseBoolPipe', () => {
});
describe('when validation fails', () => {
it('should throw an error', async () => {
return expect(target.transform('123abc', {} as ArgumentMetadata)).to.be
.rejected;
return expect(
target.transform('123abc', {} as ArgumentMetadata),
).to.be.rejectedWith(BadRequestException);
});
it('should mention the field name in the error', async () => {
return expect(
target.transform('123abc', { data: 'foo' } as ArgumentMetadata),
).to.be.rejectedWith(/.*Validation failed.*"foo".*/);
});
});
});
Expand Down
23 changes: 19 additions & 4 deletions packages/common/test/pipes/parse-float.pipe.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as sinon from 'sinon';
import { expect } from 'chai';
import { ArgumentMetadata } from '../../interfaces';
import { ParseFloatPipe } from '../../pipes/parse-float.pipe';
import { HttpException } from '../../exceptions';
import { BadRequestException, HttpException } from '../../exceptions';

class CustomTestError extends HttpException {
constructor() {
Expand All @@ -13,9 +13,7 @@ class CustomTestError extends HttpException {
describe('ParseFloatPipe', () => {
let target: ParseFloatPipe;
beforeEach(() => {
target = new ParseFloatPipe({
exceptionFactory: (error: any) => new CustomTestError(),
});
target = new ParseFloatPipe();
});
describe('transform', () => {
describe('when validation passes', () => {
Expand All @@ -35,6 +33,23 @@ describe('ParseFloatPipe', () => {
it('should throw an error', async () => {
return expect(
target.transform('123.123abc', {} as ArgumentMetadata),
).to.be.rejectedWith(BadRequestException);
});
it('should mention the field name in the error', async () => {
return expect(
target.transform('123.123abc', { data: 'foo' } as ArgumentMetadata),
).to.be.rejectedWith(/.*Validation failed.*"foo".*/);
});
});
describe('with an exceptionFactory', () => {
beforeEach(() => {
target = new ParseFloatPipe({
exceptionFactory: (error: any) => new CustomTestError(),
});
});
it('uses it when the validation fails', async () => {
return expect(
target.transform('123abc', {} as ArgumentMetadata),
).to.be.rejectedWith(CustomTestError);
});
});
Expand Down
25 changes: 20 additions & 5 deletions packages/common/test/pipes/parse-int.pipe.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { expect } from 'chai';
import { ArgumentMetadata } from '../../interfaces';
import { ParseIntPipe } from '../../pipes/parse-int.pipe';
import { HttpException } from '../../exceptions';
import { BadRequestException, HttpException } from '../../exceptions';

class CustomTestError extends HttpException {
constructor() {
Expand All @@ -12,9 +12,7 @@ class CustomTestError extends HttpException {
describe('ParseIntPipe', () => {
let target: ParseIntPipe;
beforeEach(() => {
target = new ParseIntPipe({
exceptionFactory: (error: any) => new CustomTestError(),
});
target = new ParseIntPipe();
});
describe('transform', () => {
describe('when validation passes', () => {
Expand All @@ -37,14 +35,31 @@ describe('ParseIntPipe', () => {
});
});
describe('when validation fails', () => {
it('should mention the field name in the error', async () => {
return expect(
target.transform('123abc', { data: 'foo' } as ArgumentMetadata),
).to.be.rejectedWith(/.*Validation failed.*"foo".*/);
});
it('should throw an error', async () => {
return expect(
target.transform('123abc', {} as ArgumentMetadata),
).to.be.rejectedWith(CustomTestError);
).to.be.rejectedWith(BadRequestException);
});
it('should throw an error when number has wrong number encoding', async () => {
return expect(
target.transform('0xFF', {} as ArgumentMetadata),
).to.be.rejectedWith(BadRequestException);
});
});
describe('with an exceptionFactory', () => {
beforeEach(() => {
target = new ParseIntPipe({
exceptionFactory: (error: any) => new CustomTestError(),
});
});
it('uses it when the validation fails', async () => {
return expect(
target.transform('123abc', {} as ArgumentMetadata),
).to.be.rejectedWith(CustomTestError);
});
});
Expand Down
7 changes: 7 additions & 0 deletions packages/common/test/pipes/parse-uuid.pipe.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,13 @@ describe('ParseUUIDPipe', () => {
).to.be.rejectedWith(TestException);
});

it('should mention the field name in the error', async () => {
target = new ParseUUIDPipe();
await expect(
target.transform('123a', { data: 'foo' } as ArgumentMetadata),
).to.be.rejectedWith(/.*Validation failed.*"foo".*/);
});

it('should throw an error - not a string', async () => {
target = new ParseUUIDPipe({ exceptionFactory });
await expect(
Expand Down