Skip to content

Commit ade12ad

Browse files
fix
1 parent 63b0e40 commit ade12ad

File tree

2 files changed

+6
-42
lines changed

2 files changed

+6
-42
lines changed

tools/spectral/ipa/rulesets/functions/eachEnumValueMustBeUpperSnakeCase.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
import { hasException } from './utils/exceptions.js';
2-
import { getSchemaPath, resolveObject } from './utils/componentUtils.js';
2+
import { resolveObject } from './utils/componentUtils.js';
33
import { casing } from '@stoplight/spectral-functions';
44

55
const RULE_NAME = 'xgen-IPA-123-enum-values-must-be-upper-snake-case';
66
const ERROR_MESSAGE = 'enum value must be UPPER_SNAKE_CASE.';
77

8+
export function getSchemaPathFromEnumPath(path) {
9+
return path.slice(0, path.length - 1);
10+
}
11+
812
export default (input, _, { path, documentInventory }) => {
913
const oas = documentInventory.resolved;
10-
const schemaPath = getSchemaPath(path);
14+
const schemaPath = getSchemaPathFromEnumPath(path);
1115
const schemaObject = resolveObject(oas, schemaPath);
1216
if (hasException(schemaObject, RULE_NAME)) {
1317
return;

tools/spectral/ipa/rulesets/functions/utils/componentUtils.js

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -13,46 +13,6 @@ export function isPathParam(str) {
1313
return pathParamRegEx.test(str) || pathParamWithCustomMethodRegEx.test(str);
1414
}
1515

16-
/**
17-
* Extracts the schema path from the given JSONPath array.
18-
*
19-
* This function handles the following types of paths 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 directly within a schema under `components.schemas`.
24-
* - The function returns the first three elements (`["components", "schemas", "schemaName"]`).
25-
*
26-
* 2. **Component Schema Property Paths**:
27-
* - Represented as: `components.schemas.schemaName.properties.propertyName.enum`
28-
* - This path indicates that the enum is defined for a specific property of a schema.
29-
* - The function returns up to the property level (`["components", "schemas", "schemaName", "properties", "propertyName"]`).
30-
*
31-
* 3. **Parameter Schema Paths**:
32-
* - Represented as: `paths.*.method.parameters[*].schema.enum`
33-
* - This path indicates that the enum is part of a parameter's schema in an operation.
34-
* - The function identifies the location of `schema` in the path and returns everything up to (and including) it.
35-
*
36-
* @param {string[]} path - An array representing the JSONPath structure of the OpenAPI definition.
37-
* @returns {string[]} The truncated path pointing to the schema object or property.
38-
*/
39-
40-
export function getSchemaPath(path) {
41-
if (path.includes('components')) {
42-
const propertyIndex = path.findIndex((item) => item === 'properties');
43-
if (propertyIndex !== -1) {
44-
// Path is for a component.schema.property enum
45-
return path.slice(0, propertyIndex + 2);
46-
}
47-
// Path is for a component.schema enum
48-
return path.slice(0, 3);
49-
} else if (path.includes('paths')) {
50-
const schemaIndex = path.findIndex((item) => item === 'schema');
51-
return path.slice(0, schemaIndex + 1);
52-
}
53-
return [];
54-
}
55-
5616
/**
5717
* Resolves the value of a nested property within an OpenAPI structure using a given path.
5818
*

0 commit comments

Comments
 (0)