@hyperjump/json-schema
Advanced tools
Comparing version 1.2.0 to 1.2.1
export type JsonType = "object" | "array" | "string" | "number" | "boolean" | "null"; | ||
export type JsonSchemaType = JsonType | "integer"; | ||
type PathRelative = (from: string, to: string) => string; | ||
export const pathRelative: PathRelative; | ||
export const pathRelative: (from: string, to: string) => string; | ||
export type Replacer = (key: string, value: unknown) => unknown; | ||
export const jsonStringify: (value: unknown, replacer?: Replacer, space?: string) => string; |
import { resolveIri, toAbsoluteIri, parseIriReference } from "@hyperjump/uri"; | ||
import * as JsonPointer from "@hyperjump/json-pointer"; | ||
@@ -89,1 +90,63 @@ | ||
}; | ||
const defaultReplacer = (key, value) => value; | ||
export const jsonStringify = (value, replacer = defaultReplacer, space = "") => { | ||
return stringifyValue(value, replacer, space, "", JsonPointer.nil, 1); | ||
}; | ||
const stringifyValue = (value, replacer, space, key, pointer, depth) => { | ||
value = replacer(key, value, pointer); | ||
let result; | ||
if (Array.isArray(value)) { | ||
result = stringifyArray(value, replacer, space, pointer, depth); | ||
} else if (typeof value === "object" && value !== null) { | ||
result = stringifyObject(value, replacer, space, pointer, depth); | ||
} else { | ||
result = JSON.stringify(value); | ||
} | ||
return result; | ||
}; | ||
const stringifyArray = (value, replacer, space, pointer, depth) => { | ||
if (value.length === 0) { | ||
return "[]"; | ||
} | ||
const padding = space ? `\n${space.repeat(depth - 1)}` : ""; | ||
let result = "[" + padding + space; | ||
for (let index = 0; index < value.length; index++) { | ||
const indexPointer = JsonPointer.append(index, pointer); | ||
const stringifiedValue = stringifyValue(value[index], replacer, space, String(index), indexPointer, depth + 1); | ||
result += stringifiedValue === undefined ? "null" : stringifiedValue; | ||
if (index + 1 < value.length) { | ||
result += `,${padding}${space}`; | ||
} | ||
} | ||
return result + padding + "]"; | ||
}; | ||
const stringifyObject = (value, replacer, space, pointer, depth) => { | ||
const entries = Object.entries(value); | ||
if (entries.length === 0) { | ||
return "{}"; | ||
} | ||
const padding = space ? `\n${space.repeat(depth - 1)}` : ""; | ||
const colonSpacing = space ? " " : ""; | ||
let result = "{" + padding + space; | ||
for (let index = 0; index < entries.length; index++) { | ||
const [key, value] = entries[index]; | ||
const keyPointer = JsonPointer.append(key, pointer); | ||
const stringifiedValue = stringifyValue(value, replacer, space, key, keyPointer, depth + 1); | ||
if (stringifiedValue !== undefined) { | ||
result += JSON.stringify(key) + ":" + colonSpacing + stringifiedValue; | ||
if (entries[index + 1]) { | ||
result += `,${padding}${space}`; | ||
} | ||
} | ||
} | ||
return result + padding + "}"; | ||
}; |
@@ -1,2 +0,2 @@ | ||
import * as JsonPointer from "@hyperjump/json-pointer"; | ||
import { append as pointerAppend } from "@hyperjump/json-pointer"; | ||
import curry from "just-curry-it"; | ||
@@ -30,3 +30,3 @@ import { toAbsoluteUri, jsonTypeOf } from "./common.js"; | ||
...doc, | ||
pointer: JsonPointer.append(key, doc.pointer), | ||
pointer: pointerAppend(key, doc.pointer), | ||
value: value(doc)[key] | ||
@@ -33,0 +33,0 @@ }); |
import curry from "just-curry-it"; | ||
import * as Pact from "@hyperjump/pact"; | ||
import * as Json from "@hyperjump/json"; | ||
import * as JsonPointer from "@hyperjump/json-pointer"; | ||
import { jsonTypeOf, resolveUri, toAbsoluteUri, uriFragment, pathRelative } from "./common.js"; | ||
import { nil as nilPointer, append as pointerAppend, get as pointerGet } from "@hyperjump/json-pointer"; | ||
import { jsonTypeOf, resolveUri, toAbsoluteUri, uriFragment, pathRelative, jsonStringify } from "./common.js"; | ||
import fetch from "./fetch.js"; | ||
@@ -67,3 +66,3 @@ import { hasDialect, loadDialect, getKeywordName } from "./keywords.js"; | ||
dialectId: dialectId, | ||
schema: processSchema(schema, id, dialectId, JsonPointer.nil, anchors, dynamicAnchors), | ||
schema: processSchema(schema, id, dialectId, nilPointer, anchors, dynamicAnchors), | ||
anchors: anchors, | ||
@@ -130,7 +129,7 @@ dynamicAnchors: dynamicAnchors, | ||
for (const key in subject) { | ||
subject[key] = processSchema(subject[key], id, dialectId, JsonPointer.append(key, pointer), anchors, dynamicAnchors); | ||
subject[key] = processSchema(subject[key], id, dialectId, pointerAppend(key, pointer), anchors, dynamicAnchors); | ||
} | ||
} else if (Array.isArray(subject)) { | ||
for (let index = 0; index < subject.length; index++) { | ||
subject[index] = processSchema(subject[index], id, dialectId, JsonPointer.append(index, pointer), anchors, dynamicAnchors); | ||
subject[index] = processSchema(subject[index], id, dialectId, pointerAppend(index, pointer), anchors, dynamicAnchors); | ||
} | ||
@@ -153,3 +152,3 @@ } | ||
dialectId: undefined, | ||
pointer: JsonPointer.nil, | ||
pointer: nilPointer, | ||
schema: undefined, | ||
@@ -190,3 +189,3 @@ value: undefined, | ||
pointer: pointer, | ||
value: JsonPointer.get(pointer, storedSchema.schema) | ||
value: pointerGet(pointer, storedSchema.schema) | ||
}; | ||
@@ -217,3 +216,3 @@ | ||
...doc, | ||
pointer: JsonPointer.append(`${key}`, doc.pointer), | ||
pointer: pointerAppend(`${key}`, doc.pointer), | ||
value: value(doc)[key], | ||
@@ -269,3 +268,3 @@ validated: storedSchema.validated | ||
const schema = JSON.parse(Json.stringify(schemaDoc.schema, (key, value, pointer) => { | ||
const schema = JSON.parse(jsonStringify(schemaDoc.schema, (key, value, pointer) => { | ||
if (Reference.isReference(value)) { | ||
@@ -272,0 +271,0 @@ const refValue = Reference.value(value); |
{ | ||
"name": "@hyperjump/json-schema", | ||
"version": "1.2.0", | ||
"version": "1.2.1", | ||
"description": "A JSON Schema validator with support for custom keywords, vocabularies, and dialects", | ||
@@ -65,4 +65,3 @@ "type": "module", | ||
"dependencies": { | ||
"@hyperjump/json": "^0.1.0", | ||
"@hyperjump/json-pointer": "^0.9.5", | ||
"@hyperjump/json-pointer": "^1.0.0", | ||
"@hyperjump/pact": "^0.2.4", | ||
@@ -69,0 +68,0 @@ "@hyperjump/uri": "^1.0.0", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
288530
7
7816
+ Added@hyperjump/json-pointer@1.1.0(transitive)
- Removed@hyperjump/json@^0.1.0
- Removed@hyperjump/json@0.1.0(transitive)
- Removed@hyperjump/json-pointer@0.9.8(transitive)
- Removedjust-curry-it@5.3.0(transitive)
- Removedmoo@0.5.2(transitive)