adopted-ember-addons / validated-changeset

Buffering changes to form data
MIT License
36 stars 27 forks source link

Cannot get property in an array of objects #178

Open AmauryD opened 2 years ago

AmauryD commented 2 years ago

Hello,

I'm trying to get a property in an array of objects. It seems that the changes are not retrieved properly with .get(). Sadly, I don't know enough about this library to identify the problem.

Also, i didn't see examples of changeset with an array of objects in the documentation. Is it recommended ?

I made a simple reproduction test that explains the problem.

 describe('arrays of objects', () => {
    test('changes in array are gettable', () => {
      let initialData = { contacts: [] };

      const changeset = Changeset(initialData);

      changeset.set("contacts", [{
        name :'william'
      }]);

// returns undefined
      expect(changeset.get("contacts.0")).toStrictEqual({
        name :'william'
      });
      expect(changeset.get("contacts.0.name")).toStrictEqual(
        'william'
      );
    });
  });

node version : 14.19.1 validated-changeset: 1.3.3

Thank you in advance.

AmauryD commented 2 years ago

I dug a bit in the code, by changing this line it seems to work and the tests still passes. It is just a quickfix btw.

https://github.com/validated-changeset/validated-changeset/blob/132b516328a7faa9a243f4e81e28f3bd81138115/src/index.ts#L1019

this.isObject(normalizedBaseChanges) || Array.isArray(normalizedBaseChanges)

The fix is in my fork (https://github.com/AmauryD/validated-changeset)

snewcomer commented 2 years ago

@AmauryD Would you like putting up a PR with your changes?

AmauryD commented 2 years ago

@AmauryD Would you like putting up a PR with your changes?

It is done in #179 ! 😃