-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement YAML parser adapter for JSON Schema 2020-12
Refs #1868
- Loading branch information
Showing
21 changed files
with
981 additions
and
0 deletions.
There are no files selected for viewing
8 changes: 8 additions & 0 deletions
8
packages/apidom-parser-adapter-json-schema-yaml-2020-12/.eslintignore
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,8 @@ | ||
/**/*.js | ||
/**/*.mjs | ||
/**/*.cjs | ||
/dist | ||
/types | ||
/config | ||
/.nyc_output | ||
/node_modules |
7 changes: 7 additions & 0 deletions
7
packages/apidom-parser-adapter-json-schema-yaml-2020-12/.gitignore
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,7 @@ | ||
/src/**/*.mjs | ||
/src/**/*.cjs | ||
/test/**/*.mjs | ||
/dist | ||
/types | ||
/NOTICE | ||
/swagger-api-apidom-parser-adapter-json-schema-yaml-2020-12-*.tgz |
6 changes: 6 additions & 0 deletions
6
packages/apidom-parser-adapter-json-schema-yaml-2020-12/.mocharc.json
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,6 @@ | ||
{ | ||
"recursive": true, | ||
"spec": "test/**/*.mjs", | ||
"file": ["test/mocha-bootstrap.mjs"], | ||
"ignore": ["test/perf/**/*.mjs"] | ||
} |
2 changes: 2 additions & 0 deletions
2
packages/apidom-parser-adapter-json-schema-yaml-2020-12/.npmrc
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,2 @@ | ||
save-prefix="=" | ||
save=false |
90 changes: 90 additions & 0 deletions
90
packages/apidom-parser-adapter-json-schema-yaml-2020-12/README.md
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,90 @@ | ||
# @swagger-api/apidom-parser-adapter-json-schema-yaml-2020-12 | ||
|
||
`@swagger-api/apidom-parser-adapter-json-schema-yaml-2020-12` is a parser adapter for the [JSON Schema 2020-12](https://json-schema.org/draft/2020-12/draft-bhutton-json-schema-01) in [YAML format](https://yaml.org/spec/1.2/spec.html). | ||
Under the hood this adapter uses [apidom-parser-adapter-yaml-1-2](https://github.com/swagger-api/apidom/tree/main/packages/apidom-parser-adapter-yaml-1-2) | ||
to parse a source string into generic ApiDOM in [base ApiDOM namespace](https://github.com/swagger-api/apidom/tree/main/packages/apidom#base-namespace) | ||
which is then refracted with [JSON Schema 2020-12 Refractors](https://github.com/swagger-api/apidom/tree/main/packages/apidom-ns-json-schema-2020-12#refractors). | ||
|
||
## Installation | ||
|
||
After [prerequisites](https://github.com/swagger-api/apidom/blob/main/README.md#prerequisites) for installing this package are satisfied, you can install it | ||
via [npm CLI](https://docs.npmjs.com/cli) by running the following command: | ||
|
||
```sh | ||
$ npm install @swagger-api/apidom-parser-adapter-json-schema-yaml-2020-12 | ||
``` | ||
|
||
## Parser adapter API | ||
|
||
This parser adapter is fully compatible with parser adapter interface required by [@swagger-api/apidom-parser](https://github.com/swagger-api/apidom/tree/main/packages/apidom-parser#mounting-parser-adapters) | ||
and implements all required properties. | ||
|
||
### mediaTypes | ||
|
||
Defines list of media types that this parser adapter recognizes. | ||
|
||
```js | ||
[ | ||
'application/schema;version=2020-12', | ||
'application/schema+yaml;version=2020-12', | ||
] | ||
``` | ||
|
||
### detect | ||
|
||
[Detection](https://github.com/swagger-api/apidom/blob/main/packages/apidom-parser-adapter-json-schema-yaml-2020-12/src/adapter.ts#L13) is based on a regular expression matching required JSON Schema 2020-12 symbols in YAML format. | ||
|
||
### namespace | ||
|
||
This adapter exposes an instance of [JSON Schema 2020-12 ApiDOM namespace](https://github.com/swagger-api/apidom/tree/main/packages/apidom-ns-json-schema-2020-12#json-schema-2020-12-namespace). | ||
|
||
### parse | ||
|
||
`parse` function consumes various options as a second argument. Here is a list of these options: | ||
|
||
Option | Type | Default | Description | ||
--- | --- | --- | --- | ||
<a name="specObj"></a>`specObj` | `Object` | [Specification Object](https://github.com/swagger-api/apidom/blob/main/packages/apidom-ns-json-schema-2020-12/src/refractor/specification.ts) | This specification object drives the YAML AST transformation to JSON Schema 2020-12 ApiDOM namespace. | ||
<a name="sourceMap"></a>`sourceMap` | `Boolean` | `false` | Indicate whether to generate source maps. | ||
<a name="refractorOpts"></a>`refractorOpts` | `Object` | `{}` | Refractor options are [passed to refractors](https://github.com/swagger-api/apidom/tree/main/packages/apidom-ns-json-schema-2020-12#refractor-plugins) during refracting phase. | ||
|
||
All unrecognized arbitrary options will be ignored. | ||
|
||
## Usage | ||
|
||
This parser adapter can be used directly or indirectly via [apidom-parser](https://github.com/swagger-api/apidom/tree/main/packages/apidom-parser). | ||
|
||
### Direct usage | ||
|
||
During direct usage you don't need to provide `mediaType` as the `parse` function is already pre-bound | ||
with [supported media types](#mediatypes). | ||
|
||
```js | ||
import { parse, detect } from '@swagger-api/apidom-parser-adapter-json-schema-yaml-2020-12'; | ||
|
||
// detecting | ||
await detect('$schema: https://json-schema.org/draft/2020-12/schema'); // => true | ||
await detect('test'); // => false | ||
|
||
// parsing | ||
const parseResult = await parse('$schema: https://json-schema.org/draft/2020-12/schema', { | ||
sourceMap: true, | ||
}); | ||
``` | ||
|
||
### Indirect usage | ||
|
||
You can omit the `mediaType` option here, but please read [Word on detect vs mediaTypes](https://github.com/swagger-api/apidom/tree/main/packages/apidom-parser#word-on-detect-vs-mediatypes) before you do so. | ||
|
||
```js | ||
import ApiDOMParser from '@swagger-api/apidom-parser'; | ||
import * as jsonSchemaYamlAdapter from '@swagger-api/apidom-parser-adapter-json-schema-yaml-2020-12'; | ||
|
||
const parser = new ApiDOMParser(); | ||
|
||
parser.use(jsonSchemaYamlAdapter); | ||
|
||
const parseResult = await parser.parse('$schema: https://json-schema.org/draft/2020-12/schema', { | ||
mediaType: jsonSchemaYamlAdapter.mediaTypes.latest('yaml'), | ||
}); | ||
``` |
5 changes: 5 additions & 0 deletions
5
...es/apidom-parser-adapter-json-schema-yaml-2020-12/config/api-extractor/api-extractor.json
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,5 @@ | ||
{ | ||
"$schema": "https://developer.microsoft.com/json-schemas/api-extractor/v7/api-extractor.schema.json", | ||
"extends": "../../../../api-extractor.json", | ||
"mainEntryPointFilePath": "../../types/adapter.d.ts" | ||
} |
92 changes: 92 additions & 0 deletions
92
packages/apidom-parser-adapter-json-schema-yaml-2020-12/config/webpack/browser.config.js
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,92 @@ | ||
import path from 'node:path'; | ||
import { nonMinimizeTrait, minimizeTrait } from './traits.config.js'; | ||
|
||
const browser = { | ||
mode: 'production', | ||
entry: ['./src/adapter.ts'], | ||
target: 'web', | ||
performance: { | ||
maxEntrypointSize: 2100000, | ||
maxAssetSize: 2100000, | ||
}, | ||
output: { | ||
path: path.resolve('./dist'), | ||
filename: 'apidom-parser-adapter-json-schema-yaml-2020-12.browser.js', | ||
libraryTarget: 'umd', | ||
library: 'apidomParserAdapterJSONSchemaYAML202012', | ||
}, | ||
resolve: { | ||
extensions: ['.ts', '.mjs', '.js', '.json'], | ||
fallback: { | ||
fs: false, | ||
path: false, | ||
}, | ||
}, | ||
module: { | ||
rules: [ | ||
{ | ||
test: /\.wasm$/, | ||
loader: 'file-loader', | ||
type: 'javascript/auto', | ||
}, | ||
{ | ||
test: /\.(ts|js)?$/, | ||
exclude: /node_modules/, | ||
use: { | ||
loader: 'babel-loader', | ||
options: { | ||
babelrc: true, | ||
rootMode: 'upward', | ||
}, | ||
}, | ||
}, | ||
], | ||
}, | ||
...nonMinimizeTrait, | ||
}; | ||
|
||
const browserMin = { | ||
mode: 'production', | ||
entry: ['./src/adapter.ts'], | ||
target: 'web', | ||
performance: { | ||
maxEntrypointSize: 310000, | ||
maxAssetSize: 310000, | ||
}, | ||
output: { | ||
path: path.resolve('./dist'), | ||
filename: 'apidom-parser-adapter-json-schema-yaml-2020-12.browser.min.js', | ||
libraryTarget: 'umd', | ||
library: 'apidomParserAdapterJSONSchemaYAML202012', | ||
}, | ||
resolve: { | ||
extensions: ['.ts', '.mjs', '.js', '.json'], | ||
fallback: { | ||
fs: false, | ||
path: false, | ||
}, | ||
}, | ||
module: { | ||
rules: [ | ||
{ | ||
test: /\.wasm$/, | ||
loader: 'file-loader', | ||
type: 'javascript/auto', | ||
}, | ||
{ | ||
test: /\.(ts|js)?$/, | ||
exclude: /node_modules/, | ||
use: { | ||
loader: 'babel-loader', | ||
options: { | ||
babelrc: true, | ||
rootMode: 'upward', | ||
}, | ||
}, | ||
}, | ||
], | ||
}, | ||
...minimizeTrait, | ||
}; | ||
|
||
export default [browser, browserMin]; |
32 changes: 32 additions & 0 deletions
32
packages/apidom-parser-adapter-json-schema-yaml-2020-12/config/webpack/traits.config.js
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,32 @@ | ||
import webpack from 'webpack'; | ||
import TerserPlugin from 'terser-webpack-plugin'; | ||
|
||
export const nonMinimizeTrait = { | ||
optimization: { | ||
minimize: false, | ||
usedExports: false, | ||
concatenateModules: false, | ||
}, | ||
}; | ||
|
||
export const minimizeTrait = { | ||
plugins: [ | ||
new webpack.LoaderOptionsPlugin({ | ||
minimize: true, | ||
}), | ||
], | ||
optimization: { | ||
minimizer: [ | ||
new TerserPlugin({ | ||
terserOptions: { | ||
compress: { | ||
warnings: false, | ||
}, | ||
output: { | ||
comments: false, | ||
}, | ||
}, | ||
}), | ||
], | ||
}, | ||
}; |
63 changes: 63 additions & 0 deletions
63
packages/apidom-parser-adapter-json-schema-yaml-2020-12/package.json
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,63 @@ | ||
{ | ||
"name": "@swagger-api/apidom-parser-adapter-json-schema-yaml-2020-12", | ||
"version": "1.0.0-beta.7", | ||
"description": "Parser adapter for parsing YAML documents into JSON Schema 2020-12 namespace.", | ||
"publishConfig": { | ||
"access": "public", | ||
"registry": "https://registry.npmjs.org" | ||
}, | ||
"type": "module", | ||
"sideEffects": false, | ||
"unpkg": "./dist/apidom-parser-apdater-json-schema-yaml-2020-12.browser.min.js", | ||
"main": "./src/adapter.cjs", | ||
"exports": { | ||
"types": "./types/apidom-parser-adapter-json-schema-yaml-2020-12.d.ts", | ||
"import": "./src/adapter.mjs", | ||
"require": "./src/adapter.cjs" | ||
}, | ||
"types": "./types/apidom-parser-adapter-json-schema-yaml-2020-12.d.ts", | ||
"scripts": { | ||
"build": "npm run clean && run-p --max-parallel ${CPU_CORES:-2} typescript:declaration build:es build:cjs build:umd:browser", | ||
"build:es": "cross-env BABEL_ENV=es babel src --out-dir src --extensions '.ts' --out-file-extension '.mjs' --root-mode 'upward'", | ||
"build:cjs": "cross-env BABEL_ENV=cjs babel src --out-dir src --extensions '.ts' --out-file-extension '.cjs' --root-mode 'upward'", | ||
"build:umd:browser": "cross-env BABEL_ENV=browser webpack --config config/webpack/browser.config.js --progress", | ||
"lint": "eslint ./", | ||
"lint:fix": "eslint ./ --fix", | ||
"clean": "rimraf --glob 'src/**/*.mjs' 'src/**/*.cjs' 'test/**/*.mjs' ./dist ./types", | ||
"typescript:check-types": "tsc --noEmit && tsc -p ./test/tsconfig.json --noEmit", | ||
"typescript:declaration": "tsc -p tsconfig.declaration.json && api-extractor run -l -c ./config/api-extractor/api-extractor.json", | ||
"test": "npm run build:es && cross-env BABEL_ENV=es babel test --out-dir test --extensions '.ts' --out-file-extension '.mjs' --root-mode 'upward' && cross-env NODE_ENV=test mocha", | ||
"perf": "cross-env BABEL_ENV=es babel ./test/perf/index.ts --out-file ./test/perf/index.mjs --root-mode 'upward' && cross-env NODE_ENV=test node ./test/perf/index.mjs", | ||
"perf:lexical-analysis": "cross-env BABEL_ENV=es babel ./test/perf/lexical-analysis.ts --out-file ./test/perf/lexical-analysis.mjs --root-mode 'upward' && cross-env NODE_ENV=test node ./test/perf/lexical-analysis.mjs", | ||
"perf:syntactic-analysis": "cross-env BABEL_ENV=es babel ./test/perf/syntactic-analysis.ts --out-file ./test/perf/syntactic-analysis.mjs --root-mode 'upward' && cross-env NODE_ENV=test node ./test/perf/syntactic-analysis.mjs", | ||
"perf:refract": "cross-env BABEL_ENV=es babel ./test/perf/refract.ts --out-file ./test/perf/refract.mjs --root-mode 'upward' && cross-env NODE_ENV=test node ./test/perf/refract.mjs", | ||
"perf:parse": "cross-env BABEL_ENV=es babel ./test/perf/parse.ts --out-file ./test/perf/parse.mjs --root-mode 'upward' && cross-env NODE_ENV=test node ./test/perf/parse.mjs", | ||
"prepack": "copyfiles -u 3 ../../LICENSES/* LICENSES && copyfiles -u 2 ../../NOTICE .", | ||
"postpack": "rimraf NOTICE LICENSES" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/swagger-api/apidom.git" | ||
}, | ||
"author": "Vladimir Gorej", | ||
"license": "Apache-2.0", | ||
"dependencies": { | ||
"@babel/runtime-corejs3": "^7.20.7", | ||
"@swagger-api/apidom-core": "^1.0.0-beta.7", | ||
"@swagger-api/apidom-ns-json-schema-2020-12": "^1.0.0-beta.7", | ||
"@swagger-api/apidom-parser-adapter-yaml-1-2": "^1.0.0-beta.7", | ||
"@types/ramda": "~0.30.0", | ||
"ramda": "~0.30.0", | ||
"ramda-adjunct": "^5.0.0" | ||
}, | ||
"files": [ | ||
"src/**/*.mjs", | ||
"src/**/*.cjs", | ||
"dist/", | ||
"types/apidom-parser-adapter-json-schema-yaml-2020-12.d.ts", | ||
"LICENSES", | ||
"NOTICE", | ||
"README.md", | ||
"CHANGELOG.md" | ||
] | ||
} |
48 changes: 48 additions & 0 deletions
48
packages/apidom-parser-adapter-json-schema-yaml-2020-12/src/adapter.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { propOr, omit } from 'ramda'; | ||
import { isNotUndefined } from 'ramda-adjunct'; | ||
import { ParseResultElement, createNamespace } from '@swagger-api/apidom-core'; | ||
import { | ||
parse as parseYAML, | ||
detect as detectYAML, | ||
} from '@swagger-api/apidom-parser-adapter-yaml-1-2'; | ||
import jsonSchemaNamespace, { JSONSchemaElement } from '@swagger-api/apidom-ns-json-schema-2020-12'; | ||
|
||
export { default as mediaTypes } from './media-types.ts'; | ||
|
||
/** | ||
* @public | ||
*/ | ||
export const detectionRegExp = | ||
/(?<YAML>^(["']?)\$schema\2\s*:\s*(["']?)https:\/\/json-schema\.org\/draft\/(?<version_yaml>2020-12)\/schema\3)|(?<JSON>"\$schema"\s*:\s*"https:\/\/json-schema\.org\/draft\/(?<version_json>2020-12)\/schema")/m; | ||
|
||
/** | ||
* @public | ||
*/ | ||
export const detect = async (source: string): Promise<boolean> => | ||
detectionRegExp.test(source) && (await detectYAML(source)); | ||
|
||
/** | ||
* @public | ||
*/ | ||
export const parse = async ( | ||
source: string, | ||
options: Record<string, unknown> = {}, | ||
): Promise<ParseResultElement> => { | ||
const refractorOpts: Record<string, unknown> = propOr({}, 'refractorOpts', options); | ||
const parserOpts = omit(['refractorOpts'], options); | ||
const parseResultElement = await parseYAML(source, parserOpts); | ||
const { result } = parseResultElement; | ||
|
||
if (isNotUndefined(result)) { | ||
const jsonSchemaElement = JSONSchemaElement.refract(result, refractorOpts); | ||
jsonSchemaElement.classes.push('result'); | ||
parseResultElement.replaceResult(jsonSchemaElement); | ||
} | ||
|
||
return parseResultElement; | ||
}; | ||
|
||
/** | ||
* @public | ||
*/ | ||
export const namespace = createNamespace(jsonSchemaNamespace); |
11 changes: 11 additions & 0 deletions
11
packages/apidom-parser-adapter-json-schema-yaml-2020-12/src/media-types.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { mediaTypes, JSONSchema202012MediaTypes } from '@swagger-api/apidom-ns-json-schema-2020-12'; | ||
|
||
/** | ||
* @public | ||
*/ | ||
const yamlMediaTypes = new JSONSchema202012MediaTypes( | ||
...mediaTypes.filterByFormat('generic'), | ||
...mediaTypes.filterByFormat('yaml'), | ||
); | ||
|
||
export default yamlMediaTypes; |
Oops, something went wrong.