Skip to content

Commit 1cc6db5

Browse files
committed
Introduce context
1 parent b46b102 commit 1cc6db5

9 files changed

+116
-107
lines changed

src/compileParameter.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import { compileValueSchema } from './compileValueSchema';
1717
* }
1818
*/
1919
export function compileParameter(compiler: Compiler, parameter: OpenAPIParameter) {
20-
return compiler.declareValidationFunction(parameter, ({ value, path, error }) => {
20+
return compiler.declareValidationFunction(parameter, ({ value, path, context, error }) => {
2121
const nodes: namedTypes.BlockStatement['body'] = [];
2222

2323
const paramValue = builders.memberExpression(
@@ -43,7 +43,7 @@ export function compileParameter(compiler: Compiler, parameter: OpenAPIParameter
4343
builders.variableDeclaration('const', [
4444
builders.variableDeclarator(
4545
builders.identifier('result'),
46-
builders.callExpression(schemaFn, [path, paramValue]),
46+
builders.callExpression(schemaFn, [path, paramValue, context]),
4747
),
4848
]),
4949
);

src/compilePath.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import { compileOperation } from './compileOperation';
1616
* }
1717
*/
1818
export function compilePath(compiler: Compiler, pathOperations: OpenAPIPath) {
19-
return compiler.declareValidationFunction(pathOperations, ({ value, path, error }) => {
19+
return compiler.declareValidationFunction(pathOperations, ({ value, path, context, error }) => {
2020
const nodes: namedTypes.BlockStatement['body'] = [];
2121

2222
Object.entries(pathOperations).forEach(([method, operation]) => {
@@ -31,7 +31,7 @@ export function compilePath(compiler: Compiler, pathOperations: OpenAPIPath) {
3131
),
3232
builders.blockStatement([
3333
builders.returnStatement(
34-
builders.callExpression(fnOperation, [path, value]),
34+
builders.callExpression(fnOperation, [path, value, context]),
3535
),
3636
]),
3737
),

src/compileValidateRequest.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import { ValidationErrorIdentifier } from './error';
2323
*/
2424
export function compileValidateRequest(compiler: Compiler, spec: OpenAPISpec) {
2525
const request = builders.identifier('request');
26+
const context = builders.identifier('context');
2627

2728
const nodes: namedTypes.Program['body'] = [];
2829
const functionNodes: namedTypes.FunctionDeclaration['body']['body'] = [];
@@ -63,6 +64,7 @@ export function compileValidateRequest(compiler: Compiler, spec: OpenAPISpec) {
6364
builders.callExpression(operationFn, [
6465
builders.arrayExpression([]),
6566
request,
67+
context,
6668
]),
6769
),
6870
]),
@@ -84,7 +86,7 @@ export function compileValidateRequest(compiler: Compiler, spec: OpenAPISpec) {
8486
builders.exportNamedDeclaration(
8587
builders.functionDeclaration(
8688
builders.identifier('validateRequest'),
87-
[request],
89+
[request, context],
8890
builders.blockStatement(functionNodes),
8991
),
9092
),

src/compileValueSchema.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ export function compileValueSchema(compiler: Compiler, schema: OpenAPIValueSchem
5959
}
6060

6161
function compileAnyOfSchema(compiler: Compiler, schema: OpenAPIAnyOfSchema) {
62-
return compiler.declareValidationFunction(schema, ({ value, path, error }) => {
62+
return compiler.declareValidationFunction(schema, ({ value, path, context, error }) => {
6363
const nodes: namedTypes.BlockStatement['body'] = [];
6464

6565
schema.anyOf.forEach((subSchema, index) => {
@@ -73,7 +73,7 @@ function compileAnyOfSchema(compiler: Compiler, schema: OpenAPIAnyOfSchema) {
7373
builders.variableDeclaration('const', [
7474
builders.variableDeclarator(
7575
identifier,
76-
builders.callExpression(fnIdentifier, [path, value]),
76+
builders.callExpression(fnIdentifier, [path, value, context]),
7777
),
7878
]),
7979
);
@@ -100,7 +100,7 @@ function compileAnyOfSchema(compiler: Compiler, schema: OpenAPIAnyOfSchema) {
100100
}
101101

102102
function compileOneOfSchema(compiler: Compiler, schema: OpenAPIOneOfSchema) {
103-
return compiler.declareValidationFunction(schema, ({ value, path, error }) => {
103+
return compiler.declareValidationFunction(schema, ({ value, path, context, error }) => {
104104
const nodes: namedTypes.BlockStatement['body'] = [];
105105

106106
// Declare the variable to use as a result, then iterate over each schema
@@ -118,7 +118,7 @@ function compileOneOfSchema(compiler: Compiler, schema: OpenAPIOneOfSchema) {
118118
builders.variableDeclaration('const', [
119119
builders.variableDeclarator(
120120
altIdentifier,
121-
builders.callExpression(fnIdentifier, [path, value]),
121+
builders.callExpression(fnIdentifier, [path, value, context]),
122122
),
123123
]),
124124
);
@@ -165,7 +165,7 @@ function compileOneOfSchema(compiler: Compiler, schema: OpenAPIOneOfSchema) {
165165
}
166166

167167
function compileAllOfSchema(compiler: Compiler, schema: OpenAPIAllOfSchema) {
168-
return compiler.declareValidationFunction(schema, ({ value, path, error }) => {
168+
return compiler.declareValidationFunction(schema, ({ value, path, context, error }) => {
169169
const nodes: namedTypes.BlockStatement['body'] = [];
170170

171171
const resultIdentifier = builders.identifier('result');
@@ -183,7 +183,7 @@ function compileAllOfSchema(compiler: Compiler, schema: OpenAPIAllOfSchema) {
183183
builders.assignmentExpression(
184184
'=',
185185
resultIdentifier,
186-
builders.callExpression(fnIdentifier, [path, resultIdentifier]),
186+
builders.callExpression(fnIdentifier, [path, resultIdentifier, context]),
187187
),
188188
),
189189
);
@@ -207,7 +207,7 @@ function compileAllOfSchema(compiler: Compiler, schema: OpenAPIAllOfSchema) {
207207
}
208208

209209
function compileObjectSchema(compiler: Compiler, schema: OpenAPIObjectSchema) {
210-
return compiler.declareValidationFunction(schema, ({ path, value, error }) => {
210+
return compiler.declareValidationFunction(schema, ({ path, value, context, error }) => {
211211
const nodes: namedTypes.BlockStatement['body'] = [];
212212
const endNodes: namedTypes.BlockStatement['body'] = [];
213213

@@ -312,6 +312,7 @@ function compileObjectSchema(compiler: Compiler, schema: OpenAPIObjectSchema) {
312312
propNameLiteral,
313313
]),
314314
subValueIdentifier,
315+
context
315316
]),
316317
),
317318
]),
@@ -412,6 +413,7 @@ function compileObjectSchema(compiler: Compiler, schema: OpenAPIObjectSchema) {
412413
property: builders.identifier('key'),
413414
computed: true,
414415
}),
416+
context,
415417
],
416418
),
417419
),
@@ -451,7 +453,7 @@ function compileObjectSchema(compiler: Compiler, schema: OpenAPIObjectSchema) {
451453
}
452454

453455
function compileArraySchema(compiler: Compiler, schema: OpenAPIArraySchema) {
454-
return compiler.declareValidationFunction(schema, ({ path, value, error }) => {
456+
return compiler.declareValidationFunction(schema, ({ path, value, context, error }) => {
455457
const nodes: namedTypes.BlockStatement['body'] = [];
456458

457459
nodes.push(...compileNullableCheck(compiler, schema, value));
@@ -545,6 +547,7 @@ function compileArraySchema(compiler: Compiler, schema: OpenAPIArraySchema) {
545547
index,
546548
]),
547549
builders.memberExpression(value, index, true),
550+
context,
548551
],
549552
),
550553
),

src/compiler.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,15 @@ export class Compiler {
6666
value: namedTypes.Identifier;
6767
/** Identifier for the path argument being passed to the function */
6868
path: namedTypes.Identifier;
69+
/** Identifier for the context argument being passed to the function */
70+
context: namedTypes.Identifier;
6971
/** Generate an error */
7072
error: (message: string) => namedTypes.NewExpression;
7173
}) => namedTypes.BlockStatement['body'],
7274
) {
7375
const pathIdentifier = builders.identifier('path');
7476
const valueIdentifier = builders.identifier('value');
77+
const contextIdentifier = builders.identifier('context');
7578

7679
const error = (message: string) => {
7780
return builders.newExpression(ValidationErrorIdentifier, [
@@ -83,11 +86,12 @@ export class Compiler {
8386
return this.declareForInput(input, (id) => {
8487
return builders.functionDeclaration(
8588
id,
86-
[pathIdentifier, valueIdentifier],
89+
[pathIdentifier, valueIdentifier, contextIdentifier],
8790
builders.blockStatement(
8891
gen({
8992
value: valueIdentifier,
9093
path: pathIdentifier,
94+
context: contextIdentifier,
9195
error,
9296
}),
9397
),

src/tests/__snapshots__/compileOperation.test.ts.snap

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Bun Snapshot v1, https://goo.gl/fbAQLP
22

33
exports[`With body without 1`] = `
4-
"export function validateRequest(request) {
4+
"export function validateRequest(request, context) {
55
return new ValidationError([], 'no operation match path');
66
}
77
class ValidationError extends Error {
@@ -10,7 +10,7 @@ class ValidationError extends Error {
1010
this.path = path;
1111
}
1212
}
13-
function obj0(path, value) {
13+
function obj0(path, value, context) {
1414
if (value.body !== undefined) {
1515
return new ValidationError(path, 'body is not allowed');
1616
}
@@ -19,7 +19,7 @@ function obj0(path, value) {
1919
`;
2020

2121
exports[`With body required 1`] = `
22-
"export function validateRequest(request) {
22+
"export function validateRequest(request, context) {
2323
return new ValidationError([], 'no operation match path');
2424
}
2525
class ValidationError extends Error {
@@ -28,13 +28,13 @@ class ValidationError extends Error {
2828
this.path = path;
2929
}
3030
}
31-
function obj2(path, value) {
31+
function obj2(path, value, context) {
3232
if (typeof value !== 'number') {
3333
return new ValidationError(path, 'Expected a number');
3434
}
3535
return value;
3636
}
37-
function obj1(path, value) {
37+
function obj1(path, value, context) {
3838
if (typeof value !== 'object' || value === null) {
3939
return new ValidationError(path, 'Expected an object');
4040
}
@@ -44,7 +44,7 @@ function obj1(path, value) {
4444
const result0 = obj2([
4545
...path,
4646
'foo'
47-
], value0);
47+
], value0, context);
4848
if (result0 instanceof ValidationError) {
4949
return result0;
5050
}
@@ -56,7 +56,7 @@ function obj1(path, value) {
5656
}
5757
return value;
5858
}
59-
function obj0(path, value) {
59+
function obj0(path, value, context) {
6060
if (value.body === undefined) {
6161
return new ValidationError(path, 'body is required');
6262
}

src/tests/__snapshots__/compilePath.test.ts.snap

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Bun Snapshot v1, https://goo.gl/fbAQLP
22

33
exports[`with get and post 1`] = `
4-
"export function validateRequest(request) {
4+
"export function validateRequest(request, context) {
55
return new ValidationError([], 'no operation match path');
66
}
77
class ValidationError extends Error {
@@ -10,19 +10,19 @@ class ValidationError extends Error {
1010
this.path = path;
1111
}
1212
}
13-
function obj1(path, value) {
13+
function obj1(path, value, context) {
1414
if (value.body !== undefined) {
1515
return new ValidationError(path, 'body is not allowed');
1616
}
1717
return value;
1818
}
19-
function obj4(path, value) {
19+
function obj4(path, value, context) {
2020
if (typeof value !== 'number') {
2121
return new ValidationError(path, 'Expected a number');
2222
}
2323
return value;
2424
}
25-
function obj3(path, value) {
25+
function obj3(path, value, context) {
2626
if (typeof value !== 'object' || value === null) {
2727
return new ValidationError(path, 'Expected an object');
2828
}
@@ -32,7 +32,7 @@ function obj3(path, value) {
3232
const result0 = obj4([
3333
...path,
3434
'foo'
35-
], value0);
35+
], value0, context);
3636
if (result0 instanceof ValidationError) {
3737
return result0;
3838
}
@@ -44,7 +44,7 @@ function obj3(path, value) {
4444
}
4545
return value;
4646
}
47-
function obj2(path, value) {
47+
function obj2(path, value, context) {
4848
if (value.body === undefined) {
4949
return new ValidationError(path, 'body is required');
5050
}
@@ -59,12 +59,12 @@ function obj2(path, value) {
5959
}
6060
return value;
6161
}
62-
function obj0(path, value) {
62+
function obj0(path, value, context) {
6363
if (value.method === 'get') {
64-
return obj1(path, value);
64+
return obj1(path, value, context);
6565
}
6666
if (value.method === 'post') {
67-
return obj2(path, value);
67+
return obj2(path, value, context);
6868
}
6969
return new ValidationError(path, 'method not supported');
7070
}"

0 commit comments

Comments
 (0)