|
| 1 | +# @swagger-api/apidom-parser-adapter-json-schema-json-2020-12 |
| 2 | + |
| 3 | +`@swagger-api/apidom-parser-adapter-json-schema-json-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 [JSON format](https://www.json.org/json-en.html). |
| 4 | +Under the hood this adapter uses [apidom-parser-adapter-json](https://github.com/swagger-api/apidom/tree/main/packages/apidom-parser-adapter-json) |
| 5 | +to parse a source string into generic ApiDOM in [base ApiDOM namespace](https://github.com/swagger-api/apidom/tree/main/packages/apidom#base-namespace) |
| 6 | +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). |
| 7 | + |
| 8 | +## Installation |
| 9 | + |
| 10 | +After [prerequisites](https://github.com/swagger-api/apidom/blob/main/README.md#prerequisites) for installing this package are satisfied, you can install it |
| 11 | +via [npm CLI](https://docs.npmjs.com/cli) by running the following command: |
| 12 | + |
| 13 | +```sh |
| 14 | + $ npm install @swagger-api/apidom-parser-adapter-json-schema-json-2020-12 |
| 15 | +``` |
| 16 | + |
| 17 | +## Parser adapter API |
| 18 | + |
| 19 | +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) |
| 20 | +and implements all required properties. |
| 21 | + |
| 22 | +### mediaTypes |
| 23 | + |
| 24 | +Defines list of media types that this parser adapter recognizes. |
| 25 | + |
| 26 | +```js |
| 27 | +[ |
| 28 | + 'application/schema;version=2020-12', |
| 29 | + 'application/schema+json;version=2020-12', |
| 30 | +] |
| 31 | +``` |
| 32 | + |
| 33 | +### detect |
| 34 | + |
| 35 | +[Detection](https://github.com/swagger-api/apidom/blob/main/packages/apidom-parser-adapter-json-schema-json-2020-12/src/adapter.ts#L13) is based on a regular expression matching required JSON Schema 2020-12 symbols in JSON format. |
| 36 | + |
| 37 | +### namespace |
| 38 | + |
| 39 | +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). |
| 40 | + |
| 41 | +### parse |
| 42 | + |
| 43 | +`parse` function consumes various options as a second argument. Here is a list of these options: |
| 44 | + |
| 45 | +Option | Type | Default | Description |
| 46 | +--- | --- | --- | --- |
| 47 | +<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 JSON AST transformation to JSON Schema 2020-12 ApiDOM namespace. |
| 48 | +<a name="sourceMap"></a>`sourceMap` | `Boolean` | `false` | Indicate whether to generate source maps. |
| 49 | +<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. |
| 50 | + |
| 51 | +All unrecognized arbitrary options will be ignored. |
| 52 | + |
| 53 | +## Usage |
| 54 | + |
| 55 | +This parser adapter can be used directly or indirectly via [@swagger-api/apidom-parser](https://github.com/swagger-api/apidom/tree/main/packages/apidom-parser). |
| 56 | + |
| 57 | +### Direct usage |
| 58 | + |
| 59 | +During direct usage you don't need to provide `mediaType` as the `parse` function is already pre-bound |
| 60 | +with [supported media types](#mediatypes). |
| 61 | + |
| 62 | +```js |
| 63 | +import { parse, detect } from '@swagger-api/apidom-parser-adapter-json-schema-json-2020-12'; |
| 64 | + |
| 65 | +// detecting |
| 66 | +await detect('{"$schema": "https://json-schema.org/draft/2020-12/schema"}'); // => true |
| 67 | +await detect('test'); // => false |
| 68 | + |
| 69 | +// parsing |
| 70 | +const parseResult = await parse('{"$schema": "https://json-schema.org/draft/2020-12/schema"}', { |
| 71 | + sourceMap: true, |
| 72 | +}); |
| 73 | +``` |
| 74 | + |
| 75 | +### Indirect usage |
| 76 | + |
| 77 | +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. |
| 78 | + |
| 79 | +```js |
| 80 | +import ApiDOMParser from '@swagger-api/apidom-parser'; |
| 81 | +import * as jsonSchemaJsonAdapter from '@swagger-api/apidom-parser-adapter-json-schema-json-2020-12'; |
| 82 | + |
| 83 | +const parser = new ApiDOMParser(); |
| 84 | + |
| 85 | +parser.use(jsonSchemaJsonAdapter); |
| 86 | + |
| 87 | +const parseResult = await parser.parse( |
| 88 | + '{"$schema": "https://json-schema.org/draft/2020-12/schema"}', |
| 89 | + { mediaType: jsonSchemaJsonAdapter.mediaTypes.latest('json') }, |
| 90 | +); |
| 91 | +``` |
0 commit comments