-
Notifications
You must be signed in to change notification settings - Fork 70
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
Ability to define and reuse schemas for primative types #269
Comments
Hello @elee1766 I'm not sure what you're asking 😄 👉 /**
* 0x prefixed hexadecimal string
*
* @pattern ^0x[a-fA-F0-9]*$
*/
export type Hex = string;
export interface Foo {
lorum: Hex;
}
export interface Bar {
ipsum: Hex;
} will output: // Generated by ts-to-zod
import { z } from "zod";
export const hexSchema = z.string().regex(/^0x[a-fA-F0-9]*$/);
export const fooSchema = z.object({
lorum: hexSchema,
});
export const barSchema = z.object({
ipsum: hexSchema,
}); 👉 it also supports imports, so if you you want to have /**
* @pattern ^0x[a-fA-F0-9]*$
*/
export type Hex = string; in a specific file — say /**
* ts-to-zod configuration.
*
* @type {import("./src/config").TsToZodConfig}
*/
module.exports = [
{ name: "hexa", input: "src/hexa.ts", output: "src/hexa.zod.ts" },
]; any file importing // Input
import { Hex } from "./hexa";
export interface Foo {
lorum: Hex;
}
// Output
import { z } from "zod";
import { hexSchema } from "./hexa.zod";
export const fooSchema = z.object({
lore: hexSchema
}); 👉 As for 3rd party libraries, if you have a specific example, we could evolve the Config specification to enable imports of Zod Schema from those. |
oh, maybe it was the templated literal that was messing with things then? It wasn't working for me. Let me try again and see what happens. thanks! |
Hey @elee1766, |
hey yeah! so it's working for us for string types but not string template types. it would be nice if we could also get it working for a string template type. is there any way we can hack around it ? |
Hey, A workaround you can rely on is the use of the // input
/**
* 0x prefixed hexadecimal string
*
* @schema string().regex(/^0x[a-fA-F0-9]*$/)
*/
export type Hex = `0x${string}`;
// output
// Generated by ts-to-zod
import { z } from "zod";
export const hexSchema = z.string().regex(/^0x[a-fA-F0-9]*$/); You basically can define whatever you want to be output after the |
that is good enough i think! |
Feature description
I would like to define a schema for an existing type somehow, and use that instead of defining a format everywhere.
For instance, it would be nice to be able to do something like
which would ideally output something like
This could potentially also allow schema from third party package to be supported (though their definitions may have to be declared using something other than the jsdoc)
I have not looked too deeply into the code - does something like this seem feasible to implement?
The text was updated successfully, but these errors were encountered: