Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ruleset-migrator): relax validation #2307

Merged
merged 1 commit into from
Oct 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 12 additions & 10 deletions packages/ruleset-migrator/src/__tests__/ruleset.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,16 +149,18 @@ describe('migrator', () => {
`);
});

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

it('should follow links correctly', async () => {
Expand Down
29 changes: 25 additions & 4 deletions packages/ruleset-migrator/src/transformers/formats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,36 @@ import { builders as b, namedTypes } from 'ast-types';
import { Transformer, TransformerCtx } from '../types';
import { assertArray, assertString } from '../validation';

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 FORMATS = [
'oas2',
'oas3',
'oas3.0',
'oas3.1',
'asyncapi2',
'json-schema',
'json-schema-loose',
'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',
];

function safeFormat(format: string): string {
return format
.replace(/\.|(?<=[0-9])-(?=[0-9])/g, '_')
.replace(/-([0-9a-z])/g, (match, char) => String(char).toUpperCase());
}

const REPLACEMENTS = Object.fromEntries(
schema.properties.formats.items.enum.map(format => [
FORMATS.map(format => [
format,
(ALIASES[format] ?? format)
.replace(/\.|(?<=[0-9])-(?=[0-9])/g, '_')
Expand All @@ -26,7 +47,7 @@ function transform(input: unknown, ctx: TransformerCtx): namedTypes.ArrayExpress
new Set(
input.map(format => {
assertString(format);
return ctx.tree.addImport(REPLACEMENTS[format], '@stoplight/spectral-formats');
return ctx.tree.addImport(REPLACEMENTS[format] ?? safeFormat(format), '@stoplight/spectral-formats');
}),
),
),
Expand Down
55 changes: 0 additions & 55 deletions packages/ruleset-migrator/src/validation/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,44 +6,6 @@ const schema = {
properties: {
aliases: {
type: 'object',
additionalProperties: {
oneOf: [
{
type: 'array',
items: {
type: 'string',
},
},
{
type: 'object',
properties: {
description: {
type: 'string',
},
targets: {
type: 'array',
minItems: 1,
items: {
type: 'object',
properties: {
formats: {
$ref: '#/properties/formats',
},
given: {
type: 'array',
items: {
type: 'string',
},
},
},
required: ['formats', 'given'],
},
},
},
required: ['targets'],
},
],
},
},
except: {
type: 'object',
Expand Down Expand Up @@ -85,25 +47,8 @@ const schema = {
},
formats: {
type: 'array',
minItems: 1,
items: {
type: 'string',
enum: [
'oas2',
'oas3',
'oas3.0',
'oas3.1',
'asyncapi2',
'json-schema',
'json-schema-loose',
'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: {
Expand Down
155 changes: 3 additions & 152 deletions packages/ruleset-migrator/src/validation/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,166 +2,17 @@

export interface Ruleset {
aliases?: {
[k: string]:
| string[]
| {
description?: string;
targets: [
{
formats: [
(
| 'oas2'
| 'oas3'
| 'oas3.0'
| 'oas3.1'
| 'asyncapi2'
| 'json-schema'
| 'json-schema-loose'
| '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'
),
...(
| 'oas2'
| 'oas3'
| 'oas3.0'
| 'oas3.1'
| 'asyncapi2'
| 'json-schema'
| 'json-schema-loose'
| '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'
)[]
];
given: string[];
[k: string]: unknown;
},
...{
formats: [
(
| 'oas2'
| 'oas3'
| 'oas3.0'
| 'oas3.1'
| 'asyncapi2'
| 'json-schema'
| 'json-schema-loose'
| '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'
),
...(
| 'oas2'
| 'oas3'
| 'oas3.0'
| 'oas3.1'
| 'asyncapi2'
| 'json-schema'
| 'json-schema-loose'
| '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'
)[]
];
given: string[];
[k: string]: unknown;
}[]
];
[k: string]: unknown;
};
[k: string]: unknown;
};
except?: {
[k: string]: string[];
};
extends?: string | (string | [string, 'all' | 'recommended' | 'off'])[];
formats?: [
(
| 'oas2'
| 'oas3'
| 'oas3.0'
| 'oas3.1'
| 'asyncapi2'
| 'json-schema'
| 'json-schema-loose'
| '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'
),
...(
| 'oas2'
| 'oas3'
| 'oas3.0'
| 'oas3.1'
| 'asyncapi2'
| 'json-schema'
| 'json-schema-loose'
| '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'
)[]
];
formats?: string[];
functions?: string[];
functionsDir?: string;
rules?: {
formats?: [
(
| 'oas2'
| 'oas3'
| 'oas3.0'
| 'oas3.1'
| 'asyncapi2'
| 'json-schema'
| 'json-schema-loose'
| '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'
),
...(
| 'oas2'
| 'oas3'
| 'oas3.0'
| 'oas3.1'
| 'asyncapi2'
| 'json-schema'
| 'json-schema-loose'
| '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'
)[]
];
formats?: string[];
[k: string]: unknown;
};
[k: string]: unknown;
Expand Down