adapt-schemas 2.0.0 → 2.1.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/lib/Schema.js +10 -10
- package/package.json +1 -1
package/lib/Schema.js
CHANGED
|
@@ -302,10 +302,10 @@ class Schema extends EventEmitter {
|
|
|
302
302
|
const errors = compiled.errors && compiled.errors
|
|
303
303
|
.filter(e => e.keyword === 'required' ? !opts.ignoreRequired : true)
|
|
304
304
|
.map(e => e.instancePath ? `${e.instancePath} ${e.message}` : e.message)
|
|
305
|
-
.
|
|
305
|
+
.join(', ')
|
|
306
306
|
|
|
307
307
|
if (errors?.length) {
|
|
308
|
-
throw new SchemaError('VALIDATION_FAILED', `Validation failed for schema: ${this.name}`, {
|
|
308
|
+
throw new SchemaError('VALIDATION_FAILED', `Validation failed for schema: ${this.name} with errors: ${errors}`, {
|
|
309
309
|
schemaName: this.name,
|
|
310
310
|
errors,
|
|
311
311
|
data
|
|
@@ -379,22 +379,22 @@ class Schema extends EventEmitter {
|
|
|
379
379
|
* @param {Function} predicate - `(schemaField) => boolean`
|
|
380
380
|
* @param {Object} [schema] - Internal: schema properties to walk (defaults to built.properties)
|
|
381
381
|
* @param {string} [parentPath=''] - Internal: the slash-delimited path accumulated so far
|
|
382
|
-
* @returns {Array<Object>} Array of `{ path, key, data, value }` matches
|
|
382
|
+
* @returns {Array<Object>} Array of `{ path, key, data, value, schemaField }` matches
|
|
383
383
|
*/
|
|
384
384
|
walk (data, predicate, schema, parentPath = '') {
|
|
385
385
|
schema = schema ?? this.built.properties
|
|
386
386
|
const matches = []
|
|
387
|
-
for (const [key,
|
|
387
|
+
for (const [key, schemaField] of Object.entries(schema)) {
|
|
388
388
|
if (data[key] === undefined) continue
|
|
389
389
|
const currentPath = parentPath ? `${parentPath}/${key}` : key
|
|
390
|
-
if (
|
|
391
|
-
matches.push(...this.walk(data[key], predicate,
|
|
392
|
-
} else if (
|
|
390
|
+
if (schemaField.properties) {
|
|
391
|
+
matches.push(...this.walk(data[key], predicate, schemaField.properties, currentPath))
|
|
392
|
+
} else if (schemaField?.items?.properties) {
|
|
393
393
|
data[key].forEach((item, i) => {
|
|
394
|
-
matches.push(...this.walk(item, predicate,
|
|
394
|
+
matches.push(...this.walk(item, predicate, schemaField.items.properties, `${currentPath}/${i}`))
|
|
395
395
|
})
|
|
396
|
-
} else if (predicate(
|
|
397
|
-
matches.push({ path: currentPath, key, data, value: data[key] })
|
|
396
|
+
} else if (predicate(schemaField)) {
|
|
397
|
+
matches.push({ path: currentPath, key, data, value: data[key], schemaField })
|
|
398
398
|
}
|
|
399
399
|
}
|
|
400
400
|
return matches
|