Skip to content

Commit e3cca53

Browse files
committed
fix(spectral): fix issue in 0.4.0 package causing a runtime error
- clone local copy of getLintTargets from spectral repository - clone local copy of tests for getLintTargets function - update package version to 0.4.1 and add lodash dependency
1 parent d5c4d86 commit e3cca53

File tree

6 files changed

+252
-13
lines changed

6 files changed

+252
-13
lines changed

.npmignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
*
2-
!dist/functions/*.js
2+
!dist/functions/*.{js,d.ts}
3+
!dist/utils/*.{js,d.ts}
34
!.spectral.yaml
45
!*.oas.rules.yml

package-lock.json

Lines changed: 24 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@ukhsa-collaboration/spectral-rules",
3-
"version": "0.4.0",
3+
"version": "0.4.1",
44
"description": "UKHSA Spectral ruleset",
55
"main": ".spectral.yaml",
66
"type": "module",
@@ -31,19 +31,21 @@
3131
"ruleset"
3232
],
3333
"dependencies": {
34-
"@stoplight/spectral-documentation": "^1.3.1"
34+
"@stoplight/spectral-core": "^1.20.0",
35+
"@stoplight/spectral-documentation": "^1.3.1",
36+
"lodash": "^4.17.21"
3537
},
3638
"devDependencies": {
3739
"@commitlint/cli": "^20.1.0",
3840
"@commitlint/config-conventional": "^20.0.0",
3941
"@stoplight/spectral-cli": "^6.15.0",
40-
"@stoplight/spectral-core": "^1.20.0",
4142
"@stoplight/spectral-functions": "^1.10.1",
4243
"@stoplight/spectral-parsers": "^1.0.5",
4344
"@stoplight/spectral-ruleset-bundler": "^1.6.3",
4445
"@swc/core": "^1.13.21",
4546
"@swc/jest": "^0.2.39",
4647
"@types/jest": "^30.0.0",
48+
"@types/lodash": "^4.17.20",
4749
"cpx2": "^8.0.0",
4850
"husky": "^9.1.7",
4951
"jest": "^30.2.0",
Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
/**
2+
* originally sourced from: https://github.com/stoplightio/spectral/blob/develop/packages/core/src/runner/utils/__tests__/getLintTargets.test.ts
3+
*/
4+
import { getLintTargets } from '../../utils/getLintTargets';
5+
6+
describe('getLintTargets', () => {
7+
describe('when @key is given as field', () => {
8+
it('given object, returns its keys', () => {
9+
expect(getLintTargets({ a: null, b: true }, '@key')).toStrictEqual([
10+
{
11+
path: ['a'],
12+
value: 'a',
13+
},
14+
{
15+
path: ['b'],
16+
value: 'b',
17+
},
18+
]);
19+
});
20+
21+
it('given array, returns its indicies', () => {
22+
expect(getLintTargets(['foo', 'bar'], '@key')).toStrictEqual([
23+
{
24+
path: ['0'],
25+
value: '0',
26+
},
27+
{
28+
path: ['1'],
29+
value: '1',
30+
},
31+
]);
32+
});
33+
34+
it('given primitive property, returns the whole input', () => {
35+
expect(getLintTargets('abc', '0')).toStrictEqual([
36+
{
37+
path: [],
38+
value: 'abc',
39+
},
40+
]);
41+
});
42+
});
43+
44+
describe('when property path is given as field', () => {
45+
it('given existing property, returns lint targets', () => {
46+
expect(getLintTargets({ a: null }, 'a')).toStrictEqual([
47+
{
48+
path: ['a'],
49+
value: null,
50+
},
51+
]);
52+
53+
expect(getLintTargets({ foo: ['a'] }, 'foo[0]')).toStrictEqual([
54+
{
55+
path: ['foo', '0'],
56+
value: 'a',
57+
},
58+
]);
59+
60+
expect(getLintTargets(['foo'], '0')).toStrictEqual([
61+
{
62+
path: ['0'],
63+
value: 'foo',
64+
},
65+
]);
66+
67+
expect(getLintTargets({ a: void 0 }, 'a')).toStrictEqual([
68+
{
69+
path: ['a'],
70+
value: void 0,
71+
},
72+
]);
73+
});
74+
75+
it('given non-existing property, returns lint target with undefined value', () => {
76+
expect(getLintTargets({ a: null }, 'b')).toStrictEqual([
77+
{
78+
path: ['b'],
79+
value: void 0,
80+
},
81+
]);
82+
83+
expect(getLintTargets(['foo'], '1')).toStrictEqual([
84+
{
85+
path: ['1'],
86+
value: void 0,
87+
},
88+
]);
89+
});
90+
91+
it('given primitive property, returns the whole input', () => {
92+
expect(getLintTargets('abc', '0')).toStrictEqual([
93+
{
94+
path: [],
95+
value: 'abc',
96+
},
97+
]);
98+
});
99+
});
100+
101+
describe('when JSON Path expression is given as field', () => {
102+
it('given existing property, returns lint targets', () => {
103+
expect(getLintTargets({ a: null }, '$')).toStrictEqual([
104+
{
105+
path: [],
106+
value: {
107+
a: null,
108+
},
109+
},
110+
]);
111+
112+
expect(getLintTargets({ foo: ['a'] }, '$.foo.*')).toStrictEqual([
113+
{
114+
path: ['foo', '0'],
115+
value: 'a',
116+
},
117+
]);
118+
});
119+
120+
it('given non-existing property, returns lint target with undefined value', () => {
121+
expect(getLintTargets({ a: null }, '$.b')).toStrictEqual([
122+
{
123+
path: [],
124+
value: void 0,
125+
},
126+
]);
127+
128+
expect(getLintTargets(['foo'], '$..bar')).toStrictEqual([
129+
{
130+
path: [],
131+
value: void 0,
132+
},
133+
]);
134+
});
135+
136+
it('given primitive property, returns the whole input', () => {
137+
expect(getLintTargets('abc', '0')).toStrictEqual([
138+
{
139+
path: [],
140+
value: 'abc',
141+
},
142+
]);
143+
});
144+
});
145+
146+
it('given no field, returns the whole input', () => {
147+
expect(getLintTargets({ a: true }, void 0)).toStrictEqual([
148+
{
149+
path: [],
150+
value: { a: true },
151+
},
152+
]);
153+
});
154+
});

src/functions/overrideSeverity.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,14 @@ import type {
77
Rule,
88
} from '@stoplight/spectral-core';
99
import { DiagnosticSeverity } from '@stoplight/types';
10+
import { printPath, PrintStyle } from '@stoplight/spectral-runtime';
1011
import { EOL } from "node:os";
1112
/**
12-
* shouldn't really do this however there is no other way to get these functions, and it keeps us
13-
* aligned with spectral internals
13+
* this is a utility function obtained from spectral-core
14+
* https://github.com/stoplightio/spectral/blob/develop/packages/core/src/runner/utils/getLintTargets.ts
1415
*/
15-
import { getLintTargets, type ILintTarget } from '../../node_modules/@stoplight/spectral-core/dist/runner/utils/getLintTargets.js';
16-
import { printPath, PrintStyle } from '../../node_modules/@stoplight/spectral-runtime/dist/utils/printPath.js';
16+
import { getLintTargets } from '../utils/getLintTargets.js';
17+
1718

1819
/**
1920
* Configuration options provided by the ruleset when invoking the override.
@@ -56,7 +57,7 @@ const overrideSeverity = (
5657
if (targetField.trim().length === 0) {
5758
throw new Error('overrideSeverity requires a non-empty target selector');
5859
}
59-
const targets = getLintTargets(targetValue, targetField) as ILintTarget[];
60+
const targets = getLintTargets(targetValue, targetField)
6061
const matchingTarget = targets.find(({ value }) => value === options.value);
6162

6263
if (matchingTarget && typeof matchingTarget.value === 'string') {

0 commit comments

Comments
 (0)