-
-
Notifications
You must be signed in to change notification settings - Fork 91
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
feat: support more validation libraries #6
Comments
Probably need an adapter for each validator, for couple of reasons:
|
We support a bunch of validators in tRPC: https://github.com/trpc/trpc/blob/main/packages/server/src/core/parser.ts https://github.com/trpc/trpc/blob/main/packages/server/src/core/internals/getParseFn.ts But separate entrypoints could also work i guess: import { createEnv } from "@t3-oss/env-core/zod";
const env = createEnv({
// ...
server: {
FOO: z.string(),
}
})
import { createEnv } from "@t3-oss/env-core/scale";
const env = createEnv({
server: $.object(
$.field('FOO', $.str)
),
}); |
Can I look into integrating Yup as an option alongside Zod? |
sure! |
Will it also export zod / other validators from within t3-env or would you still need to add zod as a dependency? Something like:
|
@aalakoski Most probably no. |
Tanner's pattern of validating the router search params, could be something to consider here. The process in t3-env would look like this.
Code-example with zod: import { createEnv } from "@t3-oss/env-core";
import { z } from "zod";
const env = createEnv({
server: z.object({ FOO: z.string().min(1) }).parse,
client: z.object({ BAR: z.string().min(1) }).parse
}); Code example with a custom validator import { createEnv } from "@t3-oss/env-core";
// custom validator which could be swapped for Yup, Joi, etc....
type ServerVars = { FOO: string }
function serverValidator(values: any): ServerVars {
// validation magic
return { FOO: "FOOOOOOOOO" }
};
type ClientVars = { BAR: string }
function clientValidator(values: any): ClientVars {
// validation magic
return { BAR: "BOOOOOOOOO" }
};
const env = createEnv({
server: serverValidator, // (values) => serverValidator(values)
client: clientValidator // (values) => clientValidator(values)
}); This pattern completely opens it up to the user to bring their own validation libraries and logic into the picture, with |
No - we won't bundle the validator |
why does it need validation library tho isn't is simple enough task to do plainly? what am I missing? |
Did a very naiive valibot replacement. |
any plan support more validation libraies? i want use valibot instead of zod |
This seems to be no longer working, as parse is no longer a function of schemas. |
why not using the patterns as in https://github.com/react-hook-form/resolvers/tree/master ? |
Yeah, maybe we have to, as Valibot provides a separate |
I just stumbled upon this issue, and maybe this could be useful https://typeschema.com ? |
but it only supports async parse |
would you guys be open to making |
I think we'd make a |
+1 for this pattern. It also opens for opportunity for contributors to contribute to more resolvers for various schema validation libraries, once the API of the resolver has been standardized. BTW, is this the next big thing the team is planning? Is there any rough target date for making it happen? I would be happy to contribute (I really want a Valibot resolver) if this is the route the team is going. |
I very interested in getting my hand dirty on this one, but the only problem is that how will the present is going to fit in, as now all the presents are defined with zod schema. |
We can have entrypoint for each |
A few schema library authors are currently working on a common interface. This may allow this library to support any schema library that supports this interface in the future. This will simplify the integration of schema libraries and prevent vendor lock-in, which could lead to more innovation in the long run. |
So this is the common interface https://github.com/standard-schema/standard-schema, which is pretty cool. |
As of v3.24 (Dec 10), zod also implements the standard-schema |
Instead of
it should be
and it should not be tied to zod, meaning something like scale-ts could work for example:
import * as $ from 'scale-codec';
this would require some more considerations for typings as we'd need to pull out the shape of each to get access to the raw keys to compare against the client prefix
Should probably implement #169 first in some half-generic way 🤔
The text was updated successfully, but these errors were encountered: