From ae540f66d3eafe94f69da44df1aeca98c97fd8c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Niels=20Lyngs=C3=B8?= Date: Fri, 31 Jan 2025 21:06:07 +0100 Subject: [PATCH] correcting logic --- .../controllers/validation.controller.ts | 19 +++------------- .../utils/replace-start-of-path.function.ts | 14 ++++++++++++ .../utils/replace-start-of-path.test.ts | 22 +++++++++++++++++++ 3 files changed, 39 insertions(+), 16 deletions(-) create mode 100644 src/Umbraco.Web.UI.Client/src/packages/core/validation/utils/replace-start-of-path.function.ts create mode 100644 src/Umbraco.Web.UI.Client/src/packages/core/validation/utils/replace-start-of-path.test.ts diff --git a/src/Umbraco.Web.UI.Client/src/packages/core/validation/controllers/validation.controller.ts b/src/Umbraco.Web.UI.Client/src/packages/core/validation/controllers/validation.controller.ts index 49715f1fe6..ce7e4cbcea 100644 --- a/src/Umbraco.Web.UI.Client/src/packages/core/validation/controllers/validation.controller.ts +++ b/src/Umbraco.Web.UI.Client/src/packages/core/validation/controllers/validation.controller.ts @@ -7,20 +7,7 @@ import type { UmbContextProviderController } from '@umbraco-cms/backoffice/conte import { type UmbClassInterface, UmbControllerBase } from '@umbraco-cms/backoffice/class-api'; import type { UmbControllerHost } from '@umbraco-cms/backoffice/controller-api'; import { UmbObjectState } from '@umbraco-cms/backoffice/observable-api'; - -/** - * Helper method to replace the start of a string with another string. - * @param path {string} - * @param startFrom {string} - * @param startTo {string} - * @returns {string} - */ -function ReplaceStartOfString(path: string, startFrom: string, startTo: string): string { - if (path.startsWith(startFrom + '.')) { - return startTo + path.slice(startFrom.length); - } - return path; -} +import { ReplaceStartOfPath } from '../utils/replace-start-of-path.function.js'; /** * Validation Context is the core of Validation. @@ -143,7 +130,7 @@ export class UmbValidationController extends UmbControllerBase implements UmbVal } this.#parentMessages = msgs; msgs.forEach((msg) => { - const path = ReplaceStartOfString(msg.path, this.#baseDataPath!, '$'); + const path = ReplaceStartOfPath(msg.path, this.#baseDataPath!, '$'); // Notice, the local message uses the same key. [NL] this.messages.addMessage(msg.type, path, msg.body, msg.key); }); @@ -164,7 +151,7 @@ export class UmbValidationController extends UmbControllerBase implements UmbVal this.#localMessages = msgs; msgs.forEach((msg) => { // replace this.#baseDataPath (if it starts with it) with $ in the path, so it becomes relative to the parent context - const path = ReplaceStartOfString(msg.path, '$', this.#baseDataPath!); + const path = ReplaceStartOfPath(msg.path, '$', this.#baseDataPath!); // Notice, the parent message uses the same key. [NL] this.#parent!.messages.addMessage(msg.type, path, msg.body, msg.key); }); diff --git a/src/Umbraco.Web.UI.Client/src/packages/core/validation/utils/replace-start-of-path.function.ts b/src/Umbraco.Web.UI.Client/src/packages/core/validation/utils/replace-start-of-path.function.ts new file mode 100644 index 0000000000..8e34feeaf4 --- /dev/null +++ b/src/Umbraco.Web.UI.Client/src/packages/core/validation/utils/replace-start-of-path.function.ts @@ -0,0 +1,14 @@ +/** + * Helper method to replace the start of a JSON Path with another JSON Path. + * @param path {string} + * @param startFrom {string} + * @param startTo {string} + * @returns {string} + */ +export function ReplaceStartOfPath(path: string, startFrom: string, startTo: string): string { + // if the path conitnues with a . or [ aftr startFrom, then replace it with startTo, otherwise if identical then it is also a match. [NL] + if (path.startsWith(startFrom + '.') || path === startFrom) { + return startTo + path.slice(startFrom.length); + } + return path; +} diff --git a/src/Umbraco.Web.UI.Client/src/packages/core/validation/utils/replace-start-of-path.test.ts b/src/Umbraco.Web.UI.Client/src/packages/core/validation/utils/replace-start-of-path.test.ts new file mode 100644 index 0000000000..7b90efc2cc --- /dev/null +++ b/src/Umbraco.Web.UI.Client/src/packages/core/validation/utils/replace-start-of-path.test.ts @@ -0,0 +1,22 @@ +import { expect } from '@open-wc/testing'; +import { ReplaceStartOfPath } from './replace-start-of-path.function.js'; + +describe('ReplaceStartOfPath', () => { + it('replaces a dot path', () => { + const result = ReplaceStartOfPath('$.start.test', '$.start', '$'); + + expect(result).to.eq('$.test'); + }); + + it('replaces a array path', () => { + const result = ReplaceStartOfPath('$.start[0].test', '$.start[0]', '$'); + + expect(result).to.eq('$.test'); + }); + + it('replaces a exact path', () => { + const result = ReplaceStartOfPath('$.start.test', '$.start.test', '$'); + + expect(result).to.eq('$'); + }); +});