Skip to content

Commit 95271f6

Browse files
committed
Add function isObjectReasonablyEqual
1 parent b1a4a60 commit 95271f6

File tree

1 file changed

+21
-4
lines changed

1 file changed

+21
-4
lines changed

lib/src/schema.js

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,18 @@ import {
2121
hasNoValues,
2222
} from './utils';
2323

24+
function isObjectReasonablyEqual(foo, bar) {
25+
const fooKeys = new Set(Object.getOwnPropertySymbols(foo).map((key, value) => isDefined(value) ? key : undefined).filter(isDefined));
26+
const barKeys = new Set(Object.getOwnPropertySymbols(bar).map((key, value) => isDefined(value) ? key : undefined).filter(isDefined));
27+
if (fooKeys.count !== barKeys.count) {
28+
return false;
29+
}
30+
if ([...fooKeys].some((fooKey) => !barKeys.has(fooKey))) {
31+
return false;
32+
}
33+
return [...fooKeys].every((fooKey) => foo[fooKey] === bar[fooKey]);
34+
}
35+
2436
export function accumulateValues(
2537
obj,
2638
schema,
@@ -76,7 +88,7 @@ export function accumulateValues(
7688
}
7789
return schema.defaultValue;
7890
}
79-
// FIXME: should we instead use (return nullable ? null : undefined)?
91+
// TODO: should we instead use (return nullable ? null : undefined)?
8092
return [];
8193
}
8294
return values;
@@ -216,7 +228,6 @@ export function accumulateErrors(
216228
return error;
217229
}
218230

219-
// FIXME: only copy oldErrors when a change is detected
220231
export function accumulateDifferentialErrors(
221232
oldObj,
222233
newObj,
@@ -246,7 +257,7 @@ export function accumulateDifferentialErrors(
246257
const isSchemaForObject = !!fields;
247258

248259
if (isSchemaForArray) {
249-
const errors = {};
260+
let errors = {};
250261
// FIXME: make this more performant? deps?
251262
if (validation) {
252263
const validationErrors = validation(newObj, baseValue, context);
@@ -296,6 +307,9 @@ export function accumulateDifferentialErrors(
296307
}
297308
});
298309

310+
if (isObjectReasonablyEqual(oldError, errors)) {
311+
return oldError;
312+
}
299313
return hasNoKeys(errors) ? undefined : errors;
300314
}
301315

@@ -341,6 +355,9 @@ export function accumulateDifferentialErrors(
341355
}
342356
});
343357

358+
if (isObjectReasonablyEqual(oldError, errors)) {
359+
return oldError;
360+
}
344361
return hasNoKeys(errors) ? undefined : errors;
345362
}
346363

@@ -397,7 +414,7 @@ export function analyzeErrors(errors) {
397414
}
398415

399416
// FIXME: move this to a helper
400-
// FIXME: check conditions for change in context and baseValue
417+
// TODO: check conditions for change in context and baseValue
401418
export function addCondition(
402419
schema,
403420
value,

0 commit comments

Comments
 (0)