Skip to content

Commit cd252e1

Browse files
committed
chore: cleanup
1 parent 3c68b7a commit cd252e1

File tree

11 files changed

+35
-30
lines changed

11 files changed

+35
-30
lines changed

src/compileSchema.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { draft07 } from "./draft07";
66
import { draft2019 } from "./draft2019";
77
import { draft2020 } from "./draft2020";
88
import { pick } from "./utils/pick";
9-
import { JsonSchema, Draft } from "./types";
9+
import { JsonSchema, Draft, BooleanSchema } from "./types";
1010
import { TemplateOptions } from "./methods/getData";
1111
import { SchemaNode, SchemaNodeMethods, addKeywords, isSchemaNode } from "./SchemaNode";
1212

@@ -29,7 +29,6 @@ function getDraft(drafts: Draft[], $schema: string) {
2929
* node will be reused for each task, but will create a compiledNode for bound data.
3030
*/
3131
export function compileSchema(schema: JsonSchema, options: CompileOptions = {}) {
32-
/** @todo this option has to be passed to all drafts (remotes) */
3332
let formatAssertion = options.formatAssertion ?? true;
3433
const drafts = options.drafts ?? defaultDrafts;
3534
const draft = getDraft(drafts, schema?.$schema);

src/draft04/keywords/$ref.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -145,5 +145,6 @@ function getRef(node: SchemaNode, $ref = node?.$ref): SchemaNode | undefined {
145145
// console.error("REF: UNFOUND 2", $ref);
146146
return undefined;
147147
}
148-
console.error("REF: UNHANDLED", $ref);
148+
149+
console.error("REF: INVALID", $ref);
149150
}

src/draft2019-09/keywords/items.ts

-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ function validateItems({ node, data, pointer = "#", path }: JsonSchemaValidatorP
5757
// note: schema is valid when data does not have enough elements as defined by array-list
5858
for (let i = 0; i < Math.min(node.prefixItems.length, data.length); i += 1) {
5959
const itemData = data[i];
60-
// @todo v1 reevaluate: incomplete schema is created here?
6160
const itemNode = node.prefixItems[i];
6261
const result = validateNode(itemNode, itemData, `${pointer}/${i}`, path);
6362
errors.push(...result);

src/draft2019-09/methods/getData.ts

-1
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,6 @@ const TYPE: Record<string, (node: SchemaNode, data: unknown, opts: TemplateOptio
309309
// @ts-expect-error asd
310310
if ((node.items && canResolveRef(node.items, opts)) || data?.length > 0) {
311311
// @attention this should disable cache or break intended behaviour as we reset it after loop
312-
// @todo test recursion of items
313312
// intention: reset cache after each property. last call will add counters
314313
const cache = { ...opts.cache };
315314
for (let i = 0, l = Math.max(minItems, d.length); i < l; i += 1) {

src/keywords/$defs.ts

+6-8
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,6 @@
11
import { Keyword } from "../Keyword";
22
import { SchemaNode } from "../types";
33

4-
// @todo this should be done with every added property in evaluationPath
5-
// @todo this creates a mixed schema, where $defs, etc are not uri-encoded (would be %24defs)
6-
function urlEncodeJsonPointerProperty(property: string) {
7-
property = property.replace(/~/g, "~0");
8-
property = property.replace(/\//g, "~1");
9-
return encodeURIComponent(property);
10-
}
11-
124
export const $defsKeyword: Keyword = {
135
id: "$defs",
146
keyword: "$defs",
@@ -37,3 +29,9 @@ export function parseDefs(node: SchemaNode) {
3729
});
3830
}
3931
}
32+
33+
function urlEncodeJsonPointerProperty(property: string) {
34+
property = property.replace(/~/g, "~0");
35+
property = property.replace(/\//g, "~1");
36+
return encodeURIComponent(property);
37+
}

src/keywords/$ref.ts

+2-5
Original file line numberDiff line numberDiff line change
@@ -194,14 +194,11 @@ export function getRef(node: SchemaNode, $ref = node?.$ref): SchemaNode | undefi
194194
return compileNext(node.context.remotes[$ref], node.evaluationPath);
195195
}
196196
if ($ref[0] === "#") {
197-
// @todo there is a bug joining multiple fragments to e.g. #/base#/examples/0
198-
// from "$id": "/base" + $ref "#/examples/0" (in refOfUnknownKeyword spec)
199-
const ref = $ref.match(/#[^#]*$/)?.pop(); // sanitize pointer
200197
// support refOfUnknownKeyword
201198
const rootSchema = node.context.rootNode.schema;
202-
const targetSchema = get(rootSchema, ref);
199+
const targetSchema = get(rootSchema, $ref);
203200
if (targetSchema) {
204-
return node.compileSchema(targetSchema, `${node.evaluationPath}/$ref`, ref);
201+
return node.compileSchema(targetSchema, `${node.evaluationPath}/$ref`, $ref);
205202
}
206203
}
207204
// console.error("REF: UNFOUND 1", $ref);

src/types.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { Draft } from "./Draft";
22
import { errors } from "./errors/errors";
33
import { SchemaNode, isSchemaNode, GetNodeOptions } from "./SchemaNode";
44

5-
export type JsonBooleanSchema = boolean;
5+
export type BooleanSchema = boolean;
66
export interface JsonSchema {
77
[p: string]: any;
88
}

src/utils/getSchemaType.ts

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { getTypeOf } from "./getTypeOf";
22
import { isObject } from "../utils/isObject";
3-
import { SchemaNode } from "../types";
3+
import { BooleanSchema, JsonSchema, SchemaNode } from "../types";
44

55
export const SCHEMA_TYPES = ["string", "number", "integer", "boolean", "null", "array", "object"];
66
const OBJECT_PROPERTIES = [
@@ -45,22 +45,22 @@ const ARRAY_PROPERTIES = [
4545
*/
4646
export function getSchemaType(node: SchemaNode, data: unknown): keyof typeof SCHEMA_TYPES | undefined {
4747
const dataType = getTypeOf(data);
48-
// @ts-expect-error boolean schema true
49-
if (node.schema === true) {
48+
const schema = node.schema as JsonSchema | BooleanSchema;
49+
if (schema === true) {
5050
return SCHEMA_TYPES.includes(dataType) ? (dataType as keyof typeof SCHEMA_TYPES) : undefined;
5151
}
5252
// boolean schema false or invalid schema
53-
if (!isObject(node.schema)) {
53+
if (!isObject(schema)) {
5454
return undefined;
5555
}
56-
const schemaType = node.schema.type;
56+
const schemaType = schema.type;
5757

5858
// type: []
5959
if (Array.isArray(schemaType)) {
6060
if (schemaType.includes(dataType)) {
6161
return dataType as keyof typeof SCHEMA_TYPES;
6262
}
63-
const defaultType = getTypeOf(node.schema.default);
63+
const defaultType = getTypeOf(schema.default);
6464
if (schemaType.includes(defaultType)) {
6565
return defaultType as keyof typeof SCHEMA_TYPES;
6666
}
@@ -73,13 +73,13 @@ export function getSchemaType(node: SchemaNode, data: unknown): keyof typeof SCH
7373
}
7474

7575
// type: undefined, enum: []
76-
if (Array.isArray(node.schema.enum)) {
77-
const schemaEnum: unknown[] = node.schema.enum;
76+
if (Array.isArray(schema.enum)) {
77+
const schemaEnum: unknown[] = schema.enum;
7878
const enumSchemaType = schemaEnum.map((value) => getTypeOf(value)).filter((p, i, l) => l.indexOf(p) === i);
7979
if (enumSchemaType.includes(dataType)) {
8080
return dataType as keyof typeof SCHEMA_TYPES;
8181
}
82-
const defaultType = getTypeOf(node.schema.default);
82+
const defaultType = getTypeOf(schema.default);
8383
if (enumSchemaType.includes(defaultType)) {
8484
return defaultType as keyof typeof SCHEMA_TYPES;
8585
}

src/utils/joinId.test.ts

+8
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,12 @@ describe("joinId", () => {
7979

8080
assert.equal(url, "https://example.com/absref/foobar.json");
8181
});
82+
83+
it("should ignore relative origin when next id starts with fragment url", () => {
84+
// there is a bug joining multiple fragments to e.g. #/base#/examples/0
85+
// from "$id": "/base" + $ref "#/examples/0" (in refOfUnknownKeyword spec)
86+
const url = joinId("/base", "#/examples/0");
87+
88+
assert.equal(url, "#/examples/0");
89+
});
8290
});

src/utils/joinId.ts

+4
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ function _joinScope(previous?: string, id?: string) {
2020
return id.replace(trailingHash, "");
2121
}
2222
if (id[0] === "#") {
23+
if (previous[0] === "/") {
24+
return id;
25+
}
26+
2327
return `${previous.replace(idAndPointer, "")}${id.replace(suffixes, "")}`;
2428
}
2529
if (isDomain.test(id)) {

src/validateNode.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import { JsonSchema, SchemaNode } from "./types";
1+
import { BooleanSchema, JsonSchema, SchemaNode } from "./types";
22
import { ValidationPath, ValidationResult } from "./Keyword";
33
import sanitizeErrors from "./utils/sanitizeErrors";
44

55
export function validateNode(node: SchemaNode, data: unknown, pointer: string, path?: ValidationPath) {
66
path?.push({ pointer, node });
7-
const schema = node.schema as boolean | JsonSchema;
7+
const schema = node.schema as BooleanSchema | JsonSchema;
88
if (schema === true) {
99
return [];
1010
}

0 commit comments

Comments
 (0)