Code-Hex / graphql-codegen-typescript-validation-schema

GraphQL Code Generator plugin to generate form validation schema from your GraphQL schema

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Support valibot (lightweight version of zod)

lopezjurip opened this issue · comments

Consider Valibot over Zod: achieves lighter bundles & modularity, enhancing performance & flexibility.

https://bundlephobia.com/package/zod@3.22.4
It's 57KB gziped.

See: https://github.com/fabian-hiller/valibot

@lopezjurip Hi!
I agree adding valibot support to this plugin.
Could you send me PR for this? Thanks!

@Code-Hex @lopezjurip
I am a fan of graphql-codegen-typescript-validation-schema and valibot. I would like to submit a PR because I would like to use these two in a project I am working on! May I assign this issue to me?

@MH4GF Sure! I'm looking forward your PR!

@Code-Hex Please let me confirm about the definedNonNullAny that is zod's temporary hack.

Surprisingly, valibot seems to compile the following code:

import * as v from 'valibot';

type A = {
  foo: any;
};

function AParser(): v.ObjectSchema<A, undefined> {
  return v.object({
    foo: v.any()
  });
}

const result = v.safeParse(AParser(), {
  foo: 'bar',
});

console.log(result);

type InferA = v.InferOutput<ReturnType<typeof AParser>>;
// =>
// type InferA = {
//   foo: any;
// }

playground: https://valibot.dev/playground/?code=JYWwDg9gTgLgBAKjgQwM5wG5wGZQiOAcg2QBtgAjCGQgbgCh6YBPMAUzgEE4BeOAb3pwcECAC4UAO2YMAvg3rYArpIDGMYBElcACsiio2UABQBKCRgB0AeQoArNuoDKqgBZsQyADycANHBUAEzZsYEk2QIA+ASE4KDYYJShtKwh7RxhjQWFhbFELS2RpM1jZUzlGFnY4AElJbCNuPis6hqhrJRgwTq8AJQSkyQAVVjYvKrYIbF19QyhIyIVVLVR4eNQlUnhmy1RkBr0DNmNOQ7mzf2yRcSIKfUJfejKllYhSNktSCABzY3XNmDlIA

So, is the schema to be generated correct with the intention of v.object({ foo: v.any() }) ?

ref: colinhacks/zod#951

@MH4GF Thank you for your question.

In zod, I employed a hack to define a schema for the any type that excludes undefined and null. I hope to create something that aligns with this intention.

Does this answer your question?