Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/calasanmarko/zod-clone
Generates deeply cloned definitions of Zod schemas.
https://github.com/calasanmarko/zod-clone
build clone compiler deep javascript js schema transpiler ts type typescript utility validation zod
Last synced: about 1 month ago
JSON representation
Generates deeply cloned definitions of Zod schemas.
- Host: GitHub
- URL: https://github.com/calasanmarko/zod-clone
- Owner: calasanmarko
- License: mit
- Created: 2023-10-11T19:35:55.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2023-10-12T18:32:06.000Z (over 1 year ago)
- Last Synced: 2024-04-25T10:00:49.081Z (9 months ago)
- Topics: build, clone, compiler, deep, javascript, js, schema, transpiler, ts, type, typescript, utility, validation, zod
- Language: TypeScript
- Homepage: https://www.npmjs.com/package/zod-clone
- Size: 7.81 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# zod-clone
Generates deeply cloned definitions of Zod schemas.# Installation
Available as a npm package:
```
npm install --save-dev zod-clone
```# Usage
Given Zod schemas, creates a string containing a verbose redefinition of the given schema. Useful for getting rid of dependencies when exporting inferred schemas.One can get the definition directly using `cloneZodSchema()` directly, or by adding all the schemas to be cloned to a `ZodCloneStore`, then writing them to an output file for later use. For instance, take this integration using `drizzle-orm` and `drizzle-zod`:
```
import { pgTable, uuid, text, timestamp } from "drizzle-orm/pg-core";
import { createSelectSchema } from "drizzle-zod";
import { ZodCloneStore, cloneZodSchema } from "zod-clone";const cloneStore = new ZodCloneStore();
const table = pgTable("table", {
id: uuid('id').primaryKey().defaultRandom(),
name: text('name').notNull(),
timestamp: timestamp('timestamp').notNull().defaultNow(),
});const schema = cloneStore.add('schema', createSelectSchema(table));
cloneStore.clone('./generated.ts');
// Same output as the generated .ts file, but only for a single schema
console.log(cloneZodSchema('schema', schema));
```The generated file `generated.ts` will contain:
```
import { z } from "zod";export const schema = z.object({
id: new z.ZodString({ checks: [{ kind: "uuid" }], typeName: z.ZodFirstPartyTypeKind.ZodString, coerce: false }),
name: new z.ZodString({ checks: [], typeName: z.ZodFirstPartyTypeKind.ZodString, coerce: false }),
timestamp: new z.ZodDate({ checks: [], coerce: false, typeName: z.ZodFirstPartyTypeKind.ZodDate }),
});
```Note that the generated file no longer depends on `drizzle-orm` or `drizzle-zod`, but contains all the inferred logic.
# License
Made by Marko Calasan, 2023.This product is licensed under the MIT License.