diff --git a/src/lib/isLatLong.js b/src/lib/isLatLong.js index c4e622753..b0fc05230 100644 --- a/src/lib/isLatLong.js +++ b/src/lib/isLatLong.js @@ -1,6 +1,5 @@ import assertString from './util/assertString'; import merge from './util/merge'; -import includes from './util/includesString'; const lat = /^\(?[+-]?(90(\.0+)?|[1-8]?\d(\.\d+)?)$/; const long = /^\s?[+-]?(180(\.0+)?|1[0-7]\d(\.\d+)?|\d{1,2}(\.\d+)?)\)?$/; @@ -16,8 +15,8 @@ export default function isLatLong(str, options) { assertString(str); options = merge(options, defaultLatLongOptions); - if (!includes(str, ',')) return false; const pair = str.split(','); + if (pair.length !== 2) return false; if ((pair[0].startsWith('(') && !pair[1].endsWith(')')) || (pair[1].endsWith(')') && !pair[0].startsWith('('))) return false; diff --git a/test/validators.test.js b/test/validators.test.js index 98d2a12ff..d8b6bef53 100644 --- a/test/validators.test.js +++ b/test/validators.test.js @@ -12856,6 +12856,33 @@ describe('Validators', () => { }); }); + it('should reject extra decimal LatLong components', () => { + test({ + validator: 'isLatLong', + invalid: [ + '0,0,', + '0,0,0', + '0,0,garbage', + '0, 0, 0, 0', + '(0, 0),', + '(0, 0),garbage', + ], + }); + }); + + it('should reject extra DMS LatLong components', () => { + test({ + validator: 'isLatLong', + args: [{ checkDMS: true }], + invalid: [ + '40° 26′ 46″ N, 79° 58′ 56″ W,', + '40° 26′ 46″ N, 79° 58′ 56″ W,0', + '40° 26′ 46″ N, 79° 58′ 56″ W,garbage', + '40° 26′ 46″ N, 79° 58′ 56″ W,0,0', + ], + }); + }); + it('should validate postal code', () => { const fixtures = [ {