{"_id":"@adesso-se/zod-from-json-samples","_rev":"2-d4bf3210419c2da20d92fbe9013974be","name":"@adesso-se/zod-from-json-samples","dist-tags":{"latest":"1.0.0"},"versions":{"1.0.0":{"name":"@adesso-se/zod-from-json-samples","version":"1.0.0","keywords":["zod","schema","generator","inference","json","typescript","cli","codegen","openapi","validation"],"author":{"name":"Friedrich Lasnia","email":"friedrich.lasnia@adesso.de"},"license":"MIT","_id":"@adesso-se/zod-from-json-samples@1.0.0","maintainers":[{"name":"philipdallmer","email":"philipdallmer@googlemail.com"},{"name":"burningenlighenment","email":"henrik@gassmann.onl"},{"name":"adtobi","email":"tobias.struckmeier@adesso.de"},{"name":"falvhb","email":"falvhb@furikuri.de"}],"homepage":"https://github.com/adessoSE/zod-from-json-samples","bugs":{"url":"https://github.com/adessoSE/zod-from-json-samples/issues"},"bin":{"samples2zod":"src/index.js"},"dist":{"shasum":"424602b279026d7e1e99b03f5507f0a9a65501e3","tarball":"https://registry.npmjs.org/@adesso-se/zod-from-json-samples/-/zod-from-json-samples-1.0.0.tgz","fileCount":3,"integrity":"sha512-4DdETjxqx8D/lkgwXfqCXcdi5PVmsOK8X6s75fKtWazwCkiG4xiHsj61UnotG5/2VSgDwut5pwssrH0lSceFHw==","signatures":[{"sig":"MEYCIQDoSGiuLHrrVTWLUj9e0cHZa+13TMKIaKFKF0dHnJFv8gIhAIIjKgOOnhUJz9VbtOGvV+xoIJV4mpQ7uNFea6qv6ZO9","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"}],"attestations":{"url":"https://registry.npmjs.org/-/npm/v1/attestations/@adesso-se%2fzod-from-json-samples@1.0.0","provenance":{"predicateType":"https://slsa.dev/provenance/v1"}},"unpackedSize":12509},"main":"src/index.js","gitHead":"ea97d72546e0fa49313e0d1fe81ed13563e6d730","scripts":{"tag":"git tag v$(node -p \"require('./package.json').version\") && git push origin v$(node -p \"require('./package.json').version\")"},"_npmUser":{"name":"falvhb","email":"falvhb@furikuri.de"},"repository":{"url":"git+https://github.com/adessoSE/zod-from-json-samples.git","type":"git"},"_npmVersion":"11.6.2","description":"Generate a single, robust Zod schema from multiple JSON files, with support for optional fields, union types, and OpenAPI examples.","directories":{},"_nodeVersion":"25.1.0","dependencies":{"zod":"^4.1.12","glob":"^11.0.3","yargs":"^18.0.0"},"_hasShrinkwrap":false,"_npmOperationalInternal":{"tmp":"tmp/zod-from-json-samples_1.0.0_1762163002724_0.22888759501315192","host":"s3://npm-registry-packages-npm-production"}}},"time":{"created":"2025-11-03T09:43:22.632Z","modified":"2025-11-30T16:27:39.194Z","1.0.0":"2025-11-03T09:43:22.909Z"},"bugs":{"url":"https://github.com/adessoSE/zod-from-json-samples/issues"},"author":{"name":"Friedrich Lasnia","email":"friedrich.lasnia@adesso.de"},"license":"MIT","homepage":"https://github.com/adessoSE/zod-from-json-samples","keywords":["zod","schema","generator","inference","json","typescript","cli","codegen","openapi","validation"],"repository":{"url":"git+https://github.com/adessoSE/zod-from-json-samples.git","type":"git"},"description":"Generate a single, robust Zod schema from multiple JSON files, with support for optional fields, union types, and OpenAPI examples.","maintainers":[{"email":"falvhb@furikuri.de","name":"falvhb"}],"readme":"# @adesso-se/zod-from-json-samples\n\nThis utility generates a single, robust Zod schema from one or more JSON files. It intelligently infers types by merging the structures of multiple JSON samples, with built-in support for optional fields, union types, and adding examples for OpenAPI documentation.\n\n## Usage\n\nInstall the script using and run it by providing a glob pattern to select your JSON sample files. Remember to enclose the pattern in quotes.\n\n```bash\nnpm install @adesso-se/zod-from-json-samples -g\n\nsamples2zod \"path/to/your/json_samples/**/*.json\" -f\n```\n\nThe script will process all matching files and generate a `schema.js` file in your current working directory, containing the final Zod schema.\n\n### Options\n\n- `--force-optional`, `-f`\n  Force all inferred properties to be optional. This is useful if your JSON schema is unstable or if you want to validate objects that may only contain a subset of the properties found in your samples.\n\n### Example\n\nGiven two JSON files:\n\n`user1.json`:\n\n```json\n{\n  \"id\": 1,\n  \"name\": \"John Doe\",\n  \"email\": \"john.doe@example.com\"\n}\n```\n\n`user2.json`:\n\n```json\n{\n  \"id\": 2,\n  \"name\": \"Jane Doe\",\n  \"email\": null,\n  \"age\": 30\n}\n```\n\nRunning the standard command:\n\n```bash\nnpx @adesso-se/zod-from-json-samples \"user*.json\"\n```\n\nWill produce `schema.js` with the following content, where `age` is automatically detected as optional:\n\n```javascript\nexport const MySchema = z.object({\n  id: z.number().openapi({ examples: [1, 2] }),\n  name: z.string().openapi({ examples: [\"John Doe\", \"Jane Doe\"] }),\n  email: z\n    .string()\n    .openapi({ examples: [\"john.doe@example.com\"] })\n    .nullable(),\n  age: z\n    .number()\n    .openapi({ examples: [30] })\n    .optional(),\n});\n```\n\n### Example with `--force-optional`\n\nRunning the command with the new flag:\n\n```bash\nnpx @adesso-se/zod-from-json-samples \"user*.json\" --force-optional\n```\n\nWill produce `schema.js` where **all** properties are now optional:\n\n```javascript\nexport const MySchema = z.object({\n  id: z\n    .number()\n    .openapi({ examples: [1, 2] })\n    .optional(),\n  name: z\n    .string()\n    .openapi({ examples: [\"John Doe\", \"Jane Doe\"] })\n    .optional(),\n  email: z\n    .string()\n    .openapi({ examples: [\"john.doe@example.com\"] })\n    .nullable()\n    .optional(),\n  age: z\n    .number()\n    .openapi({ examples: [30] })\n    .optional(),\n});\n```\n\n## Features\n\n- **Type Inference**: Analyzes JSON files to infer data types (string, number, boolean, array, object, null).\n- **Schema Merging**: Combines multiple JSON structures into a single, comprehensive schema.\n- **Optional Fields**: Automatically marks fields as optional if they don't appear in every JSON sample.\n- **Union Types**: Creates union types when a field has different data types across samples.\n- **OpenAPI Examples**: Includes values from your JSON files as examples in the generated schema using `.openapi({ examples: [...] })`.\n\n## Zod Tip: Making an Existing Schema Optional\n\nIf you have already generated a schema and want to make all its properties optional without re-running the tool, Zod provides a handy `.partial()` method.\n\nThis is especially useful for update operations (e.g., PATCH requests) where only a subset of fields may be present.\n\n```javascript\nimport { z } from \"zod\";\n\n// Assuming MySchema is your generated schema\nimport { MySchema } from \"./schema.js\";\n\n// Create a new schema with all properties being optional\nconst PartialSchema = MySchema.partial();\n\n// Now you can validate an object with only some of the properties\nconst result = PartialSchema.safeParse({ name: \"Jane Doe\" }); // ✅ Success!\n```\n","readmeFilename":"README.md"}