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

Conditional Validation for parameter algorithm in isHash function #2296

Open
wants to merge 8 commits into
base: master
Choose a base branch
from

Conversation

KUSHAGRARAJTIWARI
Copy link

@KUSHAGRARAJTIWARI KUSHAGRARAJTIWARI commented Sep 30, 2023

  1. Added utility function to check presence of given key in given object
  2. Conditional validation for parameter algorithm in isHash function

Issue: #2295

Checklist

  • ✅ PR contains only changes related; no stray files, etc.
  • ✅ README updated (where applicable)
  • ✅ Tests written (where applicable)

kushagra-raj.tiwari added 5 commits October 1, 2023 03:02
Signed-off-by: kushagra-raj.tiwari <kushagra-raj.tiwari@apples-MacBook-Air.local>
Signed-off-by: kushagra-raj.tiwari <kushagra-raj.tiwari@apples-MacBook-Air.local>
Signed-off-by: kushagra-raj.tiwari <kushagra-raj.tiwari@apples-MacBook-Air.local>
…tion.

Signed-off-by: kushagra-raj.tiwari <kushagra-raj.tiwari@apples-MacBook-Air.local>
…in isHash function.

Signed-off-by: kushagra-raj.tiwari <kushagra-raj.tiwari@apples-MacBook-Air.local>
@codecov
Copy link

codecov bot commented Sep 30, 2023

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 99.95%. Comparing base (b958bd7) to head (2ade081).
Report is 17 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #2296   +/-   ##
=======================================
  Coverage   99.95%   99.95%           
=======================================
  Files         107      108    +1     
  Lines        2454     2458    +4     
  Branches      619      622    +3     
=======================================
+ Hits         2453     2457    +4     
  Partials        1        1           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Signed-off-by: kushagra-raj.tiwari <kushagra-raj.tiwari@apples-MacBook-Air.local>
@KUSHAGRARAJTIWARI KUSHAGRARAJTIWARI changed the title #2295 Conditional Validation of parameter algorithm in isHash function #2295 Conditional Validation for parameter algorithm in isHash function Sep 30, 2023
@KUSHAGRARAJTIWARI KUSHAGRARAJTIWARI changed the title #2295 Conditional Validation for parameter algorithm in isHash function [https://github.com/validatorjs/validator.js/issues/2295] Conditional Validation for parameter algorithm in isHash function Sep 30, 2023
@KUSHAGRARAJTIWARI KUSHAGRARAJTIWARI changed the title [https://github.com/validatorjs/validator.js/issues/2295] Conditional Validation for parameter algorithm in isHash function #2295 Conditional Validation for parameter algorithm in isHash function Sep 30, 2023
@KUSHAGRARAJTIWARI KUSHAGRARAJTIWARI changed the title #2295 Conditional Validation for parameter algorithm in isHash function Conditional Validation for parameter algorithm in isHash function Sep 30, 2023
@@ -0,0 +1,6 @@
export default function validateKey(object, key, errorMessage) {
if (object[key] === undefined) {
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
if (object[key] === undefined) {
if(object[key]) {
return true;
}
throw new Error(errorMessage);

Copy link
Author

Choose a reason for hiding this comment

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

@Daniyal-Qureshi
Thanks for your suggestion. But, if(object[key]) will result false if object[key] is any falsy value like 0, "" (empty string), null, etc. This will defeat the purpose of the function validateKey.

I used a better way of checking the key of any object. Please provide your suggestions on that code.

Copy link
Contributor

Choose a reason for hiding this comment

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

@KUSHAGRARAJTIWARI Previously, you were checking the value of the given key,I suggested the better way to check if the value is undefined, I think you need to check whether is key is present in the object or not.

Copy link
Author

Choose a reason for hiding this comment

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

Yes, you are correct. I changed the code and am now using the hasOwnProperty() method. This method sends true if the key is present in the object irrespective of the corresponding value. Using Javascript utility functions is good for code.

After your comment, I recollected that there is a function hasOwnProperty() to check if any key is present in the object or not. Thanks for your comment.

kushagra-raj.tiwari added 2 commits October 2, 2023 15:32
Signed-off-by: kushagra-raj.tiwari <kushagra-raj.tiwari@apples-MacBook-Air.local>
Signed-off-by: kushagra-raj.tiwari <kushagra-raj.tiwari@apples-MacBook-Air.local>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants