Skip to content

Commit ae540f6

Browse files
committed
correcting logic
1 parent 0b7ed5d commit ae540f6

File tree

3 files changed

+39
-16
lines changed

3 files changed

+39
-16
lines changed

src/Umbraco.Web.UI.Client/src/packages/core/validation/controllers/validation.controller.ts

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,7 @@ import type { UmbContextProviderController } from '@umbraco-cms/backoffice/conte
77
import { type UmbClassInterface, UmbControllerBase } from '@umbraco-cms/backoffice/class-api';
88
import type { UmbControllerHost } from '@umbraco-cms/backoffice/controller-api';
99
import { UmbObjectState } from '@umbraco-cms/backoffice/observable-api';
10-
11-
/**
12-
* Helper method to replace the start of a string with another string.
13-
* @param path {string}
14-
* @param startFrom {string}
15-
* @param startTo {string}
16-
* @returns {string}
17-
*/
18-
function ReplaceStartOfString(path: string, startFrom: string, startTo: string): string {
19-
if (path.startsWith(startFrom + '.')) {
20-
return startTo + path.slice(startFrom.length);
21-
}
22-
return path;
23-
}
10+
import { ReplaceStartOfPath } from '../utils/replace-start-of-path.function.js';
2411

2512
/**
2613
* Validation Context is the core of Validation.
@@ -143,7 +130,7 @@ export class UmbValidationController extends UmbControllerBase implements UmbVal
143130
}
144131
this.#parentMessages = msgs;
145132
msgs.forEach((msg) => {
146-
const path = ReplaceStartOfString(msg.path, this.#baseDataPath!, '$');
133+
const path = ReplaceStartOfPath(msg.path, this.#baseDataPath!, '$');
147134
// Notice, the local message uses the same key. [NL]
148135
this.messages.addMessage(msg.type, path, msg.body, msg.key);
149136
});
@@ -164,7 +151,7 @@ export class UmbValidationController extends UmbControllerBase implements UmbVal
164151
this.#localMessages = msgs;
165152
msgs.forEach((msg) => {
166153
// replace this.#baseDataPath (if it starts with it) with $ in the path, so it becomes relative to the parent context
167-
const path = ReplaceStartOfString(msg.path, '$', this.#baseDataPath!);
154+
const path = ReplaceStartOfPath(msg.path, '$', this.#baseDataPath!);
168155
// Notice, the parent message uses the same key. [NL]
169156
this.#parent!.messages.addMessage(msg.type, path, msg.body, msg.key);
170157
});
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
* Helper method to replace the start of a JSON Path with another JSON Path.
3+
* @param path {string}
4+
* @param startFrom {string}
5+
* @param startTo {string}
6+
* @returns {string}
7+
*/
8+
export function ReplaceStartOfPath(path: string, startFrom: string, startTo: string): string {
9+
// if the path conitnues with a . or [ aftr startFrom, then replace it with startTo, otherwise if identical then it is also a match. [NL]
10+
if (path.startsWith(startFrom + '.') || path === startFrom) {
11+
return startTo + path.slice(startFrom.length);
12+
}
13+
return path;
14+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { expect } from '@open-wc/testing';
2+
import { ReplaceStartOfPath } from './replace-start-of-path.function.js';
3+
4+
describe('ReplaceStartOfPath', () => {
5+
it('replaces a dot path', () => {
6+
const result = ReplaceStartOfPath('$.start.test', '$.start', '$');
7+
8+
expect(result).to.eq('$.test');
9+
});
10+
11+
it('replaces a array path', () => {
12+
const result = ReplaceStartOfPath('$.start[0].test', '$.start[0]', '$');
13+
14+
expect(result).to.eq('$.test');
15+
});
16+
17+
it('replaces a exact path', () => {
18+
const result = ReplaceStartOfPath('$.start.test', '$.start.test', '$');
19+
20+
expect(result).to.eq('$');
21+
});
22+
});

0 commit comments

Comments
 (0)