-
-
Notifications
You must be signed in to change notification settings - Fork 240
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(rulesets): improve {oas2,oas3}-valid-schema rule (#2574)
* feat(rulesets): improve {oas2,oas3}-valid-schema rule * build(rulesets): try sth * build(rulesets): abandon props mangling * feat(rulesets): improve oas3.1 schema * docs(repo): mention oas3-schema * chore(rulesets): remove ajv-merge-patch dep --------- Co-authored-by: Nauman <[email protected]>
- Loading branch information
1 parent
a851c98
commit 8df2c36
Showing
28 changed files
with
2,126 additions
and
989 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
**/__fixtures__/** | ||
/test-harness/**/*.yaml | ||
/test-harness/tests/ | ||
/packages/*/dist | ||
/packages/rulesets/src/oas/schemas/validators.ts | ||
/packages/*/CHANGELOG.md | ||
packages/formatters/src/html/templates.ts |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/* eslint-disable no-console */ | ||
import * as fs from 'fs'; | ||
import * as path from 'path'; | ||
import * as process from 'process'; | ||
import Ajv2020 from 'ajv/dist/2020.js'; | ||
import standaloneCode from 'ajv/dist/standalone/index.js'; | ||
import ajvErrors from 'ajv-errors'; | ||
import ajvFormats from 'ajv-formats'; | ||
import chalk from 'chalk'; | ||
import { minify } from 'terser'; | ||
import { sync } from 'gzip-size'; | ||
|
||
const cwd = path.join(__dirname, '../src'); | ||
|
||
const schemas = [ | ||
'oas/schemas/json-schema/draft-04.json', | ||
'oas/schemas/json-schema/draft-2020-12/index.json', | ||
'oas/schemas/json-schema/draft-2020-12/validation.json', | ||
'oas/schemas/oas/v2.0.json', | ||
'oas/schemas/oas/v3.0.json', | ||
'oas/schemas/oas/v3.1/dialect.schema.json', | ||
'oas/schemas/oas/v3.1/meta.schema.json', | ||
'oas/schemas/oas/v3.1/index.json', | ||
].map(async schema => JSON.parse(await fs.promises.readFile(path.join(cwd, schema), 'utf8'))); | ||
|
||
const log = process.argv.includes('--quiet') | ||
? (): void => { | ||
/* no-op */ | ||
} | ||
: console.log.bind(console); | ||
|
||
Promise.all(schemas) | ||
.then(async schemas => { | ||
const ajv = new Ajv2020({ | ||
schemas, | ||
allErrors: true, | ||
messages: true, | ||
strict: false, | ||
inlineRefs: false, | ||
formats: { | ||
'media-range': true, | ||
}, | ||
code: { | ||
esm: true, | ||
source: true, | ||
}, | ||
}); | ||
|
||
ajvFormats(ajv); | ||
ajvErrors(ajv); | ||
|
||
const target = path.join(cwd, 'oas/schemas/validators.ts'); | ||
const basename = path.basename(target); | ||
const code = standaloneCode(ajv, { | ||
oas2_0: 'http://swagger.io/v2/schema.json', | ||
oas3_0: 'https://spec.openapis.org/oas/3.0/schema/2019-04-02', | ||
oas3_1: 'https://spec.openapis.org/oas/3.1/schema/2021-09-28', | ||
}); | ||
|
||
const minified = ( | ||
await minify(code, { | ||
compress: { | ||
passes: 2, | ||
}, | ||
ecma: 2020, | ||
module: true, | ||
format: { | ||
comments: false, | ||
}, | ||
}) | ||
).code as string; | ||
|
||
log( | ||
'writing %s size is %dKB (original), %dKB (minified) %dKB (minified + gzipped)', | ||
path.join(target, '..', basename), | ||
Math.round((code.length / 1024) * 100) / 100, | ||
Math.round((minified.length / 1024) * 100) / 100, | ||
Math.round((sync(minified) / 1024) * 100) / 100, | ||
); | ||
|
||
await fs.promises.writeFile(path.join(target, '..', basename), ['// @ts-nocheck', minified].join('\n')); | ||
}) | ||
.then(() => { | ||
log(chalk.green('Validators generated.')); | ||
}) | ||
.catch(e => { | ||
console.error(chalk.red('Error generating validators %s'), e.message); | ||
process.exit(1); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.