-
-
Notifications
You must be signed in to change notification settings - Fork 242
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
- Loading branch information
Showing
20 changed files
with
1,410 additions
and
975 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/compiled.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
/* 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/v2.0.json', | ||
'oas/schemas/v3.0.json', | ||
'oas/schemas/v3.1/dialect.schema.json', | ||
'oas/schemas/v3.1/meta.schema.json', | ||
'oas/schemas/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, | ||
optimize: 1, | ||
}, | ||
}); | ||
|
||
ajvFormats(ajv); | ||
ajvErrors(ajv); | ||
|
||
const target = path.join(cwd, 'oas/schemas/compiled.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, | ||
}, | ||
ecma: 2020, | ||
module: true, | ||
mangle: { | ||
toplevel: true, | ||
module: true, | ||
}, | ||
format: { | ||
comments: false, | ||
}, | ||
}) | ||
).code!; | ||
|
||
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.