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

Allow importing code for constraints #54

Closed
franky47 opened this issue Dec 16, 2021 · 0 comments
Closed

Allow importing code for constraints #54

franky47 opened this issue Dec 16, 2021 · 0 comments

Comments

@franky47
Copy link
Contributor

franky47 commented Dec 16, 2021

This feature request may be a bit far-fetched, feel free to close if out of scope, though I might give it a go if you're interested.

I have some fields which I'd like to annotate with a /// @zod.regex() constraint. The content of the regex is defined somewhere else in the app, and needs to find its way to the generated file.

Currently, I'm copying the regex to the schema file, which is brittle as it exists in two places (and god knows it's easy enough to make mistakes in one regex, trying to keep two in sync is a footgun).

One way to make constraint extensions via documentation use external code would be to use rich-comments again, to define imports in the model.

Example API:

/// ```@zod
/// import { publicKeyRegex } from 'modules/crypto/box'
/// ```
model User {
  publicKey String /// @zod.regex(publicKeyRegex)
}

Generated code:

import * as z from 'zod'
import { publicKeyRegex } from 'modules/crypto/box'

export const userModel = z.object({
  publicKey: z.string().regex(publicKeyRegex)
})
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 a pull request may close this issue.

2 participants