hapi-server / uri-templates

Repository of tools for URI templates
Apache License 2.0
2 stars 0 forks source link

Is there any way to validate that a string matches a template exactly? #26

Closed ericthewizard closed 1 month ago

ericthewizard commented 7 months ago

For example - let’s say we have:

>>> filename = "https://spdf.gsfc.nasa.gov/pub/pre_generated_plots/kp_plots/ace/gif/ac_199811900-199812000.gif"
>>> template = "https://spdf.gsfc.nasa.gov/pub/pre_generated_plots/kp_plots/ace/gif/ac_$Y$j00-$(Y;end)$(j;end)00.gif"
>>> from URITemplate import URITemplate
>>> ut = URITemplate(template)
>>> ut.parse(filename, {})
[1998, 4, 29, 0, 0, 0, 0, 1998, 4, 30, 0, 0, 0, 0]

This works as expected, but when the filename has additional characters after the date, it still matches and extracts the times, e.g.,

>>> filename = "https://spdf.gsfc.nasa.gov/pub/pre_generated_plots/kp_plots/ace/gif/ac_199811900-199812000-this-shouldnt-match.gif"
>>> ut.parse(filename, {})
[1998, 4, 29, 0, 0, 0, 0, 1998, 4, 30, 0, 0, 0, 0]

If it includes additional characters before the date, it throws an exception:

>>> filename = "https://spdf.gsfc.nasa.gov/pub/pre_generated_plots/kp_plots/ace/gif/ac_this-shouldnt-match-199811900-199812000.gif"
>>> ut.parse(filename, {})
Traceback (most recent call last):
  File "/Users/ewgrimes/find_gif_times/URITemplate.py", line 1089, in parse
    digit = int(field)
ValueError: invalid literal for int() with base 10: 'this'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/ewgrimes/find_gif_times/URITemplate.py", line 1156, in parse
    raise Exception('fail to parse digit number %d: %s' % (idigit, field))
Exception: fail to parse digit number 1: this

Is there a way to check if a string matches the template exactly?

Thanks!

jbfaden commented 1 month ago

Eric, sorry I missed this ticket when you posted it back in January. Let me know if you are still wondering about this. Also I will check the behavior you describe, it sounds like a bug. If there is text after the last field, it should match something...

ericthewizard commented 1 month ago

Definitely still wondering about it - have you had a chance to look into it? I haven't tried the most recent version yet, to see if it's still an issue.

jbfaden commented 1 month ago

I notice it doesn't check any of the non-component characters in between, so XX_199811900-199812000.gif will also match. I'm putting in code to check these as well.

jbfaden commented 1 month ago

This is fixed and will be released a little later this morning.

jbfaden commented 1 month ago

This is also updated in the Java and Python versions.

jbfaden commented 1 month ago

And also the JavaScript version.