@maroonedog/luq 2.4.4 → 2.6.0

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.
Files changed (64) hide show
  1. package/README.md +64 -10
  2. package/dist/chain/slot-type-guard.js +20 -6
  3. package/dist/chain/slot-type-guard.mjs +20 -6
  4. package/dist/compile/resolve-presence.js +36 -5
  5. package/dist/compile/resolve-presence.mjs +36 -5
  6. package/dist/json-schema/array-keyword-guards.d.ts +31 -0
  7. package/dist/json-schema/array-keyword-guards.js +120 -0
  8. package/dist/json-schema/array-keyword-guards.mjs +114 -0
  9. package/dist/json-schema/assert-object-keyword-values.d.ts +59 -0
  10. package/dist/json-schema/assert-object-keyword-values.js +149 -0
  11. package/dist/json-schema/assert-object-keyword-values.mjs +141 -0
  12. package/dist/json-schema/assert-supported-dialect.d.ts +37 -0
  13. package/dist/json-schema/assert-supported-dialect.js +113 -0
  14. package/dist/json-schema/assert-supported-dialect.mjs +109 -0
  15. package/dist/json-schema/build-from-schema.d.ts +22 -3
  16. package/dist/json-schema/build-from-schema.js +27 -5
  17. package/dist/json-schema/build-from-schema.mjs +27 -5
  18. package/dist/json-schema/collect-definitions.d.ts +7 -0
  19. package/dist/json-schema/collect-definitions.js +9 -0
  20. package/dist/json-schema/collect-definitions.mjs +10 -1
  21. package/dist/json-schema/declare-additional-properties.d.ts +11 -1
  22. package/dist/json-schema/declare-additional-properties.js +13 -1
  23. package/dist/json-schema/declare-additional-properties.mjs +13 -1
  24. package/dist/json-schema/declare-object-keywords.d.ts +4 -0
  25. package/dist/json-schema/declare-object-keywords.js +11 -4
  26. package/dist/json-schema/declare-object-keywords.mjs +11 -4
  27. package/dist/json-schema/declare-required-properties.js +6 -0
  28. package/dist/json-schema/declare-required-properties.mjs +6 -0
  29. package/dist/json-schema/declare-value-keywords.d.ts +10 -1
  30. package/dist/json-schema/declare-value-keywords.js +43 -4
  31. package/dist/json-schema/declare-value-keywords.mjs +43 -4
  32. package/dist/json-schema/extensions/json-schema/index.d.ts +2 -2
  33. package/dist/json-schema/extensions/json-schema/index.js +4 -1
  34. package/dist/json-schema/extensions/json-schema/index.mjs +1 -1
  35. package/dist/json-schema/extensions/json-schema/json-schema.d.ts +2 -2
  36. package/dist/json-schema/extensions/json-schema/json-schema.js +3 -0
  37. package/dist/json-schema/extensions/json-schema/json-schema.mjs +4 -1
  38. package/dist/json-schema/extensions/json-schema-full-feature/index.d.ts +3 -1
  39. package/dist/json-schema/extensions/json-schema-full-feature/index.js +18 -1
  40. package/dist/json-schema/extensions/json-schema-full-feature/index.mjs +11 -0
  41. package/dist/json-schema/extensions/json-schema-full-feature/json-schema-full-feature.d.ts +9 -1
  42. package/dist/json-schema/extensions/json-schema-full-feature/json-schema-full-feature.js +9 -2
  43. package/dist/json-schema/extensions/json-schema-full-feature/json-schema-full-feature.mjs +9 -2
  44. package/dist/json-schema/flatten-array-schema.d.ts +11 -1
  45. package/dist/json-schema/flatten-array-schema.js +20 -7
  46. package/dist/json-schema/flatten-array-schema.mjs +20 -7
  47. package/dist/json-schema/index.d.ts +4 -0
  48. package/dist/json-schema/index.js +9 -1
  49. package/dist/json-schema/index.mjs +3 -0
  50. package/dist/json-schema/keyword-map-core.js +3 -1
  51. package/dist/json-schema/keyword-map-core.mjs +3 -1
  52. package/dist/json-schema/keyword-map-string.js +30 -1
  53. package/dist/json-schema/keyword-map-string.mjs +30 -1
  54. package/dist/json-schema/keyword-map.js +5 -0
  55. package/dist/json-schema/keyword-map.mjs +5 -0
  56. package/dist/json-schema/malformed-schema-error.d.ts +38 -0
  57. package/dist/json-schema/malformed-schema-error.js +103 -0
  58. package/dist/json-schema/malformed-schema-error.mjs +98 -0
  59. package/dist/json-schema/unsupported-dialect-error.d.ts +21 -0
  60. package/dist/json-schema/unsupported-dialect-error.js +58 -0
  61. package/dist/json-schema/unsupported-dialect-error.mjs +54 -0
  62. package/dist/plugins/one-of/one-of.js +18 -4
  63. package/dist/plugins/one-of/one-of.mjs +18 -4
  64. package/package.json +8 -5
@@ -6,6 +6,7 @@ exports.buildFromSchema = buildFromSchema;
6
6
  exports.fromJsonSchema = fromJsonSchema;
7
7
  const create_field_builder_1 = require("../builder/create-field-builder");
8
8
  const type_erasure_1 = require("../core/type-erasure");
9
+ const assert_supported_dialect_1 = require("./assert-supported-dialect");
9
10
  const create_structural_context_1 = require("./create-structural-context");
10
11
  const declare_presence_1 = require("./declare-presence");
11
12
  const ref_scope_1 = require("./ref-scope");
@@ -35,9 +36,12 @@ function collectDeclaredRules(declaration, bag, root, chain) {
35
36
  ]);
36
37
  }
37
38
  /** One pending `.v()` per declared path, with its callback still unrun. */
38
- function buildFieldEntries(bag, schema) {
39
+ function buildFieldEntries(bag, schema, options = {}) {
39
40
  if (!(0, draft07_types_1.isDraft07Schema)(schema))
40
41
  throw new NotASchemaError(schema);
42
+ // Before any keyword is read: what the document's keywords MEAN depends on
43
+ // the dialect it declares, and Luq reads every one of them as Draft-07.
44
+ (0, assert_supported_dialect_1.assertSupportedDialect)(schema, options);
41
45
  return (0, flatten_schema_1.flattenSchema)(schema, schema_to_declarations_1.readChildSchemas).map((declaration) => ({
42
46
  path: declaration.path,
43
47
  defaultOf: null,
@@ -53,10 +57,28 @@ function buildFieldEntries(bag, schema) {
53
57
  }));
54
58
  }
55
59
  /** The erased front door: the declarations, with nothing compiled yet. */
56
- function buildFromSchema(bag, schema, config) {
57
- return (0, create_field_builder_1.createFieldBuilderSurface)(bag, config, buildFieldEntries(bag, schema));
60
+ function buildFromSchema(bag, schema, config, options) {
61
+ return (0, create_field_builder_1.createFieldBuilderSurface)(bag, config, buildFieldEntries(bag, schema, options));
58
62
  }
59
63
  /**
64
+ * Converts a JSON Schema document into a validator.
65
+ *
66
+ * THE INPUT LIMIT IS DRAFT-07, and it is a limit on what may be handed in, not
67
+ * only a statement of how much of Draft-07 is covered. A document declaring
68
+ * 2019-09 or 2020-12 in its root `$schema` is REFUSED with an
69
+ * `UnsupportedDialectError` rather than read as Draft-07, because the two
70
+ * dialects disagree about what an unchanged keyword means: from 2019-09 on
71
+ * `$ref` is an ordinary applicator whose siblings are applied, while Draft-07
72
+ * §8.3 replaces the node, so `{"$ref": "#/$defs/name", "minLength": 5}` read as
73
+ * Draft-07 loses the `minLength` and accepts a value the document forbids. A
74
+ * document with NO `$schema` is read as Draft-07 and always has been. Pass
75
+ * `{ assumeDraft07: true }` as the fourth argument to read a newer-dialect
76
+ * document under Draft-07 rules deliberately, with that reading's consequences.
77
+ *
78
+ * `T` defaults to `Record<string, unknown>`, never `any`; under the default NO
79
+ * declared path is checked. See the header of this file, which owns that
80
+ * escape hatch.
81
+ *
60
82
  * The declared type is put back on by `eraseSchemaValidator`, the fourth
61
83
  * function in src/core/type-erasure.ts — the one file the code standard allows
62
84
  * to assert. Step 25 expressed this as an OVERLOAD PAIR instead, because
@@ -64,7 +86,7 @@ function buildFromSchema(bag, schema, config) {
64
86
  * TypeScript on lenient compatibility rules and so was never audited anywhere.
65
87
  * The call below is, in the file where every escape hatch is reviewed together.
66
88
  */
67
- function fromJsonSchema(bag, schema, config) {
68
- const planBacked = buildFromSchema(bag, schema, config).build();
89
+ function fromJsonSchema(bag, schema, config, options) {
90
+ const planBacked = buildFromSchema(bag, schema, config, options).build();
69
91
  return (0, type_erasure_1.eraseSchemaValidator)(planBacked);
70
92
  }
@@ -1,5 +1,6 @@
1
1
  import { createFieldBuilderSurface } from "../builder/create-field-builder.mjs";
2
2
  import { eraseSchemaValidator } from "../core/type-erasure.mjs";
3
+ import { assertSupportedDialect } from "./assert-supported-dialect.mjs";
3
4
  import { createStructuralContext } from "./create-structural-context.mjs";
4
5
  import { declarePresenceRules } from "./declare-presence.mjs";
5
6
  import { createLocalScope } from "./ref-scope.mjs";
@@ -28,9 +29,12 @@ function collectDeclaredRules(declaration, bag, root, chain) {
28
29
  ]);
29
30
  }
30
31
  /** One pending `.v()` per declared path, with its callback still unrun. */
31
- export function buildFieldEntries(bag, schema) {
32
+ export function buildFieldEntries(bag, schema, options = {}) {
32
33
  if (!isDraft07Schema(schema))
33
34
  throw new NotASchemaError(schema);
35
+ // Before any keyword is read: what the document's keywords MEAN depends on
36
+ // the dialect it declares, and Luq reads every one of them as Draft-07.
37
+ assertSupportedDialect(schema, options);
34
38
  return flattenSchema(schema, readChildSchemas).map((declaration) => ({
35
39
  path: declaration.path,
36
40
  defaultOf: null,
@@ -46,10 +50,28 @@ export function buildFieldEntries(bag, schema) {
46
50
  }));
47
51
  }
48
52
  /** The erased front door: the declarations, with nothing compiled yet. */
49
- export function buildFromSchema(bag, schema, config) {
50
- return createFieldBuilderSurface(bag, config, buildFieldEntries(bag, schema));
53
+ export function buildFromSchema(bag, schema, config, options) {
54
+ return createFieldBuilderSurface(bag, config, buildFieldEntries(bag, schema, options));
51
55
  }
52
56
  /**
57
+ * Converts a JSON Schema document into a validator.
58
+ *
59
+ * THE INPUT LIMIT IS DRAFT-07, and it is a limit on what may be handed in, not
60
+ * only a statement of how much of Draft-07 is covered. A document declaring
61
+ * 2019-09 or 2020-12 in its root `$schema` is REFUSED with an
62
+ * `UnsupportedDialectError` rather than read as Draft-07, because the two
63
+ * dialects disagree about what an unchanged keyword means: from 2019-09 on
64
+ * `$ref` is an ordinary applicator whose siblings are applied, while Draft-07
65
+ * §8.3 replaces the node, so `{"$ref": "#/$defs/name", "minLength": 5}` read as
66
+ * Draft-07 loses the `minLength` and accepts a value the document forbids. A
67
+ * document with NO `$schema` is read as Draft-07 and always has been. Pass
68
+ * `{ assumeDraft07: true }` as the fourth argument to read a newer-dialect
69
+ * document under Draft-07 rules deliberately, with that reading's consequences.
70
+ *
71
+ * `T` defaults to `Record<string, unknown>`, never `any`; under the default NO
72
+ * declared path is checked. See the header of this file, which owns that
73
+ * escape hatch.
74
+ *
53
75
  * The declared type is put back on by `eraseSchemaValidator`, the fourth
54
76
  * function in src/core/type-erasure.ts — the one file the code standard allows
55
77
  * to assert. Step 25 expressed this as an OVERLOAD PAIR instead, because
@@ -57,7 +79,7 @@ export function buildFromSchema(bag, schema, config) {
57
79
  * TypeScript on lenient compatibility rules and so was never audited anywhere.
58
80
  * The call below is, in the file where every escape hatch is reviewed together.
59
81
  */
60
- export function fromJsonSchema(bag, schema, config) {
61
- const planBacked = buildFromSchema(bag, schema, config).build();
82
+ export function fromJsonSchema(bag, schema, config, options) {
83
+ const planBacked = buildFromSchema(bag, schema, config, options).build();
62
84
  return eraseSchemaValidator(planBacked);
63
85
  }
@@ -45,6 +45,13 @@ export declare function advanceBase(scope: RefScope, node: Draft07SchemaObject):
45
45
  * a recursive definition BEFORE it walks into it: `#/definitions/node` inside
46
46
  * its own `properties` is a legitimate schema whose declared paths are
47
47
  * infinite, and Luq declares finite paths.
48
+ *
49
+ * The node is asked what it IS, not what it is not. Untyped JSON reaches here —
50
+ * a `properties` entry is whatever the document wrote — and "not a boolean"
51
+ * counts `null` as something to read `$ref` off, which throws before the
52
+ * malformed entry can be named. The pair is the same one resolve-ref uses:
53
+ * isDraft07Schema keeps out everything that is not a schema at all, and
54
+ * isSchemaObject drops the §4.4 boolean form, which carries no pointer.
48
55
  */
49
56
  export declare function readRefPointer(schema: Draft07Schema): string | undefined;
50
57
  /** Every definition name the document declares, under either spelling. */
@@ -90,8 +90,17 @@ function advanceBase(scope, node) {
90
90
  * a recursive definition BEFORE it walks into it: `#/definitions/node` inside
91
91
  * its own `properties` is a legitimate schema whose declared paths are
92
92
  * infinite, and Luq declares finite paths.
93
+ *
94
+ * The node is asked what it IS, not what it is not. Untyped JSON reaches here —
95
+ * a `properties` entry is whatever the document wrote — and "not a boolean"
96
+ * counts `null` as something to read `$ref` off, which throws before the
97
+ * malformed entry can be named. The pair is the same one resolve-ref uses:
98
+ * isDraft07Schema keeps out everything that is not a schema at all, and
99
+ * isSchemaObject drops the §4.4 boolean form, which carries no pointer.
93
100
  */
94
101
  function readRefPointer(schema) {
102
+ if (!(0, draft07_types_1.isDraft07Schema)(schema))
103
+ return undefined;
95
104
  if (!(0, draft07_types_1.isSchemaObject)(schema))
96
105
  return undefined;
97
106
  return schema.$ref;
@@ -1,4 +1,4 @@
1
- import { isSchemaObject } from "./draft07.types.mjs";
1
+ import { isDraft07Schema, isSchemaObject } from "./draft07.types.mjs";
2
2
  import { createLocalScope } from "./ref-scope.mjs";
3
3
  import { resolveRefInScope } from "./resolve-ref.mjs";
4
4
  import { nextBaseUri } from "./uri-reference.mjs";
@@ -80,8 +80,17 @@ export function advanceBase(scope, node) {
80
80
  * a recursive definition BEFORE it walks into it: `#/definitions/node` inside
81
81
  * its own `properties` is a legitimate schema whose declared paths are
82
82
  * infinite, and Luq declares finite paths.
83
+ *
84
+ * The node is asked what it IS, not what it is not. Untyped JSON reaches here —
85
+ * a `properties` entry is whatever the document wrote — and "not a boolean"
86
+ * counts `null` as something to read `$ref` off, which throws before the
87
+ * malformed entry can be named. The pair is the same one resolve-ref uses:
88
+ * isDraft07Schema keeps out everything that is not a schema at all, and
89
+ * isSchemaObject drops the §4.4 boolean form, which carries no pointer.
83
90
  */
84
91
  export function readRefPointer(schema) {
92
+ if (!isDraft07Schema(schema))
93
+ return undefined;
85
94
  if (!isSchemaObject(schema))
86
95
  return undefined;
87
96
  return schema.$ref;
@@ -15,5 +15,15 @@ export declare function patternPropertyKeys(schema: Draft07SchemaObject): readon
15
15
  * holds the keyword table and the compile-time check that the method exists.
16
16
  */
17
17
  export declare function applyAdditionalPropertiesBoolean(chain: ConverterChain<"object">, schema: Draft07SchemaObject, allowed: boolean): ConverterChain<"object">;
18
- /** The SCHEMA form: whatever matches neither a declaration nor a pattern follows it. */
18
+ /**
19
+ * The SCHEMA form: whatever matches neither a declaration nor a pattern
20
+ * follows it.
21
+ *
22
+ * This is the ONE place a non-boolean `additionalProperties` is consumed — the
23
+ * boolean form is taken by declareObjectRules before this runs — so it is
24
+ * where the value is checked. A string or a number reaching the sub-schema
25
+ * route is not a schema, expands to no rules, and turns
26
+ * `{"additionalProperties":"false"}` into an object that accepts every extra
27
+ * property; the check refuses it instead. Both well-formed forms are untouched.
28
+ */
19
29
  export declare function declareAdditionalPropertiesSchema(schema: Draft07SchemaObject, context: StructuralContext): readonly Rule[];
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.patternPropertyKeys = patternPropertyKeys;
4
4
  exports.applyAdditionalPropertiesBoolean = applyAdditionalPropertiesBoolean;
5
5
  exports.declareAdditionalPropertiesSchema = declareAdditionalPropertiesSchema;
6
+ const assert_object_keyword_values_1 = require("./assert-object-keyword-values");
6
7
  const apply_keyword_binding_1 = require("./apply-keyword-binding");
7
8
  const keyword_map_object_1 = require("./keyword-map-object");
8
9
  const NO_RULES = Object.freeze([]);
@@ -29,9 +30,20 @@ function applyAdditionalPropertiesBoolean(chain, schema, allowed) {
29
30
  ? (0, apply_keyword_binding_1.applyKeywordBinding)(chain, keyword_map_object_1.additionalPropertiesBinding, allowed)
30
31
  : chain.additionalProperties(allowed, undefined, patterns);
31
32
  }
32
- /** The SCHEMA form: whatever matches neither a declaration nor a pattern follows it. */
33
+ /**
34
+ * The SCHEMA form: whatever matches neither a declaration nor a pattern
35
+ * follows it.
36
+ *
37
+ * This is the ONE place a non-boolean `additionalProperties` is consumed — the
38
+ * boolean form is taken by declareObjectRules before this runs — so it is
39
+ * where the value is checked. A string or a number reaching the sub-schema
40
+ * route is not a schema, expands to no rules, and turns
41
+ * `{"additionalProperties":"false"}` into an object that accepts every extra
42
+ * property; the check refuses it instead. Both well-formed forms are untouched.
43
+ */
33
44
  function declareAdditionalPropertiesSchema(schema, context) {
34
45
  const additional = schema.additionalProperties;
46
+ (0, assert_object_keyword_values_1.assertAdditionalPropertiesIsSchema)(additional);
35
47
  if (additional === undefined || typeof additional === "boolean") {
36
48
  return NO_RULES;
37
49
  }
@@ -1,3 +1,4 @@
1
+ import { assertAdditionalPropertiesIsSchema } from "./assert-object-keyword-values.mjs";
1
2
  import { applyKeywordBinding } from "./apply-keyword-binding.mjs";
2
3
  import { additionalPropertiesBinding } from "./keyword-map-object.mjs";
3
4
  const NO_RULES = Object.freeze([]);
@@ -24,9 +25,20 @@ export function applyAdditionalPropertiesBoolean(chain, schema, allowed) {
24
25
  ? applyKeywordBinding(chain, additionalPropertiesBinding, allowed)
25
26
  : chain.additionalProperties(allowed, undefined, patterns);
26
27
  }
27
- /** The SCHEMA form: whatever matches neither a declaration nor a pattern follows it. */
28
+ /**
29
+ * The SCHEMA form: whatever matches neither a declaration nor a pattern
30
+ * follows it.
31
+ *
32
+ * This is the ONE place a non-boolean `additionalProperties` is consumed — the
33
+ * boolean form is taken by declareObjectRules before this runs — so it is
34
+ * where the value is checked. A string or a number reaching the sub-schema
35
+ * route is not a schema, expands to no rules, and turns
36
+ * `{"additionalProperties":"false"}` into an object that accepts every extra
37
+ * property; the check refuses it instead. Both well-formed forms are untouched.
38
+ */
28
39
  export function declareAdditionalPropertiesSchema(schema, context) {
29
40
  const additional = schema.additionalProperties;
41
+ assertAdditionalPropertiesIsSchema(additional);
30
42
  if (additional === undefined || typeof additional === "boolean") {
31
43
  return NO_RULES;
32
44
  }
@@ -14,6 +14,10 @@ export declare function declareDependencies(schema: Draft07SchemaObject, context
14
14
  * (declareRequiredProperties). A name in `required` with no `properties` entry
15
15
  * still gets a path on the empty schema, because §6.5.3 makes the two keywords
16
16
  * independent and the root distribution needs the path to exist.
17
+ *
18
+ * Both keywords are first READ here, for the root and for every nested node
19
+ * alike, so this is where both are checked. Unchecked, a string `required` is
20
+ * iterated character by character and every character becomes a declared path.
17
21
  */
18
22
  export declare function readPropertyChildren(schema: Draft07SchemaObject): readonly {
19
23
  step: string;
@@ -22,14 +22,12 @@ exports.readPropertyChildren = readPropertyChildren;
22
22
  const create_chain_node_1 = require("../chain/create-chain-node");
23
23
  const create_field_slots_1 = require("../chain/create-field-slots");
24
24
  const types_1 = require("../types");
25
+ const assert_object_keyword_values_1 = require("./assert-object-keyword-values");
25
26
  const declare_additional_properties_1 = require("./declare-additional-properties");
26
27
  const apply_keyword_binding_1 = require("./apply-keyword-binding");
27
28
  const collect_definitions_1 = require("./collect-definitions");
28
29
  const keyword_map_object_1 = require("./keyword-map-object");
29
30
  const NO_RULES = Object.freeze([]);
30
- function readRules(chain) {
31
- return (0, create_chain_node_1.readChainRules)(chain) ?? NO_RULES;
32
- }
33
31
  function declareObjectRules(schema, context) {
34
32
  let chain = (0, create_field_slots_1.createFieldSlots)(context.bag, context.build).object;
35
33
  if (schema.minProperties !== undefined) {
@@ -46,11 +44,12 @@ function declareObjectRules(schema, context) {
46
44
  // stays: it holds the keyword table and the check that the method exists.
47
45
  chain = (0, declare_additional_properties_1.applyAdditionalPropertiesBoolean)(chain, schema, additional);
48
46
  }
49
- return readRules(chain);
47
+ return (0, create_chain_node_1.readChainRules)(chain) ?? NO_RULES;
50
48
  }
51
49
  /** EVERY matching pattern applies; 1.x broke after the first match. */
52
50
  function declarePatternProperties(schema, context) {
53
51
  const patterns = schema.patternProperties;
52
+ (0, assert_object_keyword_values_1.assertPatternPropertiesIsSchemaMap)(patterns);
54
53
  if (patterns === undefined)
55
54
  return NO_RULES;
56
55
  const byPattern = {};
@@ -65,6 +64,7 @@ function declarePatternProperties(schema, context) {
65
64
  /** A sub-chain over the property NAMES, which are always strings. */
66
65
  function declarePropertyNames(schema, context) {
67
66
  const names = schema.propertyNames;
67
+ (0, assert_object_keyword_values_1.assertPropertyNamesIsSchema)(names);
68
68
  if (names === undefined)
69
69
  return NO_RULES;
70
70
  const plugin = context.bag.objectPropertyNames;
@@ -86,6 +86,7 @@ function splitDependencies(dependencies) {
86
86
  }
87
87
  function declareDependencies(schema, context) {
88
88
  const dependencies = schema.dependencies;
89
+ (0, assert_object_keyword_values_1.assertDependenciesIsSchemaOrNameListMap)(dependencies);
89
90
  if (dependencies === undefined)
90
91
  return NO_RULES;
91
92
  const split = splitDependencies(dependencies);
@@ -115,8 +116,14 @@ function declareDependencies(schema, context) {
115
116
  * (declareRequiredProperties). A name in `required` with no `properties` entry
116
117
  * still gets a path on the empty schema, because §6.5.3 makes the two keywords
117
118
  * independent and the root distribution needs the path to exist.
119
+ *
120
+ * Both keywords are first READ here, for the root and for every nested node
121
+ * alike, so this is where both are checked. Unchecked, a string `required` is
122
+ * iterated character by character and every character becomes a declared path.
118
123
  */
119
124
  function readPropertyChildren(schema) {
125
+ (0, assert_object_keyword_values_1.assertPropertiesIsSchemaMap)(schema.properties);
126
+ (0, assert_object_keyword_values_1.assertRequiredIsNameList)(schema.required);
120
127
  const properties = schema.properties ?? {};
121
128
  const required = schema.required ?? [];
122
129
  const children = Object.entries(properties).map(([key, member]) => ({
@@ -15,14 +15,12 @@
15
15
  import { readChainRules } from "../chain/create-chain-node.mjs";
16
16
  import { createFieldSlots } from "../chain/create-field-slots.mjs";
17
17
  import { isStringArray } from "../types/index.mjs";
18
+ import { assertDependenciesIsSchemaOrNameListMap, assertPatternPropertiesIsSchemaMap, assertPropertiesIsSchemaMap, assertPropertyNamesIsSchema, assertRequiredIsNameList, } from "./assert-object-keyword-values.mjs";
18
19
  import { applyAdditionalPropertiesBoolean } from "./declare-additional-properties.mjs";
19
20
  import { applyKeywordBinding } from "./apply-keyword-binding.mjs";
20
21
  import { toSchemaObject } from "./collect-definitions.mjs";
21
22
  import { maxPropertiesBinding, minPropertiesBinding, } from "./keyword-map-object.mjs";
22
23
  const NO_RULES = Object.freeze([]);
23
- function readRules(chain) {
24
- return readChainRules(chain) ?? NO_RULES;
25
- }
26
24
  export function declareObjectRules(schema, context) {
27
25
  let chain = createFieldSlots(context.bag, context.build).object;
28
26
  if (schema.minProperties !== undefined) {
@@ -39,11 +37,12 @@ export function declareObjectRules(schema, context) {
39
37
  // stays: it holds the keyword table and the check that the method exists.
40
38
  chain = applyAdditionalPropertiesBoolean(chain, schema, additional);
41
39
  }
42
- return readRules(chain);
40
+ return readChainRules(chain) ?? NO_RULES;
43
41
  }
44
42
  /** EVERY matching pattern applies; 1.x broke after the first match. */
45
43
  export function declarePatternProperties(schema, context) {
46
44
  const patterns = schema.patternProperties;
45
+ assertPatternPropertiesIsSchemaMap(patterns);
47
46
  if (patterns === undefined)
48
47
  return NO_RULES;
49
48
  const byPattern = {};
@@ -58,6 +57,7 @@ export function declarePatternProperties(schema, context) {
58
57
  /** A sub-chain over the property NAMES, which are always strings. */
59
58
  export function declarePropertyNames(schema, context) {
60
59
  const names = schema.propertyNames;
60
+ assertPropertyNamesIsSchema(names);
61
61
  if (names === undefined)
62
62
  return NO_RULES;
63
63
  const plugin = context.bag.objectPropertyNames;
@@ -79,6 +79,7 @@ function splitDependencies(dependencies) {
79
79
  }
80
80
  export function declareDependencies(schema, context) {
81
81
  const dependencies = schema.dependencies;
82
+ assertDependenciesIsSchemaOrNameListMap(dependencies);
82
83
  if (dependencies === undefined)
83
84
  return NO_RULES;
84
85
  const split = splitDependencies(dependencies);
@@ -108,8 +109,14 @@ export function declareDependencies(schema, context) {
108
109
  * (declareRequiredProperties). A name in `required` with no `properties` entry
109
110
  * still gets a path on the empty schema, because §6.5.3 makes the two keywords
110
111
  * independent and the root distribution needs the path to exist.
112
+ *
113
+ * Both keywords are first READ here, for the root and for every nested node
114
+ * alike, so this is where both are checked. Unchecked, a string `required` is
115
+ * iterated character by character and every character becomes a declared path.
111
116
  */
112
117
  export function readPropertyChildren(schema) {
118
+ assertPropertiesIsSchemaMap(schema.properties);
119
+ assertRequiredIsNameList(schema.required);
113
120
  const properties = schema.properties ?? {};
114
121
  const required = schema.required ?? [];
115
122
  const children = Object.entries(properties).map(([key, member]) => ({
@@ -19,6 +19,7 @@ exports.declareRequiredProperties = declareRequiredProperties;
19
19
  // ===========================================================================
20
20
  const create_rule_1 = require("../plugin-kit/create-rule");
21
21
  const types_1 = require("../types");
22
+ const assert_object_keyword_values_1 = require("./assert-object-keyword-values");
22
23
  const NO_RULES = Object.freeze([]);
23
24
  /**
24
25
  * `required` as a rule on the OBJECT, not as a presence rule on each child.
@@ -36,6 +37,11 @@ const NO_RULES = Object.freeze([]);
36
37
  */
37
38
  function declareRequiredProperties(schema, context) {
38
39
  const required = schema.required;
40
+ // Checked again here, and not only where the children are read: this rule
41
+ // closes over `required` and calls `.filter` on it at VALIDATION time, so an
42
+ // unchecked non-array escapes the build and surfaces as a raw TypeError on
43
+ // the first request.
44
+ (0, assert_object_keyword_values_1.assertRequiredIsNameList)(required);
39
45
  if (required === undefined || required.length === 0)
40
46
  return NO_RULES;
41
47
  const missingOf = (value) => required.filter((key) => !Object.prototype.hasOwnProperty.call(value, key));
@@ -16,6 +16,7 @@
16
16
  // ===========================================================================
17
17
  import { check } from "../plugin-kit/create-rule.mjs";
18
18
  import { PASS, fail, isArray, isPlainObject } from "../types/index.mjs";
19
+ import { assertRequiredIsNameList } from "./assert-object-keyword-values.mjs";
19
20
  const NO_RULES = Object.freeze([]);
20
21
  /**
21
22
  * `required` as a rule on the OBJECT, not as a presence rule on each child.
@@ -33,6 +34,11 @@ const NO_RULES = Object.freeze([]);
33
34
  */
34
35
  export function declareRequiredProperties(schema, context) {
35
36
  const required = schema.required;
37
+ // Checked again here, and not only where the children are read: this rule
38
+ // closes over `required` and calls `.filter` on it at VALIDATION time, so an
39
+ // unchecked non-array escapes the build and surfaces as a raw TypeError on
40
+ // the first request.
41
+ assertRequiredIsNameList(required);
36
42
  if (required === undefined || required.length === 0)
37
43
  return NO_RULES;
38
44
  const missingOf = (value) => required.filter((key) => !Object.prototype.hasOwnProperty.call(value, key));
@@ -1,7 +1,16 @@
1
1
  import type { Rule } from "../plugin-kit/compiled-rule";
2
2
  import type { Draft07SchemaObject, Draft07TypeKeyword } from "./draft07.types";
3
3
  import type { StructuralContext } from "./structural-expansion.types";
4
- /** The declared names, always as a list, with unknown names rejected loudly. */
4
+ /**
5
+ * The declared names, always as a list. Every departure from the meta-schema
6
+ * is a refusal rather than a name dropped from the list: a dropped name leaves
7
+ * NO type rule at all (the list is what declareTypeRules counts) and also
8
+ * turns permitsNull true, so `{"type":"strig"}` would both stop checking the
9
+ * type and start accepting null.
10
+ *
11
+ * `schema.type` is typed by draft07.types.ts, but the document it came from is
12
+ * plain JSON that nothing has checked, so the value is re-read as `unknown`.
13
+ */
5
14
  export declare function readDeclaredTypes(schema: Draft07SchemaObject): readonly Draft07TypeKeyword[];
6
15
  /** True when the document permits an explicit null at this position. */
7
16
  export declare function permitsNull(schema: Draft07SchemaObject): boolean;
@@ -19,6 +19,11 @@ exports.declareEnumRules = declareEnumRules;
19
19
  // primitive type (`object` is the only type plugin in the catalogue and it is
20
20
  // not in the bag).
21
21
  //
22
+ // `type` is also the ONE keyword here whose value is refused rather than
23
+ // normalised, because it is the only rule in the converter that judges the
24
+ // value's type: a name this module cannot read would take the whole check with
25
+ // it. See readDeclaredTypes.
26
+ //
22
27
  // `null` never reaches a check — src/runtime/decide-presence.ts settles absence
23
28
  // first — so `type: "null"` reads as "every value that got this far is wrong",
24
29
  // which is exactly right: the only null-shaped value was already accepted by
@@ -30,8 +35,9 @@ const create_rule_1 = require("../plugin-kit/create-rule");
30
35
  const types_1 = require("../types");
31
36
  const apply_keyword_binding_1 = require("./apply-keyword-binding");
32
37
  const keyword_map_core_1 = require("./keyword-map-core");
38
+ const malformed_schema_error_1 = require("./malformed-schema-error");
33
39
  const NO_RULES = Object.freeze([]);
34
- /** Every Draft-07 `type` name, so an unknown one is a build-time refusal. */
40
+ /** The seven names, and the test each one stands for. Nothing else is a type. */
35
41
  const JSON_TYPE_TESTS = {
36
42
  string: types_1.isString,
37
43
  number: (value) => typeof value === "number",
@@ -44,13 +50,46 @@ const JSON_TYPE_TESTS = {
44
50
  function isTypeKeyword(name) {
45
51
  return Object.prototype.hasOwnProperty.call(JSON_TYPE_TESTS, name);
46
52
  }
47
- /** The declared names, always as a list, with unknown names rejected loudly. */
53
+ /**
54
+ * One member of `type`, checked against the seven names.
55
+ *
56
+ * The string check is not decoration and must stay FIRST: the lookup below is
57
+ * a property lookup, so a non-string key is coerced to its string form, and
58
+ * `null` coerces to the name "null" — an unguarded lookup therefore reads
59
+ * `{"type": null}` as `{"type": "null"}` and changes what the document says.
60
+ */
61
+ function readTypeName(name) {
62
+ if (!(0, types_1.isString)(name)) {
63
+ throw new malformed_schema_error_1.MalformedSchemaError("type", "every member of the array form must be a string", name);
64
+ }
65
+ if (!isTypeKeyword(name)) {
66
+ throw new malformed_schema_error_1.MalformedSchemaError("type", `the name "${name}" must be one of the seven type names`, name);
67
+ }
68
+ return name;
69
+ }
70
+ /**
71
+ * The declared names, always as a list. Every departure from the meta-schema
72
+ * is a refusal rather than a name dropped from the list: a dropped name leaves
73
+ * NO type rule at all (the list is what declareTypeRules counts) and also
74
+ * turns permitsNull true, so `{"type":"strig"}` would both stop checking the
75
+ * type and start accepting null.
76
+ *
77
+ * `schema.type` is typed by draft07.types.ts, but the document it came from is
78
+ * plain JSON that nothing has checked, so the value is re-read as `unknown`.
79
+ */
48
80
  function readDeclaredTypes(schema) {
49
81
  const declared = schema.type;
50
82
  if (declared === undefined)
51
83
  return Object.freeze([]);
52
- const names = (0, types_1.isArray)(declared) ? declared : [declared];
53
- return Object.freeze(names.filter((name) => isTypeKeyword(name)));
84
+ if ((0, types_1.isString)(declared))
85
+ return Object.freeze([readTypeName(declared)]);
86
+ if (!(0, types_1.isArray)(declared)) {
87
+ throw new malformed_schema_error_1.MalformedSchemaError("type", "the value must be a type name or an array of type names", declared);
88
+ }
89
+ if (declared.length === 0) {
90
+ throw new malformed_schema_error_1.MalformedSchemaError("type", "the array form must hold at least one name (minItems 1)", declared);
91
+ }
92
+ return Object.freeze(declared.map((name) => readTypeName(name)));
54
93
  }
55
94
  /** True when the document permits an explicit null at this position. */
56
95
  function permitsNull(schema) {
@@ -11,6 +11,11 @@
11
11
  // primitive type (`object` is the only type plugin in the catalogue and it is
12
12
  // not in the bag).
13
13
  //
14
+ // `type` is also the ONE keyword here whose value is refused rather than
15
+ // normalised, because it is the only rule in the converter that judges the
16
+ // value's type: a name this module cannot read would take the whole check with
17
+ // it. See readDeclaredTypes.
18
+ //
14
19
  // `null` never reaches a check — src/runtime/decide-presence.ts settles absence
15
20
  // first — so `type: "null"` reads as "every value that got this far is wrong",
16
21
  // which is exactly right: the only null-shaped value was already accepted by
@@ -22,8 +27,9 @@ import { check } from "../plugin-kit/create-rule.mjs";
22
27
  import { PASS, fail, isArray, isPlainObject, isString } from "../types/index.mjs";
23
28
  import { applyKeywordBinding } from "./apply-keyword-binding.mjs";
24
29
  import { constBinding } from "./keyword-map-core.mjs";
30
+ import { MalformedSchemaError } from "./malformed-schema-error.mjs";
25
31
  const NO_RULES = Object.freeze([]);
26
- /** Every Draft-07 `type` name, so an unknown one is a build-time refusal. */
32
+ /** The seven names, and the test each one stands for. Nothing else is a type. */
27
33
  const JSON_TYPE_TESTS = {
28
34
  string: isString,
29
35
  number: (value) => typeof value === "number",
@@ -36,13 +42,46 @@ const JSON_TYPE_TESTS = {
36
42
  function isTypeKeyword(name) {
37
43
  return Object.prototype.hasOwnProperty.call(JSON_TYPE_TESTS, name);
38
44
  }
39
- /** The declared names, always as a list, with unknown names rejected loudly. */
45
+ /**
46
+ * One member of `type`, checked against the seven names.
47
+ *
48
+ * The string check is not decoration and must stay FIRST: the lookup below is
49
+ * a property lookup, so a non-string key is coerced to its string form, and
50
+ * `null` coerces to the name "null" — an unguarded lookup therefore reads
51
+ * `{"type": null}` as `{"type": "null"}` and changes what the document says.
52
+ */
53
+ function readTypeName(name) {
54
+ if (!isString(name)) {
55
+ throw new MalformedSchemaError("type", "every member of the array form must be a string", name);
56
+ }
57
+ if (!isTypeKeyword(name)) {
58
+ throw new MalformedSchemaError("type", `the name "${name}" must be one of the seven type names`, name);
59
+ }
60
+ return name;
61
+ }
62
+ /**
63
+ * The declared names, always as a list. Every departure from the meta-schema
64
+ * is a refusal rather than a name dropped from the list: a dropped name leaves
65
+ * NO type rule at all (the list is what declareTypeRules counts) and also
66
+ * turns permitsNull true, so `{"type":"strig"}` would both stop checking the
67
+ * type and start accepting null.
68
+ *
69
+ * `schema.type` is typed by draft07.types.ts, but the document it came from is
70
+ * plain JSON that nothing has checked, so the value is re-read as `unknown`.
71
+ */
40
72
  export function readDeclaredTypes(schema) {
41
73
  const declared = schema.type;
42
74
  if (declared === undefined)
43
75
  return Object.freeze([]);
44
- const names = isArray(declared) ? declared : [declared];
45
- return Object.freeze(names.filter((name) => isTypeKeyword(name)));
76
+ if (isString(declared))
77
+ return Object.freeze([readTypeName(declared)]);
78
+ if (!isArray(declared)) {
79
+ throw new MalformedSchemaError("type", "the value must be a type name or an array of type names", declared);
80
+ }
81
+ if (declared.length === 0) {
82
+ throw new MalformedSchemaError("type", "the array form must hold at least one name (minItems 1)", declared);
83
+ }
84
+ return Object.freeze(declared.map((name) => readTypeName(name)));
46
85
  }
47
86
  /** True when the document permits an explicit null at this position. */
48
87
  export function permitsNull(schema) {
@@ -1,4 +1,4 @@
1
1
  export { SCHEMA_BRANCH_LABEL, collectDocumentRules, jsonSchemaPlugin, } from "./json-schema";
2
2
  export type { JsonSchemaOptions } from "./json-schema";
3
- export { NotASchemaError, UnsupportedKeywordError, RefResolutionError, buildFieldEntries, buildFromSchema, fromJsonSchema, isDraft07Schema, listBoundPluginNames, listDraft07Keywords, listFormatNames, } from "../../index";
4
- export type { Draft07Schema, Draft07SchemaObject, JsonSchemaBag, } from "../../index";
3
+ export { DRAFT07_DIALECT_URI, MalformedSchemaError, NotASchemaError, UnsupportedDialectError, UnsupportedKeywordError, RefResolutionError, buildFieldEntries, buildFromSchema, fromJsonSchema, isDraft07Schema, listBoundPluginNames, listDraft07Keywords, listFormatNames, } from "../../index";
4
+ export type { DialectOptions, Draft07Schema, Draft07SchemaObject, JsonSchemaBag, } from "../../index";
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.listFormatNames = exports.listDraft07Keywords = exports.listBoundPluginNames = exports.isDraft07Schema = exports.fromJsonSchema = exports.buildFromSchema = exports.buildFieldEntries = exports.RefResolutionError = exports.UnsupportedKeywordError = exports.NotASchemaError = exports.jsonSchemaPlugin = exports.collectDocumentRules = exports.SCHEMA_BRANCH_LABEL = void 0;
3
+ exports.listFormatNames = exports.listDraft07Keywords = exports.listBoundPluginNames = exports.isDraft07Schema = exports.fromJsonSchema = exports.buildFromSchema = exports.buildFieldEntries = exports.RefResolutionError = exports.UnsupportedKeywordError = exports.UnsupportedDialectError = exports.NotASchemaError = exports.MalformedSchemaError = exports.DRAFT07_DIALECT_URI = exports.jsonSchemaPlugin = exports.collectDocumentRules = exports.SCHEMA_BRANCH_LABEL = void 0;
4
4
  // ===========================================================================
5
5
  // The public subpath `@maroonedog/luq/plugins/jsonSchema`. Re-exports only.
6
6
  //
@@ -15,7 +15,10 @@ Object.defineProperty(exports, "SCHEMA_BRANCH_LABEL", { enumerable: true, get: f
15
15
  Object.defineProperty(exports, "collectDocumentRules", { enumerable: true, get: function () { return json_schema_1.collectDocumentRules; } });
16
16
  Object.defineProperty(exports, "jsonSchemaPlugin", { enumerable: true, get: function () { return json_schema_1.jsonSchemaPlugin; } });
17
17
  var index_1 = require("../../index");
18
+ Object.defineProperty(exports, "DRAFT07_DIALECT_URI", { enumerable: true, get: function () { return index_1.DRAFT07_DIALECT_URI; } });
19
+ Object.defineProperty(exports, "MalformedSchemaError", { enumerable: true, get: function () { return index_1.MalformedSchemaError; } });
18
20
  Object.defineProperty(exports, "NotASchemaError", { enumerable: true, get: function () { return index_1.NotASchemaError; } });
21
+ Object.defineProperty(exports, "UnsupportedDialectError", { enumerable: true, get: function () { return index_1.UnsupportedDialectError; } });
19
22
  Object.defineProperty(exports, "UnsupportedKeywordError", { enumerable: true, get: function () { return index_1.UnsupportedKeywordError; } });
20
23
  Object.defineProperty(exports, "RefResolutionError", { enumerable: true, get: function () { return index_1.RefResolutionError; } });
21
24
  Object.defineProperty(exports, "buildFieldEntries", { enumerable: true, get: function () { return index_1.buildFieldEntries; } });
@@ -8,4 +8,4 @@
8
8
  // a consumer of this subpath pays only for the plugins they import themselves.
9
9
  // ===========================================================================
10
10
  export { SCHEMA_BRANCH_LABEL, collectDocumentRules, jsonSchemaPlugin, } from "./json-schema.mjs";
11
- export { NotASchemaError, UnsupportedKeywordError, RefResolutionError, buildFieldEntries, buildFromSchema, fromJsonSchema, isDraft07Schema, listBoundPluginNames, listDraft07Keywords, listFormatNames, } from "../../index.mjs";
11
+ export { DRAFT07_DIALECT_URI, MalformedSchemaError, NotASchemaError, UnsupportedDialectError, UnsupportedKeywordError, RefResolutionError, buildFieldEntries, buildFromSchema, fromJsonSchema, isDraft07Schema, listBoundPluginNames, listDraft07Keywords, listFormatNames, } from "../../index.mjs";