Skip to content

Commit d5ce09e

Browse files
authored
feat(ruleset-migrator): relax validation (stoplightio#2307)
1 parent 9b50f88 commit d5ce09e

File tree

4 files changed

+40
-221
lines changed

4 files changed

+40
-221
lines changed

packages/ruleset-migrator/src/__tests__/ruleset.test.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -149,16 +149,18 @@ describe('migrator', () => {
149149
`);
150150
});
151151

152-
describe('error handling', () => {
153-
it('given unknown format, should throw', async () => {
154-
await vol.promises.writeFile(path.join(cwd, 'unknown-format.json'), `{ "formats": ["json-schema-draft-2"] }`);
155-
await expect(
156-
migrateRuleset(path.join(cwd, 'unknown-format.json'), {
157-
format: 'esm',
158-
fs: vol as any,
159-
}),
160-
).rejects.toThrow('Invalid ruleset provided');
161-
});
152+
it('given an unknown format, should not throw', async () => {
153+
await vol.promises.writeFile(path.join(cwd, 'unknown-format.json'), `{ "formats": ["json-schema-draft-2"] }`);
154+
await expect(
155+
migrateRuleset(path.join(cwd, 'unknown-format.json'), {
156+
format: 'esm',
157+
fs: vol as any,
158+
}),
159+
).resolves.toEqual(`import {jsonSchemaDraft2} from "@stoplight/spectral-formats";
160+
export default {
161+
"formats": [jsonSchemaDraft2]
162+
};
163+
`);
162164
});
163165

164166
it('should follow links correctly', async () => {

packages/ruleset-migrator/src/transformers/formats.ts

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,36 @@ import { builders as b, namedTypes } from 'ast-types';
22
import { Transformer, TransformerCtx } from '../types';
33
import { assertArray, assertString } from '../validation';
44

5-
import schema from '../validation/schema';
6-
75
const ALIASES: Record<string, string> = {
86
'json-schema-2019-09': 'json-schema-draft-2019-09',
97
'json-schema-2020-12': 'json-schema-draft-2020-12',
108
};
119

10+
const FORMATS = [
11+
'oas2',
12+
'oas3',
13+
'oas3.0',
14+
'oas3.1',
15+
'asyncapi2',
16+
'json-schema',
17+
'json-schema-loose',
18+
'json-schema-draft4',
19+
'json-schema-draft6',
20+
'json-schema-draft7',
21+
'json-schema-draft-2019-09',
22+
'json-schema-2019-09',
23+
'json-schema-draft-2020-12',
24+
'json-schema-2020-12',
25+
];
26+
27+
function safeFormat(format: string): string {
28+
return format
29+
.replace(/\.|(?<=[0-9])-(?=[0-9])/g, '_')
30+
.replace(/-([0-9a-z])/g, (match, char) => String(char).toUpperCase());
31+
}
32+
1233
const REPLACEMENTS = Object.fromEntries(
13-
schema.properties.formats.items.enum.map(format => [
34+
FORMATS.map(format => [
1435
format,
1536
(ALIASES[format] ?? format)
1637
.replace(/\.|(?<=[0-9])-(?=[0-9])/g, '_')
@@ -26,7 +47,7 @@ function transform(input: unknown, ctx: TransformerCtx): namedTypes.ArrayExpress
2647
new Set(
2748
input.map(format => {
2849
assertString(format);
29-
return ctx.tree.addImport(REPLACEMENTS[format], '@stoplight/spectral-formats');
50+
return ctx.tree.addImport(REPLACEMENTS[format] ?? safeFormat(format), '@stoplight/spectral-formats');
3051
}),
3152
),
3253
),

packages/ruleset-migrator/src/validation/schema.ts

Lines changed: 0 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -6,44 +6,6 @@ const schema = {
66
properties: {
77
aliases: {
88
type: 'object',
9-
additionalProperties: {
10-
oneOf: [
11-
{
12-
type: 'array',
13-
items: {
14-
type: 'string',
15-
},
16-
},
17-
{
18-
type: 'object',
19-
properties: {
20-
description: {
21-
type: 'string',
22-
},
23-
targets: {
24-
type: 'array',
25-
minItems: 1,
26-
items: {
27-
type: 'object',
28-
properties: {
29-
formats: {
30-
$ref: '#/properties/formats',
31-
},
32-
given: {
33-
type: 'array',
34-
items: {
35-
type: 'string',
36-
},
37-
},
38-
},
39-
required: ['formats', 'given'],
40-
},
41-
},
42-
},
43-
required: ['targets'],
44-
},
45-
],
46-
},
479
},
4810
except: {
4911
type: 'object',
@@ -85,25 +47,8 @@ const schema = {
8547
},
8648
formats: {
8749
type: 'array',
88-
minItems: 1,
8950
items: {
9051
type: 'string',
91-
enum: [
92-
'oas2',
93-
'oas3',
94-
'oas3.0',
95-
'oas3.1',
96-
'asyncapi2',
97-
'json-schema',
98-
'json-schema-loose',
99-
'json-schema-draft4',
100-
'json-schema-draft6',
101-
'json-schema-draft7',
102-
'json-schema-draft-2019-09',
103-
'json-schema-2019-09',
104-
'json-schema-draft-2020-12',
105-
'json-schema-2020-12',
106-
],
10752
},
10853
},
10954
functions: {

packages/ruleset-migrator/src/validation/types.ts

Lines changed: 3 additions & 152 deletions
Original file line numberDiff line numberDiff line change
@@ -2,166 +2,17 @@
22

33
export interface Ruleset {
44
aliases?: {
5-
[k: string]:
6-
| string[]
7-
| {
8-
description?: string;
9-
targets: [
10-
{
11-
formats: [
12-
(
13-
| 'oas2'
14-
| 'oas3'
15-
| 'oas3.0'
16-
| 'oas3.1'
17-
| 'asyncapi2'
18-
| 'json-schema'
19-
| 'json-schema-loose'
20-
| 'json-schema-draft4'
21-
| 'json-schema-draft6'
22-
| 'json-schema-draft7'
23-
| 'json-schema-draft-2019-09'
24-
| 'json-schema-2019-09'
25-
| 'json-schema-draft-2020-12'
26-
| 'json-schema-2020-12'
27-
),
28-
...(
29-
| 'oas2'
30-
| 'oas3'
31-
| 'oas3.0'
32-
| 'oas3.1'
33-
| 'asyncapi2'
34-
| 'json-schema'
35-
| 'json-schema-loose'
36-
| 'json-schema-draft4'
37-
| 'json-schema-draft6'
38-
| 'json-schema-draft7'
39-
| 'json-schema-draft-2019-09'
40-
| 'json-schema-2019-09'
41-
| 'json-schema-draft-2020-12'
42-
| 'json-schema-2020-12'
43-
)[]
44-
];
45-
given: string[];
46-
[k: string]: unknown;
47-
},
48-
...{
49-
formats: [
50-
(
51-
| 'oas2'
52-
| 'oas3'
53-
| 'oas3.0'
54-
| 'oas3.1'
55-
| 'asyncapi2'
56-
| 'json-schema'
57-
| 'json-schema-loose'
58-
| 'json-schema-draft4'
59-
| 'json-schema-draft6'
60-
| 'json-schema-draft7'
61-
| 'json-schema-draft-2019-09'
62-
| 'json-schema-2019-09'
63-
| 'json-schema-draft-2020-12'
64-
| 'json-schema-2020-12'
65-
),
66-
...(
67-
| 'oas2'
68-
| 'oas3'
69-
| 'oas3.0'
70-
| 'oas3.1'
71-
| 'asyncapi2'
72-
| 'json-schema'
73-
| 'json-schema-loose'
74-
| 'json-schema-draft4'
75-
| 'json-schema-draft6'
76-
| 'json-schema-draft7'
77-
| 'json-schema-draft-2019-09'
78-
| 'json-schema-2019-09'
79-
| 'json-schema-draft-2020-12'
80-
| 'json-schema-2020-12'
81-
)[]
82-
];
83-
given: string[];
84-
[k: string]: unknown;
85-
}[]
86-
];
87-
[k: string]: unknown;
88-
};
5+
[k: string]: unknown;
896
};
907
except?: {
918
[k: string]: string[];
929
};
9310
extends?: string | (string | [string, 'all' | 'recommended' | 'off'])[];
94-
formats?: [
95-
(
96-
| 'oas2'
97-
| 'oas3'
98-
| 'oas3.0'
99-
| 'oas3.1'
100-
| 'asyncapi2'
101-
| 'json-schema'
102-
| 'json-schema-loose'
103-
| 'json-schema-draft4'
104-
| 'json-schema-draft6'
105-
| 'json-schema-draft7'
106-
| 'json-schema-draft-2019-09'
107-
| 'json-schema-2019-09'
108-
| 'json-schema-draft-2020-12'
109-
| 'json-schema-2020-12'
110-
),
111-
...(
112-
| 'oas2'
113-
| 'oas3'
114-
| 'oas3.0'
115-
| 'oas3.1'
116-
| 'asyncapi2'
117-
| 'json-schema'
118-
| 'json-schema-loose'
119-
| 'json-schema-draft4'
120-
| 'json-schema-draft6'
121-
| 'json-schema-draft7'
122-
| 'json-schema-draft-2019-09'
123-
| 'json-schema-2019-09'
124-
| 'json-schema-draft-2020-12'
125-
| 'json-schema-2020-12'
126-
)[]
127-
];
11+
formats?: string[];
12812
functions?: string[];
12913
functionsDir?: string;
13014
rules?: {
131-
formats?: [
132-
(
133-
| 'oas2'
134-
| 'oas3'
135-
| 'oas3.0'
136-
| 'oas3.1'
137-
| 'asyncapi2'
138-
| 'json-schema'
139-
| 'json-schema-loose'
140-
| 'json-schema-draft4'
141-
| 'json-schema-draft6'
142-
| 'json-schema-draft7'
143-
| 'json-schema-draft-2019-09'
144-
| 'json-schema-2019-09'
145-
| 'json-schema-draft-2020-12'
146-
| 'json-schema-2020-12'
147-
),
148-
...(
149-
| 'oas2'
150-
| 'oas3'
151-
| 'oas3.0'
152-
| 'oas3.1'
153-
| 'asyncapi2'
154-
| 'json-schema'
155-
| 'json-schema-loose'
156-
| 'json-schema-draft4'
157-
| 'json-schema-draft6'
158-
| 'json-schema-draft7'
159-
| 'json-schema-draft-2019-09'
160-
| 'json-schema-2019-09'
161-
| 'json-schema-draft-2020-12'
162-
| 'json-schema-2020-12'
163-
)[]
164-
];
15+
formats?: string[];
16516
[k: string]: unknown;
16617
};
16718
[k: string]: unknown;

0 commit comments

Comments
 (0)