Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions packages/core/src/util/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@ import { isOneOfEnumSchema } from './schema';
import filter from 'lodash/filter';
import isEqual from 'lodash/isEqual';

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

const invalidProperty = getInvalidProperty(error);
if (invalidProperty !== undefined && !controlPath.endsWith(invalidProperty)) {
if (invalidProperty !== undefined) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you know why we originally added the endsWith check? There must have been a reason

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not completely sure. Three reasons come to mind:

  • AJV calculated error paths differently in the past
  • This was a precaution that was never actually necessary
  • We handled more cases in getInvalidProperty explicitly in the past. However, the current three cases all need the invalidProperty attached anyway because they point to the parent object. See code snipped below
const getInvalidProperty = (error: ErrorObject): string | undefined => {
  switch (error.keyword) {
    case 'required':
    case 'dependencies':
      return error.params.missingProperty;
    case 'additionalProperties':
      return error.params.additionalProperty;
    default:
      return undefined;
  }
};

controlPath = `${controlPath}.${invalidProperty}`;
}

// remove '.' chars at the beginning of paths
controlPath = controlPath.replace(/^./, '');
controlPath = controlPath.replace(/^\./, '');

// decode JSON Pointer escape sequences
controlPath = decode(controlPath);
Expand Down
Loading
Loading