{"id":22382366,"url":"https://github.com/jcoreio/zod-invertible","last_synced_at":"2026-04-02T02:36:45.377Z","repository":{"id":247471995,"uuid":"825940381","full_name":"jcoreio/zod-invertible","owner":"jcoreio","description":"declare zod schemas that can be inverted to format from output to input","archived":false,"fork":false,"pushed_at":"2025-05-21T19:41:00.000Z","size":110,"stargazers_count":1,"open_issues_count":1,"forks_count":0,"subscribers_count":0,"default_branch":"master","last_synced_at":"2025-06-27T05:42:00.053Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","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/jcoreio.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","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}},"created_at":"2024-07-08T19:54:47.000Z","updated_at":"2025-05-21T19:40:20.000Z","dependencies_parsed_at":"2024-07-09T00:45:38.620Z","dependency_job_id":"3ae9d4fe-30d7-4a83-a52a-520a3a3408ac","html_url":"https://github.com/jcoreio/zod-invertible","commit_stats":null,"previous_names":["jcoreio/zod-invertible"],"tags_count":4,"template":false,"template_full_name":null,"purl":"pkg:github/jcoreio/zod-invertible","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jcoreio%2Fzod-invertible","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jcoreio%2Fzod-invertible/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jcoreio%2Fzod-invertible/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jcoreio%2Fzod-invertible/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jcoreio","download_url":"https://codeload.github.com/jcoreio/zod-invertible/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jcoreio%2Fzod-invertible/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":267626026,"owners_count":24117694,"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","status":"online","status_checked_at":"2025-07-29T02:00:12.549Z","response_time":2574,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":"2024-12-05T00:12:42.469Z","updated_at":"2026-04-02T02:36:45.334Z","avatar_url":"https://github.com/jcoreio.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# zod-invertible\n\ndeclare zod schemas that can be inverted to format from output to input\n\n[![CircleCI](https://circleci.com/gh/jcoreio/zod-invertible.svg?style=svg)](https://circleci.com/gh/jcoreio/zod-invertible)\n[![Coverage Status](https://codecov.io/gh/jcoreio/zod-invertible/branch/master/graph/badge.svg)](https://codecov.io/gh/jcoreio/zod-invertible)\n[![semantic-release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg)](https://github.com/semantic-release/semantic-release)\n[![Commitizen friendly](https://img.shields.io/badge/commitizen-friendly-brightgreen.svg)](http://commitizen.github.io/cz-cli/)\n[![npm version](https://badge.fury.io/js/zod-invertible.svg)](https://badge.fury.io/js/zod-invertible)\n\n# Limitations and notes\n\n`zod-invertible` doesn't support the following schemas, and `invert` will throw an error if it encounters them:\n\n- `z.preprocess(...)`\n- `ZodType.transform(...)` (outside of the transform created by `invertible`)\n\nHowever, you may work around this by marking the schema with `ignoreEffect`:\n\n```ts\nimport { schema, ignoreEffect } from 'zod-intervible'\n\nconst innerSchema = ...\nconst schema = ignoreEffect(z.preprocess((value) =\u003e ..., innerSchema))\nconst inverse = invert(schema) // equivalent to invert(innerSchema)\n```\n\nThe inverse of `ZodType.default(...)` will not have a default, which should be okay, since the input to the inverse should\nalways be defined.\n\nThe inverse of `ZodType.catch(...)` will not catch. If you want to use a fallback value if errors are thrown during parsing or\nformatting, add your own logic in the `parse` and `format` functions you pass to `invertible`.\n\nIf the output types of `z.union([...])` options overlap, then `invert(z.union([...])).parse(output)` may format with a\ndifferent union option than the one that parsed the input.\n\nIf you have recursive schemas (using `z.lazy()`), the inverse `z.lazy()` schemas will re-invert the resolved schema each time\nthey are used. I may fix this in the future with a `WeakMap` inverse cache.\n\n# Example\n\n```sh\nnpm i zod-invertible\n# OR\npnpm i zod-invertible\n```\n\n```ts\nimport z from 'zod'\nimport { invertible, invert, ZodInvertible } from 'zod-intertible'\n\nconst StringToNumber = invertible(\n  z.string(),\n  (s, ctx) =\u003e {\n    const result = parseFloat(s)\n    if (isNaN(result)) {\n      ctx.addIssue({\n        code: z.ZodIssueCode.custom,\n        message: 'invalid float',\n      })\n    }\n    return result\n  },\n  z.number(),\n  (n) =\u003e String(n)\n)\n\nStringToNumber instanceof ZodInvertible // true\n\nStringToNumber.parse('3.5') // 3.5\n\nconst NumberToString = invert(StringToNumber)\n\nNumberToString.parse(3.5) // '3.5'\n\nconst obj = z.object({ foo: StringToNumber })\nobj.parse({ foo: '3.5' }) // { foo: 3.5 }\n// invert works deeply:\ninvert(obj).parse({ foo: 3.5 }) // { foo: '3.5' }\n```\n\n## `invertible(inputSchema, parse, outputSchema, format)`\n\nCreates an invertible schema that transforms from an input type to a different output type.\nReturns a `ZodInvertible`, which is a subclass of `ZodPipeline`.\n\n### `inputSchema`\n\nThe `ZodType` for validating the input value\n\n### `parse`\n\nThe function for transforming the input value into the output value. It is called with two arguments:\n\n- `value`: the output of `inputSchema`\n- `ctx`: the zod `RefinementCtx`\n\n`parse` may be `async`.\n\n### `outputSchema`\n\nThe `ZodType` for validating the output\n\n### `format`\n\nThe function for converting from the output value back into the input value. It is called with two arguments:\n\n- `value`: the input of `outputSchema`\n- `ctx`: the zod `RefinementCtx`\n\n`format` may be `async`.\n\n## `invert(schema)`\n\nDeeply inverts a zod schema, inverting any `ZodInvertible` schemas inside it, and otherwise preserving the structure of\nobjects, arrays, etc.\n\nIf the zod schema parses input type `I` into output type `O`, the inverted schema will parse input type `O` into output type `I`.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjcoreio%2Fzod-invertible","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjcoreio%2Fzod-invertible","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjcoreio%2Fzod-invertible/lists"}