1+ /**
2+ * Checks if a string belongs to a path parameter or a path parameter with a custom method.
3+ *
4+ * A path parameter has the format: `{paramName}`
5+ * A path parameter with a custom method has the format: `{paramName}:customMethod`
6+ *
7+ * @param {string } str - A string extracted from a path split by slashes.
8+ * @returns {boolean } True if the string matches the expected formats, false otherwise.
9+ */
10+ export function isPathParam ( str ) {
11+ const pathParamRegEx = new RegExp ( `^{[a-z][a-zA-Z0-9]*}$` ) ;
12+ const pathParamWithCustomMethodRegEx = new RegExp ( `^{[a-z][a-zA-Z0-9]*}:[a-z][a-zA-Z0-9]*$` ) ;
13+ return pathParamRegEx . test ( str ) || pathParamWithCustomMethodRegEx . test ( str ) ;
14+ }
15+
16+ /**
17+ * Extracts the schema path from the given JSONPath array.
18+ *
19+ * This function is designed to handle two types of paths commonly encountered in OpenAPI definitions:
20+ *
21+ * 1. **Component Schema Paths**:
22+ * - Represented as: `components.schemas.schemaName.*.enum`
23+ * - This path indicates that the enum is defined within a schema under `components.schemas`.
24+ * - The function returns the first three elements (`["components", "schemas", "schemaName"]`).
25+ *
26+ * 2. **Parameter Schema Paths**:
27+ * - Represented as: `paths.*.method.parameters[*].schema.enum`
28+ * - This path indicates that the enum is part of a parameter's schema in an operation.
29+ * - The function identifies the location of `schema` in the path and returns everything up to (and including) it.
30+ *
31+ * @param {string[] } path - An array representing the JSONPath structure of the OpenAPI definition.
32+ * @returns {string[] } The truncated path pointing to the schema object.
33+ */
34+ export function getSchemaPath ( path ) {
35+ if ( path . includes ( 'components' ) ) {
36+ return path . slice ( 0 , 3 ) ;
37+ } else if ( path . includes ( 'paths' ) ) {
38+ const index = path . findIndex ( ( item ) => item === 'schema' ) ;
39+ return path . slice ( 0 , index + 1 ) ;
40+ }
41+ }
42+
43+ /**
44+ * Resolves the value of a nested property within an OpenAPI structure using a given path.
45+ *
46+ * This function traverses an OpenAPI object based on a specified path (array of keys)
47+ * and retrieves the value at the end of the path. If any key in the path is not found,
48+ * or the value is undefined at any point, the function will return `undefined`.
49+ *
50+ * @param {Object } oas - The entire OpenAPI Specification object.
51+ * This represents the root of the OpenAPI document.
52+ * @param {string[] } objectPath - An array of strings representing the path to the desired value.
53+ * Each element corresponds to a key in the OAS object hierarchy.
54+ * For example, `['components', 'schemas', 'MySchema', 'properties']`.
55+ * @returns {* } The value at the specified path within the OpenAPI object, or `undefined` if the path is invalid.
56+ *
57+ * @example
58+ * const oas = {
59+ * components: {
60+ * schemas: {
61+ * MySchema: {
62+ * properties: {
63+ * fieldName: { type: 'string' }
64+ * }
65+ * }
66+ * }
67+ * }
68+ * };
69+ *
70+ * const result = resolveObject(oas, ['components', 'schemas', 'MySchema', 'properties']);
71+ * console.log(result); // Output: { fieldName: { type: 'string' } }
72+ */
73+ export function resolveObject ( oas , objectPath ) {
74+ return objectPath . reduce ( ( current , key ) => {
75+ return current && current [ key ] ? current [ key ] : undefined ;
76+ } , oas ) ;
77+ }
0 commit comments