Gathering detailed insights and metrics for gen-typed-validators
Gathering detailed insights and metrics for gen-typed-validators
Gathering detailed insights and metrics for gen-typed-validators
Gathering detailed insights and metrics for gen-typed-validators
npm install gen-typed-validators
Typescript
Module System
Node Version
NPM Version
TypeScript (95.21%)
JavaScript (4.79%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
MIT License
60 Commits
1 Forks
2 Watchers
2 Branches
2 Contributors
Updated on May 31, 2022
Latest Version
1.1.5
Package Id
gen-typed-validators@1.1.5
Unpacked Size
520.14 kB
Size
120.85 kB
File Count
120
NPM Version
7.10.0
Node Version
14.18.2
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
16
60
Automatically generate runtime validators from your Flow or TypeScript type definitions! (using typed-validators
)
Say you want to generate validators for a User
type. Just add a const UserType: t.TypeAlias<User> = null
declaration
after it and run this codemod:
1// User.ts 2 3export type Address = { 4 line1: string 5 line2?: string 6 city: string 7 zipCode: string 8} 9 10export type User = { 11 email: string 12 firstName?: string 13 lastName?: string 14 address?: Address 15} 16 17export const UserType: t.TypeAlias<User> = null
1$ gen-typed-validators User.ts 2 3/Users/andy/github/typed-validators-codemods/User.ts 4====================================== 5 6+ modified - original 7 8@@ -1,15 +1,44 @@ 9+import * as t from 'typed-validators' 10 export type Address = { 11 line1: string 12 line2?: string 13 city: string 14 zipCode: string 15 } 16 17+export const AddressType: t.TypeAlias<Address> = t.alias( 18+ 'Address', 19+ t.object({ 20+ required: { 21+ line1: t.string(), 22+ city: t.string(), 23+ zipCode: t.string(), 24+ }, 25+ 26+ optional: { 27+ line2: t.string(), 28+ }, 29+ }) 30+) 31+ 32 export type User = { 33 email: string 34 firstName?: string 35 lastName?: string 36 address?: Address 37 } 38 39-export const UserType: t.TypeAlias<User> = null 40+export const UserType: t.TypeAlias<User> = t.alias( 41+ 'User', 42+ t.object({ 43+ required: { 44+ email: t.string(), 45+ }, 46+ 47+ optional: { 48+ firstName: t.string(), 49+ lastName: t.string(), 50+ address: t.ref(() => AddressType), 51+ }, 52+ }) 53+) 54 55? write: (y/N)
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:
1// Address.ts 2 3export type Address = { 4 line1: string 5 line2?: string 6 city: string 7 zipCode: string 8} 9 10// User.ts 11 12import { Address } from './Address' 13 14export type User = { 15 email: string 16 firstName?: string 17 lastName?: string 18 address?: Address 19} 20 21export const UserType: t.TypeAlias<User> = null
1$ gen-typed-validators User.ts 2 3/Users/andy/github/typed-validators-codemods/Address.ts 4====================================== 5 6+ modified - original 7 8@@ -1,6 +1,22 @@ 9+import * as t from 'typed-validators' 10 export type Address = { 11 line1: string 12 line2?: string 13 city: string 14 zipCode: string 15 } 16+ 17+export const AddressType: t.TypeAlias<Address> = t.alias( 18+ 'Address', 19+ t.object({ 20+ required: { 21+ line1: t.string(), 22+ city: t.string(), 23+ zipCode: t.string(), 24+ }, 25+ 26+ optional: { 27+ line2: t.string(), 28+ }, 29+ }) 30+) 31 32 33 34/Users/andy/github/typed-validators-codemods/User.ts 35====================================== 36 37+ modified - original 38 39@@ -1,10 +1,25 @@ 40-import { Address } from './Address' 41+import { Address, AddressType } from './Address' 42 43+import * as t from 'typed-validators' 44+ 45 export type User = { 46 email: string 47 firstName?: string 48 lastName?: string 49 address?: Address 50 } 51 52-export const UserType: t.TypeAlias<User> = null 53+export const UserType: t.TypeAlias<User> = t.alias( 54+ 'User', 55+ t.object({ 56+ required: { 57+ email: t.string(), 58+ }, 59+ 60+ optional: { 61+ firstName: t.string(), 62+ lastName: t.string(), 63+ address: t.ref(() => AddressType), 64+ }, 65+ }) 66+) 67 68? write: (y/N)
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:
any
unknown
/mixed
|
)&
){ [string]: number }
){ 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 }
Record
typesextends
{| foo: number, ...Bar |}
, { foo: number, ...$Exact<Bar>, ... }
readonly
$ReadOnly
$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.
gen-typed-validators <files>
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.
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
Found 0/30 approved changesets -- score normalized to 0
Reason
no SAST tool detected
Details
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
47 existing vulnerabilities detected
Details
Score
Last Scanned on 2025-07-07
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More