@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.
- package/dist/build-dynamic-ref-map.js +30 -53
- package/dist/derive-root-type-name.js +19 -49
- package/dist/escape-regex-pattern.js +39 -80
- package/dist/extract-dynamic-anchor-defs.js +5 -22
- package/dist/extract-refs.js +31 -63
- package/dist/generate-index-barrel.js +53 -84
- package/dist/generate-type-definition.js +372 -458
- package/dist/has-ref.js +5 -7
- package/dist/is-object.js +4 -14
- package/dist/mjst-extension.js +25 -44
- package/dist/multiple-of-check.js +12 -26
- package/dist/parse-documentation.js +86 -128
- package/dist/quote-js-string.js +4 -18
- package/dist/ref-to-filename.js +40 -105
- package/dist/ref-to-name.js +17 -53
- package/dist/resolve-dynamic-refs.js +47 -61
- package/dist/resolve-ref.js +37 -73
- package/dist/safe-accessor.js +18 -45
- package/dist/schema-guards.js +158 -175
- package/dist/unknown-key-check.js +19 -37
- package/dist/upgrade-draft07-schema.js +86 -149
- package/dist/validate-array.js +20 -31
- package/dist/validate-record.js +18 -24
- package/dist/walk-ref-graph.js +113 -158
- package/package.json +2 -2
package/dist/schema-guards.js
CHANGED
|
@@ -1,175 +1,158 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
};
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
};
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
};
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
};
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
};
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
};
|
|
118
|
-
|
|
119
|
-
export
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
'patternProperties' in schema &&
|
|
160
|
-
typeof schema.patternProperties === 'object' &&
|
|
161
|
-
schema.patternProperties !== null);
|
|
162
|
-
};
|
|
163
|
-
/** Type guard to check if schema has a `contains` subschema (array). */
|
|
164
|
-
export const hasContains = (schema) => {
|
|
165
|
-
return isSchemaObject(schema) && 'contains' in schema;
|
|
166
|
-
};
|
|
167
|
-
/** Type guard to check if schema has a `not` subschema. */
|
|
168
|
-
export const hasNot = (schema) => {
|
|
169
|
-
return isSchemaObject(schema) && 'not' in schema;
|
|
170
|
-
};
|
|
171
|
-
/** Type guard to check if schema has an `if` subschema (with optional then/else). */
|
|
172
|
-
export const hasIf = (schema) => {
|
|
173
|
-
return isSchemaObject(schema) && 'if' in schema;
|
|
174
|
-
};
|
|
175
|
-
export { hasRef } from './has-ref.js';
|
|
1
|
+
const isSchemaObject = (schema) => {
|
|
2
|
+
return typeof schema === "object" && schema !== null && typeof schema !== "boolean";
|
|
3
|
+
};
|
|
4
|
+
const hasType = (schema) => {
|
|
5
|
+
return isSchemaObject(schema) && "type" in schema && typeof schema.type === "string";
|
|
6
|
+
};
|
|
7
|
+
const isObjectSchema = (schema) => {
|
|
8
|
+
return isSchemaObject(schema) && ("type" in schema && schema.type === "object" || "properties" in schema);
|
|
9
|
+
};
|
|
10
|
+
const hasProperties = (schema) => {
|
|
11
|
+
return isSchemaObject(schema) && "properties" in schema && typeof schema.properties === "object" && schema.properties !== null;
|
|
12
|
+
};
|
|
13
|
+
const hasEnum = (schema) => {
|
|
14
|
+
return isSchemaObject(schema) && "enum" in schema && Array.isArray(schema.enum);
|
|
15
|
+
};
|
|
16
|
+
const hasConst = (schema) => {
|
|
17
|
+
return isSchemaObject(schema) && "const" in schema;
|
|
18
|
+
};
|
|
19
|
+
const hasPattern = (schema) => {
|
|
20
|
+
return isSchemaObject(schema) && "pattern" in schema && typeof schema.pattern === "string";
|
|
21
|
+
};
|
|
22
|
+
const hasFormat = (schema) => {
|
|
23
|
+
return isSchemaObject(schema) && "format" in schema && typeof schema.format === "string";
|
|
24
|
+
};
|
|
25
|
+
const hasDefault = (schema) => {
|
|
26
|
+
return isSchemaObject(schema) && "default" in schema;
|
|
27
|
+
};
|
|
28
|
+
const hasExamples = (schema) => {
|
|
29
|
+
return isSchemaObject(schema) && "examples" in schema && Array.isArray(schema.examples);
|
|
30
|
+
};
|
|
31
|
+
const hasOneOf = (schema) => {
|
|
32
|
+
return isSchemaObject(schema) && "oneOf" in schema && Array.isArray(schema.oneOf);
|
|
33
|
+
};
|
|
34
|
+
const hasAnyOf = (schema) => {
|
|
35
|
+
return isSchemaObject(schema) && "anyOf" in schema && Array.isArray(schema.anyOf);
|
|
36
|
+
};
|
|
37
|
+
const hasAllOf = (schema) => {
|
|
38
|
+
return isSchemaObject(schema) && "allOf" in schema && Array.isArray(schema.allOf);
|
|
39
|
+
};
|
|
40
|
+
const hasRequired = (schema) => {
|
|
41
|
+
return isSchemaObject(schema) && "required" in schema && Array.isArray(schema.required);
|
|
42
|
+
};
|
|
43
|
+
const hasItems = (schema) => {
|
|
44
|
+
return isSchemaObject(schema) && "items" in schema && typeof schema.items === "object" && schema.items !== null && typeof schema.items !== "boolean";
|
|
45
|
+
};
|
|
46
|
+
const hasAdditionalProperties = (schema) => {
|
|
47
|
+
return isSchemaObject(schema) && "additionalProperties" in schema;
|
|
48
|
+
};
|
|
49
|
+
const hasMinLength = (schema) => {
|
|
50
|
+
return isSchemaObject(schema) && "minLength" in schema && typeof schema.minLength === "number";
|
|
51
|
+
};
|
|
52
|
+
const hasMaxLength = (schema) => {
|
|
53
|
+
return isSchemaObject(schema) && "maxLength" in schema && typeof schema.maxLength === "number";
|
|
54
|
+
};
|
|
55
|
+
const hasMinimum = (schema) => {
|
|
56
|
+
return isSchemaObject(schema) && "minimum" in schema && typeof schema.minimum === "number";
|
|
57
|
+
};
|
|
58
|
+
const hasMaximum = (schema) => {
|
|
59
|
+
return isSchemaObject(schema) && "maximum" in schema && typeof schema.maximum === "number";
|
|
60
|
+
};
|
|
61
|
+
const hasExclusiveMinimum = (schema) => {
|
|
62
|
+
return isSchemaObject(schema) && "exclusiveMinimum" in schema && typeof schema.exclusiveMinimum === "number";
|
|
63
|
+
};
|
|
64
|
+
const hasExclusiveMaximum = (schema) => {
|
|
65
|
+
return isSchemaObject(schema) && "exclusiveMaximum" in schema && typeof schema.exclusiveMaximum === "number";
|
|
66
|
+
};
|
|
67
|
+
const hasStrictExclusiveMinimum = (schema) => {
|
|
68
|
+
if (!isSchemaObject(schema))
|
|
69
|
+
return false;
|
|
70
|
+
const flag = schema.exclusiveMinimum;
|
|
71
|
+
return flag === true;
|
|
72
|
+
};
|
|
73
|
+
const hasStrictExclusiveMaximum = (schema) => {
|
|
74
|
+
if (!isSchemaObject(schema))
|
|
75
|
+
return false;
|
|
76
|
+
const flag = schema.exclusiveMaximum;
|
|
77
|
+
return flag === true;
|
|
78
|
+
};
|
|
79
|
+
const hasMultipleOf = (schema) => {
|
|
80
|
+
return isSchemaObject(schema) && "multipleOf" in schema && typeof schema.multipleOf === "number";
|
|
81
|
+
};
|
|
82
|
+
const hasMinItems = (schema) => {
|
|
83
|
+
return isSchemaObject(schema) && "minItems" in schema && typeof schema.minItems === "number";
|
|
84
|
+
};
|
|
85
|
+
const hasMaxItems = (schema) => {
|
|
86
|
+
return isSchemaObject(schema) && "maxItems" in schema && typeof schema.maxItems === "number";
|
|
87
|
+
};
|
|
88
|
+
const hasUniqueItems = (schema) => {
|
|
89
|
+
return isSchemaObject(schema) && "uniqueItems" in schema && typeof schema.uniqueItems === "boolean";
|
|
90
|
+
};
|
|
91
|
+
const hasMinProperties = (schema) => {
|
|
92
|
+
return isSchemaObject(schema) && "minProperties" in schema && typeof schema.minProperties === "number";
|
|
93
|
+
};
|
|
94
|
+
const hasMaxProperties = (schema) => {
|
|
95
|
+
return isSchemaObject(schema) && "maxProperties" in schema && typeof schema.maxProperties === "number";
|
|
96
|
+
};
|
|
97
|
+
const hasDependentRequired = (schema) => {
|
|
98
|
+
return isSchemaObject(schema) && "dependentRequired" in schema && typeof schema.dependentRequired === "object" && schema.dependentRequired !== null;
|
|
99
|
+
};
|
|
100
|
+
const hasPropertyNames = (schema) => {
|
|
101
|
+
return isSchemaObject(schema) && "propertyNames" in schema;
|
|
102
|
+
};
|
|
103
|
+
const hasDependentSchemas = (schema) => {
|
|
104
|
+
return isSchemaObject(schema) && "dependentSchemas" in schema && typeof schema.dependentSchemas === "object" && schema.dependentSchemas !== null;
|
|
105
|
+
};
|
|
106
|
+
const hasPatternProperties = (schema) => {
|
|
107
|
+
return isSchemaObject(schema) && "patternProperties" in schema && typeof schema.patternProperties === "object" && schema.patternProperties !== null;
|
|
108
|
+
};
|
|
109
|
+
const hasContains = (schema) => {
|
|
110
|
+
return isSchemaObject(schema) && "contains" in schema;
|
|
111
|
+
};
|
|
112
|
+
const hasNot = (schema) => {
|
|
113
|
+
return isSchemaObject(schema) && "not" in schema;
|
|
114
|
+
};
|
|
115
|
+
const hasIf = (schema) => {
|
|
116
|
+
return isSchemaObject(schema) && "if" in schema;
|
|
117
|
+
};
|
|
118
|
+
import { hasRef } from "./has-ref.js";
|
|
119
|
+
export {
|
|
120
|
+
hasAdditionalProperties,
|
|
121
|
+
hasAllOf,
|
|
122
|
+
hasAnyOf,
|
|
123
|
+
hasConst,
|
|
124
|
+
hasContains,
|
|
125
|
+
hasDefault,
|
|
126
|
+
hasDependentRequired,
|
|
127
|
+
hasDependentSchemas,
|
|
128
|
+
hasEnum,
|
|
129
|
+
hasExamples,
|
|
130
|
+
hasExclusiveMaximum,
|
|
131
|
+
hasExclusiveMinimum,
|
|
132
|
+
hasFormat,
|
|
133
|
+
hasIf,
|
|
134
|
+
hasItems,
|
|
135
|
+
hasMaxItems,
|
|
136
|
+
hasMaxLength,
|
|
137
|
+
hasMaxProperties,
|
|
138
|
+
hasMaximum,
|
|
139
|
+
hasMinItems,
|
|
140
|
+
hasMinLength,
|
|
141
|
+
hasMinProperties,
|
|
142
|
+
hasMinimum,
|
|
143
|
+
hasMultipleOf,
|
|
144
|
+
hasNot,
|
|
145
|
+
hasOneOf,
|
|
146
|
+
hasPattern,
|
|
147
|
+
hasPatternProperties,
|
|
148
|
+
hasProperties,
|
|
149
|
+
hasPropertyNames,
|
|
150
|
+
hasRef,
|
|
151
|
+
hasRequired,
|
|
152
|
+
hasStrictExclusiveMaximum,
|
|
153
|
+
hasStrictExclusiveMinimum,
|
|
154
|
+
hasType,
|
|
155
|
+
hasUniqueItems,
|
|
156
|
+
isObjectSchema,
|
|
157
|
+
isSchemaObject
|
|
158
|
+
};
|
|
@@ -1,40 +1,22 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
export const INLINE_KEY_LIMIT = 16;
|
|
8
|
-
/**
|
|
9
|
-
* Builds the "is this an undeclared key" test used by `additionalProperties:
|
|
10
|
-
* false` sweeps in the generated parsers and validators.
|
|
11
|
-
*
|
|
12
|
-
* For a small number of known keys V8 evaluates a chain of `!==` string
|
|
13
|
-
* comparisons faster than `Set.has` (which has to hash the string), and the
|
|
14
|
-
* inline form skips the per-module `Set` allocation — the same shape Ajv and
|
|
15
|
-
* TypeBox compile to. Above `inlineLimit` the chain grows long enough that the
|
|
16
|
-
* `Set`'s O(1) lookup wins, so a `Set` named `setName` is hoisted instead.
|
|
17
|
-
*
|
|
18
|
-
* @example
|
|
19
|
-
* const check = unknownKeyCheck(['id', 'name'], '_knownKeys0')
|
|
20
|
-
* check.declarations // []
|
|
21
|
-
* check.isUnknown('_k') // '_k !== "id" && _k !== "name"'
|
|
22
|
-
* check.isKnown('_k') // '_k === "id" || _k === "name"'
|
|
23
|
-
*/
|
|
24
|
-
export const unknownKeyCheck = (knownKeys, setName, inlineLimit = INLINE_KEY_LIMIT) => {
|
|
25
|
-
if (knownKeys.length === 0) {
|
|
26
|
-
return { declarations: [], isUnknown: () => 'true', isKnown: () => 'false' };
|
|
27
|
-
}
|
|
28
|
-
if (knownKeys.length <= inlineLimit) {
|
|
29
|
-
return {
|
|
30
|
-
declarations: [],
|
|
31
|
-
isUnknown: (keyVar) => knownKeys.map((key) => `${keyVar} !== ${JSON.stringify(key)}`).join(' && '),
|
|
32
|
-
isKnown: (keyVar) => knownKeys.map((key) => `${keyVar} === ${JSON.stringify(key)}`).join(' || '),
|
|
33
|
-
};
|
|
34
|
-
}
|
|
1
|
+
const INLINE_KEY_LIMIT = 16;
|
|
2
|
+
const unknownKeyCheck = (knownKeys, setName, inlineLimit = INLINE_KEY_LIMIT) => {
|
|
3
|
+
if (knownKeys.length === 0) {
|
|
4
|
+
return { declarations: [], isUnknown: () => "true", isKnown: () => "false" };
|
|
5
|
+
}
|
|
6
|
+
if (knownKeys.length <= inlineLimit) {
|
|
35
7
|
return {
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
8
|
+
declarations: [],
|
|
9
|
+
isUnknown: (keyVar) => knownKeys.map((key) => `${keyVar} !== ${JSON.stringify(key)}`).join(" && "),
|
|
10
|
+
isKnown: (keyVar) => knownKeys.map((key) => `${keyVar} === ${JSON.stringify(key)}`).join(" || ")
|
|
39
11
|
};
|
|
12
|
+
}
|
|
13
|
+
return {
|
|
14
|
+
declarations: [`const ${setName} = new Set(${JSON.stringify(knownKeys)})`],
|
|
15
|
+
isUnknown: (keyVar) => `!${setName}.has(${keyVar})`,
|
|
16
|
+
isKnown: (keyVar) => `${setName}.has(${keyVar})`
|
|
17
|
+
};
|
|
18
|
+
};
|
|
19
|
+
export {
|
|
20
|
+
INLINE_KEY_LIMIT,
|
|
21
|
+
unknownKeyCheck
|
|
40
22
|
};
|