-
-
Notifications
You must be signed in to change notification settings - Fork 15
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
Is it possible to infer the type of an object schema? #1019
Comments
Hi @christopher-caldwell, appreciate your question! Currently, we don't have the capability to infer object schemas like Zod or Superstruct in our tool. I understand the importance of a lightweight approach without impacting bundle size or performance. Your suggestion is valuable, and I'll make sure to add it to our feature backlog for consideration. Once we have any updates or developments on this front, I'll be sure to share them with you. Thanks again for your input 🚀 |
I know this is the actual "opposite" of what you need, but I am making my schema and my form values "tighter" with a helper function to create and type the Here is the function: export const createSchema = <T extends Record<string, unknown>>(
schemaBody: { [K in keyof T]: Validatable<T[K]> }
) => {
return Nope.object().shape(schemaBody);
}; and here is an example const userFormValues = {
username: '',
password: '',
confirmPassword: '',
};
// UserFormSchema -> NopeObject
const UserFormSchema = createSchema<typeof userFormValues>({
username: Nope.string().email('Must be a valid email'),
password: Nope.string().required().atLeast(8),
confirmPassword: Nope.string(),
}); This is just for basic functionality however, it has not been tested in multiple scenarios or anything, so use it at your own risk. (as I am also using it) |
This is very interesting, thanks for sharing! Looks like it could definitely work. My "gripe" is that you have to define both your schema and your type separately, rather than derive the type. I ended up going with superstruct, but I appreciate you sharing! |
Question
I'd like to be able to get a type / interface from an object type, similar to the way Zod does it. Zod is quite heavy, and I dig the lightweight approach here. This should not impact the bundle size or performance, because it's all in TS.
Another example from Superstruct
Something like:
Were the docs unclear about it?
Nothing mentioned, that I could find.
The text was updated successfully, but these errors were encountered: