Skip to content

Commit

Permalink
fix(schema): support draft 6 and 7
Browse files Browse the repository at this point in the history
  • Loading branch information
P0lip committed Sep 20, 2019
1 parent 06b402a commit ea2ddff
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/functions/__tests__/schema.test.ts
Expand Up @@ -144,7 +144,7 @@ describe('schema', () => {
});
});

describe('handles duplicate JSONSchema4 ids', () => {
test('handles duplicate JSONSchema Draft 4 ids', () => {
const testSchema: JSONSchema4 = {
id: 'test',
type: 'string',
Expand All @@ -164,7 +164,7 @@ describe('schema', () => {
expect(runSchema('a', testSchema2)).toEqual([]);
});

describe('handles duplicate JSONSchema6 ids', () => {
test('handles duplicate JSONSchema Draft 6 and 7 $ids', () => {
const testSchema: JSONSchema6 = {
$id: 'test',
type: 'string',
Expand All @@ -183,4 +183,13 @@ describe('schema', () => {
]);
expect(runSchema('a', testSchema2)).toEqual([]);
});

test.each([4, 6, 7])('accepts draft %d', draft => {
const testSchema: JSONSchema6 = {
$schema: `http://json-schema.org/draft-0${draft}/schema#`,
type: 'string',
};

expect(runSchema.bind(null, 'd', testSchema)).not.toThrow();
});
});
4 changes: 4 additions & 0 deletions src/functions/schema.ts
@@ -1,6 +1,8 @@
import * as AJV from 'ajv';
import { ValidateFunction } from 'ajv';
import * as jsonSpecv4 from 'ajv/lib/refs/json-schema-draft-04.json';
import * as jsonSpecv6 from 'ajv/lib/refs/json-schema-draft-06.json';
import * as jsonSpecv7 from 'ajv/lib/refs/json-schema-draft-07.json';
import { IOutputError } from 'better-ajv-errors';
import { JSONSchema4, JSONSchema6 } from 'json-schema';
import { IFunction, IFunctionResult, ISchemaOptions } from '../types';
Expand Down Expand Up @@ -31,6 +33,8 @@ const ajv = new AJV({
logger,
});
ajv.addMetaSchema(jsonSpecv4);
ajv.addMetaSchema(jsonSpecv6);
ajv.addMetaSchema(jsonSpecv7);
// @ts-ignore
ajv._opts.defaultMeta = jsonSpecv4.id;
// @ts-ignore
Expand Down

0 comments on commit ea2ddff

Please sign in to comment.