https://github.com/rsinohara/json-to-zod
Converts JSON objects or file to simple Zod schemas
https://github.com/rsinohara/json-to-zod
Last synced: about 2 months ago
JSON representation
Converts JSON objects or file to simple Zod schemas
- Host: GitHub
- URL: https://github.com/rsinohara/json-to-zod
- Owner: rsinohara
- License: isc
- Created: 2021-08-12T10:13:53.000Z (almost 4 years ago)
- Default Branch: master
- Last Pushed: 2025-03-11T22:16:59.000Z (3 months ago)
- Last Synced: 2025-04-10T01:06:53.748Z (about 2 months ago)
- Language: TypeScript
- Size: 20.5 KB
- Stars: 67
- Watchers: 3
- Forks: 12
- Open Issues: 4
-
Metadata Files:
- Readme: readme.md
- License: LICENSE
Awesome Lists containing this project
- awesome-zod - `json-to-zod` - Convert JSON objects into Zod schemas. (Convertors and Generators)
README
# Json-to-Zod
## Summary
A very simple CLI tool to convert JSON objects or files into zod schemas.
## Usage
### CLI
```json-to-zod -s myJson.json -t mySchema.ts```Options:
- --source/-s [source file name]
- --target/-t [(optional) target file name]
- --name/-n [(optional) schema name in output]
- --convertTuples/-c [(optional) handle tuples correctly]### Programmatic
```typescript
import { jsonToZod } from "json-to-zod"const myObject = {
hello: "hi"
}const result = jsonToZod(myObject)
console.log(result)
```
### Expected output:
```
const schema = z.object({hello: z.string()});
```### Overriding zod values
Since zod can be more specific about the primitives sometime you want to be
more precise.Eg. if an string has been parsed to:
```typescript
z.string();
```But you know it is a date and therefor should be called:
```typescript
z.string().date();
```Then you can create a configuration file called: `.jtzrc.yml` and define
schema overrides there.Take a look at the `.jtzrc.yml.example` file.
### Handling Tuples
You can use the `convertTuples` option to handle tuples correctly.```typescript
import { jsonToZod } from "json-to-zod"const myTuple = [1, 'some string']
const result = jsonToZod(myTuple, "schema", false, true)
console.log(result)
```
### Expected output:
```
const schema = z.tuple([z.number(), z.string()]);
```