Skip to content

Allow 'overwriting' fields when using 'merge' on schemas #95

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
justin0mcateer opened this issue Sep 29, 2024 · 9 comments
Open

Allow 'overwriting' fields when using 'merge' on schemas #95

justin0mcateer opened this issue Sep 29, 2024 · 9 comments
Labels
enhancement New feature or request v11 Features planned for the v10 version

Comments

@justin0mcateer
Copy link

The relatively new 'merge' function is very helpful. Thanks for adding that.

We often use the 'merge' function to produce more specific versions of some base type. In this case, it's helpful to be able to narrow certain types in the result of the intersection/merge.

As an example:

type Base = {
type: 'foo' | 'bar',
name: string,
}

type Foo = {
type: 'foo',
name: string,
fooCount: number,
}

type Bar = {
type: 'bar',
name: string,
barSize: 'small' | 'large',
}

Currently, if a field exists in both types provided to 'merge', it throws an error that 'type' is already defined. In Zod, the Schema on the right overrides the Schema on the left. This also allows for creating some type of derived schema where one or two properties are different.

@DZakh
Copy link
Owner

DZakh commented Sep 30, 2024

I'll investigate.

@DZakh DZakh added the enhancement New feature or request label Sep 30, 2024
@chenglou
Copy link

This would make porting over from Zod easier as it's Zod's behavior: https://zod.dev/?id=merge
Not sure how desirable in the end, and how complex it'd be though

@DZakh
Copy link
Owner

DZakh commented Feb 22, 2025

It shouldn't be difficult if we require them to have the same type.

@chenglou
Copy link

Same type wouldn't generalize well right? Like:

const A = S.schema({ kind: 'a' })
const B = S.schema({ kind: 'b' })
const C = S.merge(A, B)

Not sure if that's important

@DZakh
Copy link
Owner

DZakh commented Feb 22, 2025

In this cases these are Literal("a") and Literal("b") which won't merge

@chenglou
Copy link

Humm yeah and then you’d need something like Pick and Omit. And rescript kinda struggles with that as seen with #96

@DZakh
Copy link
Owner

DZakh commented Feb 22, 2025

Maybe for v11, I didn't really have a use case for this myself, since in ReScript you design data differently

@DZakh
Copy link
Owner

DZakh commented May 31, 2025

I don't know how relevant it is still, but I implemented it in the commit dbe6984

I'll create a release as soon as the PR passes all tests 🫡

@DZakh DZakh added the v11 Features planned for the v10 version label May 31, 2025
@justin0mcateer
Copy link
Author

Awesome! We have used the library a bit and are still very interested is making wider use of it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request v11 Features planned for the v10 version
Projects
None yet
Development

No branches or pull requests

3 participants