Skip to content

Commit

Permalink
Add example keyword to AJV
Browse files Browse the repository at this point in the history
  • Loading branch information
rzhang2atl committed Feb 11, 2025
1 parent bc9d442 commit de4197f
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
53 changes: 53 additions & 0 deletions __test__/chow-strict.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,57 @@ describe('strict mode', () => {
);
warnSpy.mockRestore();
});

it('should not log warning about example keyword', async () => {
const warnSpy = jest.spyOn(console, 'warn').mockImplementation();
const doc: OpenAPIObject = {
openapi: '3.0.1',
info: {
title: 'service open api spec',
version: '1.0.1',
},
components: {
schemas: {
ResolveUnsupportedError: {
type: 'object',
description: 'some description',
properties: {
error: {
type: 'object',
properties: {
message: {
type: 'string',
example: 'some example',
},
},
},
},
},
},
},
paths: {
'/resolve': {
post: {
operationId: 'resolve',
responses: {
'404': {
content: {
'application/json': {
schema: {
$ref: '#/components/schemas/ResolveUnsupportedError',
},
},
},
},
},
},
},
},
};
expect(await ChowChow.create(doc)).toBeDefined();
expect(warnSpy).not.toHaveBeenCalledWith(
'strict mode: unknown keyword: "example"'
);
warnSpy.mockRestore();
});
});
6 changes: 6 additions & 0 deletions src/compiler/CompiledSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ export default class CompiledSchema {
validate: (schema: any) =>
schema ? context.schemaContext === 'response' : true,
});
ajvInstance.addKeyword({
keyword: 'example',
metaSchema: {
type: ['object', 'array', 'string', 'number', 'boolean', 'null'],
},
});
this.validator = ajvInstance.compile(schemaObject);
}

Expand Down

0 comments on commit de4197f

Please sign in to comment.