Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added --return-errors flag #122

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,18 @@ Annotate interfaces in your project or pass. ts-auto-guard will generate guards
// my-project/Person.ts

/** @see {isPerson} ts-auto-guard:type-guard */
export interface Person { // !do not forget to export - only exported types are processed
export interface Person {
// !do not forget to export - only exported types are processed
name: string
age?: number
children: Person[]
}
```

### Process all types

Use `--export-all` parameter to process all exported types:

```
$ ts-auto-guard --export-all
```
Expand Down Expand Up @@ -130,3 +134,23 @@ export function isPerson(obj: any): obj is Person {
```

Using the `shortcircuit` option in combination with [uglify-js's `dead_code` and `global_defs` options](https://github.com/mishoo/UglifyJS2#compress-options) will let you omit the long and complicated checks from your production code.

## Return errors

Use --return-errors flag to create an extra function that will not acts as typesafe guards, but output an array of appropriate errors (the same errors as given in the debug mode).
Intended use is for example integrity testing non typesafe database outputs, that can then be printed to the screen.

```
$ ts-auto-guard --return-errors
```

```ts
// Generic usage
isObjErrorOut(obj): errors: Error[]

const returnVar = isPersonErrorOut({ name: "John", age: 20 })
// returnVar.length: 0

const returnVar = isPersonErrorOut({ name: 20, age: 20 })
// returnVar[0].message: "person.name type mismatch, expected: string, found: 20"
```
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
{
"name": "ts-auto-guard",
"version": "1.0.0-alpha.12",
"version": "1.0.0-alpha.13",
"description": "Generate type guard functions from TypeScript interfaces",
"homepage": "https://github.com/rhys-vdw/ts-auto-guard",
"repository": "github:rhys-vdw/ts-auto-guard",
"main": "lib/index.js",
"scripts": {
"test": "cross-env NODE_ENV=test && npm run lint && npm run format:check && tape -r ts-node/register tests/**/*.ts | tap-diff",
"build": "tsc",
"watch": "tsc --watch",
"prepare": "npm run build",
"lint": "eslint .",
"format": "eslint --fix --ext ts && prettier --write *.json *.js *.yml src/**/*.ts .github/**/*.yml",
Expand Down
9 changes: 9 additions & 0 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ interface ICliOptions {
project: string | undefined
help: boolean
debug: boolean
'return-errors': boolean
'export-all': boolean
}

Expand Down Expand Up @@ -57,6 +58,13 @@ const optionList = [
name: 'debug',
type: Boolean,
},
{
alias: 'r',
description:
'Return errors in the form of object { isType: boolean, errors: Error[] }',
name: 'return-errors',
type: Boolean,
},
]

const options: ICliOptions = defaults(
Expand All @@ -76,6 +84,7 @@ async function run() {
processOptions: {
debug: options.debug,
exportAll: options['export-all'],
returnErrors: options['return-errors'],
shortCircuitCondition: options.shortcircuit,
},
project,
Expand Down
Loading