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

lib: refactor validators #26809

Closed
wants to merge 9 commits into from
Closed

Conversation

BridgeAR
Copy link
Member

This makes sure our validator functions actually only validate and have no side effects. Please have a look at the individual commit messages for further details.

It is just a code cleanup and no behavior change.

Checklist
  • make -j4 test (UNIX), or vcbuild test (Windows) passes
  • tests and/or benchmarks are included
  • documentation is changed or added
  • commit message follows commit guidelines

@nodejs-github-bot nodejs-github-bot added the lib / src Issues and PRs related to general changes in the lib or src directory. label Mar 20, 2019
@BridgeAR
Copy link
Member Author

It would be nice to get some reviews here. This is a code cleanup that I wanted to do after working on #26738.

@BridgeAR
Copy link
Member Author

Rebased due to conflicts.

CI https://ci.nodejs.org/job/node-test-pull-request/21844/

Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

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

LGTM

@BridgeAR BridgeAR added the author ready PRs that have at least one approval, no pending requests for changes, and a CI started. label Mar 24, 2019
@BridgeAR
Copy link
Member Author

@nodejs-github-bot
Copy link
Collaborator

@nodejs-github-bot
Copy link
Collaborator

nodejs-github-bot commented Mar 25, 2019

The function did not only validate the mode but it returns a new
value depending on the input. Thus `validate` did not seem to be an
appropriate name.
This makes sure the validation functions do not cause any side
effects. Validation functions should ideally only validate the input
without any other effect. Since the input value must be known from
the callee, there is no reason to return the input value.
The function did not only validate the timer but it caused side
effects like a warning and potentially returned a different value
than the input value. Thus the name `validate` did not seem to be
appropriate.
The name indicated only validation while it did much more and it
returned a different value to the callee function. The underscore
was also not necessary as the function is internal one way or the
other.
The function did not only validate the input so far but it also made
a copy of the input object and returned that copy to the callee
function. That copy was not necessary for all call sites and it was
not obvious that the function did not only validate the input but
that it also returned a copy of it. This makes sure the function does
nothing more than validation and copying is happening in the callee
function when required.
This function did not only validate the input but it returned a new
value in case the hostname was valid. This simplifies the function
by always returning the required value, no matter if it is valid or
invalid, so the callee site does not have to check that anymore. On
top the function is renamed to `getHostname` to make clear that the
function does not only validate the input but it also returns a new
value.
The function does not only validate the input but it causes side
effects by adding default options to the input object in case the
option is not set.
This renames the parameters for clarity and removes the check for
undefined encoding. That will always default to `utf8`.
There are lots of places that validate for arrayBufferView and we
have multiple functions that do the same thing. Instead, move the
validation into `internal/validators` so all files can use that
instead.

There are more functions throughout the code that do the same but
it takes some more work to fully consolidate all of those.
@BridgeAR
Copy link
Member Author

Rebased due to conflicts.

@nodejs-github-bot
Copy link
Collaborator

@nodejs-github-bot
Copy link
Collaborator

BridgeAR added a commit to BridgeAR/node that referenced this pull request Mar 27, 2019
The function did not only validate the mode but it returns a new
value depending on the input. Thus `validate` did not seem to be an
appropriate name.

PR-URL: nodejs#26809
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
BridgeAR added a commit to BridgeAR/node that referenced this pull request Mar 27, 2019
This makes sure the validation functions do not cause any side
effects. Validation functions should ideally only validate the input
without any other effect. Since the input value must be known from
the callee, there is no reason to return the input value.

PR-URL: nodejs#26809
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
BridgeAR added a commit to BridgeAR/node that referenced this pull request Mar 27, 2019
The function did not only validate the timer but it caused side
effects like a warning and potentially returned a different value
than the input value. Thus the name `validate` did not seem to be
appropriate.

PR-URL: nodejs#26809
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
BridgeAR added a commit to BridgeAR/node that referenced this pull request Mar 27, 2019
The name indicated only validation while it did much more and it
returned a different value to the callee function. The underscore
was also not necessary as the function is internal one way or the
other.

PR-URL: nodejs#26809
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
BridgeAR added a commit to BridgeAR/node that referenced this pull request Mar 27, 2019
The function did not only validate the input so far but it also made
a copy of the input object and returned that copy to the callee
function. That copy was not necessary for all call sites and it was
not obvious that the function did not only validate the input but
that it also returned a copy of it. This makes sure the function does
nothing more than validation and copying is happening in the callee
function when required.

PR-URL: nodejs#26809
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
BridgeAR added a commit to BridgeAR/node that referenced this pull request Mar 27, 2019
This function did not only validate the input but it returned a new
value in case the hostname was valid. This simplifies the function
by always returning the required value, no matter if it is valid or
invalid, so the callee site does not have to check that anymore. On
top the function is renamed to `getHostname` to make clear that the
function does not only validate the input but it also returns a new
value.

PR-URL: nodejs#26809
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
BridgeAR added a commit to BridgeAR/node that referenced this pull request Mar 27, 2019
The function does not only validate the input but it causes side
effects by adding default options to the input object in case the
option is not set.

PR-URL: nodejs#26809
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
BridgeAR added a commit to BridgeAR/node that referenced this pull request Mar 27, 2019
This renames the parameters for clarity and removes the check for
undefined encoding. That will always default to `utf8`.

PR-URL: nodejs#26809
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
BridgeAR added a commit to BridgeAR/node that referenced this pull request Mar 27, 2019
There are lots of places that validate for arrayBufferView and we
have multiple functions that do the same thing. Instead, move the
validation into `internal/validators` so all files can use that
instead.

There are more functions throughout the code that do the same but
it takes some more work to fully consolidate all of those.

PR-URL: nodejs#26809
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
@BridgeAR
Copy link
Member Author

Landed in 9d854fb...7bddfcc 🎉

@BridgeAR BridgeAR closed this Mar 27, 2019
@targos
Copy link
Member

targos commented Mar 28, 2019

This depends on a semver-major PR and needs to be manually backported to v11.x-staging

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
author ready PRs that have at least one approval, no pending requests for changes, and a CI started. lib / src Issues and PRs related to general changes in the lib or src directory.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants