@amritk/helpers 0.8.0 → 0.10.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/build-dynamic-ref-map.d.ts +0 -1
- package/dist/derive-root-type-name.d.ts +0 -1
- package/dist/escape-regex-pattern.d.ts +0 -1
- package/dist/extract-dynamic-anchor-defs.d.ts +0 -1
- package/dist/extract-refs.d.ts +0 -1
- package/dist/generate-index-barrel.d.ts +0 -1
- package/dist/generate-type-definition.d.ts +0 -1
- package/dist/has-ref.d.ts +0 -1
- package/dist/is-object.d.ts +0 -1
- package/dist/mjst-extension.d.ts +0 -1
- package/dist/parse-documentation.d.ts +0 -1
- package/dist/ref-to-filename.d.ts +0 -1
- package/dist/ref-to-name.d.ts +0 -1
- package/dist/resolve-dynamic-refs.d.ts +0 -1
- package/dist/resolve-ref.d.ts +0 -1
- package/dist/safe-accessor.d.ts +0 -1
- package/dist/schema-guards.d.ts +8 -1
- package/dist/schema-guards.js +18 -0
- package/dist/unknown-key-check.d.ts +46 -0
- package/dist/unknown-key-check.js +40 -0
- package/dist/upgrade-draft07-schema.d.ts +0 -1
- package/dist/validate-array.d.ts +0 -1
- package/dist/validate-record.d.ts +0 -1
- package/dist/walk-ref-graph.d.ts +0 -1
- package/package.json +2 -4
- package/dist/build-dynamic-ref-map.d.ts.map +0 -1
- package/dist/derive-root-type-name.d.ts.map +0 -1
- package/dist/escape-regex-pattern.d.ts.map +0 -1
- package/dist/extract-dynamic-anchor-defs.d.ts.map +0 -1
- package/dist/extract-refs.d.ts.map +0 -1
- package/dist/generate-index-barrel.d.ts.map +0 -1
- package/dist/generate-type-definition.d.ts.map +0 -1
- package/dist/has-ref.d.ts.map +0 -1
- package/dist/is-object.d.ts.map +0 -1
- package/dist/mjst-extension.d.ts.map +0 -1
- package/dist/parse-documentation.d.ts.map +0 -1
- package/dist/ref-to-filename.d.ts.map +0 -1
- package/dist/ref-to-name.d.ts.map +0 -1
- package/dist/resolve-dynamic-refs.d.ts.map +0 -1
- package/dist/resolve-ref.d.ts.map +0 -1
- package/dist/safe-accessor.d.ts.map +0 -1
- package/dist/schema-guards.d.ts.map +0 -1
- package/dist/upgrade-draft07-schema.d.ts.map +0 -1
- package/dist/validate-array.d.ts.map +0 -1
- package/dist/validate-record.d.ts.map +0 -1
- package/dist/walk-ref-graph.d.ts.map +0 -1
- package/src/build-dynamic-ref-map.test.ts +0 -65
- package/src/build-dynamic-ref-map.ts +0 -42
- package/src/derive-root-type-name.test.ts +0 -42
- package/src/derive-root-type-name.ts +0 -42
- package/src/escape-regex-pattern.test.ts +0 -35
- package/src/escape-regex-pattern.ts +0 -20
- package/src/extract-dynamic-anchor-defs.test.ts +0 -29
- package/src/extract-dynamic-anchor-defs.ts +0 -33
- package/src/extract-refs.test.ts +0 -183
- package/src/extract-refs.ts +0 -73
- package/src/generate-index-barrel.test.ts +0 -41
- package/src/generate-index-barrel.ts +0 -62
- package/src/generate-type-definition.test.ts +0 -1386
- package/src/generate-type-definition.ts +0 -580
- package/src/has-ref.ts +0 -10
- package/src/is-object.test.ts +0 -65
- package/src/is-object.ts +0 -15
- package/src/mjst-extension.test.ts +0 -59
- package/src/mjst-extension.ts +0 -81
- package/src/parse-documentation.test.ts +0 -313
- package/src/parse-documentation.ts +0 -173
- package/src/ref-to-filename.test.ts +0 -64
- package/src/ref-to-filename.ts +0 -116
- package/src/ref-to-name.test.ts +0 -57
- package/src/ref-to-name.ts +0 -42
- package/src/resolve-dynamic-refs.test.ts +0 -106
- package/src/resolve-dynamic-refs.ts +0 -54
- package/src/resolve-ref.test.ts +0 -186
- package/src/resolve-ref.ts +0 -82
- package/src/safe-accessor.test.ts +0 -41
- package/src/safe-accessor.ts +0 -48
- package/src/schema-guards.test.ts +0 -427
- package/src/schema-guards.ts +0 -177
- package/src/upgrade-draft07-schema.ts +0 -179
- package/src/validate-array.test.ts +0 -52
- package/src/validate-array.ts +0 -15
- package/src/validate-record.test.ts +0 -56
- package/src/validate-record.ts +0 -19
- package/src/walk-ref-graph.test.ts +0 -118
- package/src/walk-ref-graph.ts +0 -184
package/src/safe-accessor.ts
DELETED
|
@@ -1,48 +0,0 @@
|
|
|
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
|
-
const JS_IDENTIFIER = /^[a-zA-Z_$][a-zA-Z0-9_$]*$/
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* Generates a safe property accessor for a given key on an object variable.
|
|
10
|
-
* Uses dot notation for simple identifiers and bracket notation for keys
|
|
11
|
-
* that contain special characters like hyphens.
|
|
12
|
-
*
|
|
13
|
-
* @param variable - The variable name (e.g., "input", "input?")
|
|
14
|
-
* @param key - The property name to access
|
|
15
|
-
* @returns A valid JS property access expression
|
|
16
|
-
*
|
|
17
|
-
* @example
|
|
18
|
-
* safeAccessor("input", "name") // "input.name"
|
|
19
|
-
* safeAccessor("input?", "x-linkedin") // "input?.['x-linkedin']"
|
|
20
|
-
* safeAccessor("input", "x-linkedin") // "input['x-linkedin']"
|
|
21
|
-
*/
|
|
22
|
-
export const safeAccessor = (variable: string, key: string): string => {
|
|
23
|
-
if (JS_IDENTIFIER.test(key)) {
|
|
24
|
-
return `${variable}.${key}`
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
// Handle optional chaining: "input?" -> "input?.['key']"
|
|
28
|
-
if (variable.endsWith('?')) {
|
|
29
|
-
return `${variable}.['${key}']`
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
return `${variable}['${key}']`
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
/**
|
|
36
|
-
* Generates a safe property key for use in object literals.
|
|
37
|
-
* Wraps keys that are not valid identifiers in quotes.
|
|
38
|
-
*
|
|
39
|
-
* @example
|
|
40
|
-
* safeKey("name") // "name"
|
|
41
|
-
* safeKey("x-linkedin") // "'x-linkedin'"
|
|
42
|
-
*/
|
|
43
|
-
export const safeKey = (key: string): string => {
|
|
44
|
-
if (JS_IDENTIFIER.test(key)) {
|
|
45
|
-
return key
|
|
46
|
-
}
|
|
47
|
-
return `'${key}'`
|
|
48
|
-
}
|
|
@@ -1,427 +0,0 @@
|
|
|
1
|
-
import type { JSONSchema } from 'json-schema-typed/draft-2020-12'
|
|
2
|
-
import { describe, expect, it } from 'vitest'
|
|
3
|
-
|
|
4
|
-
import {
|
|
5
|
-
hasAdditionalProperties,
|
|
6
|
-
hasAllOf,
|
|
7
|
-
hasAnyOf,
|
|
8
|
-
hasConst,
|
|
9
|
-
hasDefault,
|
|
10
|
-
hasDependentRequired,
|
|
11
|
-
hasEnum,
|
|
12
|
-
hasExamples,
|
|
13
|
-
hasExclusiveMaximum,
|
|
14
|
-
hasExclusiveMinimum,
|
|
15
|
-
hasFormat,
|
|
16
|
-
hasItems,
|
|
17
|
-
hasMaxItems,
|
|
18
|
-
hasMaximum,
|
|
19
|
-
hasMaxLength,
|
|
20
|
-
hasMaxProperties,
|
|
21
|
-
hasMinItems,
|
|
22
|
-
hasMinimum,
|
|
23
|
-
hasMinLength,
|
|
24
|
-
hasMinProperties,
|
|
25
|
-
hasMultipleOf,
|
|
26
|
-
hasOneOf,
|
|
27
|
-
hasPattern,
|
|
28
|
-
hasProperties,
|
|
29
|
-
hasPropertyNames,
|
|
30
|
-
hasRef,
|
|
31
|
-
hasRequired,
|
|
32
|
-
hasType,
|
|
33
|
-
hasUniqueItems,
|
|
34
|
-
isObjectSchema,
|
|
35
|
-
isSchemaObject,
|
|
36
|
-
} from './schema-guards'
|
|
37
|
-
|
|
38
|
-
describe('schema-guards', () => {
|
|
39
|
-
// isSchemaObject
|
|
40
|
-
it('isSchemaObject returns true for plain object', () => {
|
|
41
|
-
expect(isSchemaObject({})).toBe(true)
|
|
42
|
-
})
|
|
43
|
-
|
|
44
|
-
it('isSchemaObject returns true for object with properties', () => {
|
|
45
|
-
expect(isSchemaObject({ type: 'string' })).toBe(true)
|
|
46
|
-
})
|
|
47
|
-
|
|
48
|
-
it('isSchemaObject returns false for boolean true', () => {
|
|
49
|
-
expect(isSchemaObject(true)).toBe(false)
|
|
50
|
-
})
|
|
51
|
-
|
|
52
|
-
it('isSchemaObject returns false for boolean false', () => {
|
|
53
|
-
expect(isSchemaObject(false)).toBe(false)
|
|
54
|
-
})
|
|
55
|
-
|
|
56
|
-
// hasType
|
|
57
|
-
it('hasType returns true when type is a string', () => {
|
|
58
|
-
expect(hasType({ type: 'string' })).toBe(true)
|
|
59
|
-
})
|
|
60
|
-
|
|
61
|
-
it('hasType returns false when type is missing', () => {
|
|
62
|
-
expect(hasType({})).toBe(false)
|
|
63
|
-
})
|
|
64
|
-
|
|
65
|
-
it('hasType returns false for boolean schema', () => {
|
|
66
|
-
expect(hasType(true)).toBe(false)
|
|
67
|
-
})
|
|
68
|
-
|
|
69
|
-
it('hasType returns false when type is an array', () => {
|
|
70
|
-
expect(hasType({ type: ['string', 'number'] })).toBe(false)
|
|
71
|
-
})
|
|
72
|
-
|
|
73
|
-
// isObjectSchema
|
|
74
|
-
it('isObjectSchema returns true for schema with type object', () => {
|
|
75
|
-
expect(isObjectSchema({ type: 'object' })).toBe(true)
|
|
76
|
-
})
|
|
77
|
-
|
|
78
|
-
it('isObjectSchema returns true for schema with properties but no type', () => {
|
|
79
|
-
expect(isObjectSchema({ properties: { name: { type: 'string' } } })).toBe(true)
|
|
80
|
-
})
|
|
81
|
-
|
|
82
|
-
it('isObjectSchema returns false for string type', () => {
|
|
83
|
-
expect(isObjectSchema({ type: 'string' })).toBe(false)
|
|
84
|
-
})
|
|
85
|
-
|
|
86
|
-
it('isObjectSchema returns false for boolean schema', () => {
|
|
87
|
-
expect(isObjectSchema(true)).toBe(false)
|
|
88
|
-
})
|
|
89
|
-
|
|
90
|
-
// hasProperties
|
|
91
|
-
it('hasProperties returns true when properties is an object', () => {
|
|
92
|
-
expect(hasProperties({ properties: { name: { type: 'string' } } })).toBe(true)
|
|
93
|
-
})
|
|
94
|
-
|
|
95
|
-
it('hasProperties returns true for empty properties object', () => {
|
|
96
|
-
expect(hasProperties({ properties: {} })).toBe(true)
|
|
97
|
-
})
|
|
98
|
-
|
|
99
|
-
it('hasProperties returns false when properties is missing', () => {
|
|
100
|
-
expect(hasProperties({ type: 'object' })).toBe(false)
|
|
101
|
-
})
|
|
102
|
-
|
|
103
|
-
it('hasProperties returns false for boolean schema', () => {
|
|
104
|
-
expect(hasProperties(true)).toBe(false)
|
|
105
|
-
})
|
|
106
|
-
|
|
107
|
-
// hasEnum
|
|
108
|
-
it('hasEnum returns true when enum is an array', () => {
|
|
109
|
-
expect(hasEnum({ enum: ['a', 'b'] })).toBe(true)
|
|
110
|
-
})
|
|
111
|
-
|
|
112
|
-
it('hasEnum returns true for empty enum array', () => {
|
|
113
|
-
expect(hasEnum({ enum: [] })).toBe(true)
|
|
114
|
-
})
|
|
115
|
-
|
|
116
|
-
it('hasEnum returns false when enum is missing', () => {
|
|
117
|
-
expect(hasEnum({})).toBe(false)
|
|
118
|
-
})
|
|
119
|
-
|
|
120
|
-
it('hasEnum returns false for boolean schema', () => {
|
|
121
|
-
expect(hasEnum(false)).toBe(false)
|
|
122
|
-
})
|
|
123
|
-
|
|
124
|
-
// hasConst
|
|
125
|
-
it('hasConst returns true when const is present', () => {
|
|
126
|
-
expect(hasConst({ const: 'value' })).toBe(true)
|
|
127
|
-
})
|
|
128
|
-
|
|
129
|
-
it('hasConst returns true for null const', () => {
|
|
130
|
-
expect(hasConst({ const: null })).toBe(true)
|
|
131
|
-
})
|
|
132
|
-
|
|
133
|
-
it('hasConst returns false when const is missing', () => {
|
|
134
|
-
expect(hasConst({})).toBe(false)
|
|
135
|
-
})
|
|
136
|
-
|
|
137
|
-
// hasPattern
|
|
138
|
-
it('hasPattern returns true when pattern is a string', () => {
|
|
139
|
-
expect(hasPattern({ pattern: '^[a-z]+$' })).toBe(true)
|
|
140
|
-
})
|
|
141
|
-
|
|
142
|
-
it('hasPattern returns false when pattern is not a string', () => {
|
|
143
|
-
// Intentionally passing wrong type to verify the guard rejects it
|
|
144
|
-
expect(hasPattern({ pattern: 123 } as unknown as JSONSchema)).toBe(false)
|
|
145
|
-
})
|
|
146
|
-
|
|
147
|
-
it('hasPattern returns false when pattern is missing', () => {
|
|
148
|
-
expect(hasPattern({})).toBe(false)
|
|
149
|
-
})
|
|
150
|
-
|
|
151
|
-
// hasFormat
|
|
152
|
-
it('hasFormat returns true when format is a string', () => {
|
|
153
|
-
expect(hasFormat({ format: 'date-time' })).toBe(true)
|
|
154
|
-
})
|
|
155
|
-
|
|
156
|
-
it('hasFormat returns false when format is missing', () => {
|
|
157
|
-
expect(hasFormat({})).toBe(false)
|
|
158
|
-
})
|
|
159
|
-
|
|
160
|
-
it('hasFormat returns false when format is not a string', () => {
|
|
161
|
-
// Intentionally passing wrong type to verify the guard rejects it
|
|
162
|
-
expect(hasFormat({ format: 123 } as unknown as JSONSchema)).toBe(false)
|
|
163
|
-
})
|
|
164
|
-
|
|
165
|
-
// hasDefault
|
|
166
|
-
it('hasDefault returns true when default is present', () => {
|
|
167
|
-
expect(hasDefault({ default: 'value' })).toBe(true)
|
|
168
|
-
})
|
|
169
|
-
|
|
170
|
-
it('hasDefault returns true for undefined-like defaults', () => {
|
|
171
|
-
expect(hasDefault({ default: null })).toBe(true)
|
|
172
|
-
})
|
|
173
|
-
|
|
174
|
-
it('hasDefault returns false when default is missing', () => {
|
|
175
|
-
expect(hasDefault({})).toBe(false)
|
|
176
|
-
})
|
|
177
|
-
|
|
178
|
-
// hasExamples
|
|
179
|
-
it('hasExamples returns true when examples is an array', () => {
|
|
180
|
-
expect(hasExamples({ examples: ['foo'] })).toBe(true)
|
|
181
|
-
})
|
|
182
|
-
|
|
183
|
-
it('hasExamples returns true for empty examples array', () => {
|
|
184
|
-
expect(hasExamples({ examples: [] })).toBe(true)
|
|
185
|
-
})
|
|
186
|
-
|
|
187
|
-
it('hasExamples returns false when examples is missing', () => {
|
|
188
|
-
expect(hasExamples({})).toBe(false)
|
|
189
|
-
})
|
|
190
|
-
|
|
191
|
-
it('hasExamples returns false when examples is not an array', () => {
|
|
192
|
-
// Intentionally passing wrong type to verify the guard rejects it
|
|
193
|
-
expect(hasExamples({ examples: 'not-array' } as unknown as JSONSchema)).toBe(false)
|
|
194
|
-
})
|
|
195
|
-
|
|
196
|
-
// hasOneOf
|
|
197
|
-
it('hasOneOf returns true when oneOf is an array', () => {
|
|
198
|
-
expect(hasOneOf({ oneOf: [{ type: 'string' }] })).toBe(true)
|
|
199
|
-
})
|
|
200
|
-
|
|
201
|
-
it('hasOneOf returns false when oneOf is missing', () => {
|
|
202
|
-
expect(hasOneOf({})).toBe(false)
|
|
203
|
-
})
|
|
204
|
-
|
|
205
|
-
// hasAnyOf
|
|
206
|
-
it('hasAnyOf returns true when anyOf is an array', () => {
|
|
207
|
-
expect(hasAnyOf({ anyOf: [{ type: 'string' }] })).toBe(true)
|
|
208
|
-
})
|
|
209
|
-
|
|
210
|
-
it('hasAnyOf returns false when anyOf is missing', () => {
|
|
211
|
-
expect(hasAnyOf({})).toBe(false)
|
|
212
|
-
})
|
|
213
|
-
|
|
214
|
-
// hasAllOf
|
|
215
|
-
it('hasAllOf returns true when allOf is an array', () => {
|
|
216
|
-
expect(hasAllOf({ allOf: [{ type: 'string' }] })).toBe(true)
|
|
217
|
-
})
|
|
218
|
-
|
|
219
|
-
it('hasAllOf returns false when allOf is missing', () => {
|
|
220
|
-
expect(hasAllOf({})).toBe(false)
|
|
221
|
-
})
|
|
222
|
-
|
|
223
|
-
// hasRequired
|
|
224
|
-
it('hasRequired returns true when required is an array', () => {
|
|
225
|
-
expect(hasRequired({ required: ['name'] })).toBe(true)
|
|
226
|
-
})
|
|
227
|
-
|
|
228
|
-
it('hasRequired returns true for empty required array', () => {
|
|
229
|
-
expect(hasRequired({ required: [] })).toBe(true)
|
|
230
|
-
})
|
|
231
|
-
|
|
232
|
-
it('hasRequired returns false when required is missing', () => {
|
|
233
|
-
expect(hasRequired({})).toBe(false)
|
|
234
|
-
})
|
|
235
|
-
|
|
236
|
-
// hasItems
|
|
237
|
-
it('hasItems returns true when items is a schema object', () => {
|
|
238
|
-
expect(hasItems({ items: { type: 'string' } })).toBe(true)
|
|
239
|
-
})
|
|
240
|
-
|
|
241
|
-
it('hasItems returns false when items is boolean true', () => {
|
|
242
|
-
expect(hasItems({ items: true })).toBe(false)
|
|
243
|
-
})
|
|
244
|
-
|
|
245
|
-
it('hasItems returns false when items is missing', () => {
|
|
246
|
-
expect(hasItems({})).toBe(false)
|
|
247
|
-
})
|
|
248
|
-
|
|
249
|
-
// hasAdditionalProperties
|
|
250
|
-
it('hasAdditionalProperties returns true when additionalProperties is present', () => {
|
|
251
|
-
expect(hasAdditionalProperties({ additionalProperties: false })).toBe(true)
|
|
252
|
-
})
|
|
253
|
-
|
|
254
|
-
it('hasAdditionalProperties returns true for schema additionalProperties', () => {
|
|
255
|
-
expect(hasAdditionalProperties({ additionalProperties: { type: 'string' } })).toBe(true)
|
|
256
|
-
})
|
|
257
|
-
|
|
258
|
-
it('hasAdditionalProperties returns false when missing', () => {
|
|
259
|
-
expect(hasAdditionalProperties({})).toBe(false)
|
|
260
|
-
})
|
|
261
|
-
|
|
262
|
-
// hasMinLength
|
|
263
|
-
it('hasMinLength returns true when minLength is a number', () => {
|
|
264
|
-
expect(hasMinLength({ minLength: 1 })).toBe(true)
|
|
265
|
-
})
|
|
266
|
-
|
|
267
|
-
it('hasMinLength returns false when minLength is not a number', () => {
|
|
268
|
-
// Intentionally passing wrong type to verify the guard rejects it
|
|
269
|
-
expect(hasMinLength({ minLength: '1' } as unknown as JSONSchema)).toBe(false)
|
|
270
|
-
})
|
|
271
|
-
|
|
272
|
-
it('hasMinLength returns false when missing', () => {
|
|
273
|
-
expect(hasMinLength({})).toBe(false)
|
|
274
|
-
})
|
|
275
|
-
|
|
276
|
-
// hasMaxLength
|
|
277
|
-
it('hasMaxLength returns true when maxLength is a number', () => {
|
|
278
|
-
expect(hasMaxLength({ maxLength: 100 })).toBe(true)
|
|
279
|
-
})
|
|
280
|
-
|
|
281
|
-
it('hasMaxLength returns false when missing', () => {
|
|
282
|
-
expect(hasMaxLength({})).toBe(false)
|
|
283
|
-
})
|
|
284
|
-
|
|
285
|
-
// hasMinimum
|
|
286
|
-
it('hasMinimum returns true when minimum is a number', () => {
|
|
287
|
-
expect(hasMinimum({ minimum: 0 })).toBe(true)
|
|
288
|
-
})
|
|
289
|
-
|
|
290
|
-
it('hasMinimum returns false when missing', () => {
|
|
291
|
-
expect(hasMinimum({})).toBe(false)
|
|
292
|
-
})
|
|
293
|
-
|
|
294
|
-
// hasMaximum
|
|
295
|
-
it('hasMaximum returns true when maximum is a number', () => {
|
|
296
|
-
expect(hasMaximum({ maximum: 100 })).toBe(true)
|
|
297
|
-
})
|
|
298
|
-
|
|
299
|
-
it('hasMaximum returns false when missing', () => {
|
|
300
|
-
expect(hasMaximum({})).toBe(false)
|
|
301
|
-
})
|
|
302
|
-
|
|
303
|
-
// hasExclusiveMinimum
|
|
304
|
-
it('hasExclusiveMinimum returns true when exclusiveMinimum is a number', () => {
|
|
305
|
-
expect(hasExclusiveMinimum({ exclusiveMinimum: 0 })).toBe(true)
|
|
306
|
-
})
|
|
307
|
-
|
|
308
|
-
it('hasExclusiveMinimum returns false for boolean exclusiveMinimum', () => {
|
|
309
|
-
// Intentionally passing wrong type to verify the guard rejects it
|
|
310
|
-
expect(hasExclusiveMinimum({ exclusiveMinimum: true } as unknown as JSONSchema)).toBe(false)
|
|
311
|
-
})
|
|
312
|
-
|
|
313
|
-
it('hasExclusiveMinimum returns false when missing', () => {
|
|
314
|
-
expect(hasExclusiveMinimum({})).toBe(false)
|
|
315
|
-
})
|
|
316
|
-
|
|
317
|
-
// hasExclusiveMaximum
|
|
318
|
-
it('hasExclusiveMaximum returns true when exclusiveMaximum is a number', () => {
|
|
319
|
-
expect(hasExclusiveMaximum({ exclusiveMaximum: 100 })).toBe(true)
|
|
320
|
-
})
|
|
321
|
-
|
|
322
|
-
it('hasExclusiveMaximum returns false for boolean exclusiveMaximum', () => {
|
|
323
|
-
// Intentionally passing wrong type to verify the guard rejects it
|
|
324
|
-
expect(hasExclusiveMaximum({ exclusiveMaximum: false } as unknown as JSONSchema)).toBe(false)
|
|
325
|
-
})
|
|
326
|
-
|
|
327
|
-
it('hasExclusiveMaximum returns false when missing', () => {
|
|
328
|
-
expect(hasExclusiveMaximum({})).toBe(false)
|
|
329
|
-
})
|
|
330
|
-
|
|
331
|
-
// hasMultipleOf
|
|
332
|
-
it('hasMultipleOf returns true when multipleOf is a number', () => {
|
|
333
|
-
expect(hasMultipleOf({ multipleOf: 5 })).toBe(true)
|
|
334
|
-
})
|
|
335
|
-
|
|
336
|
-
it('hasMultipleOf returns false when missing', () => {
|
|
337
|
-
expect(hasMultipleOf({})).toBe(false)
|
|
338
|
-
})
|
|
339
|
-
|
|
340
|
-
// hasMinItems
|
|
341
|
-
it('hasMinItems returns true when minItems is a number', () => {
|
|
342
|
-
expect(hasMinItems({ minItems: 1 })).toBe(true)
|
|
343
|
-
})
|
|
344
|
-
|
|
345
|
-
it('hasMinItems returns false when missing', () => {
|
|
346
|
-
expect(hasMinItems({})).toBe(false)
|
|
347
|
-
})
|
|
348
|
-
|
|
349
|
-
// hasMaxItems
|
|
350
|
-
it('hasMaxItems returns true when maxItems is a number', () => {
|
|
351
|
-
expect(hasMaxItems({ maxItems: 10 })).toBe(true)
|
|
352
|
-
})
|
|
353
|
-
|
|
354
|
-
it('hasMaxItems returns false when missing', () => {
|
|
355
|
-
expect(hasMaxItems({})).toBe(false)
|
|
356
|
-
})
|
|
357
|
-
|
|
358
|
-
// hasUniqueItems
|
|
359
|
-
it('hasUniqueItems returns true when uniqueItems is a boolean', () => {
|
|
360
|
-
expect(hasUniqueItems({ uniqueItems: true })).toBe(true)
|
|
361
|
-
expect(hasUniqueItems({ uniqueItems: false })).toBe(true)
|
|
362
|
-
})
|
|
363
|
-
|
|
364
|
-
it('hasUniqueItems returns false when missing', () => {
|
|
365
|
-
expect(hasUniqueItems({})).toBe(false)
|
|
366
|
-
})
|
|
367
|
-
|
|
368
|
-
// hasMinProperties
|
|
369
|
-
it('hasMinProperties returns true when minProperties is a number', () => {
|
|
370
|
-
expect(hasMinProperties({ minProperties: 1 })).toBe(true)
|
|
371
|
-
})
|
|
372
|
-
|
|
373
|
-
it('hasMinProperties returns false when missing', () => {
|
|
374
|
-
expect(hasMinProperties({})).toBe(false)
|
|
375
|
-
})
|
|
376
|
-
|
|
377
|
-
it('hasMinProperties returns false for non-number value', () => {
|
|
378
|
-
// Intentionally passing wrong type to verify the guard rejects it
|
|
379
|
-
expect(hasMinProperties({ minProperties: '1' } as unknown as JSONSchema)).toBe(false)
|
|
380
|
-
})
|
|
381
|
-
|
|
382
|
-
// hasMaxProperties
|
|
383
|
-
it('hasMaxProperties returns true when maxProperties is a number', () => {
|
|
384
|
-
expect(hasMaxProperties({ maxProperties: 10 })).toBe(true)
|
|
385
|
-
})
|
|
386
|
-
|
|
387
|
-
it('hasMaxProperties returns false when missing', () => {
|
|
388
|
-
expect(hasMaxProperties({})).toBe(false)
|
|
389
|
-
})
|
|
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
|
-
|
|
410
|
-
// hasRef
|
|
411
|
-
it('hasRef returns true when $ref is a string', () => {
|
|
412
|
-
expect(hasRef({ $ref: '#/$defs/user' })).toBe(true)
|
|
413
|
-
})
|
|
414
|
-
|
|
415
|
-
it('hasRef returns false when $ref is missing', () => {
|
|
416
|
-
expect(hasRef({})).toBe(false)
|
|
417
|
-
})
|
|
418
|
-
|
|
419
|
-
it('hasRef returns false for boolean schema', () => {
|
|
420
|
-
expect(hasRef(true)).toBe(false)
|
|
421
|
-
})
|
|
422
|
-
|
|
423
|
-
it('hasRef returns false when $ref is not a string', () => {
|
|
424
|
-
// Intentionally passing wrong type to verify the guard rejects it
|
|
425
|
-
expect(hasRef({ $ref: 123 } as unknown as JSONSchema)).toBe(false)
|
|
426
|
-
})
|
|
427
|
-
})
|
package/src/schema-guards.ts
DELETED
|
@@ -1,177 +0,0 @@
|
|
|
1
|
-
import type { JSONSchema } from 'json-schema-typed/draft-2020-12'
|
|
2
|
-
|
|
3
|
-
type SchemaObject = Exclude<JSONSchema, false | boolean>
|
|
4
|
-
|
|
5
|
-
/** Type guard to check if schema is not false */
|
|
6
|
-
export const isSchemaObject = (schema: JSONSchema): schema is SchemaObject => {
|
|
7
|
-
return typeof schema === 'object' && schema !== null && typeof schema !== 'boolean'
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
/** Type guard to check if schema has a type property */
|
|
11
|
-
export const hasType = (schema: JSONSchema): schema is SchemaObject & { type: string } => {
|
|
12
|
-
return isSchemaObject(schema) && 'type' in schema && typeof schema.type === 'string'
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
/** Type guard to check if schema is an object schema */
|
|
16
|
-
export const isObjectSchema = (schema: JSONSchema): schema is JSONSchema.Object => {
|
|
17
|
-
return isSchemaObject(schema) && (('type' in schema && schema.type === 'object') || 'properties' in schema)
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
/** Type guard to check if schema has properties */
|
|
21
|
-
export const hasProperties = (
|
|
22
|
-
schema: JSONSchema,
|
|
23
|
-
): schema is SchemaObject & { properties: Record<string, JSONSchema> } => {
|
|
24
|
-
return (
|
|
25
|
-
isSchemaObject(schema) &&
|
|
26
|
-
'properties' in schema &&
|
|
27
|
-
typeof schema.properties === 'object' &&
|
|
28
|
-
schema.properties !== null
|
|
29
|
-
)
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
/** Type guard to check if schema has enum */
|
|
33
|
-
export const hasEnum = (schema: JSONSchema): schema is SchemaObject & { enum: readonly unknown[] } => {
|
|
34
|
-
return isSchemaObject(schema) && 'enum' in schema && Array.isArray(schema.enum)
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
/** Type guard to check if schema has const */
|
|
38
|
-
export const hasConst = (schema: JSONSchema): schema is SchemaObject & { const: unknown } => {
|
|
39
|
-
return isSchemaObject(schema) && 'const' in schema
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
/** Type guard to check if schema has pattern */
|
|
43
|
-
export const hasPattern = (schema: JSONSchema): schema is SchemaObject & { pattern: string } => {
|
|
44
|
-
return isSchemaObject(schema) && 'pattern' in schema && typeof schema.pattern === 'string'
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
/** Type guard to check if schema has format */
|
|
48
|
-
export const hasFormat = (schema: JSONSchema): schema is SchemaObject & { format: string } => {
|
|
49
|
-
return isSchemaObject(schema) && 'format' in schema && typeof schema.format === 'string'
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
/** Type guard to check if schema has default */
|
|
53
|
-
export const hasDefault = (schema: JSONSchema): schema is SchemaObject & { default: unknown } => {
|
|
54
|
-
return isSchemaObject(schema) && 'default' in schema
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
/** Type guard to check if schema has examples */
|
|
58
|
-
export const hasExamples = (schema: JSONSchema): schema is SchemaObject & { examples: readonly unknown[] } => {
|
|
59
|
-
return isSchemaObject(schema) && 'examples' in schema && Array.isArray(schema.examples)
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
/** Type guard to check if schema has oneOf */
|
|
63
|
-
export const hasOneOf = (schema: JSONSchema): schema is SchemaObject & { oneOf: readonly JSONSchema[] } => {
|
|
64
|
-
return isSchemaObject(schema) && 'oneOf' in schema && Array.isArray(schema.oneOf)
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
/** Type guard to check if schema has anyOf */
|
|
68
|
-
export const hasAnyOf = (schema: JSONSchema): schema is SchemaObject & { anyOf: readonly JSONSchema[] } => {
|
|
69
|
-
return isSchemaObject(schema) && 'anyOf' in schema && Array.isArray(schema.anyOf)
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
/** Type guard to check if schema has allOf */
|
|
73
|
-
export const hasAllOf = (schema: JSONSchema): schema is SchemaObject & { allOf: readonly JSONSchema[] } => {
|
|
74
|
-
return isSchemaObject(schema) && 'allOf' in schema && Array.isArray(schema.allOf)
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
/** Type guard to check if schema has required */
|
|
78
|
-
export const hasRequired = (schema: JSONSchema): schema is SchemaObject & { required: readonly string[] } => {
|
|
79
|
-
return isSchemaObject(schema) && 'required' in schema && Array.isArray(schema.required)
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
/** Type guard to check if schema has items (and it's not just boolean) */
|
|
83
|
-
export const hasItems = (schema: JSONSchema): schema is SchemaObject & { items: SchemaObject } => {
|
|
84
|
-
return (
|
|
85
|
-
isSchemaObject(schema) &&
|
|
86
|
-
'items' in schema &&
|
|
87
|
-
typeof schema.items === 'object' &&
|
|
88
|
-
schema.items !== null &&
|
|
89
|
-
typeof schema.items !== 'boolean'
|
|
90
|
-
)
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
/** Type guard to check if schema has additionalProperties */
|
|
94
|
-
export const hasAdditionalProperties = (
|
|
95
|
-
schema: JSONSchema,
|
|
96
|
-
): schema is SchemaObject & { additionalProperties: JSONSchema | boolean } => {
|
|
97
|
-
return isSchemaObject(schema) && 'additionalProperties' in schema
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
/** Type guard to check if schema has minLength */
|
|
101
|
-
export const hasMinLength = (schema: JSONSchema): schema is SchemaObject & { minLength: number } => {
|
|
102
|
-
return isSchemaObject(schema) && 'minLength' in schema && typeof schema.minLength === 'number'
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
/** Type guard to check if schema has maxLength */
|
|
106
|
-
export const hasMaxLength = (schema: JSONSchema): schema is SchemaObject & { maxLength: number } => {
|
|
107
|
-
return isSchemaObject(schema) && 'maxLength' in schema && typeof schema.maxLength === 'number'
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
/** Type guard to check if schema has minimum */
|
|
111
|
-
export const hasMinimum = (schema: JSONSchema): schema is SchemaObject & { minimum: number } => {
|
|
112
|
-
return isSchemaObject(schema) && 'minimum' in schema && typeof schema.minimum === 'number'
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
/** Type guard to check if schema has maximum */
|
|
116
|
-
export const hasMaximum = (schema: JSONSchema): schema is SchemaObject & { maximum: number } => {
|
|
117
|
-
return isSchemaObject(schema) && 'maximum' in schema && typeof schema.maximum === 'number'
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
/** Type guard to check if schema has exclusiveMinimum */
|
|
121
|
-
export const hasExclusiveMinimum = (schema: JSONSchema): schema is SchemaObject & { exclusiveMinimum: number } => {
|
|
122
|
-
return isSchemaObject(schema) && 'exclusiveMinimum' in schema && typeof schema.exclusiveMinimum === 'number'
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
/** Type guard to check if schema has exclusiveMaximum */
|
|
126
|
-
export const hasExclusiveMaximum = (schema: JSONSchema): schema is SchemaObject & { exclusiveMaximum: number } => {
|
|
127
|
-
return isSchemaObject(schema) && 'exclusiveMaximum' in schema && typeof schema.exclusiveMaximum === 'number'
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
/** Type guard to check if schema has multipleOf */
|
|
131
|
-
export const hasMultipleOf = (schema: JSONSchema): schema is SchemaObject & { multipleOf: number } => {
|
|
132
|
-
return isSchemaObject(schema) && 'multipleOf' in schema && typeof schema.multipleOf === 'number'
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
/** Type guard to check if schema has minItems */
|
|
136
|
-
export const hasMinItems = (schema: JSONSchema): schema is SchemaObject & { minItems: number } => {
|
|
137
|
-
return isSchemaObject(schema) && 'minItems' in schema && typeof schema.minItems === 'number'
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
/** Type guard to check if schema has maxItems */
|
|
141
|
-
export const hasMaxItems = (schema: JSONSchema): schema is SchemaObject & { maxItems: number } => {
|
|
142
|
-
return isSchemaObject(schema) && 'maxItems' in schema && typeof schema.maxItems === 'number'
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
/** Type guard to check if schema has uniqueItems */
|
|
146
|
-
export const hasUniqueItems = (schema: JSONSchema): schema is SchemaObject & { uniqueItems: boolean } => {
|
|
147
|
-
return isSchemaObject(schema) && 'uniqueItems' in schema && typeof schema.uniqueItems === 'boolean'
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
/** Type guard to check if schema has minProperties */
|
|
151
|
-
export const hasMinProperties = (schema: JSONSchema): schema is SchemaObject & { minProperties: number } => {
|
|
152
|
-
return isSchemaObject(schema) && 'minProperties' in schema && typeof schema.minProperties === 'number'
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
/** Type guard to check if schema has maxProperties */
|
|
156
|
-
export const hasMaxProperties = (schema: JSONSchema): schema is SchemaObject & { maxProperties: number } => {
|
|
157
|
-
return isSchemaObject(schema) && 'maxProperties' in schema && typeof schema.maxProperties === 'number'
|
|
158
|
-
}
|
|
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
|
-
|
|
177
|
-
export { hasRef } from './has-ref'
|