{"id":27732366,"url":"https://github.com/glideapps/zod-from-json-schema","last_synced_at":"2025-04-28T11:19:08.427Z","repository":{"id":285309216,"uuid":"957691705","full_name":"glideapps/zod-from-json-schema","owner":"glideapps","description":"Create Zod types from JSON Schema at runtime","archived":false,"fork":false,"pushed_at":"2025-04-11T00:39:19.000Z","size":127,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-28T11:19:04.341Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/glideapps.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2025-03-31T00:29:42.000Z","updated_at":"2025-04-11T00:39:16.000Z","dependencies_parsed_at":null,"dependency_job_id":"665ecf3f-caf4-4f78-8ae6-9364248512a3","html_url":"https://github.com/glideapps/zod-from-json-schema","commit_stats":null,"previous_names":["glideapps/zod-from-json-schema"],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/glideapps%2Fzod-from-json-schema","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/glideapps%2Fzod-from-json-schema/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/glideapps%2Fzod-from-json-schema/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/glideapps%2Fzod-from-json-schema/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/glideapps","download_url":"https://codeload.github.com/glideapps/zod-from-json-schema/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251302780,"owners_count":21567601,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2025-04-28T11:19:07.869Z","updated_at":"2025-04-28T11:19:08.420Z","avatar_url":"https://github.com/glideapps.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# zod-from-json-schema\n\n[![CI](https://github.com/glideapps/zod-from-json-schema/actions/workflows/ci.yml/badge.svg)](https://github.com/glideapps/zod-from-json-schema/actions/workflows/ci.yml)\n[![npm version](https://img.shields.io/npm/v/zod-from-json-schema.svg)](https://www.npmjs.com/package/zod-from-json-schema)\n\nA library that creates [Zod](https://github.com/colinhacks/zod) types from [JSON Schema](https://json-schema.org/) at runtime.  This is in contrast to [json-schema-to-zod](https://www.npmjs.com/package/json-schema-to-zod), which generates JavaScript source code.\n\n## Installation\n\n```bash\nnpm install zod-from-json-schema\n```\n\n## Usage\n\nThis package supports both ESM and CommonJS formats.\n\n### ESM (ES Modules)\n\n```typescript\nimport { convertJsonSchemaToZod } from 'zod-from-json-schema';\n\n// Define a JSON Schema\nconst jsonSchema = {\n  $schema: \"http://json-schema.org/draft-07/schema#\",\n  type: \"object\",\n  properties: {\n    name: { type: \"string\", minLength: 2, maxLength: 50 },\n    age: { type: \"integer\", minimum: 0, maximum: 120 },\n    email: { type: \"string\", pattern: \"^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\\\.[a-zA-Z]{2,}$\" },\n    tags: {\n      type: \"array\",\n      items: { type: \"string\" },\n      uniqueItems: true,\n      minItems: 1\n    }\n  },\n  required: [\"name\", \"email\"],\n  additionalProperties: false\n};\n\n// Convert JSON Schema to Zod schema\nconst zodSchema = convertJsonSchemaToZod(jsonSchema);\n\n// Use the Zod schema to validate data\ntry {\n  const validData = zodSchema.parse({\n    name: \"John Doe\",\n    email: \"john@example.com\",\n    age: 30,\n    tags: [\"user\", \"premium\"]\n  });\n  console.log(\"Valid data:\", validData);\n} catch (error) {\n  console.error(\"Validation error:\", error);\n}\n```\n\n### CommonJS\n\n```javascript\nconst { convertJsonSchemaToZod } = require('zod-from-json-schema');\n\n// Define a JSON Schema\nconst jsonSchema = {\n  $schema: \"http://json-schema.org/draft-07/schema#\",\n  type: \"object\",\n  properties: {\n    name: { type: \"string\", minLength: 2, maxLength: 50 },\n    age: { type: \"integer\", minimum: 0, maximum: 120 }\n  },\n  required: [\"name\"],\n  additionalProperties: false\n};\n\n// Convert JSON Schema to Zod schema\nconst zodSchema = convertJsonSchemaToZod(jsonSchema);\n\n// Use the Zod schema to validate data\ntry {\n  const validData = zodSchema.parse({\n    name: \"John Doe\",\n    age: 30\n  });\n  console.log(\"Valid data:\", validData);\n} catch (error) {\n  console.error(\"Validation error:\", error);\n}\n```\n\n## API Reference\n\n### `convertJsonSchemaToZod(schema)`\n\nConverts a JSON Schema object to a complete Zod schema.\n\n- **Parameters**:\n  - `schema` (Object): A JSON Schema object\n- **Returns**: \n  - A Zod schema that validates according to the JSON Schema\n\n### `jsonSchemaObjectToZodRawShape(schema)`\n\nExtracts the object properties from a JSON Schema object into a Zod raw shape. This is useful when you want to combine the properties with other Zod object configurations.\n\n- **Parameters**:\n  - `schema` (Object): A JSON Schema object that should have a `properties` field\n- **Returns**: \n  - A `ZodRawShape` object that can be used with `z.object()`\n\n**Example**:\n\n```typescript\nimport { jsonSchemaObjectToZodRawShape } from 'zod-from-json-schema';\nimport { z } from 'zod';\n\nconst jsonSchema = {\n  properties: {\n    name: { type: \"string\" },\n    age: { type: \"integer\" }\n  },\n  required: [\"name\"]\n};\n\n// Get just the property definitions\nconst rawShape = jsonSchemaObjectToZodRawShape(jsonSchema);\n\n// Add custom handling\nconst customSchema = z.object({\n  ...rawShape,\n  // Add additional fields not in the JSON Schema\n  createdAt: z.date().default(() =\u003e new Date())\n}).refine(data =\u003e data.age \u003e 18, {\n  message: \"Age must be over 18 to continue\"\n});\n```\n\n## Supported JSON Schema Features\n\nThis library supports the following JSON Schema features:\n\n### Basic Types\n- `string`\n- `number`\n- `integer`\n- `boolean`\n- `null`\n- `object` (with properties and required fields)\n- `array`\n\n### String Validations\n- `minLength`\n- `maxLength`\n- `pattern` (regular expressions)\n\n### Number Validations\n- `minimum`\n- `maximum`\n- `exclusiveMinimum`\n- `exclusiveMaximum`\n- `multipleOf`\n\n### Array Validations\n- `minItems`\n- `maxItems`\n- `uniqueItems`\n\n### Object Validations\n- `required` (required properties)\n- `additionalProperties` (controls passthrough behavior)\n\n### Schema Composition\n- `const` (literal values)\n- `enum` (enumerated values)\n- `anyOf` (union)\n- `allOf` (intersection)\n- `oneOf` (union)\n\n### Additional\n- `description` (carried over to Zod schemas)\n\n## License\n\nMIT\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fglideapps%2Fzod-from-json-schema","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fglideapps%2Fzod-from-json-schema","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fglideapps%2Fzod-from-json-schema/lists"}