Skip to content

Commit

Permalink
fix: changed field ownership check when mixing
Browse files Browse the repository at this point in the history
  • Loading branch information
Jenesius committed Sep 22, 2023
1 parent dbce50d commit a14602e
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 8 deletions.
11 changes: 8 additions & 3 deletions src/classes/Form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -323,9 +323,14 @@ export default class Form extends EventEmitter implements FormDependence {
.forEach(item => {

// Ранее было установлено простое поле
const isAbolish = abolishNames.find(name => (new RegExp(`^${name}\..*`)).test(item.name));

if (isAbolish) return;
// const isAbolish = abolishNames.find(name => (new RegExp(`^${name}\..*`)).test(item.name));
const isAbolish = abolishNames.find(name => isPrefixName(item.name, name));

if (isAbolish) {
debug.msg(`Field is %cskipped%c %c${item.name}%c because its child field was previously set.`,
debug.colorError, debug.colorDefault, debug.colorName, debug.colorDefault);
return;
}

// Если текущее значение - примитивное, а предыдущее нет - необходимо пометить данное поле как конечное, то
// есть все дальнейшие(внутренние поля) - является упразднёнными и их не нужно проецировать на форму.
Expand Down
23 changes: 18 additions & 5 deletions src/utils/is-prefix-name.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,22 @@
/**
* @description Return true if the prefix is some parent of provided field name.
* @example address.city.name address -> true
* @example user.type.index user.type -> true
* @example position.city.type city -> false
* @example name name -> false
* @description Return true if the prefix is some parent of provided field name. In other words, it checks that the
* second parameter is the parent of the first.
*
* @example
* // returns true
* isPrefixName("address.city.name", "address");
* @example
* // return true
* isPrefixName("user.type.index", "user.type")
* @example
* // returns false
* isPrefixName("position.city.type", "city");
* @example
* // return false
* isPrefixName("name", "name");
*
* @param fieldName Check Field
* @param prefix Parent Field or Prefix value
* */
export default function isPrefixName(fieldName: string, prefix: string) {
return (new RegExp(`^${prefix}\\.`)).test(fieldName);
Expand Down
22 changes: 22 additions & 0 deletions tests/units/form/form-set-values.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -515,5 +515,27 @@ describe("Form.setValues", () => {
expect(form.changes).toEqual({})
expect(form.values).toEqual({ username: "Jack" })
})
/**
* При установке поля CoolName, поле Cool не должно как-то влиять на поле CoolName.
*/
test("Short name should not has affect for field, that don't include first name like prefix", () => {
const form = new Form();
form.setValues({
Id: 13,
IdentityDocument: {
Name: "Test",
Type: "P"
}
})

expect(form.values).toEqual({
Id: 13,
IdentityDocument: {
Name: "Test",
Type: "P"
}
})

})

})

0 comments on commit a14602e

Please sign in to comment.