Skip to content

Commit 0e79d2f

Browse files
committed
Improve URL Value Converters documentation
1 parent 7042ecb commit 0e79d2f

File tree

1 file changed

+14
-15
lines changed

1 file changed

+14
-15
lines changed

README.md

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -216,35 +216,34 @@ Navigator.open("myapp://alert?title=Hi", context: context)
216216
```
217217

218218

219-
#### Setting custom validators for placeholders
220-
Custom validators can be defined for placeholders.
219+
#### Defining custom URL Value Converters
221220

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 URL Value Converters for URL placeholders.
223222

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`.
225226
226227
```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]) {
230231
return pathComponents[index]
231232
} else {
232233
return nil
233234
}
234235
}
235236
```
236237
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`
243242
244243
But it doesn't match with:
245-
- `myapp://user/browse/babies`
244+
- `myapp://region/ca-central-1`
246245

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 URL Value Converters.
248247

249248

250249
## License

0 commit comments

Comments
 (0)