Skip to content

Commit e6b47d9

Browse files
authored
feat: support strict parsing mode through out the packages (#46)
Closes #23
1 parent e08f97d commit e6b47d9

47 files changed

Lines changed: 338 additions & 132 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

packages/apidom-converter/test/strategies/openapi-3-1-to-openapi-3-0-3/refractor-plugins/security-requirements-empty-roles/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ describe('converter', function () {
6767
);
6868
const convertedParseResult = await convert(fixturePath, {
6969
parse: {
70-
parserOpts: { sourceMap: true },
70+
parserOpts: { sourceMap: true, strict: false },
7171
},
7272
convert: {
7373
sourceMediaType: openAPI31MediaTypes.findBy('3.1.0', 'json'),

packages/apidom-converter/test/strategies/openapi-3-1-to-openapi-3-0-3/refractor-plugins/security-scheme-type/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ describe('converter', function () {
5353
const fixturePath = path.join(__dirname, 'fixtures', 'security-scheme-type.json');
5454
const convertedParseResult = await convert(fixturePath, {
5555
parse: {
56-
parserOpts: { sourceMap: true },
56+
parserOpts: { sourceMap: true, strict: false },
5757
},
5858
convert: {
5959
sourceMediaType: openAPI31MediaTypes.findBy('3.1.0', 'json'),

packages/apidom-parser-adapter-arazzo-json-1/src/adapter.ts

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
import { propOr, omit } from 'ramda';
22
import { isNotUndefined } from 'ramda-adjunct';
33
import { Namespace, ParseResultElement } from '@speclynx/apidom-datamodel';
4-
import { parse as parseJSON, detect as detectJSON } from '@speclynx/apidom-parser-adapter-json';
4+
import {
5+
parse as parseJSON,
6+
detect as detectJSON,
7+
type ParseDetectOptions,
8+
type ParseOptions as ParseOptionsJSON,
9+
} from '@speclynx/apidom-parser-adapter-json';
510
import arazzoNamespacePlugin, { refractArazzoSpecification1 } from '@speclynx/apidom-ns-arazzo-1';
611

712
export { default as mediaTypes } from './media-types.ts';
@@ -15,15 +20,24 @@ export const detectionRegExp =
1520
/**
1621
* @public
1722
*/
18-
export const detect = async (source: string): Promise<boolean> =>
19-
detectionRegExp.test(source) && (await detectJSON(source));
23+
export const detect: typeof detectJSON = async (
24+
source: string,
25+
options: ParseDetectOptions = {},
26+
): Promise<boolean> => detectionRegExp.test(source) && (await detectJSON(source, options));
27+
28+
/**
29+
* @public
30+
*/
31+
export interface ParseOptions extends ParseOptionsJSON {
32+
refractorOpts?: Record<string, unknown>;
33+
}
2034

2135
/**
2236
* @public
2337
*/
24-
export const parse = async (
38+
export const parse: typeof parseJSON = async (
2539
source: string,
26-
options: Record<string, unknown> = {},
40+
options: ParseOptions = {},
2741
): Promise<ParseResultElement> => {
2842
const refractorOpts: Record<string, unknown> = propOr({}, 'refractorOpts', options);
2943
const parserOpts = omit(['refractorOpts'], options);

packages/apidom-parser-adapter-arazzo-yaml-1/src/adapter.ts

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
import { propOr, omit } from 'ramda';
22
import { isNotUndefined } from 'ramda-adjunct';
33
import { Namespace, ParseResultElement } from '@speclynx/apidom-datamodel';
4-
import { parse as parseYAML, detect as detectYAML } from '@speclynx/apidom-parser-adapter-yaml-1-2';
4+
import {
5+
parse as parseYAML,
6+
detect as detectYAML,
7+
type ParseDetectOptions,
8+
type ParseOptions as ParseOptionsYAML,
9+
} from '@speclynx/apidom-parser-adapter-yaml-1-2';
510
import arazzoNamespacePlugin, { refractArazzoSpecification1 } from '@speclynx/apidom-ns-arazzo-1';
611

712
export { default as mediaTypes } from './media-types.ts';
@@ -15,15 +20,24 @@ export const detectionRegExp =
1520
/**
1621
* @public
1722
*/
18-
export const detect = async (source: string): Promise<boolean> =>
19-
detectionRegExp.test(source) && (await detectYAML(source));
23+
export const detect: typeof detectYAML = async (
24+
source: string,
25+
options: ParseDetectOptions = {},
26+
): Promise<boolean> => detectionRegExp.test(source) && (await detectYAML(source, options));
27+
28+
/**
29+
* @public
30+
*/
31+
export interface ParseOptions extends ParseOptionsYAML {
32+
refractorOpts?: Record<string, unknown>;
33+
}
2034

2135
/**
2236
* @public
2337
*/
24-
export const parse = async (
38+
export const parse: typeof parseYAML = async (
2539
source: string,
26-
options: Record<string, unknown> = {},
40+
options: ParseOptions = {},
2741
): Promise<ParseResultElement> => {
2842
const refractorOpts: Record<string, unknown> = propOr({}, 'refractorOpts', options);
2943
const parserOpts = omit(['refractorOpts'], options);

packages/apidom-parser-adapter-asyncapi-json-2/src/adapter.ts

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
import { propOr, omit } from 'ramda';
22
import { isNotUndefined } from 'ramda-adjunct';
33
import { ParseResultElement, Namespace } from '@speclynx/apidom-datamodel';
4-
import { parse as parseJSON, detect as detectJSON } from '@speclynx/apidom-parser-adapter-json';
4+
import {
5+
parse as parseJSON,
6+
detect as detectJSON,
7+
type ParseDetectOptions,
8+
type ParseOptions as ParseOptionsJSON,
9+
} from '@speclynx/apidom-parser-adapter-json';
510
import asyncApiNamespacePlugin, { refractAsyncApi2 } from '@speclynx/apidom-ns-asyncapi-2';
611

712
export { default as mediaTypes } from './media-types.ts';
@@ -15,15 +20,24 @@ export const detectionRegExp =
1520
/**
1621
* @public
1722
*/
18-
export const detect = async (source: string): Promise<boolean> =>
19-
detectionRegExp.test(source) && (await detectJSON(source));
23+
export const detect: typeof detectJSON = async (
24+
source: string,
25+
options: ParseDetectOptions = {},
26+
): Promise<boolean> => detectionRegExp.test(source) && (await detectJSON(source, options));
27+
28+
/**
29+
* @public
30+
*/
31+
export interface ParseOptions extends ParseOptionsJSON {
32+
refractorOpts?: Record<string, unknown>;
33+
}
2034

2135
/**
2236
* @public
2337
*/
24-
export const parse = async (
38+
export const parse: typeof parseJSON = async (
2539
source: string,
26-
options: Record<string, unknown> = {},
40+
options: ParseOptions = {},
2741
): Promise<ParseResultElement> => {
2842
const refractorOpts: Record<string, unknown> = propOr({}, 'refractorOpts', options);
2943
const parserOpts = omit(['refractorOpts'], options);

packages/apidom-parser-adapter-asyncapi-yaml-2/src/adapter.ts

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
import { omit, propOr } from 'ramda';
22
import { isNotUndefined } from 'ramda-adjunct';
33
import { ParseResultElement, Namespace } from '@speclynx/apidom-datamodel';
4-
import { parse as parseYAML, detect as detectYAML } from '@speclynx/apidom-parser-adapter-yaml-1-2';
4+
import {
5+
parse as parseYAML,
6+
detect as detectYAML,
7+
type ParseDetectOptions,
8+
type ParseOptions as ParseOptionsYAML,
9+
} from '@speclynx/apidom-parser-adapter-yaml-1-2';
510
import asyncApiNamespace, { refractAsyncApi2 } from '@speclynx/apidom-ns-asyncapi-2';
611

712
export { default as mediaTypes } from './media-types.ts';
@@ -15,15 +20,24 @@ export const detectionRegExp =
1520
/**
1621
* @public
1722
*/
18-
export const detect = async (source: string): Promise<boolean> =>
19-
detectionRegExp.test(source) && (await detectYAML(source));
23+
export const detect: typeof detectYAML = async (
24+
source: string,
25+
options: ParseDetectOptions = {},
26+
): Promise<boolean> => detectionRegExp.test(source) && (await detectYAML(source, options));
27+
28+
/**
29+
* @public
30+
*/
31+
export interface ParseOptions extends ParseOptionsYAML {
32+
refractorOpts?: Record<string, unknown>;
33+
}
2034

2135
/**
2236
* @public
2337
*/
24-
export const parse = async (
38+
export const parse: typeof parseYAML = async (
2539
source: string,
26-
options: Record<string, unknown> = {},
40+
options: ParseOptions = {},
2741
): Promise<ParseResultElement> => {
2842
const refractorOpts: Record<string, unknown> = propOr({}, 'refractorOpts', options);
2943
const parserOpts = omit(['refractorOpts'], options);

packages/apidom-parser-adapter-json-schema-json-2020-12/src/adapter.ts

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
import { propOr, omit } from 'ramda';
22
import { isNotUndefined } from 'ramda-adjunct';
33
import { ParseResultElement, Namespace } from '@speclynx/apidom-datamodel';
4-
import { parse as parseJSON, detect as detectJSON } from '@speclynx/apidom-parser-adapter-json';
4+
import {
5+
parse as parseJSON,
6+
detect as detectJSON,
7+
type ParseDetectOptions,
8+
type ParseOptions as ParseOptionsJSON,
9+
} from '@speclynx/apidom-parser-adapter-json';
510
import jsonSchemaNamespace, { refractJSONSchema } from '@speclynx/apidom-ns-json-schema-2020-12';
611

712
export { default as mediaTypes } from './media-types.ts';
@@ -15,15 +20,24 @@ export const detectionRegExp =
1520
/**
1621
* @public
1722
*/
18-
export const detect = async (source: string): Promise<boolean> =>
19-
detectionRegExp.test(source) && (await detectJSON(source));
23+
export const detect: typeof detectJSON = async (
24+
source: string,
25+
options: ParseDetectOptions = {},
26+
): Promise<boolean> => detectionRegExp.test(source) && (await detectJSON(source, options));
27+
28+
/**
29+
* @public
30+
*/
31+
export interface ParseOptions extends ParseOptionsJSON {
32+
refractorOpts?: Record<string, unknown>;
33+
}
2034

2135
/**
2236
* @public
2337
*/
24-
export const parse = async (
38+
export const parse: typeof parseJSON = async (
2539
source: string,
26-
options: Record<string, unknown> = {},
40+
options: ParseOptions = {},
2741
): Promise<ParseResultElement> => {
2842
const refractorOpts: Record<string, unknown> = propOr({}, 'refractorOpts', options);
2943
const parserOpts = omit(['refractorOpts'], options);

packages/apidom-parser-adapter-json-schema-yaml-2020-12/src/adapter.ts

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
import { propOr, omit } from 'ramda';
22
import { isNotUndefined } from 'ramda-adjunct';
33
import { ParseResultElement, Namespace } from '@speclynx/apidom-datamodel';
4-
import { parse as parseYAML, detect as detectYAML } from '@speclynx/apidom-parser-adapter-yaml-1-2';
4+
import {
5+
parse as parseYAML,
6+
detect as detectYAML,
7+
type ParseDetectOptions,
8+
type ParseOptions as ParseOptionsYAML,
9+
} from '@speclynx/apidom-parser-adapter-yaml-1-2';
510
import jsonSchemaNamespace, { refractJSONSchema } from '@speclynx/apidom-ns-json-schema-2020-12';
611

712
export { default as mediaTypes } from './media-types.ts';
@@ -15,15 +20,24 @@ export const detectionRegExp =
1520
/**
1621
* @public
1722
*/
18-
export const detect = async (source: string): Promise<boolean> =>
19-
detectionRegExp.test(source) && (await detectYAML(source));
23+
export const detect: typeof detectYAML = async (
24+
source: string,
25+
options: ParseDetectOptions = {},
26+
): Promise<boolean> => detectionRegExp.test(source) && (await detectYAML(source, options));
27+
28+
/**
29+
* @public
30+
*/
31+
export interface ParseOptions extends ParseOptionsYAML {
32+
refractorOpts?: Record<string, unknown>;
33+
}
2034

2135
/**
2236
* @public
2337
*/
24-
export const parse = async (
38+
export const parse: typeof parseYAML = async (
2539
source: string,
26-
options: Record<string, unknown> = {},
40+
options: ParseOptions = {},
2741
): Promise<ParseResultElement> => {
2842
const refractorOpts: Record<string, unknown> = propOr({}, 'refractorOpts', options);
2943
const parserOpts = omit(['refractorOpts'], options);

packages/apidom-parser-adapter-json/src/adapter.ts

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { ParseResultElement, Namespace } from '@speclynx/apidom-datamodel';
2-
import { ApiDOMError } from '@speclynx/apidom-error';
2+
import { UnsupportedOperationError } from '@speclynx/apidom-error';
33

44
import * as native from './native/index.ts';
55
import * as treeSitter from './tree-sitter/index.ts';
@@ -27,7 +27,7 @@ export const detectionRegExp = treeSitter.detectionRegExp;
2727
/**
2828
* @public
2929
*/
30-
export interface DetectOptions {
30+
export interface ParseDetectOptions {
3131
strict?: boolean;
3232
}
3333

@@ -36,7 +36,7 @@ export interface DetectOptions {
3636
*/
3737
export const detect = async (
3838
source: string,
39-
{ strict = false }: DetectOptions = {},
39+
{ strict = false }: ParseDetectOptions = {},
4040
): Promise<boolean> => {
4141
if (strict) {
4242
return native.detect(source);
@@ -47,25 +47,20 @@ export const detect = async (
4747
/**
4848
* @public
4949
*/
50-
export interface ParseFunctionOptions {
50+
export interface ParseOptions {
5151
sourceMap?: boolean;
5252
strict?: boolean;
5353
}
5454

5555
/**
5656
* @public
5757
*/
58-
export type ParseFunction = (
58+
export const parse = async (
5959
source: string,
60-
options?: ParseFunctionOptions,
61-
) => Promise<ParseResultElement>;
62-
63-
/**
64-
* @public
65-
*/
66-
export const parse: ParseFunction = async (source, { sourceMap = false, strict = false } = {}) => {
60+
{ sourceMap = false, strict = false }: ParseOptions = {},
61+
): Promise<ParseResultElement> => {
6762
if (strict && sourceMap) {
68-
throw new ApiDOMError(
63+
throw new UnsupportedOperationError(
6964
'Cannot use sourceMap with strict parsing. Strict parsing does not support source maps.',
7065
);
7166
}

packages/apidom-parser-adapter-json/src/native/index.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ import { ParseResultElement, refract } from '@speclynx/apidom-datamodel';
44
* @public
55
*/
66
export const detect = async (source: string): Promise<boolean> => {
7+
if (source.trim().length === 0) {
8+
return false;
9+
}
10+
711
try {
812
JSON.parse(source);
913
return true;
@@ -16,10 +20,16 @@ export const detect = async (source: string): Promise<boolean> => {
1620
* @public
1721
*/
1822
export const parse = async (source: string): Promise<ParseResultElement> => {
23+
const parseResult = new ParseResultElement();
24+
25+
if (source.trim().length === 0) {
26+
return parseResult;
27+
}
28+
1929
const pojo = JSON.parse(source);
2030
const element = refract(pojo);
21-
const parseResult = new ParseResultElement();
2231
element.classes.push('result');
2332
parseResult.push(element);
33+
2434
return parseResult;
2535
};

0 commit comments

Comments
 (0)