-
Notifications
You must be signed in to change notification settings - Fork 29.8k
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: allow to validate enums with validateOneOf #34070
Conversation
We should benchmark this. I tried making some similar types of changes in here and the performance hit was significant due to issues with the way v8 optimizes these. I've been considering taking a different approach with the validators that should allow the optimizer to be more effective. |
@jasnell what subsystems do you have in mind so we could start the benchmark CI, I don't think these 2 are extensively used in our codebase (maybe path, buffer, tls, http2 are most affected by the quick |
I would do four benchmarks, they are a bit artificial but should be useful.
The approach that I'm considering as an alternative would use a builder pattern to create validators rather than using options... for instance: const { makeStringValidator } = require('internal/validators');
const fooValidator = makeStringValidator('foo', { oneOf: ['a', 'b'] });
stringValidator.fooValidator('c'); The builder would generate a more optimized version of the validator that, hopefully, takes less of a performance hit each time. I'm hoping but I'm not certain that the v8 optimizer will be capable of handling it more effectively also, but I haven't tested any of that. |
Just finally was able to get through some testing here and it (thankfully) looks like there's no issue around opt/dept in the validators. So, yay :-) ... btw, another PR to compare with here: #33288 (comment) |
I'm sorry if this is an ignorant question, but am I correct that this adds an argument to existing internal-only functions that we then don't use in any |
I presume the reason would be to start using it in various locations. Here's an example of where it could be useful: #34023 (comment) |
I would prefer this be bundled with a commit that actually uses it. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we do include something like this (and a use case), I think this should be a separate function validateOneOf
.
- It does essentially the same in both cases (except formatting, which can be slow since it's only on the error path).
- If all possible values are specified anyway, it does not matter what type they are.
- It prevents performance hits of APIs that use the existing functions (
validateNumber
orvalidateString
). - It removes the need for
ArrayIsArray
.
(Requesting changes to prevent it from landing, sorry.)
e58c2b4
to
7c15424
Compare
@Trott @tniessen @jasnell finally got time for this, changed the implementation to introduce new validator This also depends on #34671 for proper error formatting. |
throw new ERR_INVALID_ARG_VALUE(name, value, reason); | ||
} else { | ||
throw new ERR_INVALID_OPT_VALUE(name, value, reason); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FWIW, I generally ignore ERR_INVALID_OPT_VALUE
and instead use the options.${name}
pattern instead with ERR_INVALID_ARG_VALUE
.
As a semver-major follow-up to this PR, it may be worth moving the existing uses of ERR_INVALID_OPT_VALUE
over and we can discontinue use of ERR_INVALID_OPT_VALUE
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd love to see that. I always found different error codes for OPT and ARG weird since they are not really useful imo.
I will work on it 😄 .
7c15424
to
38d603f
Compare
@tniessen @jasnell @Trott @himself65 this is now ready. The |
Landed in 6726246 |
PR-URL: #34070 Reviewed-By: Zeyu Yang <himself65@outlook.com> Reviewed-By: Tobias Nießen <tniessen@tnie.de> Reviewed-By: James M Snell <jasnell@gmail.com>
PR-URL: #34070 Reviewed-By: Zeyu Yang <himself65@outlook.com> Reviewed-By: Tobias Nießen <tniessen@tnie.de> Reviewed-By: James M Snell <jasnell@gmail.com>
This will be a start to generalize all argument validation errors. As currently we throw ARG/OPT, OUT_OF_RANGE, and other more specific errors. The OPT errors didn't bring much to the errors as it's just another variant of ARG error which is sometimes more confusing (some of our code used OPT errors to denote just argument validation errors presumably because of similarity of OPT to 'option' and not 'options-object') and they don't specify the name of the options object where the invalid value is located. Much better approach would be to just specify path to the invalid value in the name of the value as it is done in this PR (i.e. 'options.format', 'options.publicKey.type' etc) Also since this decreases a variety of errors we have it'd be easier to reuse validation code across the codebase. Refs: nodejs#31251 Refs: nodejs#34070 (comment) Signed-off-by: Denys Otrishko <shishugi@gmail.com>
PR-URL: #34070 Reviewed-By: Zeyu Yang <himself65@outlook.com> Reviewed-By: Tobias Nießen <tniessen@tnie.de> Reviewed-By: James M Snell <jasnell@gmail.com>
This will be a start to generalize all argument validation errors. As currently we throw ARG/OPT, OUT_OF_RANGE, and other more specific errors. The OPT errors didn't bring much to the errors as it's just another variant of ARG error which is sometimes more confusing (some of our code used OPT errors to denote just argument validation errors presumably because of similarity of OPT to 'option' and not 'options-object') and they don't specify the name of the options object where the invalid value is located. Much better approach would be to just specify path to the invalid value in the name of the value as it is done in this PR (i.e. 'options.format', 'options.publicKey.type' etc) Also since this decreases a variety of errors we have it'd be easier to reuse validation code across the codebase. Refs: #31251 Refs: #34070 (comment) Signed-off-by: Denys Otrishko <shishugi@gmail.com> PR-URL: #34682 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com>
This will be a start to generalize all argument validation errors. As currently we throw ARG/OPT, OUT_OF_RANGE, and other more specific errors. The OPT errors didn't bring much to the errors as it's just another variant of ARG error which is sometimes more confusing (some of our code used OPT errors to denote just argument validation errors presumably because of similarity of OPT to 'option' and not 'options-object') and they don't specify the name of the options object where the invalid value is located. Much better approach would be to just specify path to the invalid value in the name of the value as it is done in this PR (i.e. 'options.format', 'options.publicKey.type' etc) Also since this decreases a variety of errors we have it'd be easier to reuse validation code across the codebase. Refs: nodejs#31251 Refs: nodejs#34070 (comment) Signed-off-by: Denys Otrishko <shishugi@gmail.com> PR-URL: nodejs#34682 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com>
Checklist
make -j4 test
(UNIX), orvcbuild test
(Windows) passes