json-schema-library 11.0.2 → 11.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.ts CHANGED
@@ -3,7 +3,7 @@ export type { CompileOptions } from "./src/compileSchema";
3
3
  export type { Context, SchemaNode, GetNodeOptions, ValidateReturnType } from "./src/SchemaNode";
4
4
  export type { DataNode } from "./src/methods/toDataNodes";
5
5
  export type { Draft, DraftVersion } from "./src/Draft";
6
- export type { JsonError, JsonAnnotation, JsonPointer, JsonSchema, OptionalNodeOrError, NodeOrError } from "./src/types";
6
+ export type { JsonError, JsonAnnotation, JsonPointer, JsonSchema, BooleanSchema, OptionalNodeOrError, NodeOrError } from "./src/types";
7
7
  export type {
8
8
  Keyword,
9
9
  Maybe,
@@ -37,7 +37,7 @@ export type { Annotation, AnnotationData, ErrorConfig } from "./src/types";
37
37
  // utilities
38
38
  export { getTypeOf } from "./src/utils/getTypeOf";
39
39
  export { isReduceable } from "./src/SchemaNode";
40
- export { isJsonError, isJsonAnnotation, isAnnotation, isSchemaNode } from "./src/types";
40
+ export { isJsonError, isJsonAnnotation, isAnnotation, isSchemaNode, isJsonSchema, isBooleanSchema } from "./src/types";
41
41
  export { extendDraft, addKeywords } from "./src/Draft";
42
42
  export { mergeNode } from "./src/mergeNode";
43
43
  export { mergeSchema } from "./src/utils/mergeSchema";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "json-schema-library",
3
- "version": "11.0.2",
3
+ "version": "11.0.3",
4
4
  "description": "Customizable and hackable json-validator and json-schema utilities for traversal, data generation and validation",
5
5
  "main": "./dist/index.cjs",
6
6
  "module": "./dist/index.mjs",
@@ -12,7 +12,12 @@
12
12
  },
13
13
  "./package.json": "./package.json"
14
14
  },
15
+ "engines": {
16
+ "yarn": ">=1.22.0",
17
+ "npm": "use yarn instead"
18
+ },
15
19
  "scripts": {
20
+ "preinstall": "npx only-allow yarn",
16
21
  "coverage": "nyc npm run test --reporter=lcov",
17
22
  "dist": "tsdown -f esm -f cjs --minify; yarn dist:iife",
18
23
  "dist:iife": "tsdown --config tsdown.iife.config.ts --no-clean --minify; mv dist/index.iife.js dist/jlib.js",
package/src/SchemaNode.ts CHANGED
@@ -15,7 +15,9 @@ import { Draft } from "./Draft";
15
15
  import { toSchemaNodes } from "./methods/toSchemaNodes";
16
16
  import {
17
17
  isJsonError,
18
+ isJsonSchema,
18
19
  JsonSchema,
20
+ BooleanSchema,
19
21
  JsonError,
20
22
  AnnotationData,
21
23
  DefaultErrors,
@@ -189,7 +191,7 @@ export interface SchemaNode extends SchemaNodeMethodsType {
189
191
  * Fixed SchemaNode mixin methods
190
192
  */
191
193
  interface SchemaNodeMethodsType {
192
- compileSchema(schema: JsonSchema, evaluationPath?: string, schemaLocation?: string, dynamicId?: string): SchemaNode;
194
+ compileSchema(schema: JsonSchema | BooleanSchema, evaluationPath?: string, schemaLocation?: string, dynamicId?: string): SchemaNode;
193
195
  createError<T extends string = DefaultErrors>(code: T, data: AnnotationData, message?: string): JsonError;
194
196
  createAnnotation<T extends string = DefaultErrors>(code: T, data: AnnotationData, message?: string): JsonAnnotation;
195
197
  createSchema(data?: unknown): JsonSchema;
@@ -241,7 +243,7 @@ interface SchemaNodeMethodsType {
241
243
  ): OptionalNodeOrError;
242
244
  resolveRef: (args?: { pointer?: string; path?: ValidationPath }) => SchemaNode;
243
245
  validate(data: unknown, pointer?: string, path?: ValidationPath): ValidateReturnType;
244
- addRemoteSchema(url: string, schema: JsonSchema): SchemaNode;
246
+ addRemoteSchema(url: string, schema: JsonSchema | BooleanSchema): SchemaNode;
245
247
  toSchemaNodes(): SchemaNode[];
246
248
  toDataNodes(data: unknown, pointer?: string): DataNode[];
247
249
  toJSON(): unknown;
@@ -494,12 +496,16 @@ export const SchemaNodeMethods = {
494
496
  * Register a JSON Schema as a remote-schema to be resolved by $ref, $anchor, etc
495
497
  * @returns the current node (not the remote schema-node)
496
498
  */
497
- addRemoteSchema(url: string, schema: JsonSchema): SchemaNode {
498
- // @draft >= 6
499
+ addRemoteSchema(url: string, schema: JsonSchema | BooleanSchema): SchemaNode {
500
+ // @draft >= 6
501
+ if (isJsonSchema(schema)) {
499
502
  schema.$id = resolveUri(schema.$id || url);
503
+ }
504
+
500
505
  const node = this as SchemaNode;
501
506
  const { context } = node;
502
- const draft = getDraft(context.drafts, schema?.$schema ?? context.rootNode.schema?.$schema);
507
+ const schemaId = isJsonSchema(schema) ? schema.$schema : undefined;
508
+ const draft = getDraft(context.drafts, schemaId ?? context.rootNode.schema?.$schema);
503
509
 
504
510
  const remoteNode: SchemaNode = {
505
511
  evaluationPath: "#",
@@ -6,7 +6,7 @@ import { draft07 } from "./draft07";
6
6
  import { draft2019 } from "./draft2019";
7
7
  import { draft2020 } from "./draft2020";
8
8
  import { pick } from "./utils/pick";
9
- import { JsonSchema, Draft } from "./types";
9
+ import { JsonSchema, BooleanSchema, Draft, isJsonSchema } from "./types";
10
10
  import { TemplateOptions } from "./methods/getData";
11
11
  import { SchemaNode, SchemaNodeMethods, addKeywords, isSchemaNode } from "./SchemaNode";
12
12
  import settings from "./settings";
@@ -31,10 +31,10 @@ function getDraft(drafts: Draft[], $schema: string) {
31
31
  * wrapping each schema with utilities and as much preevaluation as possible. Each
32
32
  * node will be reused for each task, but will create a compiledNode for bound data.
33
33
  */
34
- export function compileSchema(schema: JsonSchema, options: CompileOptions = {}) {
34
+ export function compileSchema(schema: JsonSchema | BooleanSchema, options: CompileOptions = {}) {
35
35
  let formatAssertion = options.formatAssertion ?? true;
36
36
  const drafts = options.drafts ?? defaultDrafts;
37
- const draft = getDraft(drafts, schema?.$schema);
37
+ const draft = getDraft(drafts, isJsonSchema(schema) ? schema.$schema : undefined);
38
38
 
39
39
  const node: SchemaNode = {
40
40
  evaluationPath: "#",
@@ -44,7 +44,7 @@ export function compileSchema(schema: JsonSchema, options: CompileOptions = {})
44
44
  reducers: [],
45
45
  resolvers: [],
46
46
  validators: [],
47
- schema,
47
+ schema: schema as JsonSchema,
48
48
  // @ts-expect-error self-reference added later
49
49
  context: {
50
50
  remotes: {},
@@ -60,7 +60,7 @@ export function compileSchema(schema: JsonSchema, options: CompileOptions = {})
60
60
  };
61
61
 
62
62
  node.context.rootNode = node;
63
- node.context.remotes[schema?.$id ?? "#"] = node;
63
+ node.context.remotes[(isJsonSchema(schema) ? schema.$id : undefined) ?? "#"] = node;
64
64
 
65
65
  if (options.remote) {
66
66
  const metaSchema = getRef(node, node.schema.$schema);
package/src/types.ts CHANGED
@@ -9,6 +9,13 @@ export type BooleanSchema = boolean;
9
9
  export interface JsonSchema {
10
10
  [keyword: string]: any;
11
11
  }
12
+ export function isJsonSchema(value: unknown): value is JsonSchema {
13
+ return isObject(value);
14
+ }
15
+
16
+ export function isBooleanSchema(value: unknown): value is BooleanSchema {
17
+ return typeof value === "boolean";
18
+ }
12
19
 
13
20
  export type JsonPointer = string;
14
21