Skip to content

Commit d17218f

Browse files
authored
Merge pull request #11 from cloudflare/dev
fixes a bug where root level $ref's weren't being resolved correctly
2 parents aa1c279 + a41abe9 commit d17218f

File tree

4 files changed

+85
-4
lines changed

4 files changed

+85
-4
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22

33
All notable changes to this project will be documented in this file.
44

5+
## [0.2.4] - 2025-03-24
6+
7+
### Changed
8+
9+
- Fixed a bug where root level $ref's were not being resolved correctly
10+
511
## [0.2.3] - 2025-03-19
612

713
### Changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@cloudflare/cabidela",
3-
"version": "0.2.3",
3+
"version": "0.2.4",
44
"description": "Cabidela is a small, fast, eval-less, Cloudflare Workers compatible, dynamic JSON Schema validator",
55
"main": "dist/index.js",
66
"module": "dist/index.mjs",

src/helpers.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ export const traverseSchema = (options: CabidelaOptions, definitions: any, obj:
5151
if (cb) {
5252
cb(merge);
5353
} else {
54-
// root level merge
54+
// root level
55+
hits++;
5556
Object.assign(obj, merge);
5657
delete obj[key];
5758
}
@@ -61,7 +62,14 @@ export const traverseSchema = (options: CabidelaOptions, definitions: any, obj:
6162
const { $id, $path } = parse$ref(obj[key]);
6263
const { resolvedObject } = resolvePayload($path, definitions[$id]);
6364
if (resolvedObject) {
64-
cb(resolvedObject);
65+
if (cb) {
66+
cb(resolvedObject);
67+
} else {
68+
// root level
69+
hits++;
70+
Object.assign(obj, resolvedObject);
71+
delete obj[key];
72+
}
6573
} else {
6674
throw new Error(`Could not resolve '${obj[key]}' $ref`);
6775
}

tests/05-id-refs.test.ts

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { FakeCabidela } from "./lib/fake-cabidela";
33
import { getMetaData } from "../src/helpers";
44

55
describe("ref and subschema", () => {
6-
let schema:any;
6+
let schema: any;
77
if (process.env.AJV) {
88
schema = {
99
$id: "http://example.com/schemas/main",
@@ -68,6 +68,73 @@ describe("ref and subschema", () => {
6868
test.skipIf(process.env.AJV)("consolidated schema", () => {
6969
const cabidela = new FakeCabidela(schema, { subSchemas: [contactSchema] });
7070
const cs = cabidela.getSchema();
71+
expect(cs).toStrictEqual({
72+
$id: "http://example.com/schemas/main",
73+
type: "object",
74+
properties: {
75+
name: {
76+
type: "string",
77+
},
78+
contacts: {
79+
type: "object",
80+
properties: {
81+
email: {
82+
type: "string",
83+
},
84+
phone: {
85+
type: "string",
86+
},
87+
},
88+
required: ["email", "phone"],
89+
},
90+
address: {
91+
type: "object",
92+
properties: {
93+
street: {
94+
type: "string",
95+
},
96+
city: {
97+
type: "string",
98+
},
99+
zip: {
100+
type: "string",
101+
},
102+
country: {
103+
type: "string",
104+
},
105+
},
106+
required: ["street", "city", "zip", "country"],
107+
},
108+
balance: {
109+
type: "object",
110+
properties: {
111+
currency: {
112+
type: "string",
113+
},
114+
amount: {
115+
type: "number",
116+
},
117+
},
118+
required: ["currency", "amount"],
119+
},
120+
},
121+
required: ["name", "contacts", "address"],
122+
});
123+
});
124+
125+
test.skipIf(process.env.AJV)("consolidated root schema", () => {
126+
const cabidela = new FakeCabidela(
127+
{
128+
$ref: "customer#/contacts",
129+
},
130+
{ subSchemas: [contactSchema] },
131+
);
132+
const cs = cabidela.getSchema();
133+
expect(cs).toStrictEqual({
134+
type: "object",
135+
properties: { email: { type: "string" }, phone: { type: "string" } },
136+
required: ["email", "phone"],
137+
});
71138
});
72139

73140
test.skipIf(process.env.AJV)("$defs", () => {

0 commit comments

Comments
 (0)