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

LatLng: stricter arguments validation #7765

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

johnd0e
Copy link
Collaborator

@johnd0e johnd0e commented Nov 10, 2021

Note: PR #8214 supersedes this PR!

Throw on any invalid value of lat/lng/alt
(alt is validated if is explicitly specified)

Ref: #7128


Question:

  • On undefined/null in arguments - should we throw instead of returning undefined/null?
  • Should we be same strict with alt, or better allow undefined value for compatibility purposes?

Copy link
Member

@Falke-Design Falke-Design left a comment

Choose a reason for hiding this comment

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

I like this PR 👍
Maybe you want to generalize this PR and add checks for L.Point to #7432

src/geo/LatLng.js Show resolved Hide resolved
src/layer/GeoJSON.js Outdated Show resolved Hide resolved
}
}

function checkNumber(a) {
Copy link
Member

Choose a reason for hiding this comment

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

I would suggest to move this into Utils, maybe we will have some other places where we want to use this. (#7432)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I do not agree, because the function implementation is heavily defined by it's particular application.
E.g. we may need integer numbers, or allow Infinity, etc.

Copy link
Member

Choose a reason for hiding this comment

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

Hmm you are right but I still think it would be cleaner to have on general function that can used outside of Leaflet core too. Maybe a second optional parameter allowedTypes can be passed as array ['integer', 'double', 'Infinity'].

For infinity it would be also possible to use if(value === Infinity || checkNumber(value))

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Maybe a second optional parameter allowedTypes can be passed as array

Too many parameters to take into account all possible cases. E.g. should it accept hex strings? Exponential form?
BTW, the function itself is not perfect too. I'd like to improve it, but in reasonable limits. May be you have ideas.

Copy link
Member

Choose a reason for hiding this comment

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

But we can define / document which cases / types are allowed.

A way to get 0x10 managed is to check if the string is starting with 0, length > 1 and the second char is not . for decimal numbers. With that 0010 is invalid too.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Too much actions. Can be performance penalties

@johnd0e
Copy link
Collaborator Author

johnd0e commented Nov 10, 2021

Maybe you want to generalize this PR and add checks for L.Point to #7432

In separate PR.

@johnd0e johnd0e force-pushed the source-data-validation branch 2 times, most recently from 246353e to f582a8e Compare November 10, 2021 15:25
Comment on lines 22 to 25
var c = new L.LatLng("010", "0x10"); // better'd throw here..
expect(c.lat).to.be(10);
expect(c.lng).to.be(0); // bad!
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Here are the cases that I do not like

Comment on lines +43 to +46
// do not throw atm:
// expect(L.latLng).withArgs('0010', 0).to.throwError();
// expect(L.latLng).withArgs('0x10', 0).to.throwError();
// expect(L.latLng).withArgs('1e5', 0).to.throwError();
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

And here.

Copy link
Member

Choose a reason for hiding this comment

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

1e5 = 100000 is a correct number, what is not vaild in my eyes is that the string '1e5' is converted to 1 (parseInt('1e5'))

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

is a correct number

Sure, but do we really expect such number in Leaflet?

Copy link
Member

Choose a reason for hiding this comment

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

Maybe in CRSSimple but I don't think so. I think we should only check if it is a valid number and not if the number makes sense

@johnd0e johnd0e force-pushed the source-data-validation branch 4 times, most recently from 39de142 to 137bbc3 Compare November 10, 2021 16:17
@johnd0e johnd0e marked this pull request as ready for review November 10, 2021 16:20
Throw on any invalid value of lat/lng/alt
(alt is validated if is explicitly specified)
@johnd0e
Copy link
Collaborator Author

johnd0e commented Nov 10, 2021

Found on stackoverflow: https://run.plnkr.co/plunks/93FPpacuIcXqqKMecLdk/

@Falke-Design
Copy link
Member

Infinity is now not valid anymore. And I would say if someone uses 0x10 or 1e5 then they are converted to theier decimal number and this is ok

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Validation & Checks
  
In progress
Development

Successfully merging this pull request may close these issues.

None yet

2 participants