@amritk/helpers 0.23.1 → 0.23.2
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.
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { assignKey } from "./assign-key.js";
|
|
2
2
|
import { assertSchemaDepth, MAX_SCHEMA_DEPTH } from "./max-schema-depth.js";
|
|
3
|
-
import { getMjstBrand, getMjstInstanceOf, getMjstPrimitive } from "./mjst-extension.js";
|
|
3
|
+
import { getMjstBrand, getMjstInstanceOf, getMjstPrimitive, hasMjstHint, MJST_EXTENSION_KEY } from "./mjst-extension.js";
|
|
4
4
|
import { readKey } from "./read-key.js";
|
|
5
5
|
import { refToName } from "./ref-to-name.js";
|
|
6
6
|
import { referencedConditional } from "./referenced-conditional.js";
|
|
@@ -52,7 +52,7 @@ const isPlainFragment = (schema) => {
|
|
|
52
52
|
const type = keywordOf(schema, "type");
|
|
53
53
|
if (type !== void 0 && type !== "object")
|
|
54
54
|
return false;
|
|
55
|
-
return !Object.keys(schema).some((key) => NON_PLAIN_KEYWORDS.has(key));
|
|
55
|
+
return !Object.keys(schema).some((key) => NON_PLAIN_KEYWORDS.has(key) && (key !== MJST_EXTENSION_KEY || hasMjstHint(schema)));
|
|
56
56
|
};
|
|
57
57
|
const requiredOf = (schema) => {
|
|
58
58
|
const declared = keywordOf(schema, "required");
|
package/dist/mjst-extension.d.ts
CHANGED
|
@@ -7,7 +7,12 @@ import type { JSONSchema } from 'json-schema-typed/draft-2020-12';
|
|
|
7
7
|
*/
|
|
8
8
|
export declare const MJST_EXTENSION_KEY = "x-mjst";
|
|
9
9
|
/**
|
|
10
|
-
* The shape of the `x-mjst` extension object.
|
|
10
|
+
* The shape of the `x-mjst` extension object, as the type generators read it.
|
|
11
|
+
*
|
|
12
|
+
* The object is contextual — what a key means depends on the node it sits on —
|
|
13
|
+
* and not every key is here: `@amritk/generate-markdown` reads its own from the
|
|
14
|
+
* same object (`hidden`, and its settings under `markdown`), and these readers
|
|
15
|
+
* leave them alone.
|
|
11
16
|
*
|
|
12
17
|
* - `instanceOf` names a JavaScript class the value must be an instance of at
|
|
13
18
|
* runtime (e.g. `'Date'`). It round-trips constructs like TypeBox's
|
|
@@ -32,6 +37,19 @@ export type MjstExtension = {
|
|
|
32
37
|
readonly brand?: string;
|
|
33
38
|
readonly discriminator?: string;
|
|
34
39
|
};
|
|
40
|
+
/**
|
|
41
|
+
* Whether a node's `x-mjst` carries a hint the generators read, as opposed to
|
|
42
|
+
* nothing but another tool's keys — the docs generator's `hidden` and
|
|
43
|
+
* `markdown`, which change no type and no verdict.
|
|
44
|
+
*
|
|
45
|
+
* The emitters that ask whether a node is "just" one keyword used to count the
|
|
46
|
+
* presence of `x-mjst` itself, which was right while it only ever held hints.
|
|
47
|
+
* Asked that way, a property whose only extra is where its docs go lost its
|
|
48
|
+
* tight validator and its conditional type. Presence of a hint key is the
|
|
49
|
+
* question, not whether its value is supported: an unsupported `instanceOf`
|
|
50
|
+
* still meant a node too rich for the specialised paths, and still does.
|
|
51
|
+
*/
|
|
52
|
+
export declare const hasMjstHint: (node: unknown) => boolean;
|
|
35
53
|
/**
|
|
36
54
|
* Reads the `instanceOf` class name from a schema's `x-mjst` extension, when it
|
|
37
55
|
* names a class the generators actually support (see
|
package/dist/mjst-extension.js
CHANGED
|
@@ -16,6 +16,15 @@ const readExtensionString = (node, field) => {
|
|
|
16
16
|
const value = readKey(extension, field);
|
|
17
17
|
return typeof value === "string" ? value : void 0;
|
|
18
18
|
};
|
|
19
|
+
const HINT_KEYS = ["instanceOf", "primitive", "brand", "discriminator"];
|
|
20
|
+
const hasMjstHint = (node) => {
|
|
21
|
+
if (!isSchemaObject(node))
|
|
22
|
+
return false;
|
|
23
|
+
const extension = readKey(node, MJST_EXTENSION_KEY);
|
|
24
|
+
if (typeof extension !== "object" || extension === null)
|
|
25
|
+
return false;
|
|
26
|
+
return HINT_KEYS.some((key) => readKey(extension, key) !== void 0);
|
|
27
|
+
};
|
|
19
28
|
const getMjstInstanceOf = (schema) => {
|
|
20
29
|
const instanceOf = readExtensionString(schema, "instanceOf");
|
|
21
30
|
if (instanceOf === void 0)
|
|
@@ -53,5 +62,6 @@ export {
|
|
|
53
62
|
getMjstBrand,
|
|
54
63
|
getMjstDiscriminator,
|
|
55
64
|
getMjstInstanceOf,
|
|
56
|
-
getMjstPrimitive
|
|
65
|
+
getMjstPrimitive,
|
|
66
|
+
hasMjstHint
|
|
57
67
|
};
|