Skip to content

Commit 2f3817c

Browse files
committed
add tests
fix: #90
1 parent 9934338 commit 2f3817c

File tree

5 files changed

+115
-7
lines changed

5 files changed

+115
-7
lines changed

dist/index.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.mjs

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

dist/jlib.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
import { compileSchema } from "../../compileSchema";
2+
import { getSchemaType } from "../../utils/getSchemaType";
3+
import { strict as assert } from "assert";
4+
5+
describe("issue#90 - types or refs", () => {
6+
it("should get correct type for simple ref", () => {
7+
const schema = compileSchema({
8+
type: "object",
9+
$defs: {
10+
customConst: {
11+
type: "string"
12+
}
13+
},
14+
properties: {
15+
name: { $ref: "#/$defs/customConst" }
16+
}
17+
});
18+
const nameProp = schema.getNodeChild("name").node;
19+
assert(nameProp != null);
20+
assert(getSchemaType(nameProp, undefined) === "string");
21+
});
22+
23+
it("should handle oneOf refs", () => {
24+
const schema = compileSchema({
25+
type: "object",
26+
$defs: {
27+
a: {
28+
type: "string",
29+
const: "a"
30+
},
31+
b: {
32+
type: "string",
33+
const: "b"
34+
}
35+
},
36+
37+
properties: {
38+
oneOf: {
39+
oneOf: [{ $ref: "#/$defs/a" }, { $ref: "#/$defs/b" }]
40+
}
41+
}
42+
});
43+
const oneOfProp = schema.getNodeChild("oneOf").node;
44+
assert(oneOfProp != null);
45+
assert(getSchemaType(oneOfProp, undefined) === "string");
46+
});
47+
it("should handle anyOf refs", () => {
48+
const schema = compileSchema({
49+
type: "object",
50+
$defs: {
51+
a: { type: "string", const: "a" },
52+
b: { type: "string", const: "b" }
53+
},
54+
properties: {
55+
anyOf: {
56+
anyOf: [{ $ref: "#/$defs/a" }, { $ref: "#/$defs/b" }]
57+
}
58+
}
59+
});
60+
const anyOfProp = schema.getNodeChild("anyOf").node;
61+
assert(anyOfProp != null);
62+
assert(getSchemaType(anyOfProp, undefined) === "string");
63+
});
64+
it("should handle allOf refs", () => {
65+
const schema = compileSchema({
66+
type: "object",
67+
$defs: {
68+
a: { type: "object", properties: { a: { type: "string" } } },
69+
b: { type: "object", properties: { b: { type: "string" } } }
70+
},
71+
properties: {
72+
allOf: {
73+
allOf: [{ $ref: "#/$defs/a" }, { $ref: "#/$defs/b" }]
74+
}
75+
}
76+
});
77+
const allOfProp = schema.getNodeChild("allOf").node;
78+
assert(allOfProp != null);
79+
assert(getSchemaType(allOfProp, undefined) === "object");
80+
});
81+
it("should handle if/then/else refs", () => {
82+
const schema = compileSchema({
83+
type: "object",
84+
$defs: {
85+
stringSchema: {
86+
type: "string"
87+
}
88+
},
89+
properties: {
90+
conditional: {
91+
if: { $ref: "#/$defs/stringSchema" },
92+
then: { minLength: 5 },
93+
else: { maxLength: 2 }
94+
}
95+
}
96+
});
97+
const conditionalProp = schema.getNodeChild("conditional").node;
98+
assert(conditionalProp != null);
99+
assert(getSchemaType(conditionalProp, undefined) === "string");
100+
});
101+
});

src/utils/getSchemaType.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,12 +106,12 @@ export function getSchemaType(node: SchemaNode, data: unknown): SchemaType | und
106106

107107
// nothing found yet check dynamic properties for a type
108108
if (node.if) {
109-
return getSchemaType(node.if.resolveRef?.() ?? node.if, data);
109+
return getSchemaType(node.if.getNode("#", data)?.node ?? node.if, data);
110110
}
111111

112112
if (node.allOf) {
113113
for (let i = 0; i < node.allOf.length; i += 1) {
114-
const type = getSchemaType(node.allOf[i].resolveRef?.() ?? node.allOf[i], data);
114+
const type = getSchemaType(node.allOf[i].getNode("#", data)?.node ?? node.allOf[i], data);
115115
if (type) {
116116
return type;
117117
}
@@ -120,7 +120,7 @@ export function getSchemaType(node: SchemaNode, data: unknown): SchemaType | und
120120

121121
if (node.oneOf) {
122122
for (let i = 0; i < node.oneOf.length; i += 1) {
123-
const type = getSchemaType(node.oneOf[i].resolveRef?.() ?? node.oneOf[i], data);
123+
const type = getSchemaType(node.oneOf[i].getNode("#", data)?.node ?? node.oneOf[i], data);
124124
if (type) {
125125
return type;
126126
}
@@ -129,12 +129,19 @@ export function getSchemaType(node: SchemaNode, data: unknown): SchemaType | und
129129

130130
if (node.anyOf) {
131131
for (let i = 0; i < node.anyOf.length; i += 1) {
132-
const type = getSchemaType(node.anyOf[i].resolveRef?.() ?? node.anyOf[i], data);
132+
const type = getSchemaType(node.anyOf[i].getNode("#", data)?.node ?? node.anyOf[i], data);
133133
if (type) {
134134
return type;
135135
}
136136
}
137137
}
138138

139+
if (schema.$ref) {
140+
const refNode = node.getNode("#", data)?.node;
141+
if (refNode) {
142+
return getSchemaType(refNode, data);
143+
}
144+
}
145+
139146
return undefined;
140147
}

0 commit comments

Comments
 (0)