Skip to content

fix(isLatLong): fix bug on isLatLang - #1074

Merged
profnandaa merged 2 commits into
validatorjs:masterfrom
ezkemboi:fix-lat-long-validation
Aug 2, 2019
Merged

profnandaa merged 2 commits into
validatorjs:masterfrom
ezkemboi:fix-lat-long-validation

Conversation

@ezkemboi

Copy link
Copy Markdown
Member

- when lat starts with (, it makes it a true latitude.
- Closes validatorjs#929
Comment thread src/lib/isLatLong.js Outdated
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
Copy Markdown
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

@ezkemboi ezkemboi Jul 29, 2019 •

Copy link
Copy Markdown
Member Author

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...

@chriso chriso Jul 29, 2019 •

Copy link
Copy Markdown
Collaborator

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
Copy Markdown
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.

Comment thread lib/isLatLong.js Outdated
(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
Copy Markdown
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
Copy Markdown
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

@profnandaa profnandaa left a comment

Copy link
Copy Markdown
Member

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