Skip to content

Commit

Permalink
dist: update
Browse files Browse the repository at this point in the history
  • Loading branch information
sagold committed Apr 6, 2024
1 parent c6383cb commit ae6f1d4
Show file tree
Hide file tree
Showing 31 changed files with 694 additions and 89 deletions.
3 changes: 3 additions & 0 deletions dist/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { Draft } from "./lib/draft";
import { Draft04, draft04Config } from "./lib/draft04";
import { Draft06, draft06Config } from "./lib/draft06";
import { Draft07, draft07Config } from "./lib/draft07";
import { Draft2019, draft2019Config } from "./lib/draft2019";
import { JsonEditor, draftJsonEditorConfig } from "./lib/jsoneditor";
import { isJsonError } from "./lib/types";
declare const config: {
Expand All @@ -25,6 +26,8 @@ Draft06, // core implementing draft06 specs
draft06Config, // config implementing draft06 specs
Draft07, // core implementing draft07 specs
draft07Config, // config implementing draft07 specs
Draft2019, // core implementing draft2019-09 specs
draft2019Config, // config implementing draft2019-09 specs
draftJsonEditorConfig, // adjusted config of draft04 to better support the json-editor
getTypeOf, // returns the javascript datatype
isDynamicSchema, // NEW
Expand Down
3 changes: 1 addition & 2 deletions dist/jsonSchemaLibrary.js

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion dist/jsonSchemaLibrary.js.LICENSE.txt

This file was deleted.

2 changes: 1 addition & 1 deletion dist/lib/compile/getRef.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import { JsonSchema } from "../types";
import { Context } from "./types";
export default function getRef(context: Context, rootSchema: JsonSchema, $ref: string): JsonSchema;
export default function getRef(context: Context, rootSchema: JsonSchema, $search: string | JsonSchema): JsonSchema;
8 changes: 8 additions & 0 deletions dist/lib/draft04/addRemoteSchema.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Draft } from "../draft";
import { JsonSchema } from "../types";
/**
* register a json-schema to be referenced from another json-schema
* @param url base-url of json-schema (aka id)
* @param schema
*/
export default function addRemoteSchema(draft: Draft, url: string, schema: JsonSchema): void;
5 changes: 4 additions & 1 deletion dist/lib/draft06/compile/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { Draft } from "../../draft";
import { JsonSchema } from "../../types";
/**
* @draft starting with _draft 06_ keyword `id` has been renamed to `$id`
* @draft starting with _draft 2019-09_ plain name fragments are no longer
* defined with $id, but instead with the new keyword $anchor
* (which has a different syntax)
* https://json-schema.org/draft/2019-09/release-notes#incompatible-changes
*
* compiles the input root schema for $ref resolution and returns it again
* @attention this modifies input schema but maintains object-structure
Expand Down
7 changes: 7 additions & 0 deletions dist/lib/draft2019/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { DraftConfig, Draft } from "../draft";
import { JsonSchema } from "../types";
declare const draft2019Config: DraftConfig;
declare class Draft2019 extends Draft {
constructor(schema?: JsonSchema, config?: Partial<DraftConfig>);
}
export { Draft2019, draft2019Config };
3 changes: 3 additions & 0 deletions dist/lib/draft2019/validation/keyword.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { JsonValidator } from "../../types";
declare const KeywordValidation: Record<string, JsonValidator>;
export default KeywordValidation;
14 changes: 11 additions & 3 deletions dist/lib/features/dependencies.d.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,26 @@
/**
* @draft-06
* @draft 06, 2019-09
*/
import { JsonSchema, JsonValidator } from "../types";
import { Draft } from "../draft";
/**
* @todo add support for dependentRequired (draft 2019-09)
* returns dependencies as an object json schema. does not merge with input
* json schema. you probably will need to do so to correctly resolve
* references.
*
* @returns merged json schema defined by dependencies or undefined
*/
export declare function resolveDependencies(draft: Draft, schema: JsonSchema, data: unknown): JsonSchema | undefined;
/**
* @draft 2019-09
*/
export declare const validateDependentRequired: JsonValidator;
/**
* @draft 2019-09
*/
export declare const validateDependentSchemas: JsonValidator;
/**
* validate dependencies definition for given input data
*/
declare const validateDependencies: JsonValidator;
export { validateDependencies };
export declare const validateDependencies: JsonValidator;
4 changes: 3 additions & 1 deletion dist/lib/mergeSchema.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@ import { JsonSchema } from "./types";
* merges to two json schema. In case of conflicts, will use overwrite first
* schema or directly return first json schema.
*/
export declare function mergeSchema(a: JsonSchema, b: JsonSchema): JsonSchema;
export declare function _mergeSchema(a: JsonSchema, b: JsonSchema): JsonSchema;
export declare function mergeSchema<T extends JsonSchema>(a: T, b: T): T;
export declare function mergeSchema2(a: unknown, b: unknown, property?: string): unknown;
4 changes: 4 additions & 0 deletions dist/lib/resolveRef.merge.d.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
import { JsonSchema } from "./types";
/**
* @todo update types
* Note: JsonSchema my be false
*/
export default function resolveRefMerge(schema: JsonSchema, rootSchema: JsonSchema): JsonSchema;
3 changes: 3 additions & 0 deletions dist/module/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { Draft } from "./lib/draft";
import { Draft04, draft04Config } from "./lib/draft04";
import { Draft06, draft06Config } from "./lib/draft06";
import { Draft07, draft07Config } from "./lib/draft07";
import { Draft2019, draft2019Config } from "./lib/draft2019";
import { JsonEditor, draftJsonEditorConfig } from "./lib/jsoneditor";
import { isJsonError } from "./lib/types";
const config = { strings };
Expand All @@ -24,6 +25,8 @@ Draft06, // core implementing draft06 specs
draft06Config, // config implementing draft06 specs
Draft07, // core implementing draft07 specs
draft07Config, // config implementing draft07 specs
Draft2019, // core implementing draft2019-09 specs
draft2019Config, // config implementing draft2019-09 specs
draftJsonEditorConfig, // adjusted config of draft04 to better support the json-editor
getTypeOf, // returns the javascript datatype
isDynamicSchema, // NEW
Expand Down
3 changes: 2 additions & 1 deletion dist/module/lib/addRemoteSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* @param schema
*/
export default function addRemoteSchema(draft, url, schema) {
schema.id = schema.id || url;
// @draft >= 6
schema.$id = schema.$id || url;
draft.remotes[url] = draft.compileSchema(schema);
}
88 changes: 69 additions & 19 deletions dist/module/lib/compile/getRef.js
Original file line number Diff line number Diff line change
@@ -1,71 +1,121 @@
import { get } from "@sagold/json-pointer";
import splitRef from "./splitRef";
import getTypeOf from "../getTypeOf";
const suffixes = /(#|\/)+$/g;
const suffixes = /(#)+$/g;
const isObject = (val) => getTypeOf(val) === "object";
// 1. combined is known
// 2. base or pointer is known
// 3. base + pointer is known
export default function getRef(context, rootSchema, $ref) {
if (isObject($ref)) {
$ref = $ref.__ref || $ref.$ref;
export default function getRef(context, rootSchema, $search) {
var _a, _b, _c, _d, _e;
let $ref;
if (isObject($search)) {
$ref = $search.__ref || $search.$ref;
}
else {
$ref = $search;
}
if ($ref == null) {
return rootSchema;
}
// console.log("\n$ref", $ref);
let schema;
// is it a known $ref?
const $remote = $ref.replace(suffixes, "");
if (context.remotes[$remote]) {
if (context.remotes[$remote] != null) {
schema = context.remotes[$remote];
// console.log("» remote");
if (schema && schema.$ref) {
return getRef(context, rootSchema, schema.$ref);
// console.log("»» reresolve", schema);
// @todo add missing test for the following line
return getRef(context, schema, schema);
}
// console.log("»» return", schema);
return schema;
}
if (context.ids[$ref]) {
// @ts-expect-error @draft 2019-09
const $anchor = (_a = context.anchors) === null || _a === void 0 ? void 0 : _a[$ref];
if ($anchor) {
// console.log("» anchor", $anchor);
return get(rootSchema, $anchor);
}
if (context.ids[$ref] != null) {
// console.log("» id", context.ids[$ref]);
schema = get(rootSchema, context.ids[$ref]);
if (schema && schema.$ref) {
return getRef(context, rootSchema, schema.$ref);
// @todo add missing test for the following line
return getRef(context, rootSchema, schema);
}
return schema;
}
const $inputRef = $ref;
// is it a ref with host/pointer?
const fragments = splitRef($ref);
if (fragments.length === 0) {
return rootSchema;
}
if (fragments.length === 1) {
// console.log("» frag1", fragments);
// console.log("ids", rootSchema.getContext().ids);
$ref = fragments[0];
if (context.remotes[$ref]) {
// console.log("» remote");
schema = context.remotes[$ref];
return getRef(context, rootSchema, schema.$ref);
if (schema && schema.$ref) {
return getRef(context, rootSchema, schema);
}
}
if (context.ids[$ref]) {
// console.log("» id");
schema = get(rootSchema, context.ids[$ref]);
if (schema && schema.$ref) {
return getRef(context, rootSchema, schema.$ref);
return getRef(context, rootSchema, schema);
}
return schema;
}
// @todo why this special case
const rootContextRef = (_b = rootSchema.getContext) === null || _b === void 0 ? void 0 : _b.call(rootSchema).ids[$ref];
if (rootContextRef) {
return getRef(context, rootSchema, rootContextRef);
}
}
if (fragments.length === 2) {
// console.log("» frag2", fragments);
const base = fragments[0];
$ref = fragments[1];
if (context.remotes[base]) {
if (context.remotes[base].getRef) {
return context.remotes[base].getRef($ref);
// @todo this is unnecessary due to inconsistencies
const fromRemote = (_c = context.remotes[base]) !== null && _c !== void 0 ? _c : context.remotes[`${base}/`];
if (fromRemote) {
// console.log("» remote");
// We have retrieved a different compiled json-schema. This compiled schema contains a
// separate scope (context) where we might need to work with
// ANCHOR
if (fromRemote.getContext && fromRemote.getContext().anchors[$inputRef] != null) {
// console.log("» remote » anchor");
// an anchor is stored with its scope (id) it is defined in. Thus collisions are
// avoided, but the current condition is required to resolve the anchor for now
return fromRemote.getRef($inputRef);
}
// PATH (get_ref)
if (fromRemote.getRef) {
// console.log("» remote » ref");
// resolve the local part of the reference in the new schema
return fromRemote.getRef($ref);
}
// console.log("warning: uncompiled remote - context may be wrong", base);
return getRef(context, context.remotes[base], $ref);
//log("warning: uncompiled remote - context may be wrong", base);
return getRef(context, fromRemote, $ref);
}
if (context.ids[base]) {
return getRef(context, get(rootSchema, context.ids[base]), $ref);
// @todo this is unnecessary due to inconsistencies
const fromId = (_d = context.ids[base]) !== null && _d !== void 0 ? _d : context.ids[`${base}/`];
if (fromId) {
// console.log("» id", fromId);
return getRef(context, get(rootSchema, fromId), $ref);
}
}
schema = get(rootSchema, context.ids[$ref] || $ref);
// console.log("» other");
schema = get(rootSchema, (_e = context.ids[$ref]) !== null && _e !== void 0 ? _e : $ref);
if (schema && schema.$ref) {
return getRef(context, rootSchema, schema.$ref);
return getRef(context, rootSchema, schema);
}
return schema;
}
17 changes: 14 additions & 3 deletions dist/module/lib/compile/joinScope.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
/* eslint max-statements-per-line: ["error", { "max": 2 }] */
const suffixes = /(#|\/)+$/;
const suffixes = /(#)+$/;
const trailingHash = /#$/;
const startingHashAndSlash = /^[#/]+/;
const isDomain = /^[^:]+:\/\/[^/]+\//;
const trailingFragments = /\/[^/]*$/;
const idAndPointer = /#.*$/;
// @todo add missing test for urn ids
const isURN = /^urn:uuid:[0-9A-Fa-f]/;
export default function joinScope(previous, id) {
if (previous == null && id == null) {
return "#";
}
if (id == null) {
return previous.replace(trailingHash, "");
}
if (previous == null) {
if (isURN.test(id)) {
return id;
}
if (previous == null || previous === "" || previous === "#") {
return id.replace(trailingHash, "");
}
if (id[0] === "#") {
Expand All @@ -20,5 +26,10 @@ export default function joinScope(previous, id) {
if (isDomain.test(id)) {
return id.replace(trailingHash, "");
}
return `${previous.replace(trailingFragments, "")}/${id.replace(trailingHash, "")}`;
if (isDomain.test(previous) && id.startsWith("/")) {
// we have a domain that should be joined with an absolute path
// thus we have to remove all paths from domain before joining
return `${previous.replace(/(^[^:]+:\/\/[^/]+)(.*)/, "$1")}/${id.replace(startingHashAndSlash, "")}`;
}
return `${previous.replace(trailingFragments, "")}/${id.replace(startingHashAndSlash, "")}`;
}
6 changes: 3 additions & 3 deletions dist/module/lib/compile/splitRef.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
const suffixes = /(#|\/)+$/g;
const suffixes = /(#)+$/g;
const emptyValues = ["", null, "#"];
export default function splitRef($ref) {
if (emptyValues.includes($ref)) {
return [];
}
$ref = $ref.replace(suffixes, "");
if ($ref.indexOf("#") === -1) {
return [$ref.replace(suffixes, "")];
return [$ref.replace(/(#|\/)+$/g, "")];
}
if ($ref.indexOf("#") === 0) {
return [$ref.replace(suffixes, "")];
}
const result = $ref.split("#");
result[0] = result[0].replace(suffixes, "");
result[0] = result[0].replace(/(#|\/)+$/g, "");
result[1] = `#${result[1].replace(suffixes, "")}`;
return result;
}
4 changes: 4 additions & 0 deletions dist/module/lib/config/strings.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ export default {
containsAnyError: "The array at `{{pointer}}` must contain at least one item",
ContainsArrayError: "The property at `{{pointer}}` must not be an array",
ContainsError: "The array at `{{pointer}}` must contain an element that matches `{{schema}}`",
ContainsMinError: "The array at `{{pointer}}` contains {{delta}} too few items matching `{{schema}}`",
ContainsMaxError: "The array at `{{pointer}}` contains {{delta}} too many items matching `{{schema}}`",
EnumError: "Expected given value `{{value}}` in `{{pointer}}` to be one of `{{values}}`",
ForbiddenPropertyError: "Property name `{{property}}` at `{{pointer}}` is not allowed",
FormatDateError: "Value `{{value}}` at `{{pointer}}` is not a valid date",
Expand Down Expand Up @@ -52,6 +54,8 @@ export default {
SchemaWarning: "Failed retrieving a schema from '{{pointer}}' to key '{{key}}'",
TypeError: "Expected `{{value}}` ({{received}}) in `{{pointer}}` to be of type `{{expected}}`",
UndefinedValueError: "Value must not be undefined in `{{pointer}}`",
UnevaluatedPropertyError: "Invalid unevaluated property `{{pointer}}`",
UnevaluatedItemsError: "Invalid unevaluated item `{{pointer}}`",
UniqueItemsError: "Items in array must be unique. Value `{{value}}` in `{{pointer}}` is a duplicate of {{duplicatePointer}}.",
UnknownPropertyError: "Could not find a valid schema for property `{{pointer}}` within object",
ValueNotEmptyError: "A value for `{{property}}` is required at `{{pointer}}`"
Expand Down
9 changes: 9 additions & 0 deletions dist/module/lib/draft04/addRemoteSchema.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* register a json-schema to be referenced from another json-schema
* @param url base-url of json-schema (aka id)
* @param schema
*/
export default function addRemoteSchema(draft, url, schema) {
schema.id = schema.id || url;
draft.remotes[url] = draft.compileSchema(schema);
}
2 changes: 1 addition & 1 deletion dist/module/lib/draft04/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import addRemoteSchema from "../addRemoteSchema";
import addRemoteSchema from "./addRemoteSchema";
import compileSchema from "../compileSchema";
import { each } from "../each";
import { eachSchema } from "../eachSchema";
Expand Down
Loading

0 comments on commit ae6f1d4

Please sign in to comment.