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

Getting keys from schema #993

Open
chertik77 opened this issue Dec 23, 2024 · 5 comments
Open

Getting keys from schema #993

chertik77 opened this issue Dec 23, 2024 · 5 comments

Comments

@chertik77
Copy link

chertik77 commented Dec 23, 2024

export const BoardSchema = v.object({
  title: v.string([
    v.toTrimmed(),
    v.minLength(3, 'Please enter at least 3 characters.')
  ]),
  icon: v.picklist(icons),
  background: v.string()
})

How i can get an array of keys in my schema like this?

const fields = ['icon', 'title', 'background'] as const

@andersk
Copy link
Contributor

andersk commented Dec 23, 2024

v.keyof(BoardSchema).options

Documentation: keyofPicklistSchema.options.

@chertik77
Copy link
Author

v.keyof(BoardSchema).options

Thanks a lot!

@fabian-hiller
Copy link
Owner

fabian-hiller commented Dec 23, 2024

This might work too and is probably better if you don't care about the keyof schema:

const keys = Object.keys(BoardSchema.entries);

@andersk
Copy link
Contributor

andersk commented Dec 23, 2024

Object.keys returns type string[], while .options gives the specific literal type ["title", "icon", "background"] (which I assumed you wanted from as const).

@chertik77
Copy link
Author

Object.keys returns type string[], while .options gives the specific literal type ["title", "icon", "background"] (which I assumed you wanted from as const).

Exactly, I'm gonna know 2 approaches, thanks guys

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

No branches or pull requests

3 participants