-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
31 changed files
with
694 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.