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

fix(isLatLong): fix bug on isLatLang #1074

Merged
merged 2 commits into from
Aug 2, 2019

Conversation

ezkemboi
Copy link
Member

- when lat starts with (, it makes it a true latitude.
- Closes validatorjs#929
if (
(pair[0].charAt(0) === '(' && pair[1].charAt(pair[1].length - 1) !== ')') ||
(pair[1].charAt(pair[1].length - 1) === ')' && pair[0].charAt(0) !== '(')
) return false;
Copy link
Collaborator

Choose a reason for hiding this comment

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

This will fail if str has length of zero:

> const str = ""
> const pair = str.split(',')
> pair
[ '' ]
> pair[0].charAt(0)
''
> pair[1].charAt(pair[1].length - 1)
Thrown:
TypeError: Cannot read property 'charAt' of undefined

Copy link
Member Author

@ezkemboi ezkemboi Jul 29, 2019

Choose a reason for hiding this comment

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

@chriso, will it mean that we have to catch empty validations, for instance when a user passes an empty value for either latitude or longitude?
Or we should look on ways to integrate this validation in the regex path?
Maybe you can help me get some bigger picture...

Copy link
Collaborator

@chriso chriso Jul 29, 2019

Choose a reason for hiding this comment

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

Sorry, disregard my point. I see there's an if (!str.includes(',')) return false; guard above which disallows an empty string.

Copy link
Member Author

Choose a reason for hiding this comment

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

The regex ^\(.*\) might do the same validation, but I don't know if it will throw an error.
I am open to possibilities/opinions on the same.

lib/isLatLong.js Outdated
@@ -16,6 +16,7 @@ function _default(str) {
(0, _assertString.default)(str);
if (!str.includes(',')) return false;
var pair = str.split(',');
if (pair[0].charAt(0) === '(' && pair[1].charAt(pair[1].length - 1) !== ')' || pair[1].charAt(pair[1].length - 1) === ')' && pair[0].charAt(0) !== '(') return false;
Copy link
Member

Choose a reason for hiding this comment

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

Can re-format/write this for better readability?

if (pair[0].startsWith('(') && !pair[1].endsWith(')')
  || pair[1].endsWith(')') && !pair[0].startsWith('(') return false;

Copy link
Member Author

Choose a reason for hiding this comment

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

I will work on that @profnandaa.

Thank you.

@ezkemboi ezkemboi force-pushed the fix-lat-long-validation branch from 1a5fe9a to b684474 Compare August 2, 2019 18:05
Copy link
Member

@profnandaa profnandaa left a comment

Choose a reason for hiding this comment

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

LGTM 🎉

@profnandaa profnandaa merged commit 47f3492 into validatorjs:master Aug 2, 2019
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.

isLatLong returns true
4 participants