Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cast seems to remove multi level properties like address.country, even if they are included in the array passed to the cast function #667

Open
andrew-paterson opened this issue Apr 14, 2023 · 0 comments

Comments

@andrew-paterson
Copy link

andrew-paterson commented Apr 14, 2023

Version

ember-changeset 4.1.1
ember-source 3.28

Test Case

Reduced test case here

Steps to reproduce

If running the app in the reduced test case above, simply click the button "Test" on the landing page of the above app and look at the console output.

Doing so runs this action, closely based on the docs for cast, so you could also just copy and paste this code into a component.

export default class TestComponent extends Component {
  @action
  test() {
    let user = { age: 21, address: { zipCode: '10001' } };
    let changeset = Changeset(user);
    let allowed = ['name', 'address.country'];
    console.log(`changeset.set('name', 'Jim Bob');`);
    changeset.set('name', 'Jim Bob');
    console.log(`changeset.set('address.country', 'United States');`);
    changeset.set('address.country', 'United States');
    console.log(`changeset.set('unwantedProp', 'foo');`);
    changeset.set('unwantedProp', 'foo');
    console.log(`changeset.set('address.unwantedProp', 123);`);
    changeset.set('address.unwantedProp', 123);
    console.log(`changeset.get('name // ${changeset.get('name')}`);
    console.log(`changeset.get('address.country // ${changeset.get('address.country')}`);
    console.log(`changeset.get('unwantedProp')); // ${changeset.get('unwantedProp')}`);
    console.log(`changeset.get('address.unwantedProp // ${changeset.get('address.unwantedProp')}`);
    changeset.cast(allowed); // returns changeset
    console.log(`>>>> changeset.cast([${allowed}])`);
    console.log(`changeset.get('name // ${changeset.get('name')}`);
    console.log(`changeset.get('address.country // ${changeset.get('address.country')}`);
    console.log(`changeset.get('unwantedProp')); // ${changeset.get('unwantedProp')}`);
    console.log(`changeset.get('another.unwantedProp // ${changeset.get('another.unwantedProp')}`);
    console.log(changeset.get('another.unwantedProp')); // undefined
  }
}

Expected Behavior

Logging changeset.get('address.country') after castshould beUnited States`.

changeset.set('name', 'Jim Bob');
changeset.set('address.country', 'United States');
changeset.set('unwantedProp', 'foo');
changeset.set('address.unwantedProp', 123);
changeset.get('name // Jim Bob
changeset.get('address.country // United States
changeset.get('unwantedProp')); // foo
changeset.get('address.unwantedProp // 123
>>>> changeset.cast([name,address.country])
changeset.get('name // Jim Bob
changeset.get('address.country // United States
changeset.get('unwantedProp')); // undefined
changeset.get('another.unwantedProp // undefined

Actual Behavior

Logging changeset.get('address.country') after castis not undefined, where it should beUnited States. Note that before castit logs asUnited States. Note also that the same issue does not occur for name`.

changeset.set('name', 'Jim Bob');
changeset.set('address.country', 'United States');
changeset.set('unwantedProp', 'foo');
changeset.set('address.unwantedProp', 123);
changeset.get('name // Jim Bob
changeset.get('address.country // United States
changeset.get('unwantedProp')); // foo
changeset.get('address.unwantedProp // 123
>>>> changeset.cast([name,address.country])
changeset.get('name // Jim Bob
changeset.get('address.country // undefined
changeset.get('unwantedProp')); // undefined
changeset.get('another.unwantedProp // undefined
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant