semver icon indicating copy to clipboard operation
semver copied to clipboard

Feature Request: validate string as package version for Composer

Open LeTraceurSnork opened this issue 1 year ago • 2 comments

Is there any plans to add functionality for checking whether the passed string is a valid package version for Composer?

LeTraceurSnork avatar Nov 21 '24 06:11 LeTraceurSnork

You can use new VersionParser()->normalize() from https://github.com/composer/semver/blob/main/src/VersionParser.php#L108 that will throw if it's not parseable. That's the code Composer uses so it should do what you want I guess.

Seldaek avatar Nov 25 '24 16:11 Seldaek

Oh, yeah, I saw that, and yeah, I used that exactly as you proposed, tho, I find it kinda odd - instead of just checking whether or not string is a version and getting (presumably) true/false answer, you had to ->normalize() it (not obvious solution by itself), but also you had to catch an exception after it. Not so... beautiful I guess? Not so straightforward, maybe. So, wouldn't it be more convenient to add some true/false method (that maybe do exactly that ->normalize() + catch) ?

LeTraceurSnork avatar Nov 25 '24 18:11 LeTraceurSnork