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

Made ZodEnum take readonly string array #3444

Merged
merged 1 commit into from
Apr 29, 2024
Merged

Conversation

carlpaten
Copy link
Collaborator

@carlpaten carlpaten commented Apr 25, 2024

Zod Pull Request

Enables creating dynamic schemas from as const object definitions:

const question = {
  questionId: "unit_location",
  answers: ["attic", "upstairs", "main_floor", "basement", "garage", "outside", "other"],
} as const;

function questionToZodSchema<
  Q extends string,
  A extends readonly [string, ...string[]],
>(question: QuestionSpec<Q, A>): z.ZodObject<{ [key in Q]: ZodEnum<A> }> {
  return z.object({ [question.questionId]: z.enum(question.answers) }) as any;
}

const schema = leafQuestionToZodSchema(test);
const value = schema.parse({ unit_location: "attic" });

Overview

Thank you for your contribution to our project! Before submitting your pull request, please ensure the following:

  • Your code changes are well-documented.
  • You have tested your changes.
  • You have updated any relevant documentation.

Copy link

netlify bot commented Apr 25, 2024

Deploy Preview for guileless-rolypoly-866f8a ready!

Name Link
🔨 Latest commit cd917eb
🔍 Latest deploy log https://app.netlify.com/sites/guileless-rolypoly-866f8a/deploys/662aab416f70e10008e515c7
😎 Deploy Preview https://deploy-preview-3444--guileless-rolypoly-866f8a.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@colinhacks colinhacks merged commit c1910bd into master Apr 29, 2024
19 of 20 checks passed
@colinhacks colinhacks deleted the carlpaten-patch-1 branch April 29, 2024 19:32
@colinhacks
Copy link
Owner

Thanks!

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

Successfully merging this pull request may close these issues.

2 participants