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 3810aeb
Showing 1 changed file with 10 additions and 2 deletions.
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 3810aeb

Please sign in to comment.