@amritk/helpers 0.13.2 → 0.13.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.
@@ -1,69 +1,55 @@
1
- /**
2
- * Replaces $dynamicRef with $ref in a schema using the provided anchor-to-path map.
3
- *
4
- * This creates a deep clone of the schema to avoid mutating the original, then walks
5
- * the clone and converts any { $dynamicRef: "#meta" } to { $ref: "#/$defs/schema" }
6
- * (or whatever the dynamicRefMap dictates).
7
- *
8
- * Walks both object properties and array elements, so a `$dynamicRef` nested
9
- * inside a keyword whose value is an array of subschemas (`allOf`, `anyOf`,
10
- * `oneOf`, `prefixItems`, …) is rewritten too. The build system generates a
11
- * separate file per `$def`, so each schema walked here is relatively shallow.
12
- */
13
- export const resolveDynamicRefs = (schema, dynamicRefMap) => {
14
- if (typeof schema !== 'object' || schema === null) {
15
- return schema;
1
+ const resolveDynamicRefs = (schema, dynamicRefMap) => {
2
+ if (typeof schema !== "object" || schema === null) {
3
+ return schema;
4
+ }
5
+ if (Object.keys(dynamicRefMap).length === 0) {
6
+ return schema;
7
+ }
8
+ if (!containsDynamicRef(schema)) {
9
+ return schema;
10
+ }
11
+ const clone = JSON.parse(JSON.stringify(schema));
12
+ const walk = (obj) => {
13
+ if (typeof obj !== "object" || obj === null) {
14
+ return;
16
15
  }
17
- // Skip if there are no dynamic refs to resolve
18
- if (Object.keys(dynamicRefMap).length === 0) {
19
- return schema;
16
+ if (Array.isArray(obj)) {
17
+ for (const item of obj)
18
+ walk(item);
19
+ return;
20
20
  }
21
- // Cheap read-only pre-scan: most subschemas carry no `$dynamicRef`, so returning
22
- // the original untouched avoids a full deep clone (and its allocation) on every
23
- // node of every generator's walk when the document as a whole *does* use them.
24
- if (!containsDynamicRef(schema)) {
25
- return schema;
21
+ const record = obj;
22
+ if ("$dynamicRef" in record && typeof record["$dynamicRef"] === "string") {
23
+ const resolved = dynamicRefMap[record["$dynamicRef"]];
24
+ if (resolved) {
25
+ record["$ref"] = resolved;
26
+ delete record["$dynamicRef"];
27
+ }
26
28
  }
27
- const clone = JSON.parse(JSON.stringify(schema));
28
- const walk = (obj) => {
29
- if (typeof obj !== 'object' || obj === null) {
30
- return;
31
- }
32
- if (Array.isArray(obj)) {
33
- for (const item of obj)
34
- walk(item);
35
- return;
36
- }
37
- const record = obj;
38
- if ('$dynamicRef' in record && typeof record['$dynamicRef'] === 'string') {
39
- const resolved = dynamicRefMap[record['$dynamicRef']];
40
- if (resolved) {
41
- record['$ref'] = resolved;
42
- delete record['$dynamicRef'];
43
- }
44
- }
45
- for (const key in record) {
46
- walk(record[key]);
47
- }
48
- };
49
- walk(clone);
50
- return clone;
29
+ for (const key in record) {
30
+ walk(record[key]);
31
+ }
32
+ };
33
+ walk(clone);
34
+ return clone;
51
35
  };
52
- /** True if `value` contains a `$dynamicRef` string anywhere in its subtree. */
53
36
  const containsDynamicRef = (value) => {
54
- if (typeof value !== 'object' || value === null)
55
- return false;
56
- if (Array.isArray(value)) {
57
- for (const item of value)
58
- if (containsDynamicRef(item))
59
- return true;
60
- return false;
61
- }
62
- const record = value;
63
- if (typeof record['$dynamicRef'] === 'string')
37
+ if (typeof value !== "object" || value === null)
38
+ return false;
39
+ if (Array.isArray(value)) {
40
+ for (const item of value)
41
+ if (containsDynamicRef(item))
64
42
  return true;
65
- for (const key in record)
66
- if (containsDynamicRef(record[key]))
67
- return true;
68
43
  return false;
44
+ }
45
+ const record = value;
46
+ if (typeof record["$dynamicRef"] === "string")
47
+ return true;
48
+ for (const key in record)
49
+ if (containsDynamicRef(record[key]))
50
+ return true;
51
+ return false;
52
+ };
53
+ export {
54
+ resolveDynamicRefs
69
55
  };
@@ -1,77 +1,41 @@
1
- /**
2
- * Navigates a JSON Pointer fragment (e.g. `/$defs/foo` or `/definitions/bar`)
3
- * through a schema object, returning the target or undefined if not found.
4
- */
5
1
  const navigatePointer = (pointer, schema) => {
6
- const parts = pointer.split('/').filter(Boolean);
7
- let current = schema;
8
- for (const part of parts) {
9
- const decodedPart = part.replace(/~1/g, '/').replace(/~0/g, '~');
10
- if (current && typeof current === 'object' && decodedPart in current) {
11
- const next = current[decodedPart];
12
- if (typeof next === 'object' && next !== null) {
13
- current = next;
14
- }
15
- else {
16
- return undefined;
17
- }
18
- }
19
- else {
20
- return undefined;
21
- }
2
+ const parts = pointer.split("/").filter(Boolean);
3
+ let current = schema;
4
+ for (const part of parts) {
5
+ const decodedPart = part.replace(/~1/g, "/").replace(/~0/g, "~");
6
+ if (current && typeof current === "object" && decodedPart in current) {
7
+ const next = current[decodedPart];
8
+ if (typeof next === "object" && next !== null) {
9
+ current = next;
10
+ } else {
11
+ return void 0;
12
+ }
13
+ } else {
14
+ return void 0;
22
15
  }
23
- return current;
16
+ }
17
+ return current;
24
18
  };
25
- /**
26
- * Resolves a JSON Schema $ref pointer to the actual schema definition.
27
- *
28
- * Supports three ref forms:
29
- * - Internal: `#/$defs/contact` — navigates the root schema by JSON Pointer
30
- * - URI key: `http://example.com/foo.json` — looks up the key directly in `$defs`
31
- * - URI with fragment: `http://example.com/foo.json#/definitions/bar` — looks up
32
- * the base URI in `$defs`, then navigates the fragment within that definition
33
- *
34
- * @param ref - The $ref string
35
- * @param rootSchema - The root schema containing the definitions
36
- * @returns The resolved schema or undefined if not found
37
- *
38
- * @example
39
- * ```ts
40
- * const rootSchema = {
41
- * $defs: {
42
- * contact: { type: 'object', properties: { email: { type: 'string' } } },
43
- * 'http://example.com/server.json': { type: 'object' },
44
- * }
45
- * }
46
- * resolveRef('#/$defs/contact', rootSchema)
47
- * resolveRef('http://example.com/server.json', rootSchema)
48
- * ```
49
- */
50
- export const resolveRef = (ref, rootSchema) => {
51
- // Internal reference: navigate from root by JSON Pointer
52
- if (ref.startsWith('#')) {
53
- return navigatePointer(ref.slice(1), rootSchema);
54
- }
55
- // URI ref: may have a fragment (e.g. "http://foo.com/bar.json#/definitions/baz")
56
- // A trailing bare "#" (e.g. "http://foo.com/schema#") means "the whole document" — treat as no fragment
57
- const hashIndex = ref.indexOf('#');
58
- const baseUri = hashIndex === -1 ? ref : ref.slice(0, hashIndex);
59
- const rawFragment = hashIndex === -1 ? '' : ref.slice(hashIndex + 1);
60
- const fragment = rawFragment === '' || rawFragment === '/' ? '' : rawFragment;
61
- // Look up the base URI as a key in $defs
62
- const defs = rootSchema['$defs'];
63
- if (typeof defs !== 'object' || defs === null)
64
- return undefined;
65
- const defsRecord = defs;
66
- const base = defsRecord[baseUri];
67
- if (typeof base !== 'object' || base === null)
68
- return undefined;
69
- // No fragment — return the definition directly
70
- if (!fragment)
71
- return base;
72
- // Normalize the fragment: draft-07 schemas use `/definitions/` but after
73
- // upgradeDraft07Schema the key is renamed to `/$defs/`
74
- const normalizedFragment = fragment.replace(/^\/definitions\//, '/$defs/');
75
- // Navigate the fragment within the resolved definition
76
- return navigatePointer(normalizedFragment, base);
19
+ const resolveRef = (ref, rootSchema) => {
20
+ if (ref.startsWith("#")) {
21
+ return navigatePointer(ref.slice(1), rootSchema);
22
+ }
23
+ const hashIndex = ref.indexOf("#");
24
+ const baseUri = hashIndex === -1 ? ref : ref.slice(0, hashIndex);
25
+ const rawFragment = hashIndex === -1 ? "" : ref.slice(hashIndex + 1);
26
+ const fragment = rawFragment === "" || rawFragment === "/" ? "" : rawFragment;
27
+ const defs = rootSchema["$defs"];
28
+ if (typeof defs !== "object" || defs === null)
29
+ return void 0;
30
+ const defsRecord = defs;
31
+ const base = defsRecord[baseUri];
32
+ if (typeof base !== "object" || base === null)
33
+ return void 0;
34
+ if (!fragment)
35
+ return base;
36
+ const normalizedFragment = fragment.replace(/^\/definitions\//, "/$defs/");
37
+ return navigatePointer(normalizedFragment, base);
38
+ };
39
+ export {
40
+ resolveRef
77
41
  };
@@ -1,48 +1,21 @@
1
- /**
2
- * Checks whether a property name is a valid JavaScript identifier that can be
3
- * accessed with dot notation. Property names containing hyphens, dots, or
4
- * other special characters (e.g., "x-linkedin") must use bracket notation.
5
- */
6
1
  const JS_IDENTIFIER = /^[a-zA-Z_$][a-zA-Z0-9_$]*$/;
7
- /**
8
- * Generates a safe property accessor for a given key on an object variable.
9
- * Uses dot notation for simple identifiers and bracket notation for keys
10
- * that contain special characters like hyphens.
11
- *
12
- * @param variable - The variable name (e.g., "input", "input?")
13
- * @param key - The property name to access
14
- * @returns A valid JS property access expression
15
- *
16
- * @example
17
- * safeAccessor("input", "name") // "input.name"
18
- * safeAccessor("input?", "x-linkedin") // 'input?.["x-linkedin"]'
19
- * safeAccessor("input", "x-linkedin") // 'input["x-linkedin"]'
20
- */
21
- export const safeAccessor = (variable, key) => {
22
- if (JS_IDENTIFIER.test(key)) {
23
- return `${variable}.${key}`;
24
- }
25
- // Bracket keys are schema-controlled, so escape via JSON.stringify — a key like
26
- // `it's` or `a']; evil(); //` would otherwise break or hijack the generated code.
27
- const literal = JSON.stringify(key);
28
- // Handle optional chaining: "input?" -> "input?.['key']"
29
- if (variable.endsWith('?')) {
30
- return `${variable}.[${literal}]`;
31
- }
32
- return `${variable}[${literal}]`;
2
+ const safeAccessor = (variable, key) => {
3
+ if (JS_IDENTIFIER.test(key)) {
4
+ return `${variable}.${key}`;
5
+ }
6
+ const literal = JSON.stringify(key);
7
+ if (variable.endsWith("?")) {
8
+ return `${variable}.[${literal}]`;
9
+ }
10
+ return `${variable}[${literal}]`;
33
11
  };
34
- /**
35
- * Generates a safe property key for use in object literals.
36
- * Wraps keys that are not valid identifiers in quotes.
37
- *
38
- * @example
39
- * safeKey("name") // "name"
40
- * safeKey("x-linkedin") // '"x-linkedin"'
41
- */
42
- export const safeKey = (key) => {
43
- if (JS_IDENTIFIER.test(key)) {
44
- return key;
45
- }
46
- // Schema-controlled keys must be escaped; a bare-quoted `it's` produces broken TS.
47
- return JSON.stringify(key);
12
+ const safeKey = (key) => {
13
+ if (JS_IDENTIFIER.test(key)) {
14
+ return key;
15
+ }
16
+ return JSON.stringify(key);
17
+ };
18
+ export {
19
+ safeAccessor,
20
+ safeKey
48
21
  };