-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 3ba4998
Showing
1 changed file
with
96 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
# TypeScript JSON Validator | ||
|
||
Automatically generate a validator using JSON Schema and AJV for any TypeScript type. | ||
|
||
## Usage | ||
|
||
Define a type in `src/Example.ts`, e.g.: | ||
|
||
```ts | ||
export default interface ExampleType { | ||
value: string; | ||
/** | ||
* @TJS-format email | ||
*/ | ||
email?: string; | ||
/** | ||
* @default 42 | ||
*/ | ||
answer: number; | ||
} | ||
``` | ||
|
||
To generate a validator, run: | ||
|
||
``` | ||
npx typescript-json-validator src/Example.ts ExampleType | ||
``` | ||
|
||
This will generate `src/Example.validator.ts`, which you can use: | ||
|
||
```ts | ||
import {readFileSync} from 'fs'; | ||
import validate from './Example.validator.ts'; | ||
|
||
const value: unknown = JSON.parse(readFileSync(process.argv[2], 'utf8')); | ||
|
||
// this will through a clear error if `value` is not of the | ||
// correct type. It will also fill in any default values | ||
const validatedValue = validate(value); | ||
|
||
console.log(validatedValue.value); | ||
``` | ||
|
||
Note that types will be validated automatically, but you can also use annotations to add extra runtime checks, such as e-mail formatting. For annotations see: https://github.com/YousefED/typescript-json-schema#annotations | ||
|
||
## CLI Docs | ||
|
||
<!-- USAGE --> | ||
``` | ||
Usage: typescript-json-schema <path-to-typescript-file> <type> | ||
Options: | ||
--help Show help [boolean] | ||
--version Show version number [boolean] | ||
--refs Create shared ref definitions. [boolean] [default: true] | ||
--aliasRefs Create shared ref definitions for the type aliases. | ||
[boolean] [default: false] | ||
--topRef Create a top-level ref definition. | ||
[boolean] [default: false] | ||
--titles Creates titles in the output schema. | ||
[boolean] [default: false] | ||
--defaultProps Create default properties definitions. | ||
[boolean] [default: true] | ||
--noExtraProps Disable additional properties in objects by default. | ||
[boolean] [default: false] | ||
--propOrder Create property order definitions. | ||
[boolean] [default: false] | ||
--typeOfKeyword Use typeOf keyword (https://goo.gl/DC6sni) for | ||
functions. [boolean] [default: false] | ||
--required Create required array for non-optional properties. | ||
[boolean] [default: true] | ||
--strictNullChecks Make values non-nullable by default. | ||
[boolean] [default: true] | ||
--ignoreErrors Generate even if the program has errors. | ||
[boolean] [default: false] | ||
--validationKeywords Provide additional validation keywords to include. | ||
[array] [default: []] | ||
--excludePrivate Exclude private members from the schema. | ||
[boolean] [default: false] | ||
--uniqueNames Use unique names for type symbols. | ||
[boolean] [default: false] | ||
--include Further limit tsconfig to include only matching files. | ||
[array] | ||
--rejectDateType Rejects Date fields in type definitions. | ||
[boolean] [default: false] | ||
--id ID of schema. [string] [default: ""] | ||
--useNamedExport Type name is a named export, rather than the default | ||
export of the file [boolean] [default: false] | ||
-* [default: []] | ||
``` | ||
<!-- USAGE --> | ||
|
||
## License | ||
|
||
MIT |