{"id":22382356,"url":"https://github.com/jcoreio/gen-typed-validators","last_synced_at":"2025-03-26T19:40:46.429Z","repository":{"id":57245868,"uuid":"326861577","full_name":"jcoreio/gen-typed-validators","owner":"jcoreio","description":null,"archived":false,"fork":false,"pushed_at":"2022-06-09T14:46:14.000Z","size":2136,"stargazers_count":0,"open_issues_count":0,"forks_count":1,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-03-06T08:13:06.953Z","etag":null,"topics":["codemods","refactoring","typed-validators","validation"],"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}},"created_at":"2021-01-05T02:16:12.000Z","updated_at":"2022-05-31T22:32:31.000Z","dependencies_parsed_at":"2022-08-24T16:43:46.697Z","dependency_job_id":null,"html_url":"https://github.com/jcoreio/gen-typed-validators","commit_stats":null,"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jcoreio%2Fgen-typed-validators","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jcoreio%2Fgen-typed-validators/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jcoreio%2Fgen-typed-validators/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jcoreio%2Fgen-typed-validators/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jcoreio","download_url":"https://codeload.github.com/jcoreio/gen-typed-validators/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245726710,"owners_count":20662544,"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":["codemods","refactoring","typed-validators","validation"],"created_at":"2024-12-05T00:12:41.198Z","updated_at":"2025-03-26T19:40:46.394Z","avatar_url":"https://github.com/jcoreio.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# gen-typed-validators\n\n[![CircleCI](https://circleci.com/gh/jcoreio/gen-typed-validators.svg?style=svg)](https://circleci.com/gh/jcoreio/gen-typed-validators)\n[![Coverage Status](https://codecov.io/gh/jcoreio/gen-typed-validators/branch/master/graph/badge.svg)](https://codecov.io/gh/jcoreio/gen-typed-validators)\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/gen-typed-validators.svg)](https://badge.fury.io/js/gen-typed-validators)\n\nAutomatically generate runtime validators from your Flow or TypeScript type definitions! (using `typed-validators`)\n\n# Table of Contents\n\n\u003c!-- toc --\u003e\n\n- [How it works](#how-it-works)\n- [Type Walking](#type-walking)\n- [Limitations](#limitations)\n- [CLI](#cli)\n\n\u003c!-- tocstop --\u003e\n\n# How it works\n\nSay you want to generate validators for a `User` type. Just add a `const UserType: t.TypeAlias\u003cUser\u003e = null` declaration\nafter it and run this codemod:\n\n```ts\n// User.ts\n\nexport type Address = {\n  line1: string\n  line2?: string\n  city: string\n  zipCode: string\n}\n\nexport type User = {\n  email: string\n  firstName?: string\n  lastName?: string\n  address?: Address\n}\n\nexport const UserType: t.TypeAlias\u003cUser\u003e = null\n```\n\n```diff\n$ gen-typed-validators User.ts\n\n/Users/andy/github/typed-validators-codemods/User.ts\n======================================\n\n+ modified - original\n\n@@ -1,15 +1,44 @@\n+import * as t from 'typed-validators'\n export type Address = {\n   line1: string\n   line2?: string\n   city: string\n   zipCode: string\n }\n\n+export const AddressType: t.TypeAlias\u003cAddress\u003e = t.alias(\n+  'Address',\n+  t.object({\n+    required: {\n+      line1: t.string(),\n+      city: t.string(),\n+      zipCode: t.string(),\n+    },\n+\n+    optional: {\n+      line2: t.string(),\n+    },\n+  })\n+)\n+\n export type User = {\n   email: string\n   firstName?: string\n   lastName?: string\n   address?: Address\n }\n\n-export const UserType: t.TypeAlias\u003cUser\u003e = null\n+export const UserType: t.TypeAlias\u003cUser\u003e = t.alias(\n+  'User',\n+  t.object({\n+    required: {\n+      email: t.string(),\n+    },\n+\n+    optional: {\n+      firstName: t.string(),\n+      lastName: t.string(),\n+      address: t.ref(() =\u003e AddressType),\n+    },\n+  })\n+)\n\n? write: (y/N)\n```\n\n# Type Walking\n\nNotice that the above example also creates an `AddressType` validator for the `Address` type, since `Address` is used in the `User` type. `gen-typed-validators` will walk all the dependent\ntypes, even if they're imported. For example:\n\n```ts\n// Address.ts\n\nexport type Address = {\n  line1: string\n  line2?: string\n  city: string\n  zipCode: string\n}\n\n// User.ts\n\nimport { Address } from './Address'\n\nexport type User = {\n  email: string\n  firstName?: string\n  lastName?: string\n  address?: Address\n}\n\nexport const UserType: t.TypeAlias\u003cUser\u003e = null\n```\n\n```diff\n$ gen-typed-validators User.ts\n\n/Users/andy/github/typed-validators-codemods/Address.ts\n======================================\n\n+ modified - original\n\n@@ -1,6 +1,22 @@\n+import * as t from 'typed-validators'\n export type Address = {\n   line1: string\n   line2?: string\n   city: string\n   zipCode: string\n }\n+\n+export const AddressType: t.TypeAlias\u003cAddress\u003e = t.alias(\n+  'Address',\n+  t.object({\n+    required: {\n+      line1: t.string(),\n+      city: t.string(),\n+      zipCode: t.string(),\n+    },\n+\n+    optional: {\n+      line2: t.string(),\n+    },\n+  })\n+)\n\n\n\n/Users/andy/github/typed-validators-codemods/User.ts\n======================================\n\n+ modified - original\n\n@@ -1,10 +1,25 @@\n-import { Address } from './Address'\n+import { Address, AddressType } from './Address'\n\n+import * as t from 'typed-validators'\n+\n export type User = {\n   email: string\n   firstName?: string\n   lastName?: string\n   address?: Address\n }\n\n-export const UserType: t.TypeAlias\u003cUser\u003e = null\n+export const UserType: t.TypeAlias\u003cUser\u003e = t.alias(\n+  'User',\n+  t.object({\n+    required: {\n+      email: t.string(),\n+    },\n+\n+    optional: {\n+      firstName: t.string(),\n+      lastName: t.string(),\n+      address: t.ref(() =\u003e AddressType),\n+    },\n+  })\n+)\n\n? write: (y/N)\n```\n\n# Limitations\n\n- This codemod currently doesn't preserve formatting, though if it finds `prettier` installed in your project, it will format the generated\n  code using `prettier`.\n- Definitely not all types are supported. The goal will always be to support a subset of types that can be reliably validated at runtime.\n\n  Supported types:\n\n  - All primitive values\n  - `any`\n  - `unknown`/`mixed`\n  - Arrays\n  - Tuples\n  - Unions (`|`)\n  - Intersections (`\u0026`)\n  - Objects or interfaces without indexers or methods\n    - Flow exception: only a single indexer, to indicate a record type (`{ [string]: number }`)\n    - TS execption: indexers to allow additional properties\n      - `{ foo: number, [string]: any }`\n      - `{ foo: number, [string]: unknown }`\n      - `{ foo: number, [string | symbol]: any }`\n      - `{ foo: number, [string | symbol]: unknown }`\n      - `{ foo: number, [any]: any }`\n      - `{ foo: number, [any]: unknown }`\n  - TS `Record` types\n  - Interface `extends`\n  - Flow exact and inexact object types\n  - Flow object type spread `{| foo: number, ...Bar |}`, `{ foo: number, ...$Exact\u003cBar\u003e, ... }`\n  - Class instance types\n  - Type aliases\n  - Readonly types are converted as-is (but not enforced at runtime, since readonly is strictly a compile-time hint):\n    - TS `readonly`\n    - Flow `$ReadOnly`\n    - Flow `$ReadOnlyArray`\n\n- Right now the generated validator name is `${typeName}Type` and this isn't customizable. In the future I could change it to infer from the starting validator declaration(s).\n- Imports from `node_modules` aren't currently supported. It may be possible in the future when a package already contains generated validators, and it can find them along with\n  the types in `.d.ts` or `.js.flow` files.\n\n# CLI\n\n```\ngen-typed-validators \u003cfiles\u003e\n\nOptions:\n      --version  Show version number                                   [boolean]\n  -q, --quiet    reduce output                                         [boolean]\n  -w, --write    write without asking for confirmation                 [boolean]\n  -c, --check    check that all validators match types                 [boolean]\n      --help     Show help                                             [boolean]\n```\n\nWithout the `-w` or `-c` option, it will print a diff for any changes it would make, and ask if you want to write the changes.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjcoreio%2Fgen-typed-validators","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjcoreio%2Fgen-typed-validators","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjcoreio%2Fgen-typed-validators/lists"}