Skip to content
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

Feature Request: Generics Template, and I can work on PR #1058

Open
karambarakat opened this issue Feb 16, 2025 · 1 comment
Open

Feature Request: Generics Template, and I can work on PR #1058

karambarakat opened this issue Feb 16, 2025 · 1 comment
Assignees
Labels
question Further information is requested

Comments

@karambarakat
Copy link

karambarakat commented Feb 16, 2025

I was working on generic template to be used to define many similar schemas:

const result = (error: WhatShouldGoHere) => v.variant("state", [
    v.object({
        state: v.literal("ok"),
        data: v.array(v.string()),
    }),
    v.object({
        state: v.literal("err"),
        err: error,
    }),
]);

const res1 = result(v.string());
const res2 = result(v.null());

But I realized figuring out the exact type of 'WhatShouldGoHere' is a lot of work. If you are interested in implementing a feature like this, I suggest it has a syntax like this, and I can work on a pull request.

const result = v.generic((input) => v.variant("state", [
    v.object({
        state: v.literal("ok"),
        data: v.array(v.string()),
    }),
    v.object({
        state: v.literal("err"),
        err: input,
    }),
]);
@karambarakat karambarakat changed the title Feature Request: Generics Feature Request: Generics Template, and I can work on PR Feb 16, 2025
@fabian-hiller
Copy link
Owner

fabian-hiller commented Feb 16, 2025

Thank you for creating this issue. Can you try replacing WhatShouldGoHere with v.GenericSchema? You can also use a TS generic the argument to be fully typed-safe. I can share example code later if you are interested.

@fabian-hiller fabian-hiller self-assigned this Feb 16, 2025
@fabian-hiller fabian-hiller added the question Further information is requested label Feb 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants