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

feat: add method to get validation model and it children deeply #1173

Open
wants to merge 2 commits into
base: next
Choose a base branch
from

Conversation

mateusmcordeiro
Copy link

@mateusmcordeiro mateusmcordeiro commented Mar 29, 2023

Summary

I use to use this library on my vue applications, but i aways fell its missing a method to get all the $model values from all validations and nested children included so i decided to implement it by myself.

Metadata

What kind of change does this PR introduce? (check at least one)

  • Bugfix
  • Feature
  • Code style update
  • Refactor
  • Build-related changes
  • Other, please describe:

Does this PR introduce a breaking change? (check one)

  • Yes
  • No

If yes, please describe the impact and migration path for existing applications:

The PR fulfills these requirements:

If adding a new feature, the PR's description includes:

  • A convincing reason for adding this feature (to avoid wasting your time, it's best to open a suggestion issue first and wait for approval before working on it)

Other information:

@mateusmcordeiro mateusmcordeiro changed the title add method to get and children deeply feat: add method to get and children deeply Mar 29, 2023
@dobromir-hristov
Copy link
Contributor

Could you please revert all the unnecessary changes, apart from the actual method to get the models? Your ide probably did those automatically which is unnecessary.

@mateusmcordeiro mateusmcordeiro changed the title feat: add method to get and children deeply feat: add method to get validation model and it children`s deeply Mar 29, 2023
@mateusmcordeiro mateusmcordeiro changed the title feat: add method to get validation model and it children`s deeply feat: add method to get validation model and it children deeply Mar 29, 2023
@mateusmcordeiro
Copy link
Author

@dobromir-hristov done.

Copy link
Contributor

@dobromir-hristov dobromir-hristov left a comment

Choose a reason for hiding this comment

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

Hey thanks for the cleanup. We should add some docs and probably a test or two, apart from the comments I left.

Comment on lines +227 to +231
* @param {Object} iModel - Internal $model to retrieve the $model for each layer interaction.
* @param {Object} iChildResults - Internal ChildResults from current layer interaction.
* @param {Object} iNestedResults - Internal NestedResults from current layer interaction.
* @returns {Object} - A deep object containing the values of the model and its child and nested results.
*/
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
* @param {Object} iModel - Internal $model to retrieve the $model for each layer interaction.
* @param {Object} iChildResults - Internal ChildResults from current layer interaction.
* @param {Object} iNestedResults - Internal NestedResults from current layer interaction.
* @returns {Object} - A deep object containing the values of the model and its child and nested results.
*/
* @param {Object} model - Internal $model to retrieve the $model for each layer interaction.
* @param {Object} childResults - Internal ChildResults from current layer interaction.
* @param {Object} nestedResults - Internal NestedResults from current layer interaction.
* @returns {Object} - A deep object containing the values of the model and its child and nested results.
*/

const unwrappedModel = unwrap(model)

const treatedModel =
['object', 'array'].includes(typeof unwrappedModel) && !!unwrappedModel
Copy link
Contributor

Choose a reason for hiding this comment

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

if the typeof is object or array, !! will always be true. I think that second check is not necessary.


const treatedModel =
['object', 'array'].includes(typeof unwrappedModel) && !!unwrappedModel
? { value: unwrappedModel }
Copy link
Contributor

Choose a reason for hiding this comment

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

Is the value just to mimic a ref or computed?

Comment on lines +242 to +248
for (const [key, value] of Object.entries(childResults ?? {})) {
result[key] = value?.$values?.()
}

for (const [key, value] of Object.entries(nestedResults ?? {})) {
result[key] = value?.$values?.()
}
Copy link
Contributor

Choose a reason for hiding this comment

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

You could merge the two and iterate at once? 🤔

Also why are you iterating over an object that you just built, I dont get that.... cant we just iterate over the childResults?

Comment on lines +250 to +253
return Object.keys(result).length > 0
? { ...treatedModel, ...result }
: treatedModel
}
Copy link
Contributor

Choose a reason for hiding this comment

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

do we need this check at all? What if we just spread them always?

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.

None yet

2 participants