Skip to content

Commit ec60131

Browse files
authored
feat: support draft-2020-12 (#15)
1 parent ffeb0ff commit ec60131

File tree

3 files changed

+246
-8
lines changed

3 files changed

+246
-8
lines changed

__tests__/main.test.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -524,6 +524,38 @@ describe('action', () => {
524524
expect(core.setOutput).toHaveBeenLastCalledWith('valid', true);
525525
});
526526

527+
it('using JSON Schema draft-2019-09', async () => {
528+
vi.mocked(fs.readFile).mockResolvedValueOnce(
529+
schemaContents.replace(
530+
'http://json-schema.org/draft-07/schema#',
531+
'https://json-schema.org/draft/2019-09/schema'
532+
)
533+
);
534+
535+
await main.run();
536+
expect(runSpy).toHaveReturned();
537+
expect(process.exitCode).not.toBeDefined();
538+
539+
expect(core.setOutput).toHaveBeenCalledTimes(1);
540+
expect(core.setOutput).toHaveBeenLastCalledWith('valid', true);
541+
});
542+
543+
it('using JSON Schema draft-2020-12', async () => {
544+
vi.mocked(fs.readFile).mockResolvedValueOnce(
545+
schemaContents.replace(
546+
'http://json-schema.org/draft-07/schema#',
547+
'https://json-schema.org/draft/2020-12/schema'
548+
)
549+
);
550+
551+
await main.run();
552+
expect(runSpy).toHaveReturned();
553+
expect(process.exitCode).not.toBeDefined();
554+
555+
expect(core.setOutput).toHaveBeenCalledTimes(1);
556+
expect(core.setOutput).toHaveBeenLastCalledWith('valid', true);
557+
});
558+
527559
it('but fails if $schema key is missing', async () => {
528560
vi.mocked(fs.readFile).mockResolvedValueOnce(
529561
schemaContents.replace('$schema', '_schema')

dist/index.js

Lines changed: 199 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/main.ts

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,28 @@ import * as core from '@actions/core';
77
import * as glob from '@actions/glob';
88
import * as http from '@actions/http-client';
99

10-
import type { default as Ajv } from 'ajv';
11-
import { default as Ajv2019, ErrorObject } from 'ajv/dist/2019';
10+
import type { default as Ajv, ErrorObject } from 'ajv';
11+
import { default as Ajv2019 } from 'ajv/dist/2019';
12+
import { default as Ajv2020 } from 'ajv/dist/2020';
1213
import AjvDraft04 from 'ajv-draft-04';
1314
import AjvFormats from 'ajv-formats';
1415
import * as yaml from 'yaml';
1516

1617
function newAjv(schema: Record<string, unknown>): Ajv {
1718
const draft04Schema =
1819
schema.$schema === 'http://json-schema.org/draft-04/schema#';
19-
20-
const ajv = AjvFormats(draft04Schema ? new AjvDraft04() : new Ajv2019());
21-
22-
if (!draft04Schema) {
20+
const draft2020Schema =
21+
schema.$schema === 'https://json-schema.org/draft/2020-12/schema';
22+
23+
const ajv = AjvFormats(
24+
draft04Schema
25+
? new AjvDraft04()
26+
: draft2020Schema
27+
? new Ajv2020()
28+
: new Ajv2019()
29+
);
30+
31+
if (!draft04Schema && !draft2020Schema) {
2332
/* eslint-disable @typescript-eslint/no-require-imports, @typescript-eslint/no-var-requires */
2433
ajv.addMetaSchema(require('ajv/dist/refs/json-schema-draft-06.json'));
2534
ajv.addMetaSchema(require('ajv/dist/refs/json-schema-draft-07.json'));

0 commit comments

Comments
 (0)