Skip to content

Commit

Permalink
fix(ruleset-migrator): invalid values for JSON Schema drafts 2019 & 2…
Browse files Browse the repository at this point in the history
…020 (#1756)

* fix(ruleset-migrator): invalid values for JSON Schema drafts 2019 & 2020

* feat(ruleset-migrator): alias json-schema-2019-09 and json-schema-2020-12
  • Loading branch information
P0lip committed Jul 26, 2021
1 parent 894615b commit 4b54443
Show file tree
Hide file tree
Showing 12 changed files with 78 additions and 31 deletions.
3 changes: 2 additions & 1 deletion packages/ruleset-migrator/scripts/compile-schemas.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import * as fs from 'fs';
import * as path from '@stoplight/path';
import { compile } from 'json-schema-to-typescript';
import type { JSONSchema4 } from 'json-schema';

import schema from '../src/validation/schema';

compile(schema, 'Ruleset', {
compile(<JSONSchema4>schema, 'Ruleset', {
bannerComment: '/*eslint-disable*/',
style: {
singleQuote: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ const {
oas3_1: oas3_1,
oas3_0: oas3_0,
jsonSchemaLoose: jsonSchemaLoose,
jsonSchemaDraft2019_09: jsonSchemaDraft2019_09,
} = require('@stoplight/spectral-formats');
module.exports = {
formats: [oas2, oas3_1, oas3_0, jsonSchemaLoose],
formats: [oas2, oas3_1, oas3_0, jsonSchemaLoose, jsonSchemaDraft2019_09],
rules: {},
};
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { oas2, oas3_1, oas3_0, jsonSchemaLoose } from '@stoplight/spectral-formats';
import { oas2, oas3_1, oas3_0, jsonSchemaLoose, jsonSchemaDraft2019_09 } from '@stoplight/spectral-formats';
export default {
formats: [oas2, oas3_1, oas3_0, jsonSchemaLoose],
formats: [oas2, oas3_1, oas3_0, jsonSchemaLoose, jsonSchemaDraft2019_09],
rules: {},
};
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
formats: [oas2, oas3.1, oas3.0, json-schema-loose]
formats: [oas2, oas3.1, oas3.0, json-schema-loose, json-schema-draft-2019-09]
rules: {}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const {
} = require('@stoplight/spectral-formats');
const { truthy: truthy } = require('@stoplight/spectral-functions');
module.exports = {
formats: [oas2, oas3_1, oas3_0, jsonSchemaLoose, jsonSchemaLoose],
formats: [oas2, oas3_1, oas3_0, jsonSchemaLoose],
rules: {
test: {
given: '$',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { oas2, oas3_1, oas3_0, jsonSchemaLoose, asyncapi2, oas3 } from '@stoplight/spectral-formats';
import { truthy } from '@stoplight/spectral-functions';
export default {
formats: [oas2, oas3_1, oas3_0, jsonSchemaLoose, jsonSchemaLoose],
formats: [oas2, oas3_1, oas3_0, jsonSchemaLoose],
rules: {
test: {
given: '$',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const {
jsonSchemaDraft2019_09: jsonSchemaDraft2019_09,
jsonSchemaDraft2020_12: jsonSchemaDraft2020_12,
} = require('@stoplight/spectral-formats');
const { truthy: truthy } = require('@stoplight/spectral-functions');
module.exports = {
formats: [jsonSchemaDraft2019_09, jsonSchemaDraft2020_12],
rules: {
test: {
given: '$',
then: {
function: truthy,
},
},
},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { jsonSchemaDraft2019_09, jsonSchemaDraft2020_12 } from '@stoplight/spectral-formats';
import { truthy } from '@stoplight/spectral-functions';
export default {
formats: [jsonSchemaDraft2019_09, jsonSchemaDraft2020_12],
rules: {
test: {
given: '$',
then: {
function: truthy,
},
},
},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
formats:
- json-schema-2019-09
- json-schema-draft-2019-09
- json-schema-draft-2020-12
- json-schema-2020-12
rules:
test:
given: $
then:
function: truthy
44 changes: 23 additions & 21 deletions packages/ruleset-migrator/src/transformers/formats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,34 @@ import { builders as b, namedTypes } from 'ast-types';
import { Transformer, TransformerCtx } from '../types';
import { assertArray, assertString } from '../validation';

const REPLACEMENTS = [
'oas2',
'oas3',
'oas3.0',
'oas3.1',
'asyncapi2',
'json-schema',
'json-schema-loose',
'json-schema-draft4',
'json-schema-draft6',
'json-schema-draft7',
'json-schema-2019-09',
'json-schema-2020-12',
].reduce((replacements, id) => {
replacements[id] = id.replace(/\./g, '_').replace(/-([a-z])/g, (match, char) => String(char).toUpperCase());
return replacements;
}, {});
import schema from '../validation/schema';

const ALIASES: Record<string, string> = {
'json-schema-2019-09': 'json-schema-draft-2019-09',
'json-schema-2020-12': 'json-schema-draft-2020-12',
};

const REPLACEMENTS = Object.fromEntries(
schema.properties.formats.items.enum.map(format => [
format,
(ALIASES[format] ?? format)
.replace(/\.|(?<=[0-9])-(?=[0-9])/g, '_')
.replace(/-([0-9a-z])/g, (match, char) => String(char).toUpperCase()),
]),
);

function transform(ctx: TransformerCtx, input: unknown): namedTypes.ArrayExpression {
assertArray(input);

return b.arrayExpression(
input.map(format => {
assertString(format);
return ctx.tree.addImport(REPLACEMENTS[format], '@stoplight/spectral-formats');
}),
Array.from(
new Set(
input.map(format => {
assertString(format);
return ctx.tree.addImport(REPLACEMENTS[format], '@stoplight/spectral-formats');
}),
),
),
);
}

Expand Down
6 changes: 3 additions & 3 deletions packages/ruleset-migrator/src/validation/schema.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import type { JSONSchema4 } from 'json-schema';

export { schema as default };

const schema: JSONSchema4 = {
const schema = {
$schema: 'http://json-schema.org/draft-07/schema#',
type: 'object',
properties: {
Expand Down Expand Up @@ -59,7 +57,9 @@ const schema: JSONSchema4 = {
'json-schema-draft4',
'json-schema-draft6',
'json-schema-draft7',
'json-schema-draft-2019-09',
'json-schema-2019-09',
'json-schema-draft-2020-12',
'json-schema-2020-12',
],
},
Expand Down
4 changes: 4 additions & 0 deletions packages/ruleset-migrator/src/validation/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ export interface Ruleset {
| 'json-schema-draft4'
| 'json-schema-draft6'
| 'json-schema-draft7'
| 'json-schema-draft-2019-09'
| 'json-schema-2019-09'
| 'json-schema-draft-2020-12'
| 'json-schema-2020-12'
)[];
functions?: string[];
Expand All @@ -33,7 +35,9 @@ export interface Ruleset {
| 'json-schema-draft4'
| 'json-schema-draft6'
| 'json-schema-draft7'
| 'json-schema-draft-2019-09'
| 'json-schema-2019-09'
| 'json-schema-draft-2020-12'
| 'json-schema-2020-12'
)[];
[k: string]: unknown;
Expand Down

0 comments on commit 4b54443

Please sign in to comment.