effect-http icon indicating copy to clipboard operation
effect-http copied to clipboard

Array of query parameters

Open KhraksMamtsov opened this issue 1 year ago • 3 comments

Single query parameter must be treated as single element array for appropriate schema.

export const GetManyUsersRequest = {
  query: Schema.struct({
    id: Schema.array(IdUserSchema)
  }),
};

Return 400 for GET /users?id=123 But 200 for GET /users?id=123&id=456

I have tried Schema<string[], string> for 123 -> [123] but it breaks swagger UI - it renders string input instead of several inputs for array of strings

KhraksMamtsov avatar Feb 10 '24 21:02 KhraksMamtsov

I see. Yeah, the problem is the query parameters are first parsed onto a javascript object (by the @effect/platform) and only then they are passed to the schema decoder.

The solution that comes to my mind is to adjust the internal request parser in effect-http and convert any Schema.array(X) schema into a Schema.union(X + transformation onto a single item array, Schema.array(X)) schema. I'll try to think it through and prepare a PR.

For now, you can probably use the union in your code. Unfortunately, it will not solve the wrong visualisation in the swagger UI.

export const GetManyUsersRequest = {
  query: Schema.struct({
    id: Schema.union(Schema.array(IdUserSchema), IdUserSchema)
  }),
};

sukovanej avatar Feb 10 '24 23:02 sukovanej

it should be noted that tuples also need to be taken into account AFAIR there is separate ast for tuples

KhraksMamtsov avatar Feb 11 '24 08:02 KhraksMamtsov

@sukovanej I think better not to wrap user's schema, but wrap query-element into single array if users schema implies this.

otherwise in case of error user will get unexpected parse error - he enters one schema but errors from wrapper one What do you think?

KhraksMamtsov avatar Feb 11 '24 10:02 KhraksMamtsov

Should be solved by the new QuerySchema module.

sukovanej avatar Jul 14 '24 13:07 sukovanej