@amritk/helpers 0.15.3 → 0.16.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.
- package/dist/assert-id-scopes.js +9 -8
- package/dist/assign-key.d.ts +17 -0
- package/dist/assign-key.js +10 -0
- package/dist/build-anchor-map.js +8 -8
- package/dist/build-dynamic-ref-map.js +10 -10
- package/dist/build-resource-registry.d.ts +54 -0
- package/dist/build-resource-registry.js +35 -12
- package/dist/derive-root-type-name.js +9 -5
- package/dist/escape-regex-pattern.js +42 -35
- package/dist/extract-refs.js +11 -6
- package/dist/fold-nullable.js +17 -10
- package/dist/generate-index-barrel.js +7 -11
- package/dist/generate-type-definition.js +156 -138
- package/dist/graft-external-schemas.js +3 -2
- package/dist/has-ref.js +1 -1
- package/dist/max-schema-depth.d.ts +8 -1
- package/dist/max-schema-depth.js +3 -3
- package/dist/mjst-extension.js +3 -2
- package/dist/normalize-ref-scopes.js +12 -6
- package/dist/parse-documentation.js +1 -1
- package/dist/prune-external-schemas.js +10 -8
- package/dist/quote-js-string.js +3 -1
- package/dist/read-key.d.ts +25 -0
- package/dist/read-key.js +6 -0
- package/dist/ref-to-name.d.ts +41 -0
- package/dist/ref-to-name.js +11 -5
- package/dist/resolve-dynamic-refs.js +19 -12
- package/dist/resolve-ref.js +2 -1
- package/dist/safe-accessor.d.ts +1 -1
- package/dist/safe-accessor.js +1 -1
- package/dist/schema-guards.js +34 -34
- package/dist/string-length-check.js +2 -2
- package/dist/unknown-key-check.d.ts +14 -3
- package/dist/unknown-key-check.js +2 -2
- package/dist/upgrade-draft07-schema.js +49 -34
- package/dist/validate-record.d.ts +6 -1
- package/dist/validate-record.js +1 -1
- package/dist/walk-ref-graph.js +75 -33
- package/package.json +1 -1
- package/src/has-ref.ts +1 -1
- package/src/validate-record.ts +7 -2
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { assignKey } from "./assign-key.js";
|
|
2
|
+
import { buildResourceRegistry, entersSchemaMap, escapePointerSegment, isDataPosition, resolveUri } from "./build-resource-registry.js";
|
|
2
3
|
import { assertSchemaDepth } from "./max-schema-depth.js";
|
|
3
4
|
import { resolveRef } from "./resolve-ref.js";
|
|
4
5
|
import { resolveScopedRef } from "./resolve-scoped-ref.js";
|
|
@@ -26,23 +27,28 @@ const normalizeRefScopes = (root) => {
|
|
|
26
27
|
}
|
|
27
28
|
return normalizedRef(ref, base);
|
|
28
29
|
};
|
|
29
|
-
const rewrite = (node, pointer, enclosing, depth) => {
|
|
30
|
+
const rewrite = (node, pointer, enclosing, depth, inSchemaMap = false) => {
|
|
30
31
|
assertSchemaDepth(depth, "normalizeRefScopes");
|
|
31
32
|
if (node === null || typeof node !== "object")
|
|
32
33
|
return node;
|
|
33
34
|
if (Array.isArray(node)) {
|
|
34
|
-
const mapped = node.map((item, index) => rewrite(item, `${pointer}/${index}`, enclosing, depth + 1));
|
|
35
|
+
const mapped = node.map((item, index) => rewrite(item, `${pointer}/${index}`, enclosing, depth + 1, inSchemaMap));
|
|
35
36
|
return mapped.some((item, index) => item !== node[index]) ? mapped : node;
|
|
36
37
|
}
|
|
37
38
|
const record = node;
|
|
38
39
|
const base = registry.baseAt.get(pointer) ?? enclosing;
|
|
39
40
|
let changed = false;
|
|
40
41
|
const result = {};
|
|
41
|
-
for (const [key, value] of Object.entries(record))
|
|
42
|
-
|
|
42
|
+
for (const [key, value] of Object.entries(record))
|
|
43
|
+
assignKey(result, key, value);
|
|
44
|
+
for (const key of Object.keys(record)) {
|
|
45
|
+
if (isDataPosition(key, inSchemaMap))
|
|
46
|
+
continue;
|
|
47
|
+
const value = record[key];
|
|
48
|
+
const next = !inSchemaMap && key === "$ref" && typeof value === "string" ? normalizedRef(value, base) ?? value : !inSchemaMap && key === "$dynamicRef" && typeof value === "string" ? normalizedDynamicRef(value, base) ?? value : rewrite(value, `${pointer}/${escapePointerSegment(key)}`, base, depth + 1, entersSchemaMap(key, inSchemaMap));
|
|
43
49
|
if (next !== value)
|
|
44
50
|
changed = true;
|
|
45
|
-
result
|
|
51
|
+
assignKey(result, key, next);
|
|
46
52
|
}
|
|
47
53
|
return changed ? result : record;
|
|
48
54
|
};
|
|
@@ -85,7 +85,7 @@ const parseDocumentation = (markdownDocumentation, commentUrl, fallbackCommentUr
|
|
|
85
85
|
properties
|
|
86
86
|
};
|
|
87
87
|
} catch (error) {
|
|
88
|
-
console.error(
|
|
88
|
+
console.error(`Failed to parse OpenAPI documentation for ${commentUrl}:`, error);
|
|
89
89
|
return null;
|
|
90
90
|
}
|
|
91
91
|
};
|
|
@@ -1,7 +1,9 @@
|
|
|
1
|
+
import { assignKey } from "./assign-key.js";
|
|
1
2
|
import { buildDynamicRefMap } from "./build-dynamic-ref-map.js";
|
|
2
3
|
import { assertSchemaDepth } from "./max-schema-depth.js";
|
|
4
|
+
import { readKey } from "./read-key.js";
|
|
3
5
|
const DEFS_PREFIX = "#/$defs/";
|
|
4
|
-
const unescapeSegment = (segment) => segment.indexOf("~") === -1 ? segment : segment.replace(/~1/g, "/").replace(/~0/g, "~");
|
|
6
|
+
const unescapeSegment = (segment) => segment.indexOf("~") === -1 && segment.indexOf("%25") === -1 ? segment : segment.replace(/~1/g, "/").replace(/~0/g, "~").replace(/%25/g, "%");
|
|
5
7
|
const externalTarget = (ref, external) => {
|
|
6
8
|
if (!ref.startsWith(DEFS_PREFIX))
|
|
7
9
|
return void 0;
|
|
@@ -21,21 +23,21 @@ const referencedFrom = (node, external, dynamicMap, into) => {
|
|
|
21
23
|
return;
|
|
22
24
|
}
|
|
23
25
|
const record = value;
|
|
24
|
-
const ref = record["$ref"];
|
|
26
|
+
const ref = Object.hasOwn(record, "$ref") ? record["$ref"] : void 0;
|
|
25
27
|
if (typeof ref === "string") {
|
|
26
28
|
const name = externalTarget(ref, external);
|
|
27
29
|
if (name !== void 0)
|
|
28
30
|
into.add(name);
|
|
29
31
|
}
|
|
30
|
-
const dynamicRef = record["$dynamicRef"];
|
|
32
|
+
const dynamicRef = Object.hasOwn(record, "$dynamicRef") ? record["$dynamicRef"] : void 0;
|
|
31
33
|
if (typeof dynamicRef === "string") {
|
|
32
|
-
const target = dynamicRef.startsWith("#/") ? dynamicRef : dynamicMap
|
|
34
|
+
const target = dynamicRef.startsWith("#/") ? dynamicRef : readKey(dynamicMap, dynamicRef);
|
|
33
35
|
const name = target === void 0 ? void 0 : externalTarget(target, external);
|
|
34
36
|
if (name !== void 0)
|
|
35
37
|
into.add(name);
|
|
36
38
|
}
|
|
37
|
-
for (const
|
|
38
|
-
walk(
|
|
39
|
+
for (const value2 of Object.values(record))
|
|
40
|
+
walk(value2, depth + 1);
|
|
39
41
|
};
|
|
40
42
|
walk(node, 0);
|
|
41
43
|
};
|
|
@@ -50,7 +52,7 @@ const pruneExternalSchemas = (document, external) => {
|
|
|
50
52
|
const authored = {};
|
|
51
53
|
for (const [name, definition] of Object.entries(definitions)) {
|
|
52
54
|
if (!external.has(name))
|
|
53
|
-
authored
|
|
55
|
+
assignKey(authored, name, definition);
|
|
54
56
|
}
|
|
55
57
|
const seed = { ...document, $defs: authored };
|
|
56
58
|
const reached = /* @__PURE__ */ new Set();
|
|
@@ -71,7 +73,7 @@ const pruneExternalSchemas = (document, external) => {
|
|
|
71
73
|
const kept = {};
|
|
72
74
|
for (const [name, definition] of Object.entries(definitions)) {
|
|
73
75
|
if (!external.has(name) || reached.has(name))
|
|
74
|
-
kept
|
|
76
|
+
assignKey(kept, name, definition);
|
|
75
77
|
}
|
|
76
78
|
return { ...document, $defs: kept };
|
|
77
79
|
};
|
package/dist/quote-js-string.js
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
const
|
|
1
|
+
const UNESCAPABLE_RAW = /["\\\u0000-\u001f\u2028\u2029]/;
|
|
2
|
+
const LONE_SURROGATE = /[\uD800-\uDBFF](?![\uDC00-\uDFFF])|(?<![\uD800-\uDBFF])[\uDC00-\uDFFF]/;
|
|
3
|
+
const NEEDS_ESCAPING = new RegExp(`${UNESCAPABLE_RAW.source}|${LONE_SURROGATE.source}`);
|
|
2
4
|
const quoteJsString = (text) => NEEDS_ESCAPING.test(text) ? JSON.stringify(text) : `"${text}"`;
|
|
3
5
|
export {
|
|
4
6
|
quoteJsString
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Reads `key` off a map of author-chosen names, treating an inherited name as
|
|
3
|
+
* absent.
|
|
4
|
+
*
|
|
5
|
+
* The maps this guards — `$defs`, a `$dynamicAnchor` index, a config-key table
|
|
6
|
+
* — are keyed by names taken from a schema or a config file, so `__proto__`,
|
|
7
|
+
* `constructor` and `toString` are all names an author may legitimately use.
|
|
8
|
+
* A bare index answers those from `Object.prototype`, which is never what the
|
|
9
|
+
* caller meant: `$defs.__proto__` hands back `Object.prototype` as though it
|
|
10
|
+
* were a declared definition, and `map.toString` hands back a `Function` where
|
|
11
|
+
* a schema was expected.
|
|
12
|
+
*/
|
|
13
|
+
export declare const readKey: <T>(source: Readonly<Record<string, T>>, key: string) => T | undefined;
|
|
14
|
+
/**
|
|
15
|
+
* True when `source` declares `key` itself — the presence half of the same
|
|
16
|
+
* question {@link readKey} answers by value.
|
|
17
|
+
*
|
|
18
|
+
* A caller that only needs to know whether a keyword is *there* would otherwise
|
|
19
|
+
* write `'items' in schema`, and `in` walks the prototype chain: with
|
|
20
|
+
* `Object.prototype.items` set, every node in a document declares a keyword none
|
|
21
|
+
* of them wrote, and what the generators emit from that is a different validator
|
|
22
|
+
* — an inherited `items: false` capping every array at zero, an inherited `if`
|
|
23
|
+
* sending a walk into unbounded recursion.
|
|
24
|
+
*/
|
|
25
|
+
export declare const declaresKey: (source: object, key: string) => boolean;
|
package/dist/read-key.js
ADDED
package/dist/ref-to-name.d.ts
CHANGED
|
@@ -1,3 +1,44 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Converts a filename-like string to a PascalCase TypeScript identifier,
|
|
3
|
+
* appending an optional suffix.
|
|
4
|
+
*
|
|
5
|
+
* Word boundaries are *any* run of characters that cannot appear in a JS
|
|
6
|
+
* identifier — not just `-` — so a dotted key like `io.k8s.api.core.v1.Pod`
|
|
7
|
+
* becomes `IoK8sApiCoreV1Pod` instead of the invalid `Io.k8s.api.core.v1.pod`.
|
|
8
|
+
* A name that does not start with an identifier character (a leading digit, say)
|
|
9
|
+
* is prefixed with `_`.
|
|
10
|
+
*
|
|
11
|
+
* The `_` fallback for an empty result is a backstop only: `refToFilename`
|
|
12
|
+
* already normalizes a name that would reduce to nothing into a `ref-<hash>`
|
|
13
|
+
* form, so distinct refs stay distinct before they ever reach this function.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```ts
|
|
17
|
+
* kebabToPascal('server-variable') // 'ServerVariable'
|
|
18
|
+
* kebabToPascal('channel') // 'Channel'
|
|
19
|
+
* kebabToPascal('channel', 'Object') // 'ChannelObject'
|
|
20
|
+
* kebabToPascal('io.k8s.api.core.v1.pod') // 'IoK8sApiCoreV1Pod'
|
|
21
|
+
* kebabToPascal('123abc') // '_123abc'
|
|
22
|
+
* kebabToPascal('中文') // '中文'
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
/**
|
|
26
|
+
* Splits `text` on characters TypeScript will not accept inside an identifier
|
|
27
|
+
* and joins the words in PascalCase, keeping the rest of each word so acronyms
|
|
28
|
+
* survive. Returns `''` when nothing usable is left, leaving the emptiness
|
|
29
|
+
* policy to the caller.
|
|
30
|
+
*
|
|
31
|
+
* Shared with {@link deriveRootTypeName} rather than reimplemented there: that
|
|
32
|
+
* copy split on `[^a-zA-Z0-9]+`, which is the ASCII-only class this module's
|
|
33
|
+
* `WORD_SEPARATOR` doc describes fixing. A document titled `中文` came back
|
|
34
|
+
* empty and was named `Document`, `Café Menu` came back as `CafMenu` with the
|
|
35
|
+
* `é` deleted from the middle of a word, and `Ünïcödé Doc` as `NCDDoc` — while
|
|
36
|
+
* a `$ref` to a definition of the same name was spelled correctly, so the root
|
|
37
|
+
* type and the refs into it disagreed.
|
|
38
|
+
*/
|
|
39
|
+
export declare const toPascalWords: (text: string) => string;
|
|
40
|
+
/** True when `name` may begin a TypeScript identifier. */
|
|
41
|
+
export declare const isIdentifierStart: (name: string) => boolean;
|
|
1
42
|
/**
|
|
2
43
|
* Converts a JSON Schema $ref to a type name.
|
|
3
44
|
* Derives the filename via `refToFilename` then converts to PascalCase,
|
package/dist/ref-to-name.js
CHANGED
|
@@ -1,22 +1,28 @@
|
|
|
1
1
|
import { refToFilename } from "./ref-to-filename.js";
|
|
2
2
|
const WORD_SEPARATOR = /[^\p{ID_Continue}]+|_+/u;
|
|
3
3
|
const IDENTIFIER_START = /^[\p{ID_Start}_$]/u;
|
|
4
|
-
const
|
|
5
|
-
const words = kebab.split(WORD_SEPARATOR);
|
|
4
|
+
const toPascalWords = (text) => {
|
|
6
5
|
let pascalCase = "";
|
|
7
|
-
for (const word of
|
|
6
|
+
for (const word of text.split(WORD_SEPARATOR)) {
|
|
8
7
|
if (word === void 0 || word === "")
|
|
9
8
|
continue;
|
|
10
9
|
const first = [...word][0];
|
|
11
10
|
pascalCase += first.toUpperCase() + word.slice(first.length);
|
|
12
11
|
}
|
|
12
|
+
return pascalCase;
|
|
13
|
+
};
|
|
14
|
+
const isIdentifierStart = (name) => IDENTIFIER_START.test(name);
|
|
15
|
+
const kebabToPascal = (kebab, suffix) => {
|
|
16
|
+
let pascalCase = toPascalWords(kebab);
|
|
13
17
|
if (pascalCase === "")
|
|
14
18
|
pascalCase = "_";
|
|
15
|
-
else if (!
|
|
19
|
+
else if (!isIdentifierStart(pascalCase))
|
|
16
20
|
pascalCase = `_${pascalCase}`;
|
|
17
21
|
return pascalCase + suffix;
|
|
18
22
|
};
|
|
19
23
|
const refToName = (ref, suffix = "") => kebabToPascal(refToFilename(ref), suffix);
|
|
20
24
|
export {
|
|
21
|
-
|
|
25
|
+
isIdentifierStart,
|
|
26
|
+
refToName,
|
|
27
|
+
toPascalWords
|
|
22
28
|
};
|
|
@@ -1,55 +1,62 @@
|
|
|
1
|
+
import { entersSchemaMap, isDataPosition } from "./build-resource-registry.js";
|
|
1
2
|
import { assertSchemaDepth } from "./max-schema-depth.js";
|
|
3
|
+
import { readKey } from "./read-key.js";
|
|
2
4
|
const resolveDynamicRefs = (schema, dynamicRefMap) => {
|
|
3
5
|
if (typeof schema !== "object" || schema === null) {
|
|
4
6
|
return schema;
|
|
5
7
|
}
|
|
6
|
-
if (!containsDynamicRef(schema, 0)) {
|
|
8
|
+
if (!containsDynamicRef(schema, 0, false)) {
|
|
7
9
|
return schema;
|
|
8
10
|
}
|
|
9
11
|
const clone = JSON.parse(JSON.stringify(schema));
|
|
10
|
-
const walk = (obj, depth) => {
|
|
12
|
+
const walk = (obj, depth, inSchemaMap = false) => {
|
|
11
13
|
assertSchemaDepth(depth, "resolveDynamicRefs");
|
|
12
14
|
if (typeof obj !== "object" || obj === null) {
|
|
13
15
|
return;
|
|
14
16
|
}
|
|
15
17
|
if (Array.isArray(obj)) {
|
|
16
18
|
for (const item of obj)
|
|
17
|
-
walk(item, depth + 1);
|
|
19
|
+
walk(item, depth + 1, inSchemaMap);
|
|
18
20
|
return;
|
|
19
21
|
}
|
|
20
22
|
const record = obj;
|
|
21
|
-
const dynamicRef = record
|
|
23
|
+
const dynamicRef = inSchemaMap ? void 0 : readKey(record, "$dynamicRef");
|
|
22
24
|
if (typeof dynamicRef === "string") {
|
|
23
|
-
const resolved = dynamicRefMap
|
|
25
|
+
const resolved = readKey(dynamicRefMap, dynamicRef) ?? (dynamicRef.startsWith("#/") ? dynamicRef : void 0);
|
|
24
26
|
if (resolved === void 0) {
|
|
25
27
|
throw new Error(`Unresolvable $dynamicRef "${dynamicRef}": no $dynamicAnchor named "${dynamicRef.replace(/^#/, "")}" is declared anywhere in the document. Declare the anchor, or point a plain $ref at the definition instead.`);
|
|
26
28
|
}
|
|
27
29
|
record["$ref"] = resolved;
|
|
28
30
|
delete record["$dynamicRef"];
|
|
29
31
|
}
|
|
30
|
-
for (const key
|
|
31
|
-
|
|
32
|
+
for (const key of Object.keys(record)) {
|
|
33
|
+
if (isDataPosition(key, inSchemaMap))
|
|
34
|
+
continue;
|
|
35
|
+
walk(record[key], depth + 1, entersSchemaMap(key, inSchemaMap));
|
|
32
36
|
}
|
|
33
37
|
};
|
|
34
38
|
walk(clone, 0);
|
|
35
39
|
return clone;
|
|
36
40
|
};
|
|
37
|
-
const containsDynamicRef = (value, depth) => {
|
|
41
|
+
const containsDynamicRef = (value, depth, inSchemaMap) => {
|
|
38
42
|
assertSchemaDepth(depth, "resolveDynamicRefs");
|
|
39
43
|
if (typeof value !== "object" || value === null)
|
|
40
44
|
return false;
|
|
41
45
|
if (Array.isArray(value)) {
|
|
42
46
|
for (const item of value)
|
|
43
|
-
if (containsDynamicRef(item, depth + 1))
|
|
47
|
+
if (containsDynamicRef(item, depth + 1, inSchemaMap))
|
|
44
48
|
return true;
|
|
45
49
|
return false;
|
|
46
50
|
}
|
|
47
51
|
const record = value;
|
|
48
|
-
if (typeof record
|
|
52
|
+
if (!inSchemaMap && typeof readKey(record, "$dynamicRef") === "string")
|
|
49
53
|
return true;
|
|
50
|
-
for (const key
|
|
51
|
-
if (
|
|
54
|
+
for (const key of Object.keys(record)) {
|
|
55
|
+
if (isDataPosition(key, inSchemaMap))
|
|
56
|
+
continue;
|
|
57
|
+
if (containsDynamicRef(record[key], depth + 1, entersSchemaMap(key, inSchemaMap)))
|
|
52
58
|
return true;
|
|
59
|
+
}
|
|
53
60
|
return false;
|
|
54
61
|
};
|
|
55
62
|
export {
|
package/dist/resolve-ref.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { buildAnchorMap } from "./build-anchor-map.js";
|
|
2
|
+
import { readKey } from "./read-key.js";
|
|
2
3
|
const anchorMaps = /* @__PURE__ */ new WeakMap();
|
|
3
4
|
const getAnchorMap = (rootSchema) => {
|
|
4
5
|
const existing = anchorMaps.get(rootSchema);
|
|
@@ -54,7 +55,7 @@ const resolveRef = (ref, rootSchema) => {
|
|
|
54
55
|
if (typeof defs !== "object" || defs === null)
|
|
55
56
|
return void 0;
|
|
56
57
|
const defsRecord = defs;
|
|
57
|
-
const base = defsRecord
|
|
58
|
+
const base = readKey(defsRecord, baseUri);
|
|
58
59
|
if (typeof base !== "object" || base === null)
|
|
59
60
|
return void 0;
|
|
60
61
|
if (!fragment)
|
package/dist/safe-accessor.d.ts
CHANGED
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
* safeAccessor("input?", "x-linkedin") // 'input?.["x-linkedin"]'
|
|
15
15
|
* safeAccessor("input", "x-linkedin") // 'input["x-linkedin"]'
|
|
16
16
|
* safeAccessor("input", "constructor")
|
|
17
|
-
* // '(input != null && Object.hasOwn(input, "constructor") ? input["constructor"] : undefined)'
|
|
17
|
+
* // '((input != null && Object.hasOwn(input, "constructor") ? input["constructor"] : undefined) as any)'
|
|
18
18
|
*/
|
|
19
19
|
export declare const safeAccessor: (variable: string, key: string) => string;
|
|
20
20
|
/**
|
package/dist/safe-accessor.js
CHANGED
|
@@ -4,7 +4,7 @@ const safeAccessor = (variable, key) => {
|
|
|
4
4
|
if (PROTOTYPE_MEMBERS.has(key)) {
|
|
5
5
|
const base = variable.endsWith("?") ? variable.slice(0, -1) : variable;
|
|
6
6
|
const literal2 = JSON.stringify(key);
|
|
7
|
-
return `(${base} != null && Object.hasOwn(${base}, ${literal2}) ? ${base}[${literal2}] : undefined)`;
|
|
7
|
+
return `((${base} != null && Object.hasOwn(${base}, ${literal2}) ? ${base}[${literal2}] : undefined) as any)`;
|
|
8
8
|
}
|
|
9
9
|
if (JS_IDENTIFIER.test(key)) {
|
|
10
10
|
return `${variable}.${key}`;
|
package/dist/schema-guards.js
CHANGED
|
@@ -2,67 +2,67 @@ const isSchemaObject = (schema) => {
|
|
|
2
2
|
return typeof schema === "object" && schema !== null && typeof schema !== "boolean";
|
|
3
3
|
};
|
|
4
4
|
const hasType = (schema) => {
|
|
5
|
-
return isSchemaObject(schema) && "type"
|
|
5
|
+
return isSchemaObject(schema) && Object.hasOwn(schema, "type") && typeof schema.type === "string";
|
|
6
6
|
};
|
|
7
7
|
const isObjectSchema = (schema) => {
|
|
8
|
-
return isSchemaObject(schema) && ("type"
|
|
8
|
+
return isSchemaObject(schema) && (Object.hasOwn(schema, "type") && schema.type === "object" || Object.hasOwn(schema, "properties"));
|
|
9
9
|
};
|
|
10
10
|
const hasProperties = (schema) => {
|
|
11
|
-
return isSchemaObject(schema) && "properties"
|
|
11
|
+
return isSchemaObject(schema) && Object.hasOwn(schema, "properties") && typeof schema.properties === "object" && schema.properties !== null;
|
|
12
12
|
};
|
|
13
13
|
const hasEnum = (schema) => {
|
|
14
|
-
return isSchemaObject(schema) && "enum"
|
|
14
|
+
return isSchemaObject(schema) && Object.hasOwn(schema, "enum") && Array.isArray(schema.enum);
|
|
15
15
|
};
|
|
16
16
|
const hasConst = (schema) => {
|
|
17
|
-
return isSchemaObject(schema) && "const"
|
|
17
|
+
return isSchemaObject(schema) && Object.hasOwn(schema, "const");
|
|
18
18
|
};
|
|
19
19
|
const hasPattern = (schema) => {
|
|
20
|
-
return isSchemaObject(schema) && "pattern"
|
|
20
|
+
return isSchemaObject(schema) && Object.hasOwn(schema, "pattern") && typeof schema.pattern === "string";
|
|
21
21
|
};
|
|
22
22
|
const hasFormat = (schema) => {
|
|
23
|
-
return isSchemaObject(schema) && "format"
|
|
23
|
+
return isSchemaObject(schema) && Object.hasOwn(schema, "format") && typeof schema.format === "string";
|
|
24
24
|
};
|
|
25
25
|
const hasDefault = (schema) => {
|
|
26
|
-
return isSchemaObject(schema) && "default"
|
|
26
|
+
return isSchemaObject(schema) && Object.hasOwn(schema, "default");
|
|
27
27
|
};
|
|
28
28
|
const hasExamples = (schema) => {
|
|
29
|
-
return isSchemaObject(schema) && "examples"
|
|
29
|
+
return isSchemaObject(schema) && Object.hasOwn(schema, "examples") && Array.isArray(schema.examples);
|
|
30
30
|
};
|
|
31
31
|
const hasOneOf = (schema) => {
|
|
32
|
-
return isSchemaObject(schema) && "oneOf"
|
|
32
|
+
return isSchemaObject(schema) && Object.hasOwn(schema, "oneOf") && Array.isArray(schema.oneOf);
|
|
33
33
|
};
|
|
34
34
|
const hasAnyOf = (schema) => {
|
|
35
|
-
return isSchemaObject(schema) && "anyOf"
|
|
35
|
+
return isSchemaObject(schema) && Object.hasOwn(schema, "anyOf") && Array.isArray(schema.anyOf);
|
|
36
36
|
};
|
|
37
37
|
const hasAllOf = (schema) => {
|
|
38
|
-
return isSchemaObject(schema) && "allOf"
|
|
38
|
+
return isSchemaObject(schema) && Object.hasOwn(schema, "allOf") && Array.isArray(schema.allOf);
|
|
39
39
|
};
|
|
40
40
|
const hasRequired = (schema) => {
|
|
41
|
-
return isSchemaObject(schema) && "required"
|
|
41
|
+
return isSchemaObject(schema) && Object.hasOwn(schema, "required") && Array.isArray(schema.required);
|
|
42
42
|
};
|
|
43
43
|
const hasItems = (schema) => {
|
|
44
|
-
return isSchemaObject(schema) && "items"
|
|
44
|
+
return isSchemaObject(schema) && Object.hasOwn(schema, "items") && typeof schema.items === "object" && schema.items !== null && typeof schema.items !== "boolean";
|
|
45
45
|
};
|
|
46
46
|
const hasAdditionalProperties = (schema) => {
|
|
47
|
-
return isSchemaObject(schema) && "additionalProperties"
|
|
47
|
+
return isSchemaObject(schema) && Object.hasOwn(schema, "additionalProperties");
|
|
48
48
|
};
|
|
49
49
|
const hasMinLength = (schema) => {
|
|
50
|
-
return isSchemaObject(schema) && "minLength"
|
|
50
|
+
return isSchemaObject(schema) && Object.hasOwn(schema, "minLength") && typeof schema.minLength === "number";
|
|
51
51
|
};
|
|
52
52
|
const hasMaxLength = (schema) => {
|
|
53
|
-
return isSchemaObject(schema) && "maxLength"
|
|
53
|
+
return isSchemaObject(schema) && Object.hasOwn(schema, "maxLength") && typeof schema.maxLength === "number";
|
|
54
54
|
};
|
|
55
55
|
const hasMinimum = (schema) => {
|
|
56
|
-
return isSchemaObject(schema) && "minimum"
|
|
56
|
+
return isSchemaObject(schema) && Object.hasOwn(schema, "minimum") && typeof schema.minimum === "number";
|
|
57
57
|
};
|
|
58
58
|
const hasMaximum = (schema) => {
|
|
59
|
-
return isSchemaObject(schema) && "maximum"
|
|
59
|
+
return isSchemaObject(schema) && Object.hasOwn(schema, "maximum") && typeof schema.maximum === "number";
|
|
60
60
|
};
|
|
61
61
|
const hasExclusiveMinimum = (schema) => {
|
|
62
|
-
return isSchemaObject(schema) && "exclusiveMinimum"
|
|
62
|
+
return isSchemaObject(schema) && Object.hasOwn(schema, "exclusiveMinimum") && typeof schema.exclusiveMinimum === "number";
|
|
63
63
|
};
|
|
64
64
|
const hasExclusiveMaximum = (schema) => {
|
|
65
|
-
return isSchemaObject(schema) && "exclusiveMaximum"
|
|
65
|
+
return isSchemaObject(schema) && Object.hasOwn(schema, "exclusiveMaximum") && typeof schema.exclusiveMaximum === "number";
|
|
66
66
|
};
|
|
67
67
|
const hasStrictExclusiveMinimum = (schema) => {
|
|
68
68
|
if (!isSchemaObject(schema))
|
|
@@ -77,43 +77,43 @@ const hasStrictExclusiveMaximum = (schema) => {
|
|
|
77
77
|
return flag === true;
|
|
78
78
|
};
|
|
79
79
|
const hasMultipleOf = (schema) => {
|
|
80
|
-
return isSchemaObject(schema) && "multipleOf"
|
|
80
|
+
return isSchemaObject(schema) && Object.hasOwn(schema, "multipleOf") && typeof schema.multipleOf === "number";
|
|
81
81
|
};
|
|
82
82
|
const hasMinItems = (schema) => {
|
|
83
|
-
return isSchemaObject(schema) && "minItems"
|
|
83
|
+
return isSchemaObject(schema) && Object.hasOwn(schema, "minItems") && typeof schema.minItems === "number";
|
|
84
84
|
};
|
|
85
85
|
const hasMaxItems = (schema) => {
|
|
86
|
-
return isSchemaObject(schema) && "maxItems"
|
|
86
|
+
return isSchemaObject(schema) && Object.hasOwn(schema, "maxItems") && typeof schema.maxItems === "number";
|
|
87
87
|
};
|
|
88
88
|
const hasUniqueItems = (schema) => {
|
|
89
|
-
return isSchemaObject(schema) && "uniqueItems"
|
|
89
|
+
return isSchemaObject(schema) && Object.hasOwn(schema, "uniqueItems") && typeof schema.uniqueItems === "boolean";
|
|
90
90
|
};
|
|
91
91
|
const hasMinProperties = (schema) => {
|
|
92
|
-
return isSchemaObject(schema) && "minProperties"
|
|
92
|
+
return isSchemaObject(schema) && Object.hasOwn(schema, "minProperties") && typeof schema.minProperties === "number";
|
|
93
93
|
};
|
|
94
94
|
const hasMaxProperties = (schema) => {
|
|
95
|
-
return isSchemaObject(schema) && "maxProperties"
|
|
95
|
+
return isSchemaObject(schema) && Object.hasOwn(schema, "maxProperties") && typeof schema.maxProperties === "number";
|
|
96
96
|
};
|
|
97
97
|
const hasDependentRequired = (schema) => {
|
|
98
|
-
return isSchemaObject(schema) && "dependentRequired"
|
|
98
|
+
return isSchemaObject(schema) && Object.hasOwn(schema, "dependentRequired") && typeof schema.dependentRequired === "object" && schema.dependentRequired !== null;
|
|
99
99
|
};
|
|
100
100
|
const hasPropertyNames = (schema) => {
|
|
101
|
-
return isSchemaObject(schema) && "propertyNames"
|
|
101
|
+
return isSchemaObject(schema) && Object.hasOwn(schema, "propertyNames");
|
|
102
102
|
};
|
|
103
103
|
const hasDependentSchemas = (schema) => {
|
|
104
|
-
return isSchemaObject(schema) && "dependentSchemas"
|
|
104
|
+
return isSchemaObject(schema) && Object.hasOwn(schema, "dependentSchemas") && typeof schema.dependentSchemas === "object" && schema.dependentSchemas !== null;
|
|
105
105
|
};
|
|
106
106
|
const hasPatternProperties = (schema) => {
|
|
107
|
-
return isSchemaObject(schema) && "patternProperties"
|
|
107
|
+
return isSchemaObject(schema) && Object.hasOwn(schema, "patternProperties") && typeof schema.patternProperties === "object" && schema.patternProperties !== null;
|
|
108
108
|
};
|
|
109
109
|
const hasContains = (schema) => {
|
|
110
|
-
return isSchemaObject(schema) && "contains"
|
|
110
|
+
return isSchemaObject(schema) && Object.hasOwn(schema, "contains");
|
|
111
111
|
};
|
|
112
112
|
const hasNot = (schema) => {
|
|
113
|
-
return isSchemaObject(schema) && "not"
|
|
113
|
+
return isSchemaObject(schema) && Object.hasOwn(schema, "not");
|
|
114
114
|
};
|
|
115
115
|
const hasIf = (schema) => {
|
|
116
|
-
return isSchemaObject(schema) && "if"
|
|
116
|
+
return isSchemaObject(schema) && Object.hasOwn(schema, "if");
|
|
117
117
|
};
|
|
118
118
|
import { hasRef } from "./has-ref.js";
|
|
119
119
|
export {
|
|
@@ -3,7 +3,7 @@ const minLengthPassExpr = (valueExpr, min) => {
|
|
|
3
3
|
if (min <= 0)
|
|
4
4
|
return "true";
|
|
5
5
|
if (min === 1)
|
|
6
|
-
return
|
|
6
|
+
return `(${valueExpr}.length >= 1)`;
|
|
7
7
|
return `(${valueExpr}.length >= ${min} && (${valueExpr}.length >= ${min * 2} || ${codePoints(valueExpr)} >= ${min}))`;
|
|
8
8
|
};
|
|
9
9
|
const maxLengthPassExpr = (valueExpr, max) => `(${valueExpr}.length <= ${max} || ${codePoints(valueExpr)} <= ${max})`;
|
|
@@ -11,7 +11,7 @@ const minLengthFailExpr = (valueExpr, min) => {
|
|
|
11
11
|
if (min <= 0)
|
|
12
12
|
return "false";
|
|
13
13
|
if (min === 1)
|
|
14
|
-
return
|
|
14
|
+
return `(${valueExpr}.length < 1)`;
|
|
15
15
|
return `(${valueExpr}.length < ${min} || (${valueExpr}.length < ${min * 2} && ${codePoints(valueExpr)} < ${min}))`;
|
|
16
16
|
};
|
|
17
17
|
const maxLengthFailExpr = (valueExpr, max) => `(${valueExpr}.length > ${max} && ${codePoints(valueExpr)} > ${max})`;
|
|
@@ -18,12 +18,15 @@ export type UnknownKeyCheck = {
|
|
|
18
18
|
* Builds the boolean expression that is true when `keyVar` is NOT one of the
|
|
19
19
|
* known keys — an inline chain of `!==` comparisons, a `Set.has` miss, or the
|
|
20
20
|
* constant `true` when there are no known keys (every key is undeclared).
|
|
21
|
+
*
|
|
22
|
+
* Self-parenthesized, so it drops into a `&&` / `||` chain or under a `!`
|
|
23
|
+
* as one term.
|
|
21
24
|
*/
|
|
22
25
|
readonly isUnknown: (keyVar: string) => string;
|
|
23
26
|
/**
|
|
24
27
|
* The complement of {@link isUnknown}: true when `keyVar` IS a known key — an
|
|
25
28
|
* inline chain of `===` comparisons, a `Set.has` hit, or the constant `false`
|
|
26
|
-
* when there are no known keys.
|
|
29
|
+
* when there are no known keys. Self-parenthesized, like {@link isUnknown}.
|
|
27
30
|
*/
|
|
28
31
|
readonly isKnown: (keyVar: string) => string;
|
|
29
32
|
};
|
|
@@ -37,10 +40,18 @@ export type UnknownKeyCheck = {
|
|
|
37
40
|
* TypeBox compile to. Above `inlineLimit` the chain grows long enough that the
|
|
38
41
|
* `Set`'s O(1) lookup wins, so a `Set` named `setName` is hoisted instead.
|
|
39
42
|
*
|
|
43
|
+
* Both expressions are parenthesized whichever form they take. Only the inline
|
|
44
|
+
* chains need it, but they are the ones that need it *invisibly*: `isKnown`
|
|
45
|
+
* returns `a || b` inline and an atomic `set.has(k)` above the limit, so a
|
|
46
|
+
* caller writing `x && check.isKnown(k)` got `(x && a) || b` for a 16-key object
|
|
47
|
+
* and correct code for a 17-key one — a precedence bug that appears and
|
|
48
|
+
* disappears with a performance threshold. Parenthesizing both makes the two
|
|
49
|
+
* forms interchangeable, which is what a caller already assumes they are.
|
|
50
|
+
*
|
|
40
51
|
* @example
|
|
41
52
|
* const check = unknownKeyCheck(['id', 'name'], '_knownKeys0')
|
|
42
53
|
* check.declarations // []
|
|
43
|
-
* check.isUnknown('_k') // '_k !== "id" && _k !== "name"'
|
|
44
|
-
* check.isKnown('_k') // '_k === "id" || _k === "name"'
|
|
54
|
+
* check.isUnknown('_k') // '(_k !== "id" && _k !== "name")'
|
|
55
|
+
* check.isKnown('_k') // '(_k === "id" || _k === "name")'
|
|
45
56
|
*/
|
|
46
57
|
export declare const unknownKeyCheck: (knownKeys: readonly string[], setName: string, inlineLimit?: number) => UnknownKeyCheck;
|
|
@@ -6,8 +6,8 @@ const unknownKeyCheck = (knownKeys, setName, inlineLimit = INLINE_KEY_LIMIT) =>
|
|
|
6
6
|
if (knownKeys.length <= inlineLimit) {
|
|
7
7
|
return {
|
|
8
8
|
declarations: [],
|
|
9
|
-
isUnknown: (keyVar) => knownKeys.map((key) => `${keyVar} !== ${JSON.stringify(key)}`).join(" && ")
|
|
10
|
-
isKnown: (keyVar) => knownKeys.map((key) => `${keyVar} === ${JSON.stringify(key)}`).join(" || ")
|
|
9
|
+
isUnknown: (keyVar) => `(${knownKeys.map((key) => `${keyVar} !== ${JSON.stringify(key)}`).join(" && ")})`,
|
|
10
|
+
isKnown: (keyVar) => `(${knownKeys.map((key) => `${keyVar} === ${JSON.stringify(key)}`).join(" || ")})`
|
|
11
11
|
};
|
|
12
12
|
}
|
|
13
13
|
return {
|