Skip to content

Commit

Permalink
feat: support draft-2020-12 (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
dsanders11 authored Jun 18, 2024
1 parent ffeb0ff commit ec60131
Show file tree
Hide file tree
Showing 3 changed files with 246 additions and 8 deletions.
32 changes: 32 additions & 0 deletions __tests__/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,38 @@ describe('action', () => {
expect(core.setOutput).toHaveBeenLastCalledWith('valid', true);
});

it('using JSON Schema draft-2019-09', async () => {
vi.mocked(fs.readFile).mockResolvedValueOnce(
schemaContents.replace(
'http://json-schema.org/draft-07/schema#',
'https://json-schema.org/draft/2019-09/schema'
)
);

await main.run();
expect(runSpy).toHaveReturned();
expect(process.exitCode).not.toBeDefined();

expect(core.setOutput).toHaveBeenCalledTimes(1);
expect(core.setOutput).toHaveBeenLastCalledWith('valid', true);
});

it('using JSON Schema draft-2020-12', async () => {
vi.mocked(fs.readFile).mockResolvedValueOnce(
schemaContents.replace(
'http://json-schema.org/draft-07/schema#',
'https://json-schema.org/draft/2020-12/schema'
)
);

await main.run();
expect(runSpy).toHaveReturned();
expect(process.exitCode).not.toBeDefined();

expect(core.setOutput).toHaveBeenCalledTimes(1);
expect(core.setOutput).toHaveBeenLastCalledWith('valid', true);
});

it('but fails if $schema key is missing', async () => {
vi.mocked(fs.readFile).mockResolvedValueOnce(
schemaContents.replace('$schema', '_schema')
Expand Down
201 changes: 199 additions & 2 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 15 additions & 6 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,28 @@ import * as core from '@actions/core';
import * as glob from '@actions/glob';
import * as http from '@actions/http-client';

import type { default as Ajv } from 'ajv';
import { default as Ajv2019, ErrorObject } from 'ajv/dist/2019';
import type { default as Ajv, ErrorObject } from 'ajv';
import { default as Ajv2019 } from 'ajv/dist/2019';
import { default as Ajv2020 } from 'ajv/dist/2020';
import AjvDraft04 from 'ajv-draft-04';
import AjvFormats from 'ajv-formats';
import * as yaml from 'yaml';

function newAjv(schema: Record<string, unknown>): Ajv {
const draft04Schema =
schema.$schema === 'http://json-schema.org/draft-04/schema#';

const ajv = AjvFormats(draft04Schema ? new AjvDraft04() : new Ajv2019());

if (!draft04Schema) {
const draft2020Schema =
schema.$schema === 'https://json-schema.org/draft/2020-12/schema';

const ajv = AjvFormats(
draft04Schema
? new AjvDraft04()
: draft2020Schema
? new Ajv2020()
: new Ajv2019()
);

if (!draft04Schema && !draft2020Schema) {
/* eslint-disable @typescript-eslint/no-require-imports, @typescript-eslint/no-var-requires */
ajv.addMetaSchema(require('ajv/dist/refs/json-schema-draft-06.json'));
ajv.addMetaSchema(require('ajv/dist/refs/json-schema-draft-07.json'));
Expand Down

0 comments on commit ec60131

Please sign in to comment.