Skip to content

Commit eadf9dc

Browse files
committed
Fix oneOf and nullable+enum combined
1 parent 1f90c41 commit eadf9dc

File tree

3 files changed

+66
-13
lines changed

3 files changed

+66
-13
lines changed

src/compileValueSchema.ts

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -134,9 +134,6 @@ function compileOneOfSchema(compiler: Compiler, schema: OpenAPIOneOfSchema) {
134134
),
135135
),
136136
builders.blockStatement([
137-
builders.expressionStatement(
138-
builders.assignmentExpression('=', resultIdentifier, altIdentifier),
139-
),
140137
...(index > 0
141138
? [
142139
builders.ifStatement(
@@ -153,11 +150,25 @@ function compileOneOfSchema(compiler: Compiler, schema: OpenAPIOneOfSchema) {
153150
),
154151
]
155152
: []),
153+
builders.expressionStatement(
154+
builders.assignmentExpression('=', resultIdentifier, altIdentifier),
155+
),
156156
]),
157157
),
158158
);
159159
});
160160

161+
nodes.push(
162+
builders.ifStatement(
163+
builders.binaryExpression(
164+
'===',
165+
resultIdentifier,
166+
builders.identifier('undefined'),
167+
),
168+
builders.blockStatement([builders.returnStatement(error('expected to match one'))]),
169+
),
170+
);
171+
161172
nodes.push(builders.returnStatement(resultIdentifier));
162173

163174
return nodes;
@@ -597,13 +608,14 @@ function compileNumberSchema(
597608
schema: OpenAPINumberSchema | OpenAPIIntegerSchema,
598609
) {
599610
return compiler.declareValidationFunction(schema, ({ value, error }) => {
611+
const nodes: namedTypes.BlockStatement['body'] = [];
612+
nodes.push(...compileNullableCheck(compiler, schema, value));
613+
600614
const enumCheck = compileEnumableCheck(compiler, schema, value, error);
601615
if (enumCheck) {
602-
return enumCheck;
616+
return [...nodes, ...enumCheck];
603617
}
604618

605-
const nodes: namedTypes.BlockStatement['body'] = [];
606-
nodes.push(...compileNullableCheck(compiler, schema, value));
607619
nodes.push(
608620
builders.ifStatement(
609621
builders.binaryExpression(
@@ -623,13 +635,14 @@ function compileNumberSchema(
623635

624636
function compileStringSchema(compiler: Compiler, schema: OpenAPIStringSchema) {
625637
return compiler.declareValidationFunction(schema, ({ value, context, path, error }) => {
638+
const nodes: namedTypes.BlockStatement['body'] = [];
639+
nodes.push(...compileNullableCheck(compiler, schema, value));
640+
626641
const enumCheck = compileEnumableCheck(compiler, schema, value, error);
627642
if (enumCheck) {
628-
return enumCheck;
643+
return [...nodes, ...enumCheck];
629644
}
630645

631-
const nodes: namedTypes.BlockStatement['body'] = [];
632-
nodes.push(...compileNullableCheck(compiler, schema, value));
633646
nodes.push(
634647
builders.ifStatement(
635648
builders.binaryExpression(
@@ -752,13 +765,14 @@ function compileStringSchema(compiler: Compiler, schema: OpenAPIStringSchema) {
752765

753766
function compileBooleanSchema(compiler: Compiler, schema: OpenAPIBooleanSchema) {
754767
return compiler.declareValidationFunction(schema, ({ value, error }) => {
768+
const nodes: namedTypes.BlockStatement['body'] = [];
769+
nodes.push(...compileNullableCheck(compiler, schema, value));
770+
755771
const enumCheck = compileEnumableCheck(compiler, schema, value, error);
756772
if (enumCheck) {
757-
return enumCheck;
773+
return [...nodes, ...enumCheck];
758774
}
759775

760-
const nodes: namedTypes.BlockStatement['body'] = [];
761-
nodes.push(...compileNullableCheck(compiler, schema, value));
762776
nodes.push(
763777
builders.ifStatement(
764778
builders.binaryExpression(

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -785,10 +785,13 @@ function obj0(path, value, context) {
785785
}
786786
const alt1 = obj2(path, value, context);
787787
if (!(alt1 instanceof ValidationError)) {
788-
result = alt1;
789788
if (result !== undefined) {
790789
return new ValidationError(path, 'expected to only match one of the schemas');
791790
}
791+
result = alt1;
792+
}
793+
if (result === undefined) {
794+
return new ValidationError(path, 'expected to match one');
792795
}
793796
return result;
794797
}"

tests/gitbook.test.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,3 +103,39 @@ test('PUT orgs/apple/schemas/newType', () => {
103103
},
104104
});
105105
});
106+
107+
test('POST orgs/apple/members/jony (invalid)', () => {
108+
const result = validateRequest({
109+
path: '/orgs/apple/members/jony',
110+
method: 'patch',
111+
headers: {
112+
'content-type': 'application/json',
113+
},
114+
query: {},
115+
body: {
116+
role: 'invalid',
117+
},
118+
});
119+
expect(result instanceof ValidationError ? result.path : null).toEqual(['body', 'role']);
120+
});
121+
122+
test('POST orgs/apple/members/jony (null)', () => {
123+
const result = validateRequest({
124+
path: '/orgs/apple/members/jony',
125+
method: 'patch',
126+
headers: {
127+
'content-type': 'application/json',
128+
},
129+
query: {},
130+
body: {
131+
role: null,
132+
},
133+
});
134+
expect(result).toMatchObject({
135+
operationId: 'updateMemberInOrganizationById',
136+
params: {
137+
organizationId: 'apple',
138+
userId: 'jony',
139+
},
140+
});
141+
});

0 commit comments

Comments
 (0)