Skip to content

Commit

Permalink
feat(rulesets): improve {oas2,oas3}-valid-schema rule
Browse files Browse the repository at this point in the history
  • Loading branch information
P0lip committed Jan 12, 2024
1 parent 8d31c1a commit bf5918c
Show file tree
Hide file tree
Showing 23 changed files with 1,727 additions and 986 deletions.
2 changes: 1 addition & 1 deletion .eslintignore
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ node_modules
!.yarn/versions

packages/formatters/src/html/templates.ts
packages/rulesets/src/oas/schemas/compiled.ts
packages/cli/binaries
packages/*/src/version.ts
/test-harness/tmp/
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
"node": "^12.20 || >= 14.13"
},
"scripts": {
"clean": "rimraf .cache packages/*/{dist,.cache}",
"preclean": "yarn workspaces foreach run preclean",
"clean": "yarn preclean && rimraf .cache packages/*/{dist,.cache}",
"prebuild": "yarn workspaces foreach run prebuild",
"build": "yarn prebuild && tsc --build ./tsconfig.build.json && yarn postbuild",
"postbuild": "yarn workspaces foreach run postbuild",
Expand Down
32 changes: 28 additions & 4 deletions packages/rulesets/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,26 @@
"node": ">=12"
},
"license": "Apache-2.0",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"files": [
"/dist"
],
"type": "commonjs",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"exports": {
".": {
"types": "./dist/index.d.ts",
"default": "./dist/index.js"
},
"./oas": {
"types": "./dist/oas/index.d.ts",
"default": "./dist/oas/index.js"
},
"./asyncapi": {
"types": "./dist/asyncapi/index.d.ts",
"default": "./dist/asyncapi/index.js"
}
},
"repository": {
"type": "git",
"url": "https://github.com/stoplightio/spectral.git"
Expand All @@ -27,16 +42,25 @@
"@stoplight/spectral-runtime": "^1.1.1",
"@stoplight/types": "^13.6.0",
"@types/json-schema": "^7.0.7",
"ajv": "^8.8.2",
"ajv": "^8.12.0",
"ajv-formats": "~2.1.0",
"json-schema-traverse": "^1.0.0",
"leven": "3.1.0",
"lodash": "~4.17.21",
"tslib": "^2.3.0"
},
"devDependencies": {
"@stoplight/path": "^1.3.2",
"@stoplight/spectral-parsers": "*",
"@stoplight/spectral-ref-resolver": "*",
"immer": "^9.0.6"
"gzip-size": "^6.0.0",
"immer": "^9.0.6",
"terser": "^5.26.0"
},
"scripts": {
"compile-schemas": "ts-node -T ./scripts/compile-schemas.ts",
"prelint": "yarn compile-schemas --quiet",
"pretest": "yarn compile-schemas --quiet",
"prebuild": "yarn compile-schemas --quiet"
}
}
93 changes: 93 additions & 0 deletions packages/rulesets/scripts/compile-schemas.ts
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/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,
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);
});
32 changes: 32 additions & 0 deletions packages/rulesets/src/oas/__tests__/oas2-schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,36 @@ testRule('oas2-schema', [
},
],
},

{
name: 'validate security definitions',
document: {
swagger: '2.0',
info: {
title: 'response example',
version: '1.0',
},
paths: {
'/user': {
get: {
responses: {
200: {
description: 'dummy description',
},
},
},
},
},
securityDefinitions: {
basic: null,
},
},
errors: [
{
message: 'Invalid basic authentication security definition.',
path: ['securityDefinitions', 'basic'],
severity: DiagnosticSeverity.Error,
},
],
},
]);

0 comments on commit bf5918c

Please sign in to comment.