Skip to content

Commit f133996

Browse files
authored
Merge pull request #4006 from github/mbg/json/schema-checker
Add schema checker to `json` module
2 parents 8096e4f + 0c2cea3 commit f133996

3 files changed

Lines changed: 383 additions & 49 deletions

File tree

lib/entry-points.js

Lines changed: 94 additions & 34 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/json/index.test.ts

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,76 @@ test("validateSchema - optional properties are optional", async (t) => {
6767
t.true(json.validateSchema(optionalSchema, { optionalKey: "" }));
6868
t.true(json.validateSchema(optionalSchema, { optionalKey: "foo" }));
6969
});
70+
71+
const arraySchema = {
72+
arrayKey: json.array(json.number),
73+
};
74+
75+
test("validateSchema - validates arrays", async (t) => {
76+
// Arrays of numeric elements are accepted.
77+
t.true(json.validateSchema(arraySchema, { arrayKey: [] }));
78+
t.true(json.validateSchema(arraySchema, { arrayKey: [4] }));
79+
t.true(json.validateSchema(arraySchema, { arrayKey: [4, 8] }));
80+
t.true(json.validateSchema(arraySchema, { arrayKey: [4, 8, 15] }));
81+
82+
// Other array elements are not accepted.
83+
t.false(json.validateSchema(arraySchema, { arrayKey: [4, 8, 15, "bar"] }));
84+
t.false(json.validateSchema(arraySchema, { arrayKey: [4, 8, undefined] }));
85+
t.false(json.validateSchema(arraySchema, { arrayKey: [4, 8, 15, null] }));
86+
});
87+
88+
const objectSchema = {
89+
objectKey: json.object(arraySchema),
90+
};
91+
92+
test("validateSchema - validates objects", async (t) => {
93+
// Objects of the given schema are accepted.
94+
t.true(json.validateSchema(objectSchema, { objectKey: { arrayKey: [] } }));
95+
t.true(json.validateSchema(objectSchema, { objectKey: { arrayKey: [4] } }));
96+
97+
// Other values are not accepted.
98+
t.false(json.validateSchema(objectSchema, {}));
99+
t.false(json.validateSchema(objectSchema, { objectKey: [] }));
100+
t.false(json.validateSchema(objectSchema, { objectKey: undefined }));
101+
t.false(json.validateSchema(objectSchema, { objectKey: null }));
102+
t.false(json.validateSchema(objectSchema, { objectKey: "foo" }));
103+
t.false(json.validateSchema(objectSchema, { objectKey: 123 }));
104+
});
105+
106+
const checkSchemaTestSchema = {
107+
rootKey: json.object(objectSchema),
108+
};
109+
110+
test("checkSchema - reports unknown keys", async (t) => {
111+
const result = json.checkSchema(checkSchemaTestSchema, {
112+
rootKey: {
113+
objectKey: {
114+
arrayKey: [],
115+
},
116+
nestedExtraKey: "foo",
117+
},
118+
extraKey: "bar",
119+
});
120+
121+
t.true(result.valid);
122+
t.deepEqual(
123+
result.unknownKeys.sort(),
124+
[".extraKey", ".rootKey.nestedExtraKey"].sort(),
125+
);
126+
});
127+
128+
test("checkSchema - reports invalid keys", async (t) => {
129+
const result = json.checkSchema(checkSchemaTestSchema, {
130+
rootKey: {
131+
objectKey: {
132+
arrayKey: ["foo"],
133+
},
134+
},
135+
});
136+
137+
t.false(result.valid);
138+
t.deepEqual(
139+
result.invalidKeys.sort(),
140+
[".rootKey.objectKey.arrayKey[0]"].sort(),
141+
);
142+
});

0 commit comments

Comments
 (0)