Skip to content

Commit

Permalink
fix(validator): property names in custom error messages (#63)
Browse files Browse the repository at this point in the history
fixes #50
  • Loading branch information
pmstss authored Dec 6, 2021
1 parent a7f1114 commit 51bb83f
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 0 deletions.
2 changes: 2 additions & 0 deletions packages/validator/src/humanizer/ErrorHumanizer.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
ArrayFormatter,
CustomErrorMessageFormatter,
Formatter,
NumericFormatter,
ObjectFormatter,
Expand All @@ -14,6 +15,7 @@ import { ErrorObject } from 'ajv';
export class ErrorHumanizer {
private readonly formatters: ReadonlyArray<Formatter> = [
new ArrayFormatter(),
new CustomErrorMessageFormatter(),
new NumericFormatter(),
new ObjectFormatter(),
new StringFormatter(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Formatter } from './Formatter';
import { WordingHelper } from './WordingHelper';
import { ErrorObject } from 'ajv';

export class CustomErrorMessageFormatter implements Formatter {
public format(error: ErrorObject<'errorMessage'>): string {
if (error.keyword !== 'errorMessage') {
return;
}

const propName = WordingHelper.extractPropertyName(error.instancePath);

return error.message.replace(
/^The property must/,
`The property \`${propName}\` must`
);
}
}
1 change: 1 addition & 0 deletions packages/validator/src/humanizer/formatters/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export { ArrayFormatter } from './ArrayFormatter';
export { CustomErrorMessageFormatter } from './CustomErrorMessageFormatter';
export { Formatter } from './Formatter';
export { NumericFormatter } from './NumericFormatter';
export { ObjectFormatter } from './ObjectFormatter';
Expand Down
43 changes: 43 additions & 0 deletions packages/validator/tests/error-humanizer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,49 @@ describe('ErrorHumanizer', () => {
result.should.deep.eq([expected]);
});

it('should parameterize original "errorMessage" if necessary', async () => {
const input: OpenAPIV3.Document = {
...getBaseOasDoc(),
components: {
headers: {
CustomHeader: {
schema: {},
content: {
'application/json': {
example: 42
}
}
}
}
}
};

const expected = {
message:
'Error at /components/headers/CustomHeader: The property `CustomHeader` must have either a `schema` or `content` option',
locationParts: [
{
text: 'Error at'
},
{
text: '/components/headers/CustomHeader',
jsonPointer: '/components/headers/CustomHeader'
}
],
messageParts: [
{
text: 'The property `CustomHeader` must have either a `schema` or `content` option'
}
]
};

const result = humanizer
.humanizeErrors(await oasValidator.verify(input))
.map(({ originalError, ...rest }) => ({ ...rest }));

result.should.deep.eq([expected]);
});

it('should humanize "required" error on root path', async () => {
const { host, ...input } = getBaseSwaggerDoc();

Expand Down

0 comments on commit 51bb83f

Please sign in to comment.