@atlaspack/utils 3.1.2-dev-compiled-hash-e5f8a1735.0 → 3.1.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.
- package/CHANGELOG.md +13 -0
- package/dist/schema.js +144 -143
- package/lib/schema.js +126 -142
- package/package.json +13 -14
- package/src/schema.ts +176 -157
- package/tsconfig.tsbuildinfo +1 -1
- package/LICENSE +0 -201
package/lib/schema.js
CHANGED
|
@@ -30,11 +30,9 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de
|
|
|
30
30
|
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
|
|
31
31
|
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
|
|
32
32
|
function validateSchema(schema, data) {
|
|
33
|
-
function walk(
|
|
34
|
-
// @ts-expect-error TS7006
|
|
35
|
-
schemaAncestors, dataNode, dataPath) {
|
|
33
|
+
function walk(schemaAncestors, dataNode, dataPath) {
|
|
36
34
|
let [schemaNode] = schemaAncestors;
|
|
37
|
-
if (schemaNode.type) {
|
|
35
|
+
if ('type' in schemaNode && schemaNode.type) {
|
|
38
36
|
let type = Array.isArray(dataNode) ? 'array' : typeof dataNode;
|
|
39
37
|
if (schemaNode.type !== type) {
|
|
40
38
|
return {
|
|
@@ -43,19 +41,16 @@ function validateSchema(schema, data) {
|
|
|
43
41
|
dataPath,
|
|
44
42
|
expectedTypes: [schemaNode.type],
|
|
45
43
|
ancestors: schemaAncestors,
|
|
46
|
-
prettyType: schemaNode.__type
|
|
44
|
+
prettyType: '__type' in schemaNode ? schemaNode.__type : undefined
|
|
47
45
|
};
|
|
48
46
|
} else {
|
|
49
47
|
switch (schemaNode.type) {
|
|
50
48
|
case 'array':
|
|
51
49
|
{
|
|
52
|
-
if (schemaNode.items) {
|
|
50
|
+
if ('items' in schemaNode && schemaNode.items && Array.isArray(dataNode)) {
|
|
53
51
|
let results = [];
|
|
54
|
-
// @ts-expect-error TS18046
|
|
55
52
|
for (let i = 0; i < dataNode.length; i++) {
|
|
56
|
-
let result = walk([schemaNode.items].concat(schemaAncestors),
|
|
57
|
-
// @ts-expect-error TS18046
|
|
58
|
-
dataNode[i], dataPath + '/' + i);
|
|
53
|
+
let result = walk([schemaNode.items].concat(schemaAncestors), dataNode[i], dataPath + '/' + i);
|
|
59
54
|
if (result) results.push(result);
|
|
60
55
|
}
|
|
61
56
|
if (results.length) return results.reduce((acc, v) => acc.concat(v), []);
|
|
@@ -64,130 +59,118 @@ function validateSchema(schema, data) {
|
|
|
64
59
|
}
|
|
65
60
|
case 'string':
|
|
66
61
|
{
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
62
|
+
if (typeof dataNode === 'string') {
|
|
63
|
+
let value = dataNode;
|
|
64
|
+
if ('enum' in schemaNode && schemaNode.enum) {
|
|
65
|
+
if (!schemaNode.enum.includes(value)) {
|
|
66
|
+
return {
|
|
67
|
+
type: 'enum',
|
|
68
|
+
dataType: 'value',
|
|
69
|
+
dataPath,
|
|
70
|
+
expectedValues: schemaNode.enum,
|
|
71
|
+
actualValue: value,
|
|
72
|
+
ancestors: schemaAncestors
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
} else if ('__validate' in schemaNode && schemaNode.__validate) {
|
|
76
|
+
let validationError = schemaNode.__validate(value);
|
|
77
|
+
if (typeof validationError == 'string') {
|
|
78
|
+
return {
|
|
79
|
+
type: 'other',
|
|
80
|
+
dataType: 'value',
|
|
81
|
+
dataPath,
|
|
82
|
+
message: validationError,
|
|
83
|
+
actualValue: value,
|
|
84
|
+
ancestors: schemaAncestors
|
|
85
|
+
};
|
|
86
|
+
}
|
|
91
87
|
}
|
|
92
88
|
}
|
|
93
89
|
break;
|
|
94
90
|
}
|
|
95
91
|
case 'number':
|
|
96
92
|
{
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
93
|
+
if (typeof dataNode === 'number') {
|
|
94
|
+
let value = dataNode;
|
|
95
|
+
if ('enum' in schemaNode && schemaNode.enum) {
|
|
96
|
+
if (!schemaNode.enum.includes(value)) {
|
|
97
|
+
return {
|
|
98
|
+
type: 'enum',
|
|
99
|
+
dataType: 'value',
|
|
100
|
+
dataPath,
|
|
101
|
+
expectedValues: schemaNode.enum,
|
|
102
|
+
actualValue: value,
|
|
103
|
+
ancestors: schemaAncestors
|
|
104
|
+
};
|
|
105
|
+
}
|
|
109
106
|
}
|
|
110
107
|
}
|
|
111
108
|
break;
|
|
112
109
|
}
|
|
113
110
|
case 'object':
|
|
114
111
|
{
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
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
|
-
additionalProperties = true
|
|
155
|
-
} = schemaNode;
|
|
156
|
-
// @ts-expect-error TS2407
|
|
157
|
-
for (let k in dataNode) {
|
|
158
|
-
if (invalidProps && invalidProps.includes(k)) {
|
|
159
|
-
// Don't check type on forbidden props
|
|
160
|
-
continue;
|
|
161
|
-
} else if (k in schemaNode.properties) {
|
|
162
|
-
let result = walk([schemaNode.properties[k]].concat(schemaAncestors),
|
|
163
|
-
// @ts-expect-error TS18046
|
|
164
|
-
dataNode[k], dataPath + '/' + (0, _diagnostic().encodeJSONKeyComponent)(k));
|
|
165
|
-
if (result) results.push(result);
|
|
166
|
-
} else {
|
|
167
|
-
if (typeof additionalProperties === 'boolean') {
|
|
168
|
-
if (!additionalProperties) {
|
|
169
|
-
results.push({
|
|
170
|
-
type: 'enum',
|
|
171
|
-
dataType: 'key',
|
|
172
|
-
dataPath: dataPath + '/' + (0, _diagnostic().encodeJSONKeyComponent)(k),
|
|
173
|
-
expectedValues: Object.keys(schemaNode.properties
|
|
174
|
-
// @ts-expect-error TS18046
|
|
175
|
-
).filter(p => !(p in dataNode)),
|
|
176
|
-
actualValue: k,
|
|
177
|
-
ancestors: schemaAncestors,
|
|
178
|
-
prettyType: schemaNode.__type
|
|
179
|
-
});
|
|
180
|
-
}
|
|
181
|
-
} else {
|
|
182
|
-
let result = walk([additionalProperties].concat(schemaAncestors),
|
|
183
|
-
// @ts-expect-error TS18046
|
|
184
|
-
dataNode[k], dataPath + '/' + (0, _diagnostic().encodeJSONKeyComponent)(k));
|
|
112
|
+
if (typeof dataNode === 'object' && dataNode !== null && !Array.isArray(dataNode)) {
|
|
113
|
+
let results = [];
|
|
114
|
+
let invalidProps;
|
|
115
|
+
if ('__forbiddenProperties' in schemaNode && schemaNode.__forbiddenProperties) {
|
|
116
|
+
let keys = Object.keys(dataNode);
|
|
117
|
+
invalidProps = schemaNode.__forbiddenProperties.filter(val => keys.includes(val));
|
|
118
|
+
results.push(...invalidProps.map(k => ({
|
|
119
|
+
type: 'forbidden-prop',
|
|
120
|
+
dataPath: dataPath + '/' + (0, _diagnostic().encodeJSONKeyComponent)(k),
|
|
121
|
+
dataType: 'key',
|
|
122
|
+
prop: k,
|
|
123
|
+
expectedProps: Object.keys(schemaNode.properties),
|
|
124
|
+
actualProps: keys,
|
|
125
|
+
ancestors: schemaAncestors
|
|
126
|
+
})));
|
|
127
|
+
}
|
|
128
|
+
if ('required' in schemaNode && schemaNode.required) {
|
|
129
|
+
let keys = Object.keys(dataNode);
|
|
130
|
+
let missingKeys = schemaNode.required.filter(val => !keys.includes(val));
|
|
131
|
+
results.push(...missingKeys.map(k => ({
|
|
132
|
+
type: 'missing-prop',
|
|
133
|
+
dataPath,
|
|
134
|
+
dataType: 'value',
|
|
135
|
+
prop: k,
|
|
136
|
+
expectedProps: schemaNode.required,
|
|
137
|
+
actualProps: keys,
|
|
138
|
+
ancestors: schemaAncestors
|
|
139
|
+
})));
|
|
140
|
+
}
|
|
141
|
+
if ('properties' in schemaNode && schemaNode.properties) {
|
|
142
|
+
let {
|
|
143
|
+
additionalProperties = true
|
|
144
|
+
} = schemaNode;
|
|
145
|
+
for (let k in dataNode) {
|
|
146
|
+
if (invalidProps && invalidProps.includes(k)) {
|
|
147
|
+
// Don't check type on forbidden props
|
|
148
|
+
continue;
|
|
149
|
+
} else if (k in schemaNode.properties) {
|
|
150
|
+
let result = walk([schemaNode.properties[k]].concat(schemaAncestors), dataNode[k], dataPath + '/' + (0, _diagnostic().encodeJSONKeyComponent)(k));
|
|
185
151
|
if (result) results.push(result);
|
|
152
|
+
} else {
|
|
153
|
+
if (typeof additionalProperties === 'boolean') {
|
|
154
|
+
if (!additionalProperties) {
|
|
155
|
+
results.push({
|
|
156
|
+
type: 'enum',
|
|
157
|
+
dataType: 'key',
|
|
158
|
+
dataPath: dataPath + '/' + (0, _diagnostic().encodeJSONKeyComponent)(k),
|
|
159
|
+
expectedValues: Object.keys(schemaNode.properties).filter(p => !(p in dataNode)),
|
|
160
|
+
actualValue: k,
|
|
161
|
+
ancestors: schemaAncestors,
|
|
162
|
+
prettyType: schemaNode.__type
|
|
163
|
+
});
|
|
164
|
+
}
|
|
165
|
+
} else {
|
|
166
|
+
let result = walk([additionalProperties].concat(schemaAncestors), dataNode[k], dataPath + '/' + (0, _diagnostic().encodeJSONKeyComponent)(k));
|
|
167
|
+
if (result) results.push(result);
|
|
168
|
+
}
|
|
186
169
|
}
|
|
187
170
|
}
|
|
188
171
|
}
|
|
172
|
+
if (results.length) return results.reduce((acc, v) => acc.concat(v), []);
|
|
189
173
|
}
|
|
190
|
-
if (results.length) return results.reduce((acc, v) => acc.concat(v), []);
|
|
191
174
|
break;
|
|
192
175
|
}
|
|
193
176
|
case 'boolean':
|
|
@@ -198,7 +181,7 @@ function validateSchema(schema, data) {
|
|
|
198
181
|
}
|
|
199
182
|
}
|
|
200
183
|
} else {
|
|
201
|
-
if (schemaNode.enum && !schemaNode.enum.includes(dataNode)) {
|
|
184
|
+
if ('enum' in schemaNode && schemaNode.enum && !schemaNode.enum.includes(dataNode)) {
|
|
202
185
|
return {
|
|
203
186
|
type: 'enum',
|
|
204
187
|
dataType: 'value',
|
|
@@ -208,22 +191,21 @@ function validateSchema(schema, data) {
|
|
|
208
191
|
ancestors: schemaAncestors
|
|
209
192
|
};
|
|
210
193
|
}
|
|
211
|
-
if (
|
|
212
|
-
let list = schemaNode.oneOf
|
|
194
|
+
if ('oneOf' in schemaNode || 'allOf' in schemaNode) {
|
|
195
|
+
let list = 'oneOf' in schemaNode ? schemaNode.oneOf : 'allOf' in schemaNode ? schemaNode.allOf : [];
|
|
213
196
|
let results = [];
|
|
214
197
|
for (let f of list) {
|
|
215
198
|
let result = walk([f].concat(schemaAncestors), dataNode, dataPath);
|
|
216
199
|
if (result) results.push(result);
|
|
217
200
|
}
|
|
218
|
-
if (
|
|
201
|
+
if ('oneOf' in schemaNode ? results.length == schemaNode.oneOf.length : results.length > 0) {
|
|
219
202
|
// return the result with more values / longer key
|
|
220
203
|
results.sort((a, b) => Array.isArray(a) || Array.isArray(b) ? Array.isArray(a) && !Array.isArray(b) ? -1 : !Array.isArray(a) && Array.isArray(b) ? 1 : Array.isArray(a) && Array.isArray(b) ? b.length - a.length : 0 : b.dataPath.length - a.dataPath.length);
|
|
221
204
|
return results[0];
|
|
222
205
|
}
|
|
223
|
-
} else if (schemaNode.not) {
|
|
206
|
+
} else if ('not' in schemaNode && schemaNode.not) {
|
|
224
207
|
let result = walk([schemaNode.not].concat(schemaAncestors), dataNode, dataPath);
|
|
225
|
-
|
|
226
|
-
if (!result || result.length == 0) {
|
|
208
|
+
if (!result || Array.isArray(result) && result.length == 0) {
|
|
227
209
|
return {
|
|
228
210
|
type: 'other',
|
|
229
211
|
dataPath,
|
|
@@ -244,11 +226,8 @@ var _default = exports.default = validateSchema;
|
|
|
244
226
|
function fuzzySearch(expectedValues, actualValue) {
|
|
245
227
|
let result = expectedValues.map(exp => [exp, levenshtein().distance(exp, actualValue)]).filter(
|
|
246
228
|
// Remove if more than half of the string would need to be changed
|
|
247
|
-
// @ts-expect-error TS2769
|
|
248
229
|
([, d]) => d * 2 < actualValue.length);
|
|
249
|
-
// @ts-expect-error TS2345
|
|
250
230
|
result.sort(([, a], [, b]) => a - b);
|
|
251
|
-
// @ts-expect-error TS2345
|
|
252
231
|
return result.map(([v]) => v);
|
|
253
232
|
}
|
|
254
233
|
validateSchema.diagnostic = function (schema, data, origin, message) {
|
|
@@ -268,12 +247,16 @@ validateSchema.diagnostic = function (schema, data, origin, message) {
|
|
|
268
247
|
}
|
|
269
248
|
return loadedSource;
|
|
270
249
|
}
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
data.
|
|
275
|
-
|
|
276
|
-
|
|
250
|
+
let object;
|
|
251
|
+
if ('map' in data && data.map) {
|
|
252
|
+
object = data.map.data;
|
|
253
|
+
} else if ('data' in data && data.data !== undefined) {
|
|
254
|
+
object = data.data;
|
|
255
|
+
} else if ('source' in data && data.source) {
|
|
256
|
+
object = JSON.parse(loadSource(data.source) || '');
|
|
257
|
+
} else {
|
|
258
|
+
throw new Error('Unable to get object from data');
|
|
259
|
+
}
|
|
277
260
|
let errors = validateSchema(schema, object);
|
|
278
261
|
if (errors.length) {
|
|
279
262
|
let keys = errors.map(e => {
|
|
@@ -332,21 +315,23 @@ validateSchema.diagnostic = function (schema, data, origin, message) {
|
|
|
332
315
|
};
|
|
333
316
|
});
|
|
334
317
|
let map, code;
|
|
335
|
-
|
|
336
|
-
if (data.map) {
|
|
337
|
-
// @ts-expect-error TS2339
|
|
318
|
+
if ('map' in data && data.map) {
|
|
338
319
|
map = data.map;
|
|
339
|
-
code = loadSource(data.source);
|
|
320
|
+
code = loadSource(data.source) ?? '';
|
|
340
321
|
} else {
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
322
|
+
if ('source' in data && data.source) {
|
|
323
|
+
map = loadSource(data.source) ?? '';
|
|
324
|
+
} else if ('data' in data && data.data !== undefined) {
|
|
325
|
+
map = JSON.stringify((0, _nullthrows().default)(data.data), null, '\t');
|
|
326
|
+
} else {
|
|
327
|
+
map = '';
|
|
328
|
+
}
|
|
344
329
|
code = map;
|
|
345
330
|
}
|
|
346
331
|
let codeFrames = [{
|
|
347
332
|
filePath: data.filePath ?? undefined,
|
|
348
333
|
language: 'json',
|
|
349
|
-
code,
|
|
334
|
+
code: code ?? '',
|
|
350
335
|
codeHighlights: (0, _diagnostic().generateJSONCodeHighlights)(map, keys.map(({
|
|
351
336
|
key,
|
|
352
337
|
type,
|
|
@@ -361,7 +346,6 @@ validateSchema.diagnostic = function (schema, data, origin, message) {
|
|
|
361
346
|
diagnostic: {
|
|
362
347
|
message: message,
|
|
363
348
|
origin,
|
|
364
|
-
// @ts-expect-error TS2322
|
|
365
349
|
codeFrames
|
|
366
350
|
}
|
|
367
351
|
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@atlaspack/utils",
|
|
3
|
-
"version": "3.1.2
|
|
3
|
+
"version": "3.1.2",
|
|
4
4
|
"description": "Blazing fast, zero configuration web application bundler",
|
|
5
5
|
"license": "(MIT OR Apache-2.0)",
|
|
6
6
|
"publishConfig": {
|
|
@@ -31,19 +31,20 @@
|
|
|
31
31
|
}
|
|
32
32
|
},
|
|
33
33
|
"dependencies": {
|
|
34
|
-
"@atlaspack/codeframe": "2.13.20
|
|
35
|
-
"@atlaspack/diagnostic": "2.14.
|
|
36
|
-
"@atlaspack/feature-flags": "2.26.2
|
|
37
|
-
"@atlaspack/logger": "2.14.30
|
|
38
|
-
"@atlaspack/markdown-ansi": "2.14.
|
|
39
|
-
"@atlaspack/
|
|
40
|
-
"@atlaspack/
|
|
41
|
-
"@iarna/toml": "^2.2.0",
|
|
34
|
+
"@atlaspack/codeframe": "2.13.20",
|
|
35
|
+
"@atlaspack/diagnostic": "2.14.4",
|
|
36
|
+
"@atlaspack/feature-flags": "2.26.2",
|
|
37
|
+
"@atlaspack/logger": "2.14.30",
|
|
38
|
+
"@atlaspack/markdown-ansi": "2.14.4",
|
|
39
|
+
"@atlaspack/types-internal": "2.20.8",
|
|
40
|
+
"@atlaspack/rust": "3.10.0",
|
|
42
41
|
"@parcel/source-map": "^2.1.1",
|
|
43
42
|
"@types/micromatch": "^4.0.9",
|
|
44
43
|
"@types/node-forge": "^1.3.13",
|
|
45
|
-
"ansi-html-community": "0.0.8",
|
|
46
44
|
"chalk": "^4.1.0",
|
|
45
|
+
"nullthrows": "^1.1.1",
|
|
46
|
+
"@iarna/toml": "^2.2.0",
|
|
47
|
+
"ansi-html-community": "0.0.8",
|
|
47
48
|
"clone": "^2.1.1",
|
|
48
49
|
"fast-glob": "^3.2.12",
|
|
49
50
|
"fastest-levenshtein": "^1.0.16",
|
|
@@ -53,14 +54,13 @@
|
|
|
53
54
|
"lru-cache": "^6.0.0",
|
|
54
55
|
"micromatch": "^4.0.4",
|
|
55
56
|
"node-forge": "^1.2.1",
|
|
56
|
-
"nullthrows": "^1.1.1",
|
|
57
57
|
"open": "^7.0.3",
|
|
58
58
|
"snarkdown": "^2.0.0",
|
|
59
59
|
"strip-ansi": "^6.0.0",
|
|
60
60
|
"terminal-link": "^2.1.1"
|
|
61
61
|
},
|
|
62
62
|
"devDependencies": {
|
|
63
|
-
"@atlaspack/babel-register": "2.14.
|
|
63
|
+
"@atlaspack/babel-register": "2.14.4",
|
|
64
64
|
"@types/is-glob": "^4.0.1",
|
|
65
65
|
"benny": "^3.7.1",
|
|
66
66
|
"random-int": "^1.0.0"
|
|
@@ -74,6 +74,5 @@
|
|
|
74
74
|
"type": "commonjs",
|
|
75
75
|
"scripts": {
|
|
76
76
|
"build:lib": "gulp build --gulpfile ../../../gulpfile.js --cwd ."
|
|
77
|
-
}
|
|
78
|
-
"gitHead": "e5f8a173505611c1fafafd6e7dddb2f6b483f67c"
|
|
77
|
+
}
|
|
79
78
|
}
|