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

fix: respect non-POJO objects in setValues #4289

Merged
merged 2 commits into from
May 25, 2023

Conversation

VladStepanov
Copy link
Contributor

πŸ”Ž Overview

Fixes #4287

πŸ€“ Code snippets/examples (if applicable)

βœ” Issues affected
closes #4287

Copy link
Owner

@logaretm logaretm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very good stuff, I have a couple of nitpicks but otherwise looks good.

It would be nice if we could cover the uncovered lines in tests.


export function isPlainObject(value: any) {
// eslint-disable-next-line eqeqeq
if (!isObjectLike(value) || getTag(value) != '[object Object]') {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any specific reason why we use != instead of !==. I would prefer we use !== instead of silencing the eslint rule.

Copy link
Contributor Author

@VladStepanov VladStepanov May 25, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe to install lodash and simply reuse _.isPlainObject https://lodash.com/docs/4.17.15#isPlainObject?

Because otherwise we will have to support and cover with tests in our codebase what is already covered in lodash

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I usually don't like including libs inside vee-validate due to bundle size. If it isn't far off (using Lodash.isPlainObject vs building our own) bundle-size wise (you can test it with pnpm build), Then happy to include it.

Copy link
Owner

@logaretm logaretm May 25, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another option is to use deepmerge which is around 723B only and avoid having our own merge logic. But let's try out lodash-es first and see if it doesn't add much to the bundle size.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've tested bundle size and have following result:
Main branch size:
This is a base size

Output File: vee-validate.esm.js | Size: 123.59 kB | Gzip: 25.51 kB
Output File: vee-validate.js | Size: 123.07 kB | Gzip: 23.16 kB
Output File: vee-validate.min.js | Size: 40.04 kB | Gzip: 11.96 kB

Case 1. Without lodash deps, only copy-paste sources

Output File: vee-validate.esm.js | Size: 124.29 kB | Gzip: 25.65 kB
Output File: vee-validate.js | Size: 123.87 kB | Gzip: 23.3 kB
Output File: vee-validate.min.js | Size: 40.4 kB | Gzip: 12.04 kB

Case 2. With lodash deps

Output File: vee-validate.esm.js | Size: 129.37 kB | Gzip: 26.97 kB
Output File: vee-validate.js | Size: 119.8 kB | Gzip: 24.25 kB
Output File: vee-validate.min.js | Size: 41.21 kB | Gzip: 12.25 kB

Case 3. deepmerge + is-plain-object
By default, deepmerge doesn't have isPlainObject guard, so they recommend install additional deps https://github.com/TehShrike/deepmerge#ismergeableobject
I suppose it's the worst one

Output File: vee-validate.esm.js | Size: 128.54 kB | Gzip: 26.9 kB
Output File: vee-validate.js | Size: 118.93 kB | Gzip: 24.19 kB
Output File: vee-validate.min.js | Size: 41.88 kB | Gzip: 12.52 kB

What case may i implement?

Copy link
Contributor Author

@VladStepanov VladStepanov May 25, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And one more case with lodash-es

Output File: vee-validate.esm.js | Size: 128.95 kB | Gzip: 26.89 kB
Output File: vee-validate.js | Size: 122.63 kB | Gzip: 24.28 kB
Output File: vee-validate.min.js | Size: 41.05 kB | Gzip: 12.24 kB

Copy link
Owner

@logaretm logaretm May 25, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's go with case 1, smallest is always the best. Thank you and I appreciate your patience with this PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Implemented and added some tests for coverage βœ…

Comment on lines 30 to 37
export function getTag(value: any) {
if (value == null) {
return value === undefined ? '[object Undefined]' : '[object Null]';
}
return Object.prototype.toString.call(value);
}

export function isPlainObject(value: any) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's add a link for where this is pulled from, like lodash's src code link. Just to be a reference.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added βœ…

@logaretm logaretm merged commit 0534f5c into logaretm:main May 25, 2023
@logaretm
Copy link
Owner

Amazing work, thank you a lot for reporting and owning the fix for this issue. I will tag with the next patch this evening or tomorrow.

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

Successfully merging this pull request may close these issues.

setValues doesn't respect non plain objects
2 participants