mirage / ocaml-uri

RFC3986 URI parsing library for OCaml
Other
97 stars 57 forks source link

URI validation #171

Open jameshensmancitrix opened 1 year ago

jameshensmancitrix commented 1 year ago

I am currently trying to use Uri.of_string as an assertion that a url input is valid, however I have learned this is not the case. Is there a way to validate urls within this library, it would be a useful utility and avoid having to regex directly for this.

lindig commented 1 year ago

https://github.com/mirage/ocaml-uri/blob/master/lib/uri.ml#L1105-L1112

This is the implementation of of_string and it looks like it would accept all strings. Maybe adding a predicate like is_valid or validate to the interface could support this use case.

avsm commented 1 year ago

You haven't elaborated on your usecase, but you could just do a Uri.of/to_string roundtrip and compare with the original string, if performance is not an issue.

lindig commented 1 year ago

The use case is to validate the syntax of a URI. Granted. a lot of strings are a valid URI but might still not match the expectation because, for example, the scheme is valid but unexpected.

jameshensmancitrix commented 1 year ago

The use case is validation of user inputed url strings which are going to be used to send information to. By doing this with an empty string you would have it return true Uri.of_string "" |> Uri.to_string will return "" this is case where we would consider this an invalid url. of_string takes in url strings which could generally be considered no urls such as "http:/localhost" which has incorrect syntax for the scheme. Performance in this case is not critical as it is related to user input validation, and at that is unlikely to be done much or on many urls at once.