Skip to content

Commit 7e3f68e

Browse files
committed
fix(core): Fix validation errors for nested props of same name
- Remove obsolete safeguard against adding a property name a second time to a validation error's control path. - Add comment in errors.ts to clarify behavior - Add unit tests for getControlPath and errorAt - Add example for nested required property of same name fixes #2521
1 parent e642b3d commit 7e3f68e

File tree

4 files changed

+511
-2
lines changed

4 files changed

+511
-2
lines changed

packages/core/src/util/errors.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,15 @@ import { isOneOfEnumSchema } from './schema';
3030
import filter from 'lodash/filter';
3131
import isEqual from 'lodash/isEqual';
3232

33+
/**
34+
* Checks for an additionally specified property that the error relates to.
35+
* This may be added to an error's instancePath to show it add the violating property's control.
36+
* For example, for required property errors, the instancePath points to the object containing the required property.
37+
* The missing property's name is specified in the error's params.missingProperty field and returned by this function.
38+
*
39+
* @param error The ErrorObject to check for an additionally specified property that the error relates to
40+
* @returns The invalid property name if present, otherwise undefined
41+
*/
3342
const getInvalidProperty = (error: ErrorObject): string | undefined => {
3443
switch (error.keyword) {
3544
case 'required':
@@ -51,12 +60,12 @@ export const getControlPath = (error: ErrorObject) => {
5160
controlPath = controlPath.replace(/\//g, '.');
5261

5362
const invalidProperty = getInvalidProperty(error);
54-
if (invalidProperty !== undefined && !controlPath.endsWith(invalidProperty)) {
63+
if (invalidProperty !== undefined) {
5564
controlPath = `${controlPath}.${invalidProperty}`;
5665
}
5766

5867
// remove '.' chars at the beginning of paths
59-
controlPath = controlPath.replace(/^./, '');
68+
controlPath = controlPath.replace(/^\./, '');
6069

6170
// decode JSON Pointer escape sequences
6271
controlPath = decode(controlPath);

0 commit comments

Comments
 (0)