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

onValidate does not work with arrays #93

Open
lroal opened this issue Feb 2, 2022 · 3 comments
Open

onValidate does not work with arrays #93

lroal opened this issue Feb 2, 2022 · 3 comments
Assignees
Labels

Comments

@lroal
Copy link

lroal commented Feb 2, 2022

import onChange from 'on-change';

const object = [];

let p = onChange(object, change, { pathAsArray: true, onValidate });

function change() {
    console.log('change');
}

function onValidate(path) {
    console.log('path:')
    console.log(path);
    console.log('object')
    console.log(object)
    return false;
}

p.push(1);
console.log('final')
console.log(p);

output:

path:
[]
object
[ 1 ]
final
[ 1 ]

The change seems to be already done when onValidate is called.

@lroal
Copy link
Author

lroal commented Feb 8, 2022

Has anyone had the chance to look at the issue ?

@DarrenPaulWright
Copy link
Collaborator

By default onChange wraps up the changes in one method call to one change on the item that the method was called, the same applies to onValidate. That is why you see an empty path with the pushed item there. To bypass this and get callbacks for every change within a method call, use the "details" option.

That said, returning false in the onValidate function should have reverted the change. I'll look into it.

@lroal
Copy link
Author

lroal commented May 15, 2022

Have you had any chance to look at it ? I assume this part of the code is a bit complex.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants