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: new hook useValidator #101

Merged
merged 3 commits into from
Jun 2, 2021
Merged

feat: new hook useValidator #101

merged 3 commits into from
Jun 2, 2021

Conversation

xobotyi
Copy link
Contributor

@xobotyi xobotyi commented Jun 1, 2021

What new hook does?

Performs validation when any of provided dependencies has changed.

Checklist

  • Have you read contribution guideline?
  • Does the code have comments in hard-to-understand areas?
  • Is there an existing issue for this PR?
  • Have the files been linted and formatted?
  • Have the docs been updated?
  • Have the tests been added to cover new hook?
  • Have you run the tests locally to confirm they pass?

xobotyi added 2 commits June 1, 2021 12:32
Previous version returned wrapped function that has no name and 0
length, meaning that they do not expect arguments.

Current version sets length to same value as original function.
Performs validation when any of provided dependencies has changed.
@xobotyi xobotyi added the 🎂 new hook New hook added label Jun 1, 2021
@xobotyi xobotyi requested a review from JoeDuncko June 1, 2021 10:31
@codecov
Copy link

codecov bot commented Jun 1, 2021

Codecov Report

Merging #101 (bbaae32) into master (509ec73) will not change coverage.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff            @@
##            master      #101   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           27        28    +1     
  Lines          372       395   +23     
  Branches        73        77    +4     
=========================================
+ Hits           372       395   +23     
Impacted Files Coverage Δ
src/index.ts 100.00% <100.00%> (ø)
src/useDebounceCallback/useDebounceCallback.ts 100.00% <100.00%> (ø)
src/useRafCallback/useRafCallback.ts 100.00% <100.00%> (ø)
src/useValidator/useValidator.ts 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 509ec73...bbaae32. Read the comment docs.

Copy link
Contributor

@JoeDuncko JoeDuncko left a comment

Choose a reason for hiding this comment

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

Seems to work fine, but I'm not sold on this implementation.

);

// validity state type if inferred from validator
const [validity] = useValidator(validator, [validator]);
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't know how I feel about this one. It almost feels like the better UX here would to be to either make this into useValidatorState, or to at least pass the value you are validating to useValidator.

Also, how it's set up here makes it so useValidator seems to be be pretty useless if you weren't using useDebounceCallback. You could otherwise just validate on input or on render, which both seem like better practices. Maybe I'm missing something though.

Copy link
Contributor Author

@xobotyi xobotyi Jun 2, 2021

Choose a reason for hiding this comment

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

to either make this into useValidatorState

It will be implemented as a separate hook later.

at least pass the value you are validating to useValidator

This will strict you to use validated value only, but what if you want to validate multiple values simultaneously? How to pass them to the validator? Dependencies list is an array, attempt to determine values depending on positions can cause inambiguos bugs.

seems to be be pretty useless if you weren't using useDebounceCallback.

Althoug it supports synchronous validation - it was designed mostly for async\deferred validation.

You could otherwise just validate on input or on render, which both seem like better practices.

On-input validation makes multivalue validation very complicated complicated.
On-render validation forces you to create validity storage by hand and does validation on ach render instead of on-change.

Copy link
Contributor

Choose a reason for hiding this comment

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

it was designed mostly for async\deferred validation.

How about we rename this hook to useAsyncValidator then? Or useValidatorEffect? I think that'd convey the intended use case better.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It is state, not the effect, also it is not the async - only, as for my code I have several synchronous validators.

Copy link
Contributor

Choose a reason for hiding this comment

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

Gotcha. I'm okay rolling with this for now, but I'm still not sold on it.

@xobotyi xobotyi merged commit 8c517e0 into master Jun 2, 2021
@xobotyi xobotyi deleted the useValidator branch June 2, 2021 15:50
github-actions bot pushed a commit that referenced this pull request Jun 2, 2021
# [1.25.0](v1.24.1...v1.25.0) (2021-06-02)

### Features

* new hook `useValidator` ([#101](#101)) ([8c517e0](8c517e0))
@xobotyi
Copy link
Contributor Author

xobotyi commented Jun 2, 2021

🎉 This PR is included in version 1.25.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

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

Successfully merging this pull request may close these issues.

2 participants