Effect CommunityEC
Effect Community12mo ago
14 replies
adrian_g

Issue with NonEmptyArray Schema for Single Element Arrays in Endpoint URL Parameters

I've got a following endpoint:

HttpApiEndpoint
  .get('endpoint', '/endpoint')
  .setUrlParams(Schema.Struct({ param: Schema.NonEmptyArray(Schema.String) }))


When calling it via generated client, I get an error if only 1 element is provided to the params array. It's fine with 2 or more.

The way I work around it is:

const NonEmptyArrayFromSingleton = Schema.transform(
  Schema.String,
  Schema.NonEmptyArray(Schema.String),
  {
    decode: x => [x] as const,
    encode: ([x]) => x,
  },
)

const Param = Schema.Union(
  Schema.NonEmptyArray(Schema.String),
  NonEmptyArrayFromSingleton,
)

export const UrlParams = Schema.Struct({
  param: Param,
})


but I think it makes sense that thi behaviour be provided out of the box
Was this page helpful?