Defining a Schema for Unique Comma-Separated Literal Strings

Trying to define a schema for comma separated values, unique, from a set of literal strings..

Wonder if there's any built-ins that could help, or if anyone's able to give me a nudge in the right direction..

https://effect.website/play/#1735b60430bd

const uniqueValues = S.filter((a: Array<unknown>) => a.length === new Set(a).size || "All values must be unique");

const Things = S.Array(S.Literal("foo", "bar", "baz")).pipe(uniqueValues)

const program = Effect.gen(function* () {
  console.log(S.decodeSync(Things)("foo,bar") === ["foo", "bar"])
  console.log(S.encodeSync(Things)(["foo", "bar"]) === "foo,bar")
  S.decodeSync(Things)("foo,foo") // should throw an error
  S.encodeSync(Things)(["foo", "foo"]) // this should be a type error
})
Was this page helpful?