You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Custom validators can be defined for placeholders.
219
+
#### Defining custom URL Value Converters
221
220
222
-
For example, the placeholder `<department>` should only contain the strings `['men','women','kids']`. If it doesn't contain any of these, the URL pattern should not match.
221
+
You can define custom URLValueConvertersforURLplaceholders.
223
222
224
-
Add the custom value converter to the `[String: URLValueConverter]` dictionary on your instance of `Navigator`.
223
+
For example, the placeholder `<region>` is only allowed for the strings `["us-west-1", "ap-northeast-2", "eu-west-3"]`. If it doesn't contain any of these, the URL pattern should not match.
224
+
225
+
Add a custom value converter to the `[String: URLValueConverter]` dictionary on your instance of `Navigator`.
225
226
226
227
```swift
227
-
navigator.matcher.valueConverters["dept-type"] = { pathComponents, index in
228
-
let departments = ["men", "women", "kids"]
229
-
if departments.contains(pathComponents[index]) {
228
+
navigator.matcher.valueConverters["region"] = { pathComponents, index in
229
+
let allowedRegions = ["us-west-1", "ap-northeast-2", "eu-west-3"]
230
+
if allowedRegions.contains(pathComponents[index]) {
230
231
return pathComponents[index]
231
232
} else {
232
233
return nil
233
234
}
234
235
}
235
236
```
236
237
237
-
You will then be able to add the validator to a placeholder in the same way that standard validators are included.
238
-
239
-
For example, `myapp://user/browse/<dept-type:department>` matches with:
240
-
- `myapp://user/browse/men`
241
-
- `myapp://user/browse/women`
242
-
- `myapp://user/browse/kids`
238
+
With the code above, for example, `myapp://region/<region:_>` matches with:
239
+
- `myapp://region/us-west-1`
240
+
- `myapp://region/ap-northeast-2`
241
+
- `myapp://region/eu-west-3`
243
242
244
243
But it doesn't match with:
245
-
-`myapp://user/browse/babies`
244
+
-`myapp://region/ca-central-1`
246
245
247
-
For additional information, see the [implementation](https://github.com/devxoul/URLNavigator/blob/master/Sources/URLMatcher/URLMatcher.swift) of default validators.
246
+
For additional information, see the [implementation](https://github.com/devxoul/URLNavigator/blob/master/Sources/URLMatcher/URLMatcher.swift) of default URLValueConverters.
0 commit comments