Skip to content

Commit ab92c18

Browse files
committed
Resolve ref in parameters
1 parent eeaff7c commit ab92c18

File tree

6 files changed

+50
-69
lines changed

6 files changed

+50
-69
lines changed

README.md

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,12 @@ openapi-static-validator spec.json > validate.js
1616

1717
## TODOs
1818

19-
- Path/Operations
20-
- [ ] Validate parameters
19+
- Parameters:
20+
- [ ] in `cookie`
21+
- [ ] in `header`
22+
- [ ] in `query`
2123
- JSONSchema `string` validation of:
22-
- [x] `minLength`
23-
- [x] `maxLength`
2424
- [ ] `format`
25-
- [x] `pattern`
26-
- JSONSchema `array` validation of:
27-
- [x] `minItems`
28-
- [x] `maxItems`
29-
- [x] `uniqueItems`
3025
- JSONSchema `integer`
3126
- [ ] no float
3227

src/compileOperation.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ export function compileOperation(
4747

4848
// Extract and validate path params
4949
const pathParameters = (operation.parameters ?? []).filter(
50-
(parameter) => parameter.in === 'path',
50+
(parameter) => compiler.resolveMaybeRef(parameter).in === 'path',
5151
);
5252
nodes.push(
5353
builders.expressionStatement(
@@ -56,7 +56,9 @@ export function compileOperation(
5656
builders.memberExpression(requestIdentifier, builders.identifier('params')),
5757
builders.objectExpression(
5858
pathParameters
59-
.map((parameter, index) => {
59+
.map((refParameter, index) => {
60+
const parameter = compiler.resolveMaybeRef(refParameter);
61+
6062
const identifier = builders.identifier(`pathParam${index}`);
6163
const schemaFn = compileValueSchema(compiler, parameter.schema);
6264
const regexMatchIndex = getPathParamIndex(path, parameter.name);
@@ -109,7 +111,7 @@ export function compileOperation(
109111

110112
// Validate query parameters
111113
const queryParameters = (operation.parameters ?? []).filter(
112-
(parameter) => parameter.in === 'path',
114+
(parameter) => compiler.resolveMaybeRef(parameter).in === 'path',
113115
);
114116
queryParameters.forEach((parameter) => {});
115117

src/compileParameter.ts

Lines changed: 0 additions & 55 deletions
This file was deleted.

src/compiler.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,4 +189,15 @@ export class Compiler {
189189

190190
return value;
191191
}
192+
193+
/**
194+
* Resolve a potential ref to a value.
195+
*/
196+
public resolveMaybeRef<T>(value: T | OpenAPIRef): T {
197+
if (typeof value === 'object' && value !== null && '$ref' in value) {
198+
return this.resolveRef(value);
199+
}
200+
201+
return value;
202+
}
192203
}

src/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export interface OpenAPIPath {
1818

1919
export interface OpenAPIOperation {
2020
operationId?: string;
21-
parameters?: OpenAPIParameter[];
21+
parameters?: Array<OpenAPIParameter | OpenAPIRef>;
2222
requestBody?: OpenAPIRequestBody;
2323
}
2424

tests/gitbook.test.ts

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,33 @@ test('POST /spaces/1234/hive/token', () => {
1010
},
1111
query: {},
1212
});
13-
expect(result).toMatchSnapshot();
13+
expect(result).toMatchObject({
14+
headers: {
15+
'content-type': 'application/json',
16+
},
17+
method: 'post',
18+
operationId: 'generateSpaceHiveReadAccessToken',
19+
params: {
20+
spaceId: '1234',
21+
},
22+
path: '/spaces/1234/hive/token',
23+
query: {},
24+
});
25+
});
26+
27+
test('POST orgs/appleId/custom-fields', () => {
28+
const result = validateRequest({
29+
path: '/orgs/appleId/custom-fields',
30+
method: 'post',
31+
headers: {
32+
'content-type': 'application/json',
33+
},
34+
query: {},
35+
body: { name: 'jira_board', type: 'number' },
36+
});
37+
expect(result).toMatchObject({
38+
params: {
39+
organizationId: 'appleId',
40+
},
41+
});
1442
});

0 commit comments

Comments
 (0)