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

Is it possible to infer the type of an object schema? #1019

Open
christopher-caldwell opened this issue Dec 8, 2023 · 3 comments
Open

Is it possible to infer the type of an object schema? #1019

christopher-caldwell opened this issue Dec 8, 2023 · 3 comments
Labels
enhancement New feature or request

Comments

@christopher-caldwell
Copy link

christopher-caldwell commented Dec 8, 2023

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:

import Nope from 'nope-validator'

const userSchema = Nope.object().shape({
  name: Nope.string().atMost(15).required(),
  email: Nope.string().email("Please provide a valid email").required(),
  favoriteColor: Nope.string()
});

type User = Nope.infer<typeof userSchema>  // { name: string; email: string; favoriteColor?: string }

Were the docs unclear about it?

Nothing mentioned, that I could find.

@ftonato
Copy link
Owner

ftonato commented Dec 8, 2023

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 🚀

@ftonato ftonato added the enhancement New feature or request label Dec 8, 2023
@irv-armenta-g
Copy link

@christopher-caldwell

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 Nope Schema shape.

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)

@christopher-caldwell
Copy link
Author

@christopher-caldwell

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 Nope Schema shape.

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!

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

No branches or pull requests

3 participants