Comparing version 1.6.4 to 1.7.0
@@ -17,2 +17,14 @@ # Changelog | ||
# 1.7.0 | ||
- **New Feature** | ||
- better support for custom messages, closes #148 (@gcanti) | ||
- add optional message field to `ValidationError` | ||
- add `message` argument to `failure` | ||
- `PathReporter` should account for the new field | ||
- add `actual` optional field to `ContextEntry`, closes #194 (@gcanti) | ||
- **Deprecation** | ||
- deprecate `getValidationError` (@gcanti) | ||
- deprecate `getDefaultContext` (@gcanti) | ||
# 1.6.4 | ||
@@ -19,0 +31,0 @@ |
@@ -13,2 +13,4 @@ import { Either } from 'fp-ts/lib/Either'; | ||
readonly type: Decoder<any, any>; | ||
/** the input data */ | ||
readonly actual?: unknown; | ||
} | ||
@@ -24,4 +26,8 @@ /** | ||
export interface ValidationError { | ||
/** the offending (sub)value */ | ||
readonly value: unknown; | ||
/** where the error originated */ | ||
readonly context: Context; | ||
/** optional custom error message */ | ||
readonly message?: string; | ||
} | ||
@@ -133,2 +139,3 @@ /** | ||
* @since 1.0.0 | ||
* @deprecated | ||
*/ | ||
@@ -138,2 +145,3 @@ export declare const getValidationError: (value: unknown, context: Context) => ValidationError; | ||
* @since 1.0.0 | ||
* @deprecated | ||
*/ | ||
@@ -152,3 +160,3 @@ export declare const getDefaultContext: (decoder: Decoder<any, any>) => Context; | ||
*/ | ||
export declare const failure: <T>(value: unknown, context: Context) => Either<Errors, T>; | ||
export declare const failure: <T>(value: unknown, context: Context, message?: string | undefined) => Either<Errors, T>; | ||
/** | ||
@@ -155,0 +163,0 @@ * @since 1.0.0 |
@@ -67,3 +67,3 @@ "use strict"; | ||
Type.prototype.decode = function (i) { | ||
return this.validate(i, exports.getDefaultContext(this)); | ||
return this.validate(i, [{ key: '', type: this, actual: i }]); | ||
}; | ||
@@ -89,8 +89,15 @@ return Type; | ||
* @since 1.0.0 | ||
* @deprecated | ||
*/ | ||
exports.getValidationError = function (value, context) { return ({ value: value, context: context }); }; | ||
exports.getValidationError /* istanbul ignore next */ = function (value, context) { return ({ | ||
value: value, | ||
context: context | ||
}); }; | ||
/** | ||
* @since 1.0.0 | ||
* @deprecated | ||
*/ | ||
exports.getDefaultContext = function (decoder) { return [{ key: '', type: decoder }]; }; | ||
exports.getDefaultContext /* istanbul ignore next */ = function (decoder) { return [ | ||
{ key: '', type: decoder } | ||
]; }; | ||
/** | ||
@@ -115,4 +122,4 @@ * @since 1.0.0 | ||
*/ | ||
exports.failure = function (value, context) { | ||
return exports.failures([exports.getValidationError(value, context)]); | ||
exports.failure = function (value, context, message) { | ||
return exports.failures([{ value: value, context: context, message: message }]); | ||
}; | ||
@@ -942,3 +949,3 @@ /** | ||
if (as.length > len) { | ||
errors.push(exports.getValidationError(as[len], exports.appendContext(c, String(len), exports.never))); | ||
errors.push({ value: as[len], context: exports.appendContext(c, String(len), exports.never) }); | ||
} | ||
@@ -1154,10 +1161,9 @@ return errors.length ? exports.failures(errors) : exports.success(t); | ||
delete ir[k]; | ||
return "break-loop"; | ||
return "break"; | ||
} | ||
}; | ||
loop: for (var j = 0; j < cis.length; j++) { | ||
for (var j = 0; j < cis.length; j++) { | ||
var state_1 = _loop_1(j); | ||
switch (state_1) { | ||
case "break-loop": break loop; | ||
} | ||
if (state_1 === "break") | ||
break; | ||
} | ||
@@ -1295,3 +1301,3 @@ } | ||
if (!hasOwnProperty.call(props, key)) { | ||
errors.push(exports.getValidationError(o[key], exports.appendContext(c, key, exports.never))); | ||
errors.push({ value: o[key], context: exports.appendContext(c, key, exports.never) }); | ||
} | ||
@@ -1298,0 +1304,0 @@ } |
@@ -13,4 +13,6 @@ "use strict"; | ||
} | ||
function getMessage(v, context) { | ||
return "Invalid value " + stringify(v) + " supplied to " + getContextPath(context); | ||
function getMessage(e) { | ||
return e.message !== undefined | ||
? e.message | ||
: "Invalid value " + stringify(e.value) + " supplied to " + getContextPath(e.context); | ||
} | ||
@@ -21,3 +23,3 @@ /** | ||
function failure(es) { | ||
return es.map(function (e) { return getMessage(e.value, e.context); }); | ||
return es.map(getMessage); | ||
} | ||
@@ -24,0 +26,0 @@ exports.failure = failure; |
{ | ||
"name": "io-ts", | ||
"version": "1.6.4", | ||
"version": "1.7.0", | ||
"description": "TypeScript compatible runtime type system for IO validation", | ||
@@ -51,3 +51,3 @@ "files": [ | ||
"tslint-config-standard": "^8.0.1", | ||
"typescript": "^3.2.2" | ||
"typescript": "^3.2.4" | ||
}, | ||
@@ -54,0 +54,0 @@ "tags": [ |
@@ -85,2 +85,4 @@ [![build status](https://img.shields.io/travis/gcanti/io-ts/master.svg?style=flat-square)](https://travis-ci.org/gcanti/io-ts) | ||
The stable version is tested against TypeScript 3.2.4. | ||
| io-ts version | required TypeScript version | | ||
@@ -151,2 +153,24 @@ | ------------- | --------------------------- | | ||
# Custom error messages | ||
You can set your own error message by providing a `message` argument to `failure` | ||
Example | ||
```ts | ||
const NumberFromString = new t.Type<number, string, unknown>( | ||
'NumberFromString', | ||
t.number.is, | ||
(u, c) => | ||
t.string.validate(u, c).chain(s => { | ||
const n = +s | ||
return isNaN(n) ? t.failure(u, c, 'cannot parse to a number') : t.success(n) | ||
}), | ||
String | ||
) | ||
console.log(PathReporter.report(NumberFromString.decode('a'))) | ||
// => ['cannot parse to a number'] | ||
``` | ||
# Community | ||
@@ -153,0 +177,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
101671
2236
557