{"id":17914409,"url":"https://github.com/lawvs/zod-compare","last_synced_at":"2026-02-14T10:08:52.472Z","repository":{"id":224223733,"uuid":"762752658","full_name":"lawvs/zod-compare","owner":"lawvs","description":"⚖️ Compare two Zod schemas recursively.","archived":false,"fork":false,"pushed_at":"2026-01-11T14:22:16.000Z","size":684,"stargazers_count":9,"open_issues_count":3,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2026-01-11T17:55:29.927Z","etag":null,"topics":["runtime-validation","schema-validation","static-types","type-inference","typescript","zod"],"latest_commit_sha":null,"homepage":"http://www.waterwater.moe/zod-compare/","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/lawvs.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,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2024-02-24T15:42:53.000Z","updated_at":"2026-01-11T14:22:02.000Z","dependencies_parsed_at":"2025-11-27T17:03:47.594Z","dependency_job_id":null,"html_url":"https://github.com/lawvs/zod-compare","commit_stats":null,"previous_names":["lawvs/zod-compare"],"tags_count":12,"template":false,"template_full_name":null,"purl":"pkg:github/lawvs/zod-compare","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lawvs%2Fzod-compare","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lawvs%2Fzod-compare/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lawvs%2Fzod-compare/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lawvs%2Fzod-compare/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/lawvs","download_url":"https://codeload.github.com/lawvs/zod-compare/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lawvs%2Fzod-compare/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29442363,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-14T07:24:13.446Z","status":"ssl_error","status_checked_at":"2026-02-14T07:23:58.969Z","response_time":53,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"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":["runtime-validation","schema-validation","static-types","type-inference","typescript","zod"],"created_at":"2024-10-28T19:58:13.961Z","updated_at":"2026-02-14T10:08:52.466Z","avatar_url":"https://github.com/lawvs.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# ⚖️ Zod Compare\n\n[![Build](https://github.com/lawvs/zod-compare/actions/workflows/build.yml/badge.svg)](https://github.com/lawvs/zod-compare/actions/workflows/build.yml)\n[![npm](https://img.shields.io/npm/v/zod-compare)](https://www.npmjs.com/package/zod-compare)\n\nCompare two [Zod](https://zod.dev/) schemas recursively.\n\n`zod-compare` provides functions to compare Zod schemas, allowing you to determine whether two schemas are the same or compatible. Supports Zod v4.\n\n## Installation\n\n```bash\n# npm\nnpm install zod zod-compare\n\n# yarn\nyarn add zod zod-compare\n\n# pnpm\npnpm add zod zod-compare\n```\n\n## Usage\n\n```ts\nimport { z } from \"zod\";\nimport { isSameType, isCompatibleType } from \"zod-compare\";\n\nisSameType(z.string(), z.string()); // true\nisSameType(z.string(), z.number()); // false\nisSameType(\n  z.object({ name: z.string(), other: z.number() }),\n  z.object({ name: z.string() }),\n);\n// false\n\nisCompatibleType(\n  z.object({ name: z.string(), other: z.number() }),\n  z.object({ name: z.string() }),\n);\n// true\n```\n\nUse the top-level helpers to compare schemas:\n\n- `isSameType(a, b)`: true only if the two schemas have the same shape and types (ignores refinements like min/max/length, transforms, etc.)\n- `isCompatibleType(higherType, lowerType)`: true if the looser schema (higherType) can be accepted wherever the stricter schema (lowerType) is expected\n\n## Advanced Usage\n\n### Custom Rules\n\nYou can use `createCompareFn` to create a custom comparison function.\n\n```ts\nimport { z } from \"zod\";\nimport {\n  createCompareFn,\n  isSameTypePresetRules,\n  defineCompareRule,\n} from \"zod-compare\";\n\nconst customRule = defineCompareRule(\n  \"compare description\",\n  (a, b, next, recheck, context) =\u003e {\n    // In Zod 4, .describe() stores metadata via z.globalRegistry\n    const metaA = z.globalRegistry.get(a);\n    const metaB = z.globalRegistry.get(b);\n    if (metaA?.description !== metaB?.description) {\n      return false;\n    }\n    return next();\n  },\n);\n\nconst strictIsSameType = createCompareFn([\n  customRule,\n  ...isSameTypePresetRules,\n]);\n```\n\n## Debugging\n\nYou can pass a `context` object to the comparison functions to get more information about the comparison process.\n\n```ts\nconst context = {\n  stacks: [],\n};\nisSameType(\n  z.object({ name: z.string(), other: z.number() }),\n  z.object({ name: z.string(), other: z.string() }),\n  context,\n);\n\n// type stacks = { name: string; target: [a: ZodType, b: ZodType]; result: boolean; }[]\nconsole.log(context.stacks);\n```\n\n## Caveats\n\nThe default rules `isSameTypePresetRules` will disregard any custom validations like `min`, `max`, `length`, among others. Additionally, these default rules cannot be utilized for comparing `ZodLazy`, `ZodEffects`, `ZodDefault`, `ZodCatch`, `ZodPipeline`, `ZodTransformer`, `ZodError` types.\n\nIf there is a necessity to compare these types, custom rules can be established using `defineCompareRule`.\n\n## API\n\n### `isSameType`\n\nCompares two Zod schemas and returns `true` if they are the same.\n\n```ts\nimport { isSameType } from \"zod-compare\";\n\ntype isSameType: (a: $ZodType, b: $ZodType, context?: CompareContext) =\u003e boolean;\n```\n\n### `createCompareFn`\n\nCreates a custom comparison function.\n\n```ts\nimport { createCompareFn, defineCompareRule } from \"zod-compare\";\n\ntype defineCompareRule = (\n  name: string,\n  compare: CompareFn,\n) =\u003e CompareRule;\n\ntype createCompareFn = (rules: CompareRule[]) =\u003e typeof isSameType;\n\n// Example\nconst isSameType = createCompareFn(isSameTypePresetRules);\nconst isCompatibleType = createCompareFn(isCompatibleTypePresetRules);\n```\n\n### `isCompatibleType` (Experimental API)\n\nCompares two Zod schemas and returns `true` if they are compatible.\n\n```ts\nimport { isCompatibleType } from \"zod-compare\";\n// The `higherType` should be a looser type\n// The `lowerType` should be a stricter type\ntype isCompatibleType: (higherType: $ZodType, lowerType: $ZodType) =\u003e boolean;\n```\n\n### Preset Rules\n\nYou can use the preset rules `isSameTypePresetRules` and `isCompatibleTypePresetRules` to create custom comparison functions.\n\n```ts\nimport { isSameTypePresetRules, isCompatibleTypePresetRules } from \"zod-compare\";\n\ntype isSameTypePresetRules: CompareRule[];\ntype isCompatibleTypePresetRules: CompareRule[];\n\n// Example\nconst yourIsSameType = createCompareFn([customRule, ...isSameTypePresetRules]);\n```\n\n### Types\n\n```ts\ntype CompareContext = {\n  stacks?: {\n    name: string;\n    target: [a: $ZodTypes, b: $ZodTypes];\n    result: boolean;\n  }[];\n} \u0026 Record\u003cstring, unknown\u003e;\n\ntype CompareFn = (\n  a: $ZodTypes,\n  b: $ZodTypes,\n  next: () =\u003e boolean,\n  recheck: (a: $ZodType, b: $ZodType) =\u003e boolean,\n  context: CompareContext,\n) =\u003e boolean;\n\ntype CompareRule = {\n  name: string;\n  compare: CompareFn;\n};\n```\n\n## License\n\nMIT\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flawvs%2Fzod-compare","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flawvs%2Fzod-compare","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flawvs%2Fzod-compare/lists"}