Skip to content

Fix LatLng.validate throwing on null instead of returning false#10276

Merged
jonkoops merged 1 commit into
Leaflet:mainfrom
chatman-media:fix/latlng-validate-null
Jul 2, 2026
Merged

Fix LatLng.validate throwing on null instead of returning false#10276
jonkoops merged 1 commit into
Leaflet:mainfrom
chatman-media:fix/latlng-validate-null

Conversation

@chatman-media

Copy link
Copy Markdown
Contributor

LatLng.validate(null) throws TypeError: Cannot use 'in' operator to search for 'lat' in null instead of returning false.

typeof null === 'object' is true, so the object branch reaches 'lat' in null and throws. This is asymmetric with validate(undefined), which correctly returns false. The fix guards the object check with lat !== null.

As a side effect, new LatLng(null) now throws the intended descriptive Invalid LatLng object error (from the constructor's validate call) instead of the opaque internal TypeError.

Added a validate spec covering valid inputs and invalid inputs (undefined/null/'foo'/short array) returning false without throwing. The null case fails without this fix.

typeof null === 'object', so the object branch reached `'lat' in null`
and threw `TypeError: Cannot use 'in' operator to search for 'lat' in
null` — asymmetric with `validate(undefined)` which correctly returns
false. Guard the object check with `lat !== null`.

This also makes `new LatLng(null)` throw the intended descriptive
`Invalid LatLng object` error (raised by the constructor's validate
call) instead of the opaque internal TypeError.

@jonkoops jonkoops left a comment

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.

Good catch! LGTM

@jonkoops jonkoops merged commit 3ee4034 into Leaflet:main Jul 2, 2026
12 checks passed
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.

2 participants