Skip to content

Commit 595da06

Browse files
committed
New Feature: better support for custom messages, closes #148
- add optional message field to ValidationError - add message argument to failure - PathReporter should account for the new field
1 parent 7f95606 commit 595da06

File tree

4 files changed

+22
-6
lines changed

4 files changed

+22
-6
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "io-ts",
3-
"version": "1.6.4",
3+
"version": "1.7.0",
44
"description": "TypeScript compatible runtime type system for IO validation",
55
"files": [
66
"lib"

src/PathReporter.ts

+5-3
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,17 @@ function getContextPath(context: Context): string {
99
return context.map(({ key, type }) => `${key}: ${type.name}`).join('/')
1010
}
1111

12-
function getMessage(v: any, context: Context): string {
13-
return `Invalid value ${stringify(v)} supplied to ${getContextPath(context)}`
12+
function getMessage(e: ValidationError): string {
13+
return e.message !== undefined
14+
? e.message
15+
: `Invalid value ${stringify(e.value)} supplied to ${getContextPath(e.context)}`
1416
}
1517

1618
/**
1719
* @since 1.0.0
1820
*/
1921
export function failure(es: Array<ValidationError>): Array<string> {
20-
return es.map(e => getMessage(e.value, e.context))
22+
return es.map(getMessage)
2123
}
2224

2325
/**

src/index.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ export interface Context extends ReadonlyArray<ContextEntry> {}
2525
export interface ValidationError {
2626
readonly value: unknown
2727
readonly context: Context
28+
readonly message?: string
2829
}
2930

3031
/**
@@ -194,8 +195,8 @@ export const failures = <T>(errors: Errors): Validation<T> => new Left(errors)
194195
/**
195196
* @since 1.0.0
196197
*/
197-
export const failure = <T>(value: unknown, context: Context): Validation<T> =>
198-
failures([getValidationError(value, context)])
198+
export const failure = <T>(value: unknown, context: Context, message?: string): Validation<T> =>
199+
failures([{ value, context, message }])
199200

200201
/**
201202
* @since 1.0.0

test/PathReporter.ts

+13
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,17 @@ describe('PathReporter', () => {
1313
it('should say something whene there are no errors', () => {
1414
assert.deepEqual(PathReporter.report(t.number.decode(1)), ['No errors!'])
1515
})
16+
17+
it('should account for the optional message field', () => {
18+
const UpperCase = new t.Type<string, string, unknown>(
19+
'UpperCase',
20+
(u): u is string => t.string.is(u) && u.toLocaleUpperCase() === u,
21+
(u, c) =>
22+
t.string
23+
.validate(u, c)
24+
.chain(s => (s.toLocaleUpperCase() === s ? t.success(s) : t.failure(u, c, 'should be upper case'))),
25+
t.string.encode
26+
)
27+
assert.deepEqual(PathReporter.report(UpperCase.decode('a')), ['should be upper case'])
28+
})
1629
})

0 commit comments

Comments
 (0)