https://github.com/jcoreio/gen-typed-validators
https://github.com/jcoreio/gen-typed-validators
codemods refactoring typed-validators validation
Last synced: about 1 year ago
JSON representation
- Host: GitHub
- URL: https://github.com/jcoreio/gen-typed-validators
- Owner: jcoreio
- License: mit
- Created: 2021-01-05T02:16:12.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2022-06-09T14:46:14.000Z (about 4 years ago)
- Last Synced: 2025-03-06T08:13:06.953Z (over 1 year ago)
- Topics: codemods, refactoring, typed-validators, validation
- Language: TypeScript
- Homepage:
- Size: 2.04 MB
- Stars: 0
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# gen-typed-validators
[](https://circleci.com/gh/jcoreio/gen-typed-validators)
[](https://codecov.io/gh/jcoreio/gen-typed-validators)
[](https://github.com/semantic-release/semantic-release)
[](http://commitizen.github.io/cz-cli/)
[](https://badge.fury.io/js/gen-typed-validators)
Automatically generate runtime validators from your Flow or TypeScript type definitions! (using `typed-validators`)
# Table of Contents
- [How it works](#how-it-works)
- [Type Walking](#type-walking)
- [Limitations](#limitations)
- [CLI](#cli)
# How it works
Say you want to generate validators for a `User` type. Just add a `const UserType: t.TypeAlias = null` declaration
after it and run this codemod:
```ts
// User.ts
export type Address = {
line1: string
line2?: string
city: string
zipCode: string
}
export type User = {
email: string
firstName?: string
lastName?: string
address?: Address
}
export const UserType: t.TypeAlias = null
```
```diff
$ gen-typed-validators User.ts
/Users/andy/github/typed-validators-codemods/User.ts
======================================
+ modified - original
@@ -1,15 +1,44 @@
+import * as t from 'typed-validators'
export type Address = {
line1: string
line2?: string
city: string
zipCode: string
}
+export const AddressType: t.TypeAlias
= t.alias(
+ 'Address',
+ t.object({
+ required: {
+ line1: t.string(),
+ city: t.string(),
+ zipCode: t.string(),
+ },
+
+ optional: {
+ line2: t.string(),
+ },
+ })
+)
+
export type User = {
email: string
firstName?: string
lastName?: string
address?: Address
}
-export const UserType: t.TypeAlias = null
+export const UserType: t.TypeAlias = t.alias(
+ 'User',
+ t.object({
+ required: {
+ email: t.string(),
+ },
+
+ optional: {
+ firstName: t.string(),
+ lastName: t.string(),
+ address: t.ref(() => AddressType),
+ },
+ })
+)
? write: (y/N)
```
# Type Walking
Notice 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
types, even if they're imported. For example:
```ts
// Address.ts
export type Address = {
line1: string
line2?: string
city: string
zipCode: string
}
// User.ts
import { Address } from './Address'
export type User = {
email: string
firstName?: string
lastName?: string
address?: Address
}
export const UserType: t.TypeAlias = null
```
```diff
$ gen-typed-validators User.ts
/Users/andy/github/typed-validators-codemods/Address.ts
======================================
+ modified - original
@@ -1,6 +1,22 @@
+import * as t from 'typed-validators'
export type Address = {
line1: string
line2?: string
city: string
zipCode: string
}
+
+export const AddressType: t.TypeAlias
= t.alias(
+ 'Address',
+ t.object({
+ required: {
+ line1: t.string(),
+ city: t.string(),
+ zipCode: t.string(),
+ },
+
+ optional: {
+ line2: t.string(),
+ },
+ })
+)
/Users/andy/github/typed-validators-codemods/User.ts
======================================
+ modified - original
@@ -1,10 +1,25 @@
-import { Address } from './Address'
+import { Address, AddressType } from './Address'
+import * as t from 'typed-validators'
+
export type User = {
email: string
firstName?: string
lastName?: string
address?: Address
}
-export const UserType: t.TypeAlias = null
+export const UserType: t.TypeAlias = t.alias(
+ 'User',
+ t.object({
+ required: {
+ email: t.string(),
+ },
+
+ optional: {
+ firstName: t.string(),
+ lastName: t.string(),
+ address: t.ref(() => AddressType),
+ },
+ })
+)
? write: (y/N)
```
# Limitations
- This codemod currently doesn't preserve formatting, though if it finds `prettier` installed in your project, it will format the generated
code using `prettier`.
- Definitely not all types are supported. The goal will always be to support a subset of types that can be reliably validated at runtime.
Supported types:
- All primitive values
- `any`
- `unknown`/`mixed`
- Arrays
- Tuples
- Unions (`|`)
- Intersections (`&`)
- Objects or interfaces without indexers or methods
- Flow exception: only a single indexer, to indicate a record type (`{ [string]: number }`)
- TS execption: indexers to allow additional properties
- `{ foo: number, [string]: any }`
- `{ foo: number, [string]: unknown }`
- `{ foo: number, [string | symbol]: any }`
- `{ foo: number, [string | symbol]: unknown }`
- `{ foo: number, [any]: any }`
- `{ foo: number, [any]: unknown }`
- TS `Record` types
- Interface `extends`
- Flow exact and inexact object types
- Flow object type spread `{| foo: number, ...Bar |}`, `{ foo: number, ...$Exact, ... }`
- Class instance types
- Type aliases
- Readonly types are converted as-is (but not enforced at runtime, since readonly is strictly a compile-time hint):
- TS `readonly`
- Flow `$ReadOnly`
- Flow `$ReadOnlyArray`
- 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).
- 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
the types in `.d.ts` or `.js.flow` files.
# CLI
```
gen-typed-validators
Options:
--version Show version number [boolean]
-q, --quiet reduce output [boolean]
-w, --write write without asking for confirmation [boolean]
-c, --check check that all validators match types [boolean]
--help Show help [boolean]
```
Without 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.