Effect CommunityEC
Effect Community2y ago
12 replies
addamsson

Composing Schemas to Include ID in User DTO

How can I compose schemas? I have this:
const UnsavedUserDto = S.struct({
    name: S.string.pipe(
        S.nonEmpty({
            message: () => "Name cannot be empty",
        }),
    ),
});

const UserDto = S.union(UnsavedUserDto, S.struct({ id: S.UUID }));

and my problem is when I use this I only get back {name: "..."} (
id
is stripped) even though this compiles:
type UserDto = S.Schema.To<typeof UserDto>;

const user: UserDto = {
    id: "123",
    name: "John",
};

Is there some simple way to do schema composition? In Zod I can just do A.and(B) or A.extend({ ... }. What's the equivalent in schema?
Was this page helpful?