Skip to content

Commit

Permalink
export pathReporterFailure
Browse files Browse the repository at this point in the history
  • Loading branch information
gcanti committed Mar 4, 2017
1 parent 0e37320 commit 2c4a4ef
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
9 changes: 8 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,14 @@
- add `readonly` combinator
- add `never` type
- **Breaking Changes**
- remove `maybe` combinator
- remove `maybe` combinator, can be defined in userland as
```ts
export function maybe<RT extends t.Any>(type: RT, name?: string): t.UnionType<[RT, typeof t.null], t.TypeOf<RT> | null> {
return t.union([type, t.null], name)
}
```
- **Polish**
- export `pathReporterFailure` function from default reporters
- **Bug Fix**
- revert pruning excess properties
- revert `intersection` combinator accepting only `InterfaceType`s
Expand Down
12 changes: 10 additions & 2 deletions src/reporters/default.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Reporter } from './Reporter'
import { isLeft } from 'fp-ts/lib/Either'
import { Context, getFunctionName } from '../index'
import { Context, getFunctionName, ValidationError } from '../index'

function stringify(value: any): string {
return typeof value === 'function' ? getFunctionName(value) : JSON.stringify(value)
Expand All @@ -10,9 +10,17 @@ function getContextPath(context: Context): string {
return context.map(({ key, type }) => `${key}: ${type.name}`).join('/')
}

function getMessage(value: any, context: Context): string {
return `Invalid value ${stringify(value)} supplied to ${getContextPath(context)}`
}

export function pathReporterFailure(es: Array<ValidationError>): Array<string> {
return es.map(e => getMessage(e.value, e.context))
}

export const PathReporter: Reporter<Array<string>> = {
report: validation => validation.fold(
es => es.map(e => `Invalid value ${stringify(e.value)} supplied to ${getContextPath(e.context)}`),
pathReporterFailure,
() => ['No errors!'],
)
}
Expand Down

0 comments on commit 2c4a4ef

Please sign in to comment.