-
I'm looking to create a new route for my app, which will take a
Presumably I can parse the parameter by separating out the comma-separated values like so:
But I was wondering if there was a built-in way/better to do this with Thanks a lot! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hi @mergesort, the second argument of So you can create a custom conversion for turning a string into an array of struct CommaSeparatedURLs: Conversion {
func apply(_ input: Substring) throws -> [URL] {
input.split(separator: ",").compactMap { URL(string: String($0)) }
}
func unapply(_ output: [URL]) throws -> Substring {
output.map(\.absoluteString).joined(separator: ",")[...]
}
} And then you can use this with Field("links", CommaSeparatedURLs()) And you can also add a |
Beta Was this translation helpful? Give feedback.
Hi @mergesort, the second argument of
Field
is aConversion
, which describes a way of transforming an input to an output and back into an input. It's similar to a parser, but it does not consume anything. It just transforms back-and-forth.So you can create a custom conversion for turning a string into an array of
URL
s and back:And then you can use this with
Field
:And …