@amritk/helpers 0.7.1 → 0.8.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/escape-regex-pattern.d.ts +17 -0
- package/dist/escape-regex-pattern.d.ts.map +1 -0
- package/dist/escape-regex-pattern.js +20 -0
- package/dist/resolve-dynamic-refs.d.ts +4 -3
- package/dist/resolve-dynamic-refs.d.ts.map +1 -1
- package/dist/resolve-dynamic-refs.js +10 -4
- package/dist/schema-guards.d.ts +8 -0
- package/dist/schema-guards.d.ts.map +1 -1
- package/dist/schema-guards.js +11 -0
- package/package.json +1 -1
- package/src/escape-regex-pattern.test.ts +35 -0
- package/src/escape-regex-pattern.ts +20 -0
- package/src/resolve-dynamic-refs.test.ts +12 -0
- package/src/resolve-dynamic-refs.ts +10 -4
- package/src/schema-guards.test.ts +21 -0
- package/src/schema-guards.ts +17 -0
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Escapes a JSON Schema `pattern` so it can be embedded between the slashes of
|
|
3
|
+
* a generated regex literal (`/…/`).
|
|
4
|
+
*
|
|
5
|
+
* A `pattern` is an ECMA-262 regex *body*, and the generated text goes into a
|
|
6
|
+
* regex literal — not a string literal — so backslashes are regex syntax and
|
|
7
|
+
* must be left exactly as-is (doubling `\d` to `\\d` would change it from "a
|
|
8
|
+
* digit" to "a literal backslash followed by d"). The only character that would
|
|
9
|
+
* corrupt the surrounding literal is an *unescaped* `/`, which would close it
|
|
10
|
+
* early. So we escape bare slashes to `\/` while leaving every existing escape
|
|
11
|
+
* sequence (including an already-escaped `\/`) untouched.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* escapeRegexPattern('\\d{4}/\\d{2}') // → '\\d{4}\\/\\d{2}' (i.e. \d{4}\/\d{2})
|
|
15
|
+
*/
|
|
16
|
+
export declare const escapeRegexPattern: (pattern: string) => string;
|
|
17
|
+
//# sourceMappingURL=escape-regex-pattern.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"escape-regex-pattern.d.ts","sourceRoot":"","sources":["../src/escape-regex-pattern.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AACH,eAAO,MAAM,kBAAkB,YAAa,MAAM,KAAG,MAIwB,CAAA"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Escapes a JSON Schema `pattern` so it can be embedded between the slashes of
|
|
3
|
+
* a generated regex literal (`/…/`).
|
|
4
|
+
*
|
|
5
|
+
* A `pattern` is an ECMA-262 regex *body*, and the generated text goes into a
|
|
6
|
+
* regex literal — not a string literal — so backslashes are regex syntax and
|
|
7
|
+
* must be left exactly as-is (doubling `\d` to `\\d` would change it from "a
|
|
8
|
+
* digit" to "a literal backslash followed by d"). The only character that would
|
|
9
|
+
* corrupt the surrounding literal is an *unescaped* `/`, which would close it
|
|
10
|
+
* early. So we escape bare slashes to `\/` while leaving every existing escape
|
|
11
|
+
* sequence (including an already-escaped `\/`) untouched.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* escapeRegexPattern('\\d{4}/\\d{2}') // → '\\d{4}\\/\\d{2}' (i.e. \d{4}\/\d{2})
|
|
15
|
+
*/
|
|
16
|
+
export const escapeRegexPattern = (pattern) =>
|
|
17
|
+
// Match either an escape sequence (`\` + any char, kept verbatim) or a bare
|
|
18
|
+
// `/` (escaped). Consuming escape pairs first means the slash in `\/` is never
|
|
19
|
+
// seen as bare, so it is not double-escaped.
|
|
20
|
+
pattern.replace(/\\[\s\S]|\//g, (match) => (match === '/' ? '\\/' : match));
|
|
@@ -6,9 +6,10 @@ import type { JSONSchema } from 'json-schema-typed/draft-2020-12';
|
|
|
6
6
|
* the clone and converts any { $dynamicRef: "#meta" } to { $ref: "#/$defs/schema" }
|
|
7
7
|
* (or whatever the dynamicRefMap dictates).
|
|
8
8
|
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
9
|
+
* Walks both object properties and array elements, so a `$dynamicRef` nested
|
|
10
|
+
* inside a keyword whose value is an array of subschemas (`allOf`, `anyOf`,
|
|
11
|
+
* `oneOf`, `prefixItems`, …) is rewritten too. The build system generates a
|
|
12
|
+
* separate file per `$def`, so each schema walked here is relatively shallow.
|
|
12
13
|
*/
|
|
13
14
|
export declare const resolveDynamicRefs: (schema: JSONSchema, dynamicRefMap: Record<string, string>) => JSONSchema;
|
|
14
15
|
//# sourceMappingURL=resolve-dynamic-refs.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"resolve-dynamic-refs.d.ts","sourceRoot":"","sources":["../src/resolve-dynamic-refs.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,iCAAiC,CAAA;AAEjE
|
|
1
|
+
{"version":3,"file":"resolve-dynamic-refs.d.ts","sourceRoot":"","sources":["../src/resolve-dynamic-refs.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,iCAAiC,CAAA;AAEjE;;;;;;;;;;;GAWG;AACH,eAAO,MAAM,kBAAkB,WAAY,UAAU,iBAAiB,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,KAAG,UAuC9F,CAAA"}
|
|
@@ -5,9 +5,10 @@
|
|
|
5
5
|
* the clone and converts any { $dynamicRef: "#meta" } to { $ref: "#/$defs/schema" }
|
|
6
6
|
* (or whatever the dynamicRefMap dictates).
|
|
7
7
|
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
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.
|
|
11
12
|
*/
|
|
12
13
|
export const resolveDynamicRefs = (schema, dynamicRefMap) => {
|
|
13
14
|
if (typeof schema !== 'object' || schema === null) {
|
|
@@ -19,7 +20,12 @@ export const resolveDynamicRefs = (schema, dynamicRefMap) => {
|
|
|
19
20
|
}
|
|
20
21
|
const clone = JSON.parse(JSON.stringify(schema));
|
|
21
22
|
const walk = (obj) => {
|
|
22
|
-
if (typeof obj !== 'object' || obj === null
|
|
23
|
+
if (typeof obj !== 'object' || obj === null) {
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
26
|
+
if (Array.isArray(obj)) {
|
|
27
|
+
for (const item of obj)
|
|
28
|
+
walk(item);
|
|
23
29
|
return;
|
|
24
30
|
}
|
|
25
31
|
const record = obj;
|
package/dist/schema-guards.d.ts
CHANGED
|
@@ -108,5 +108,13 @@ export declare const hasMinProperties: (schema: JSONSchema) => schema is SchemaO
|
|
|
108
108
|
export declare const hasMaxProperties: (schema: JSONSchema) => schema is SchemaObject & {
|
|
109
109
|
maxProperties: number;
|
|
110
110
|
};
|
|
111
|
+
/** Type guard to check if schema has dependentRequired (2020-12). */
|
|
112
|
+
export declare const hasDependentRequired: (schema: JSONSchema) => schema is SchemaObject & {
|
|
113
|
+
dependentRequired: Record<string, readonly string[]>;
|
|
114
|
+
};
|
|
115
|
+
/** Type guard to check if schema has a propertyNames subschema. */
|
|
116
|
+
export declare const hasPropertyNames: (schema: JSONSchema) => schema is SchemaObject & {
|
|
117
|
+
propertyNames: JSONSchema;
|
|
118
|
+
};
|
|
111
119
|
export { hasRef } from './has-ref.js';
|
|
112
120
|
//# sourceMappingURL=schema-guards.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"schema-guards.d.ts","sourceRoot":"","sources":["../src/schema-guards.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,iCAAiC,CAAA;AAEjE,KAAK,YAAY,GAAG,OAAO,CAAC,UAAU,EAAE,KAAK,GAAG,OAAO,CAAC,CAAA;AAExD,iDAAiD;AACjD,eAAO,MAAM,cAAc,W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,MAAM,EAAE,CAAC,CAAA;CAOjF,CAAA;AAED,mEAAmE;AACnE,eAAO,MAAM,gBAAgB,WAAY,UAAU,KAAG,MAAM,IAAI,YAAY,GAAG;IAAE,aAAa,EAAE,UAAU,CAAA;CAEzG,CAAA;AAED,OAAO,EAAE,MAAM,EAAE,MAAM,WAAW,CAAA"}
|
package/dist/schema-guards.js
CHANGED
|
@@ -117,4 +117,15 @@ export const hasMinProperties = (schema) => {
|
|
|
117
117
|
export const hasMaxProperties = (schema) => {
|
|
118
118
|
return isSchemaObject(schema) && 'maxProperties' in schema && typeof schema.maxProperties === 'number';
|
|
119
119
|
};
|
|
120
|
+
/** Type guard to check if schema has dependentRequired (2020-12). */
|
|
121
|
+
export const hasDependentRequired = (schema) => {
|
|
122
|
+
return (isSchemaObject(schema) &&
|
|
123
|
+
'dependentRequired' in schema &&
|
|
124
|
+
typeof schema.dependentRequired === 'object' &&
|
|
125
|
+
schema.dependentRequired !== null);
|
|
126
|
+
};
|
|
127
|
+
/** Type guard to check if schema has a propertyNames subschema. */
|
|
128
|
+
export const hasPropertyNames = (schema) => {
|
|
129
|
+
return isSchemaObject(schema) && 'propertyNames' in schema;
|
|
130
|
+
};
|
|
120
131
|
export { hasRef } from './has-ref.js';
|
package/package.json
CHANGED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest'
|
|
2
|
+
|
|
3
|
+
import { escapeRegexPattern } from './escape-regex-pattern'
|
|
4
|
+
|
|
5
|
+
describe('escape-regex-pattern', () => {
|
|
6
|
+
it('escapes bare forward slashes so the regex literal does not close early', () => {
|
|
7
|
+
expect(escapeRegexPattern('a/b')).toBe('a\\/b')
|
|
8
|
+
// Input \d{4}/\d{2}/\d{2} → \d{4}\/\d{2}\/\d{2}
|
|
9
|
+
expect(escapeRegexPattern('\\d{4}/\\d{2}/\\d{2}')).toBe('\\d{4}\\/\\d{2}\\/\\d{2}')
|
|
10
|
+
})
|
|
11
|
+
|
|
12
|
+
it('leaves backslash escape sequences exactly as-is (does not double them)', () => {
|
|
13
|
+
// \d must stay \d — doubling it would match a literal backslash, not a digit.
|
|
14
|
+
expect(escapeRegexPattern('\\d+')).toBe('\\d+')
|
|
15
|
+
expect(escapeRegexPattern('\\w\\s')).toBe('\\w\\s')
|
|
16
|
+
})
|
|
17
|
+
|
|
18
|
+
it('does not double-escape an already-escaped slash', () => {
|
|
19
|
+
// \/ (escaped slash) stays \/, not \\\/.
|
|
20
|
+
expect(escapeRegexPattern('\\/')).toBe('\\/')
|
|
21
|
+
})
|
|
22
|
+
|
|
23
|
+
it('leaves regex metacharacters that do not affect the literal untouched', () => {
|
|
24
|
+
expect(escapeRegexPattern('^[a-z]+$')).toBe('^[a-z]+$')
|
|
25
|
+
})
|
|
26
|
+
|
|
27
|
+
it('round-trips: the escaped body parses to a regex equivalent to the source pattern', () => {
|
|
28
|
+
for (const pattern of ['\\d{4}/\\d{2}', 'a/b/c', '^https?:\\/\\/', '\\w+@\\w+']) {
|
|
29
|
+
// Build the literal the generator emits and read its source back out.
|
|
30
|
+
const re = new RegExp(escapeRegexPattern(pattern))
|
|
31
|
+
// The RegExp's own source, with \/ normalized back to /, equals the input.
|
|
32
|
+
expect(re.source.replace(/\\\//g, '/')).toBe(pattern.replace(/\\\//g, '/'))
|
|
33
|
+
}
|
|
34
|
+
})
|
|
35
|
+
})
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Escapes a JSON Schema `pattern` so it can be embedded between the slashes of
|
|
3
|
+
* a generated regex literal (`/…/`).
|
|
4
|
+
*
|
|
5
|
+
* A `pattern` is an ECMA-262 regex *body*, and the generated text goes into a
|
|
6
|
+
* regex literal — not a string literal — so backslashes are regex syntax and
|
|
7
|
+
* must be left exactly as-is (doubling `\d` to `\\d` would change it from "a
|
|
8
|
+
* digit" to "a literal backslash followed by d"). The only character that would
|
|
9
|
+
* corrupt the surrounding literal is an *unescaped* `/`, which would close it
|
|
10
|
+
* early. So we escape bare slashes to `\/` while leaving every existing escape
|
|
11
|
+
* sequence (including an already-escaped `\/`) untouched.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* escapeRegexPattern('\\d{4}/\\d{2}') // → '\\d{4}\\/\\d{2}' (i.e. \d{4}\/\d{2})
|
|
15
|
+
*/
|
|
16
|
+
export const escapeRegexPattern = (pattern: string): string =>
|
|
17
|
+
// Match either an escape sequence (`\` + any char, kept verbatim) or a bare
|
|
18
|
+
// `/` (escaped). Consuming escape pairs first means the slash in `\/` is never
|
|
19
|
+
// seen as bare, so it is not double-escaped.
|
|
20
|
+
pattern.replace(/\\[\s\S]|\//g, (match) => (match === '/' ? '\\/' : match))
|
|
@@ -67,6 +67,18 @@ describe('resolve-dynamic-refs', () => {
|
|
|
67
67
|
expect(resolveDynamicRefs(false, { '#meta': '#/$defs/schema' })).toBe(false)
|
|
68
68
|
})
|
|
69
69
|
|
|
70
|
+
it('replaces $dynamicRef nested inside an array keyword (allOf)', () => {
|
|
71
|
+
const schema = {
|
|
72
|
+
allOf: [{ $dynamicRef: '#meta' }, { type: 'object' as const, properties: { x: { $dynamicRef: '#meta' } } }],
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
const result = resolveDynamicRefs(schema, { '#meta': '#/$defs/schema' })
|
|
76
|
+
|
|
77
|
+
expect(result).toEqual({
|
|
78
|
+
allOf: [{ $ref: '#/$defs/schema' }, { type: 'object', properties: { x: { $ref: '#/$defs/schema' } } }],
|
|
79
|
+
})
|
|
80
|
+
})
|
|
81
|
+
|
|
70
82
|
it('handles nested $dynamicRef in property sub-schemas', () => {
|
|
71
83
|
const schema = {
|
|
72
84
|
type: 'object' as const,
|
|
@@ -7,9 +7,10 @@ import type { JSONSchema } from 'json-schema-typed/draft-2020-12'
|
|
|
7
7
|
* the clone and converts any { $dynamicRef: "#meta" } to { $ref: "#/$defs/schema" }
|
|
8
8
|
* (or whatever the dynamicRefMap dictates).
|
|
9
9
|
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
10
|
+
* Walks both object properties and array elements, so a `$dynamicRef` nested
|
|
11
|
+
* inside a keyword whose value is an array of subschemas (`allOf`, `anyOf`,
|
|
12
|
+
* `oneOf`, `prefixItems`, …) is rewritten too. The build system generates a
|
|
13
|
+
* separate file per `$def`, so each schema walked here is relatively shallow.
|
|
13
14
|
*/
|
|
14
15
|
export const resolveDynamicRefs = (schema: JSONSchema, dynamicRefMap: Record<string, string>): JSONSchema => {
|
|
15
16
|
if (typeof schema !== 'object' || schema === null) {
|
|
@@ -24,7 +25,12 @@ export const resolveDynamicRefs = (schema: JSONSchema, dynamicRefMap: Record<str
|
|
|
24
25
|
const clone = JSON.parse(JSON.stringify(schema)) as Record<string, unknown>
|
|
25
26
|
|
|
26
27
|
const walk = (obj: unknown): void => {
|
|
27
|
-
if (typeof obj !== 'object' || obj === null
|
|
28
|
+
if (typeof obj !== 'object' || obj === null) {
|
|
29
|
+
return
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
if (Array.isArray(obj)) {
|
|
33
|
+
for (const item of obj) walk(item)
|
|
28
34
|
return
|
|
29
35
|
}
|
|
30
36
|
|
|
@@ -7,6 +7,7 @@ import {
|
|
|
7
7
|
hasAnyOf,
|
|
8
8
|
hasConst,
|
|
9
9
|
hasDefault,
|
|
10
|
+
hasDependentRequired,
|
|
10
11
|
hasEnum,
|
|
11
12
|
hasExamples,
|
|
12
13
|
hasExclusiveMaximum,
|
|
@@ -25,6 +26,7 @@ import {
|
|
|
25
26
|
hasOneOf,
|
|
26
27
|
hasPattern,
|
|
27
28
|
hasProperties,
|
|
29
|
+
hasPropertyNames,
|
|
28
30
|
hasRef,
|
|
29
31
|
hasRequired,
|
|
30
32
|
hasType,
|
|
@@ -386,6 +388,25 @@ describe('schema-guards', () => {
|
|
|
386
388
|
expect(hasMaxProperties({})).toBe(false)
|
|
387
389
|
})
|
|
388
390
|
|
|
391
|
+
// hasDependentRequired
|
|
392
|
+
it('hasDependentRequired returns true for an object value', () => {
|
|
393
|
+
expect(hasDependentRequired({ dependentRequired: { a: ['b'] } })).toBe(true)
|
|
394
|
+
})
|
|
395
|
+
|
|
396
|
+
it('hasDependentRequired returns false when missing or non-object', () => {
|
|
397
|
+
expect(hasDependentRequired({})).toBe(false)
|
|
398
|
+
expect(hasDependentRequired({ dependentRequired: null } as unknown as JSONSchema)).toBe(false)
|
|
399
|
+
})
|
|
400
|
+
|
|
401
|
+
// hasPropertyNames
|
|
402
|
+
it('hasPropertyNames returns true when the keyword is present', () => {
|
|
403
|
+
expect(hasPropertyNames({ propertyNames: { pattern: '^[a-z]+$' } })).toBe(true)
|
|
404
|
+
})
|
|
405
|
+
|
|
406
|
+
it('hasPropertyNames returns false when missing', () => {
|
|
407
|
+
expect(hasPropertyNames({})).toBe(false)
|
|
408
|
+
})
|
|
409
|
+
|
|
389
410
|
// hasRef
|
|
390
411
|
it('hasRef returns true when $ref is a string', () => {
|
|
391
412
|
expect(hasRef({ $ref: '#/$defs/user' })).toBe(true)
|
package/src/schema-guards.ts
CHANGED
|
@@ -157,4 +157,21 @@ export const hasMaxProperties = (schema: JSONSchema): schema is SchemaObject & {
|
|
|
157
157
|
return isSchemaObject(schema) && 'maxProperties' in schema && typeof schema.maxProperties === 'number'
|
|
158
158
|
}
|
|
159
159
|
|
|
160
|
+
/** Type guard to check if schema has dependentRequired (2020-12). */
|
|
161
|
+
export const hasDependentRequired = (
|
|
162
|
+
schema: JSONSchema,
|
|
163
|
+
): schema is SchemaObject & { dependentRequired: Record<string, readonly string[]> } => {
|
|
164
|
+
return (
|
|
165
|
+
isSchemaObject(schema) &&
|
|
166
|
+
'dependentRequired' in schema &&
|
|
167
|
+
typeof schema.dependentRequired === 'object' &&
|
|
168
|
+
schema.dependentRequired !== null
|
|
169
|
+
)
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/** Type guard to check if schema has a propertyNames subschema. */
|
|
173
|
+
export const hasPropertyNames = (schema: JSONSchema): schema is SchemaObject & { propertyNames: JSONSchema } => {
|
|
174
|
+
return isSchemaObject(schema) && 'propertyNames' in schema
|
|
175
|
+
}
|
|
176
|
+
|
|
160
177
|
export { hasRef } from './has-ref'
|