frzi / swiftui-router

Path-based routing in SwiftUI
MIT License
900 stars 43 forks source link

Regex Exception #21

Closed ghost closed 3 years ago

ghost commented 3 years ago

Regex error is irgnored at and it can lead to long debug sessions.

In our case we had a kebab-case parameter. This parameter will be used as named capture group in regex and will throw an error NSRegularExpression Error: Error Domain=NSCocoaErrorDomain Code=2048 "The value “^(/route-to/(?<parameter-one>[^/?]+)/(?<parameter-two>[^/?]+))$” is invalid." UserInfo={NSInvalidValue=^(/route-to/(?<parameter-one>[^/?]+)/(?<parameter-two>[^/?]+))$} .

Maybe it would be good to know in the README in how to use parameters.

Actual

route is ignored /route-to/:parameter-one/:parameter-two

Expected

route is registered or throw an error / or logging information. /route-to/:parameter-one/:parameter-two

frzi commented 3 years ago

Ah, excellent catch, yes! Because route globs are compiled into a NSRegularExpression, the only supported (or rather, the safest) characters for parameters are alphanumeric (A-Z, a-z and 0-9).

I agree, this should be mentioned somewhere in the README as well as in the code documentation.

Will have to think about the best way to respond to invalid Regex patterns. A possible solution would be to use fatalError() in debug mode, while ignoring and logging the error in production mode.

Thanks for filing the issue!