Skip to content

Commit

Permalink
chore: cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
sagold committed Apr 13, 2024
1 parent 7c455c0 commit db80a49
Show file tree
Hide file tree
Showing 25 changed files with 118 additions and 172 deletions.
15 changes: 7 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -799,21 +799,20 @@ A _resolver_ is a simple method implementing a specific feature of json-schema t

#### `resolveRef` with merge

The default json-schema behaviour for `$ref` resolution is to replace the schema where a `$ref` is defined. In some scenarios you what to add context-specific information (e.g., a specific _title_). For this, a modified `$ref`-resolver is exposed by `json-schema-library`:
Until _draft07_ json-schema behaviour for `$ref` resolution is to replace the schema where a `$ref` is defined. Since _draft2019-09_ $ref resolution merges the resolved schema, which can be used to add context-specific information (e.g., a specific _title_).
To add this behaviour to older drafts, a `$ref`-resolver is exposed by `json-schema-library`:

```ts
import { Draft2019, resolveRefMerge } from "json-schema-library";
const jsonSchema = new Draft2019(mySchema, { resolveRef: resolveRefMerge });
import { Draft2019, resolveRef } from "json-schema-library";
const jsonSchema = new Draft2019(mySchema, { resolveRef });
```

`resolveRefMerge` performs a shallow merge (first level of properties), adding the local schemas properties last.

**Caution:** With this resolver, it is possible to overwrite json-schema behavioural properties. Treat with care.
`resolveRef` performs a shallow merge (first level of properties), adding the local schemas properties last. The ref-resolver for draft07 and below is exported as `resolveRefStrict`.

<details><summary>Example</summary>

```ts
import { Draft2019, resolveRefMerge } from "json-schema-library";
import { Draft07, resolveRef } from "json-schema-library";
const mySchema = {
type: "object",
properties: {
Expand All @@ -830,7 +829,7 @@ const mySchema = {
}
};

const jsonSchema = new Draft2019(mySchema, { resolveRef: resolveRefMerge });
const jsonSchema = new Draft07(mySchema, { resolveRef });
const subHeaderSchema = jsonSchema.getSchema("#/subHeader");

expect(subHeaderSchema).to.eq({
Expand Down
6 changes: 3 additions & 3 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { createError, createCustomError } from "./lib/utils/createCustomError";
import getTypeOf from "./lib/getTypeOf";
import { resolveOneOf, resolveOneOfFuzzy } from "./lib/features/oneOf";
import { resolveAllOf } from "./lib/features/allOf";
import resolveRef from "./lib/resolveRef.strict";
import resolveRefMerge from "./lib/resolveRef.merge";
import resolveRefStrict from "./lib/resolveRef.strict";
import resolveRef from "./lib/resolveRef";
import settings from "./lib/config/settings";
import strings from "./lib/config/strings";
import validateAsync from "./lib/validateAsync";
Expand Down Expand Up @@ -49,8 +49,8 @@ export {
resolveDynamicSchema, // v8
resolveOneOf,
resolveOneOfFuzzy,
resolveRefStrict,
resolveRef,
resolveRefMerge,
settings,
validateAsync // async validation of data by a schema
};
Expand Down
2 changes: 0 additions & 2 deletions lib/compileSchema.ts

This file was deleted.

13 changes: 9 additions & 4 deletions lib/draft/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import addRemoteSchema from "../addRemoteSchema";
import compileSchema from "../compileSchema";
import compileSchema from "../compile";
import copy from "../utils/copy";
import createSchemaOf from "../createSchemaOf";
import getChildSchemaSelection from "../getChildSchemaSelection";
Expand Down Expand Up @@ -32,6 +32,7 @@ export type DraftConfig = {
validateFormat: Record<string, JsonValidator>;
templateDefaultOptions?: TemplateOptions;

createNode: typeof createNode;
addRemoteSchema: typeof addRemoteSchema;
compileSchema: typeof compileSchema;
createSchemaOf: typeof createSchemaOf;
Expand Down Expand Up @@ -113,7 +114,7 @@ export class Draft {
* @param [pointer] - pointer to current data. Default to rootPointer
*/
each(data: any, callback: EachCallback, schema?: JsonSchema, pointer?: JsonPointer) {
const node = createNode(this, schema ?? this.rootSchema, pointer);
const node = this.createNode(schema ?? this.rootSchema, pointer);
return this.config.each(node, data, callback);
}

Expand Down Expand Up @@ -178,6 +179,10 @@ export class Draft {
return this.config.isValid(this, data, schema, pointer);
}

createNode(schema: JsonSchema, pointer = "#") {
return this.config.createNode(this, schema, pointer);
}

resolveAnyOf(node: SchemaNode, data: unknown): SchemaNode | JsonError {
return this.config.resolveAnyOf(node, data);
}
Expand Down Expand Up @@ -217,7 +222,7 @@ export class Draft {
if (isSchemaNode(key)) {
return this.config.step(key, schema, data);
}
const node = createNode(this, schema ?? this.rootSchema, pointer);
const node = this.createNode(schema ?? this.rootSchema, pointer);
return this.config.step(node, key, data);
}

Expand All @@ -238,7 +243,7 @@ export class Draft {
return this.config.validate(inuptNode, inputData);
}

const node = createNode(this, schema, pointer);
const node = this.createNode(schema, pointer);
return this.config.validate(node, data);
}
}
20 changes: 11 additions & 9 deletions lib/draft04/index.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,27 @@
import addRemoteSchema from "./addRemoteSchema";
import compileSchema from "../compileSchema";
import { each } from "../each";
import { eachSchema } from "../eachSchema";
import compileSchema from "../compile";
import createSchemaOf from "../createSchemaOf";
import ERRORS from "../validation/errors";
import FORMATS from "../validation/format";
import getChildSchemaSelection from "../getChildSchemaSelection";
import getSchema from "../getSchema";
import getTemplate from "../getTemplate";
import isValid from "../isValid";
import KEYWORDS from "../validation/keyword";
import merge from "../utils/merge";
import { resolveAllOf } from "../features/allOf";
import { resolveAnyOf } from "../features/anyOf";
import { resolveOneOf } from "../features/oneOf";
import resolveRef from "../resolveRef.strict";
import settings from "../config/settings";
import step from "../step";
import createSchemaOf from "../createSchemaOf";
import getChildSchemaSelection from "../getChildSchemaSelection";
import TYPES from "../validation/type";
import validate from "../validate";
import { createNode } from "../schemaNode";
import { DraftConfig, Draft } from "../draft";
import { each } from "../each";
import { eachSchema } from "../eachSchema";
import { JsonSchema } from "../types";
import settings from "../config/settings";
import { resolveAllOf } from "../features/allOf";
import { resolveAnyOf } from "../features/anyOf";
import { resolveOneOf } from "../features/oneOf";

const draft04Config: DraftConfig = {
typeKeywords: {
Expand Down Expand Up @@ -75,6 +76,7 @@ const draft04Config: DraftConfig = {
],
null: ["allOf", "anyOf", "enum", "format", "not", "oneOf"]
},
createNode,
validateKeyword: KEYWORDS,
validateType: TYPES,
validateFormat: FORMATS,
Expand Down
20 changes: 11 additions & 9 deletions lib/draft06/index.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,27 @@
import addRemoteSchema from "../addRemoteSchema";
import compileSchema from "../draft06/compile";
import { each } from "../each";
import { eachSchema } from "../eachSchema";
import createSchemaOf from "../createSchemaOf";
import ERRORS from "../validation/errors";
import FORMATS from "../validation/format";
import getChildSchemaSelection from "../getChildSchemaSelection";
import getSchema from "../getSchema";
import getTemplate from "../getTemplate";
import isValid from "../isValid";
import KEYWORDS from "../draft06/validation/keyword";
import merge from "../utils/merge";
import { resolveAllOf } from "../features/allOf";
import { resolveAnyOf } from "../features/anyOf";
import { resolveOneOf } from "../features/oneOf";
import resolveRef from "../resolveRef.strict";
import createSchemaOf from "../createSchemaOf";
import getChildSchemaSelection from "../getChildSchemaSelection";
import settings from "../config/settings";
import step from "../step";
import TYPES from "../draft06/validation/type";
import TYPES from "../validation/type";
import validate from "../validate";
import { createNode } from "../schemaNode";
import { DraftConfig, Draft } from "../draft";
import { each } from "../each";
import { eachSchema } from "../eachSchema";
import { JsonSchema } from "../types";
import settings from "../config/settings";
import { resolveAllOf } from "../features/allOf";
import { resolveAnyOf } from "../features/anyOf";
import { resolveOneOf } from "../features/oneOf";

const draft06Config: DraftConfig = {
typeKeywords: {
Expand Down Expand Up @@ -88,6 +89,7 @@ const draft06Config: DraftConfig = {
validateFormat: FORMATS,
errors: ERRORS,

createNode,
addRemoteSchema,
compileSchema,
createSchemaOf,
Expand Down
47 changes: 0 additions & 47 deletions lib/draft06/validation/type.ts

This file was deleted.

20 changes: 11 additions & 9 deletions lib/draft07/index.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,27 @@
import addRemoteSchema from "../addRemoteSchema";
import compileSchema from "../draft06/compile";
import { each } from "../each";
import { eachSchema } from "../eachSchema";
import createSchemaOf from "../createSchemaOf";
import ERRORS from "../validation/errors";
import FORMATS from "../validation/format";
import getChildSchemaSelection from "../getChildSchemaSelection";
import getSchema from "../getSchema";
import getTemplate from "../getTemplate";
import isValid from "../isValid";
import KEYWORDS from "../draft06/validation/keyword";
import merge from "../utils/merge";
import { resolveAllOf } from "../features/allOf";
import { resolveAnyOf } from "../features/anyOf";
import { resolveOneOf } from "../features/oneOf";
import resolveRef from "../resolveRef.strict";
import createSchemaOf from "../createSchemaOf";
import getChildSchemaSelection from "../getChildSchemaSelection";
import settings from "../config/settings";
import step from "../step";
import TYPES from "../draft06/validation/type";
import TYPES from "../validation/type";
import validate from "../validate";
import { createNode } from "../schemaNode";
import { DraftConfig, Draft } from "../draft";
import { each } from "../each";
import { eachSchema } from "../eachSchema";
import { JsonSchema } from "../types";
import settings from "../config/settings";
import { resolveAllOf } from "../features/allOf";
import { resolveAnyOf } from "../features/anyOf";
import { resolveOneOf } from "../features/oneOf";

const draft07Config: DraftConfig = {
typeKeywords: {
Expand Down Expand Up @@ -88,6 +89,7 @@ const draft07Config: DraftConfig = {
validateFormat: FORMATS,
errors: ERRORS,

createNode,
addRemoteSchema,
compileSchema,
createSchemaOf,
Expand Down
22 changes: 12 additions & 10 deletions lib/draft2019/index.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,27 @@
import addRemoteSchema from "../addRemoteSchema";
import compileSchema from "../draft06/compile";
import { each } from "../each";
import { eachSchema } from "../eachSchema";
import createSchemaOf from "../createSchemaOf";
import ERRORS from "../validation/errors";
import FORMATS from "../validation/format";
import getChildSchemaSelection from "../getChildSchemaSelection";
import getSchema from "../getSchema";
import getTemplate from "../getTemplate";
import isValid from "../isValid";
import KEYWORDS from "./validation/keyword";
import merge from "../utils/merge";
import { resolveAllOf } from "../features/allOf";
import { resolveAnyOf } from "../features/anyOf";
import { resolveOneOf } from "../features/oneOf";
import createSchemaOf from "../createSchemaOf";
import getChildSchemaSelection from "../getChildSchemaSelection";
import resolveRef from "../resolveRef";
import settings from "../config/settings";
import step from "../step";
import TYPES from "../draft06/validation/type";
import TYPES from "../validation/type";
import validate from "../validate";
import { createNode } from "../schemaNode";
import { DraftConfig, Draft } from "../draft";
import { each } from "../each";
import { eachSchema } from "../eachSchema";
import { JsonSchema } from "../types";
import settings from "../config/settings";
import resolveRef from "../resolveRef.merge";
import { resolveAllOf } from "../features/allOf";
import { resolveAnyOf } from "../features/anyOf";
import { resolveOneOf } from "../features/oneOf";


const draft2019Config: DraftConfig = {
Expand Down Expand Up @@ -93,6 +94,7 @@ const draft2019Config: DraftConfig = {
validateFormat: FORMATS,
errors: ERRORS,

createNode,
addRemoteSchema,
compileSchema,
createSchemaOf,
Expand Down
1 change: 0 additions & 1 deletion lib/draft2019/validation/keyword.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { validateDependentSchemas, validateDependentRequired } from "../../featu
import { JsonValidator } from "../../validation/type";
import { SchemaNode } from "../../schemaNode";


/**
* Get a list of tests to search for a matching pattern to a property
*/
Expand Down
2 changes: 1 addition & 1 deletion lib/each.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export type EachCallback = (schema: JsonSchema, data: unknown, pointer: JsonPoin
* @param [pointer] - pointer to current data. Default to rootPointer
*/
export function each(schemaNode: SchemaNode, data: any, callback: EachCallback) {
const node = schemaNode.draft.resolveRef(schemaNode);
const node = schemaNode.resolveRef();
const { draft, schema, pointer } = node;
callback(schema, data, pointer);
const dataType = getTypeOf(data);
Expand Down
8 changes: 4 additions & 4 deletions lib/features/allOf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,19 @@ import { JsonValidator } from "../validation/type";
* when complete this will have much duplication to step.object etc
*/
export function resolveSchema(node: SchemaNode, data: unknown): SchemaNode | JsonError {
const schema = shallowCloneSchemaNode(node.schema);
const ifSchema = resolveIfSchema(node, data);
if (ifSchema) {
return ifSchema;
}
const schema = shallowCloneSchemaNode(node.schema);
return node.next(omit(schema, "if", "then", "else"));
}

export function resolveAllOf(node: SchemaNode, data: any): SchemaNode | JsonError {
const { schema, draft } = node;
const { schema } = node;
let mergedSchema = shallowCloneSchemaNode(schema);
for (let i = 0; i < schema.allOf.length; i += 1) {
const allOfNode = draft.resolveRef(node.next(schema.allOf[i] as JsonSchema));
const allOfNode = node.next(schema.allOf[i] as JsonSchema).resolveRef();
// @todo introduce draft.resolveSchema to iteratively resolve
const allOfSchema = resolveSchema(allOfNode, data).schema;
mergedSchema = mergeSchema(mergedSchema, allOfSchema);
Expand All @@ -53,7 +53,7 @@ export function mergeAllOfSchema(draft: Draft, schema: JsonSchema): JsonSchema |
if (subschema == null) {
return;
}
const subSchemaNode = draft.resolveRef(createNode(draft, subschema));
const subSchemaNode = draft.createNode(subschema).resolveRef();
resolvedSchema = mergeSchema(resolvedSchema, subSchemaNode.schema);
});
return resolvedSchema;
Expand Down
Loading

0 comments on commit db80a49

Please sign in to comment.