Skip to content

Commit 018aedc

Browse files
committed
Minor cleanup and refactoring
1 parent 25d8fac commit 018aedc

File tree

2 files changed

+37
-64
lines changed

2 files changed

+37
-64
lines changed

src/index.js

Lines changed: 29 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,7 @@ import {
1212
} from "./jsonast-util.js";
1313

1414
/**
15-
* @import {
16-
* Json,
17-
* JsonNode,
18-
* JsonObjectNode,
19-
* JsonStringNode
20-
* } from "./jsonast.d.ts"
15+
* @import { Json, JsonNode, JsonObjectNode, JsonStringNode } from "./jsonast.d.ts"
2116
*/
2217

2318

@@ -139,10 +134,8 @@ keywordHandlers.set("additionalProperties", (additionalPropertiesNode, instanceN
139134
let isValid = true;
140135
for (const propertyNode of instanceNode.children) {
141136
const [propertyNameNode, instancePropertyNode] = propertyNode.children;
142-
if (!isDefinedProperty.test(propertyNameNode.value)) {
143-
if (!validateSchema(additionalPropertiesNode, instancePropertyNode)) {
144-
isValid = false;
145-
}
137+
if (!isDefinedProperty.test(propertyNameNode.value) && !validateSchema(additionalPropertiesNode, instancePropertyNode)) {
138+
isValid = false;
146139
}
147140
}
148141

@@ -384,35 +377,25 @@ keywordHandlers.set("dependentRequired", (dependentRequiredNode, instanceNode) =
384377

385378
assertNodeType(dependentRequiredNode, "object");
386379

387-
let isValid = true;
388-
for (const propertyNode of dependentRequiredNode.children) {
380+
return dependentRequiredNode.children.every((propertyNode) => {
389381
const [keyNode, requiredPropertiesNode] = propertyNode.children;
390-
if (jsonObjectHas(keyNode.value, instanceNode)) {
391-
assertNodeType(requiredPropertiesNode, "array");
392-
const isConditionValid = requiredPropertiesNode.children.every((requiredPropertyNode) => {
393-
assertNodeType(requiredPropertyNode, "string");
394-
return jsonObjectHas(requiredPropertyNode.value, instanceNode);
395-
});
396-
397-
if (!isConditionValid) {
398-
isValid = false;
399-
}
382+
if (!jsonObjectHas(keyNode.value, instanceNode)) {
383+
return true;
400384
}
401-
}
402385

403-
return isValid;
386+
assertNodeType(requiredPropertiesNode, "array");
387+
return requiredPropertiesNode.children.every((requiredPropertyNode) => {
388+
assertNodeType(requiredPropertyNode, "string");
389+
return jsonObjectHas(requiredPropertyNode.value, instanceNode);
390+
});
391+
});
404392
});
405393

406394
keywordHandlers.set("enum", (enumNode, instanceNode) => {
407395
assertNodeType(enumNode, "array");
408396

409397
const instanceValue = jsonStringify(jsonValue(instanceNode));
410-
for (const enumItemNode of enumNode.children) {
411-
if (jsonStringify(jsonValue(enumItemNode)) === instanceValue) {
412-
return true;
413-
}
414-
}
415-
return false;
398+
return enumNode.children.some((enumItemNode) => jsonStringify(jsonValue(enumItemNode)) === instanceValue);
416399
});
417400

418401
keywordHandlers.set("exclusiveMaximum", (exclusiveMaximumNode, instanceNode) => {
@@ -422,8 +405,7 @@ keywordHandlers.set("exclusiveMaximum", (exclusiveMaximumNode, instanceNode) =>
422405

423406
assertNodeType(exclusiveMaximumNode, "number");
424407

425-
const isValid = instanceNode.value < exclusiveMaximumNode.value;
426-
return isValid;
408+
return instanceNode.value < exclusiveMaximumNode.value;
427409
});
428410

429411
keywordHandlers.set("exclusiveMinimum", (exclusiveMinimumNode, instanceNode) => {
@@ -443,8 +425,7 @@ keywordHandlers.set("maxItems", (maxItemsNode, instanceNode) => {
443425

444426
assertNodeType(maxItemsNode, "number");
445427

446-
const isValid = instanceNode.children.length <= maxItemsNode.value;
447-
return isValid;
428+
return instanceNode.children.length <= maxItemsNode.value;
448429
});
449430

450431
keywordHandlers.set("minItems", (minItemsNode, instanceNode) => {
@@ -484,8 +465,7 @@ keywordHandlers.set("maxProperties", (maxPropertiesNode, instanceNode) => {
484465

485466
assertNodeType(maxPropertiesNode, "number");
486467

487-
const isValid = instanceNode.children.length <= maxPropertiesNode.value;
488-
return isValid;
468+
return instanceNode.children.length <= maxPropertiesNode.value;
489469
});
490470

491471
keywordHandlers.set("minProperties", (minPropertiesNode, instanceNode) => {
@@ -495,8 +475,7 @@ keywordHandlers.set("minProperties", (minPropertiesNode, instanceNode) => {
495475

496476
assertNodeType(minPropertiesNode, "number");
497477

498-
const isValid = instanceNode.children.length >= minPropertiesNode.value;
499-
return isValid;
478+
return instanceNode.children.length >= minPropertiesNode.value;
500479
});
501480

502481
keywordHandlers.set("maximum", (maximumNode, instanceNode) => {
@@ -506,8 +485,7 @@ keywordHandlers.set("maximum", (maximumNode, instanceNode) => {
506485

507486
assertNodeType(maximumNode, "number");
508487

509-
const isValid = instanceNode.value <= maximumNode.value;
510-
return isValid;
488+
return instanceNode.value <= maximumNode.value;
511489
});
512490

513491
keywordHandlers.set("minimum", (minimumNode, instanceNode) => {
@@ -550,6 +528,7 @@ keywordHandlers.set("required", (requiredNode, instanceNode) => {
550528
}
551529

552530
assertNodeType(requiredNode, "array");
531+
553532
for (const requiredPropertyNode of requiredNode.children) {
554533
assertNodeType(requiredPropertyNode, "string");
555534
if (!jsonObjectHas(requiredPropertyNode.value, instanceNode)) {
@@ -560,27 +539,19 @@ keywordHandlers.set("required", (requiredNode, instanceNode) => {
560539
});
561540

562541
keywordHandlers.set("type", (typeNode, instanceNode) => {
563-
if (typeNode.type === "json") {
564-
if (typeNode.jsonType === "string") {
542+
switch (typeNode.jsonType) {
543+
case "string":
565544
return isTypeOf(instanceNode, typeNode.value);
566-
}
567-
568-
if (typeNode.jsonType === "array") {
569-
for (const itemNode of typeNode.children) {
570-
if (itemNode.type !== "json" || itemNode.jsonType != "string") {
571-
throw Error("Invalid Schema");
572-
}
573545

574-
if (isTypeOf(instanceNode, itemNode.value)) {
575-
return true;
576-
}
577-
}
546+
case "array":
547+
return typeNode.children.some((itemNode) => {
548+
assertNodeType(itemNode, "string");
549+
return isTypeOf(instanceNode, itemNode.value);
550+
});
578551

579-
return false;
580-
}
552+
default:
553+
throw Error("Invalid Schema");
581554
}
582-
583-
throw Error("Invalid Schema");
584555
});
585556

586557
/** @type (instanceNode: JsonNode, type: string) => boolean */
@@ -603,7 +574,7 @@ keywordHandlers.set("uniqueItems", (uniqueItemsNode, instanceNode) => {
603574
return new Set(normalizedItems).size === normalizedItems.length;
604575
});
605576

606-
keywordHandlers.set("$id", (idNode, instanceNode, schemaNode) => {
577+
keywordHandlers.set("$id", (idNode, _instanceNode, schemaNode) => {
607578
if (!idNode.location.endsWith("#/$id")) {
608579
throw Error(`Embedded schemas are not supported. Found at ${schemaNode.location}`);
609580
}

src/jsonast-util.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,18 @@ import * as JsonPointer from "@hyperjump/json-pointer";
1919

2020
/** @type (json: Json, uri?: string, pointer?: string) => JsonNode */
2121
export const toJsonNode = (json, uri = "", pointer = "") => {
22+
const location = `${uri}#${encodeURI(pointer)}`;
23+
2224
switch (typeof json) {
2325
case "boolean":
24-
return { type: "json", jsonType: "boolean", value: json, location: `${uri}#${pointer}` };
26+
return { type: "json", jsonType: "boolean", value: json, location: location };
2527
case "number":
26-
return { type: "json", jsonType: "number", value: json, location: `${uri}#${pointer}` };
28+
return { type: "json", jsonType: "number", value: json, location: location };
2729
case "string":
28-
return { type: "json", jsonType: "string", value: json, location: `${uri}#${pointer}` };
30+
return { type: "json", jsonType: "string", value: json, location: location };
2931
case "object":
3032
if (json === null) {
31-
return { type: "json", jsonType: "null", value: json, location: `${uri}#${pointer}` };
33+
return { type: "json", jsonType: "null", value: json, location: location };
3234
} else if (Array.isArray(json)) {
3335
return {
3436
type: "json",
@@ -40,7 +42,7 @@ export const toJsonNode = (json, uri = "", pointer = "") => {
4042
};
4143
} else {
4244
/** @type JsonObjectNode */
43-
const objectNode = { type: "json", jsonType: "object", children: [], location: `${uri}#${pointer}` };
45+
const objectNode = { type: "json", jsonType: "object", children: [], location: location };
4446

4547
for (const property in json) {
4648
/** @type JsonPropertyNode */
@@ -87,7 +89,7 @@ export const jsonPointerStep = (segment, node, uri = "#") => {
8789
};
8890

8991
/** @type (pointer: string, tree: JsonNode, uri?: string) => JsonNode */
90-
export const jsonPointerGet = (pointer, tree, uri) => {
92+
export const jsonPointerGet = (pointer, tree, uri = "") => {
9193
let currentPointer = "";
9294
let node = tree;
9395
for (const segment of pointerSegments(pointer)) {

0 commit comments

Comments
 (0)