Add schema for parsing arrays from delimited strings
vecerek opened this issue · comments
🚀 Feature request
A function that returns a schema that can parse delimited strings into arrays of strings.
Current Behavior
Missing
Desired Behavior
const commaSeparatedList = S.arrayFromDelimitedString(",")
const parse = S.parseSync(commaSeparatedList)
const a: readonly string[] = parse("1,2,3")
// ["1", "2", "3"]
Suggested Solution
Who does this impact? Who is this for?
This can be helpful to both HTTP clients and servers that have a need to parse query params representing a list of ids or other values, e.g.:
GET /api/users/show_many?ids=123,456,789
Describe alternatives you've considered
Additional context
Your environment
Software | Version(s) |
---|---|
@effect/schema | |
TypeScript |
Now available since https://github.com/Effect-TS/schema/releases/tag/v0.30.4.
The way to achieve this is different from the proposal, though:
//$ExpectType Schema<string, ReadonlyArray<string>>
S.string.pipe(S.split(","))
//$ExpectType Schema<string, ReadonlyArray<number>>
S.string.pipe(S.split(","), S.compose(S.array(S.NumberFromString)))