io-ts-reporters
Advanced tools
Comparing version 1.2.1 to 1.2.2
{ | ||
"name": "io-ts-reporters", | ||
"version": "1.2.1", | ||
"version": "1.2.2", | ||
"description": "Formatting of io-ts validation errors", | ||
@@ -5,0 +5,0 @@ "main": "./target/src/index.js", |
@@ -57,13 +57,21 @@ /** | ||
export const TYPE_MAX_LEN = 160; // Two lines of 80-col text | ||
const truncateType = (type: string): string => | ||
type.length > TYPE_MAX_LEN ? `${type.slice(0, TYPE_MAX_LEN - 3)}...` : type; | ||
const truncateType = (type: string, options: ReporterOptions = {}): string => { | ||
const { truncateLongTypes = true } = options; | ||
if (truncateLongTypes && type.length > TYPE_MAX_LEN) { | ||
return `${type.slice(0, TYPE_MAX_LEN - 3)}...`; | ||
} | ||
return type; | ||
}; | ||
const errorMessageSimple = ( | ||
expectedType: string, | ||
path: string, | ||
error: t.ValidationError | ||
error: t.ValidationError, | ||
options?: ReporterOptions | ||
) => | ||
// https://github.com/elm-lang/core/blob/18c9e84e975ed22649888bfad15d1efdb0128ab2/src/Native/Json.js#L199 | ||
[ | ||
`Expecting ${truncateType(expectedType)}`, | ||
`Expecting ${truncateType(expectedType, options)}`, | ||
path === '' ? '' : `at ${path}`, | ||
@@ -79,3 +87,4 @@ `but instead got: ${jsToString(error.value)}`, | ||
path: string, | ||
value: unknown | ||
value: unknown, | ||
options?: ReporterOptions | ||
) => | ||
@@ -85,3 +94,3 @@ // https://github.com/elm-lang/core/blob/18c9e84e975ed22649888bfad15d1efdb0128ab2/src/Native/Json.js#L199 | ||
'Expecting one of:\n', | ||
expectedTypes.map(type => ` ${truncateType(type)}`).join('\n'), | ||
expectedTypes.map(type => ` ${truncateType(type, options)}`).join('\n'), | ||
path === '' ? '\n' : `\nat ${path} `, | ||
@@ -104,3 +113,4 @@ `but instead got: ${jsToString(value)}` | ||
path: string, | ||
errors: NEA.NonEmptyArray<t.ValidationError> | ||
errors: NEA.NonEmptyArray<t.ValidationError>, | ||
options?: ReporterOptions | ||
) => { | ||
@@ -124,7 +134,11 @@ const expectedTypes = pipe( | ||
return expected.length > 0 | ||
? O.some(errorMessageUnion(expected, path, value)) | ||
? O.some(errorMessageUnion(expected, path, value, options)) | ||
: O.none; | ||
}; | ||
const formatValidationCommonError = (path: string, error: t.ValidationError) => | ||
const formatValidationCommonError = ( | ||
path: string, | ||
error: t.ValidationError, | ||
options?: ReporterOptions | ||
) => | ||
pipe( | ||
@@ -134,3 +148,3 @@ error, | ||
O.map(errorContext => | ||
errorMessageSimple(errorContext.type.name, path, error) | ||
errorMessageSimple(errorContext.type.name, path, error, options) | ||
) | ||
@@ -143,6 +157,10 @@ ); | ||
const format = (path: string, errors: NEA.NonEmptyArray<t.ValidationError>) => | ||
const format = ( | ||
path: string, | ||
errors: NEA.NonEmptyArray<t.ValidationError>, | ||
options?: ReporterOptions | ||
) => | ||
NEA.tail(errors).length > 0 | ||
? formatValidationErrorOfUnion(path, errors) | ||
: formatValidationCommonError(path, NEA.head(errors)); | ||
? formatValidationErrorOfUnion(path, errors, options) | ||
: formatValidationCommonError(path, NEA.head(errors), options); | ||
@@ -155,4 +173,6 @@ /** | ||
*/ | ||
export const formatValidationError = (error: t.ValidationError) => | ||
formatValidationCommonError(keyPath(error.context), error); | ||
export const formatValidationError = ( | ||
error: t.ValidationError, | ||
options?: ReporterOptions | ||
) => formatValidationCommonError(keyPath(error.context), error, options); | ||
@@ -177,7 +197,10 @@ /** | ||
*/ | ||
export const formatValidationErrors = (errors: t.Errors) => | ||
export const formatValidationErrors = ( | ||
errors: t.Errors, | ||
options?: ReporterOptions | ||
) => | ||
pipe( | ||
errors, | ||
groupByKey, | ||
R.mapWithIndex(format), | ||
R.mapWithIndex((path, errors) => format(path, errors, options)), | ||
R.compact, | ||
@@ -189,2 +212,10 @@ R.toArray, | ||
/** | ||
* @category formatters | ||
* @since 1.2.2 | ||
*/ | ||
export interface ReporterOptions { | ||
truncateLongTypes?: boolean; | ||
} | ||
/** | ||
* Deprecated, use the default export instead. | ||
@@ -196,6 +227,9 @@ * | ||
*/ | ||
export const reporter = <T>(validation: t.Validation<T>) => | ||
export const reporter = <T>( | ||
validation: t.Validation<T>, | ||
options?: ReporterOptions | ||
) => | ||
pipe( | ||
validation, | ||
E.mapLeft(formatValidationErrors), | ||
E.mapLeft(errors => formatValidationErrors(errors, options)), | ||
E.fold( | ||
@@ -207,3 +241,10 @@ errors => errors, | ||
const prettyReporter: Reporter<string[]> = { report: reporter }; | ||
interface PrettyReporter extends Reporter<string[]> { | ||
report: <T>( | ||
validation: t.Validation<T>, | ||
options?: ReporterOptions | ||
) => string[]; | ||
} | ||
const prettyReporter: PrettyReporter = { report: reporter }; | ||
export default prettyReporter; |
@@ -16,3 +16,3 @@ import * as E from 'fp-ts/lib/Either'; | ||
*/ | ||
export declare const formatValidationError: (error: t.ValidationError) => O.Option<string>; | ||
export declare const formatValidationError: (error: t.ValidationError, options?: ReporterOptions | undefined) => O.Option<string>; | ||
/** | ||
@@ -36,4 +36,11 @@ * Format validation errors (`t.Errors`). | ||
*/ | ||
export declare const formatValidationErrors: (errors: t.Errors) => string[]; | ||
export declare const formatValidationErrors: (errors: t.Errors, options?: ReporterOptions | undefined) => string[]; | ||
/** | ||
* @category formatters | ||
* @since 1.2.2 | ||
*/ | ||
export interface ReporterOptions { | ||
truncateLongTypes?: boolean; | ||
} | ||
/** | ||
* Deprecated, use the default export instead. | ||
@@ -45,4 +52,7 @@ * | ||
*/ | ||
export declare const reporter: <T>(validation: E.Either<t.Errors, T>) => string[]; | ||
declare const prettyReporter: Reporter<string[]>; | ||
export declare const reporter: <T>(validation: E.Either<t.Errors, T>, options?: ReporterOptions | undefined) => string[]; | ||
interface PrettyReporter extends Reporter<string[]> { | ||
report: <T>(validation: t.Validation<T>, options?: ReporterOptions) => string[]; | ||
} | ||
declare const prettyReporter: PrettyReporter; | ||
export default prettyReporter; |
@@ -59,9 +59,14 @@ "use strict"; | ||
exports.TYPE_MAX_LEN = 160; // Two lines of 80-col text | ||
var truncateType = function (type) { | ||
return type.length > exports.TYPE_MAX_LEN ? type.slice(0, exports.TYPE_MAX_LEN - 3) + "..." : type; | ||
var truncateType = function (type, options) { | ||
if (options === void 0) { options = {}; } | ||
var _a = options.truncateLongTypes, truncateLongTypes = _a === void 0 ? true : _a; | ||
if (truncateLongTypes && type.length > exports.TYPE_MAX_LEN) { | ||
return type.slice(0, exports.TYPE_MAX_LEN - 3) + "..."; | ||
} | ||
return type; | ||
}; | ||
var errorMessageSimple = function (expectedType, path, error) { | ||
var errorMessageSimple = function (expectedType, path, error, options) { | ||
// https://github.com/elm-lang/core/blob/18c9e84e975ed22649888bfad15d1efdb0128ab2/src/Native/Json.js#L199 | ||
return [ | ||
"Expecting " + truncateType(expectedType), | ||
"Expecting " + truncateType(expectedType, options), | ||
path === '' ? '' : "at " + path, | ||
@@ -74,7 +79,7 @@ "but instead got: " + jsToString(error.value), | ||
}; | ||
var errorMessageUnion = function (expectedTypes, path, value) { | ||
var errorMessageUnion = function (expectedTypes, path, value, options) { | ||
// https://github.com/elm-lang/core/blob/18c9e84e975ed22649888bfad15d1efdb0128ab2/src/Native/Json.js#L199 | ||
return [ | ||
'Expecting one of:\n', | ||
expectedTypes.map(function (type) { return " " + truncateType(type); }).join('\n'), | ||
expectedTypes.map(function (type) { return " " + truncateType(type, options); }).join('\n'), | ||
path === '' ? '\n' : "\nat " + path + " ", | ||
@@ -91,3 +96,3 @@ "but instead got: " + jsToString(value) | ||
}; | ||
var formatValidationErrorOfUnion = function (path, errors) { | ||
var formatValidationErrorOfUnion = function (path, errors, options) { | ||
var expectedTypes = pipeable_1.pipe(errors, A.map(getValidationContext), A.map(findExpectedType), A.compact); | ||
@@ -100,8 +105,8 @@ var value = pipeable_1.pipe(expectedTypes, A.head, O.map(function (v) { return v.actual; }), O.getOrElse(function () { return undefined; })); | ||
return expected.length > 0 | ||
? O.some(errorMessageUnion(expected, path, value)) | ||
? O.some(errorMessageUnion(expected, path, value, options)) | ||
: O.none; | ||
}; | ||
var formatValidationCommonError = function (path, error) { | ||
var formatValidationCommonError = function (path, error, options) { | ||
return pipeable_1.pipe(error, getErrorFromCtx, O.map(function (errorContext) { | ||
return errorMessageSimple(errorContext.type.name, path, error); | ||
return errorMessageSimple(errorContext.type.name, path, error, options); | ||
})); | ||
@@ -112,6 +117,6 @@ }; | ||
}); | ||
var format = function (path, errors) { | ||
var format = function (path, errors, options) { | ||
return NEA.tail(errors).length > 0 | ||
? formatValidationErrorOfUnion(path, errors) | ||
: formatValidationCommonError(path, NEA.head(errors)); | ||
? formatValidationErrorOfUnion(path, errors, options) | ||
: formatValidationCommonError(path, NEA.head(errors), options); | ||
}; | ||
@@ -124,5 +129,3 @@ /** | ||
*/ | ||
exports.formatValidationError = function (error) { | ||
return formatValidationCommonError(keyPath(error.context), error); | ||
}; | ||
exports.formatValidationError = function (error, options) { return formatValidationCommonError(keyPath(error.context), error, options); }; | ||
/** | ||
@@ -146,4 +149,4 @@ * Format validation errors (`t.Errors`). | ||
*/ | ||
exports.formatValidationErrors = function (errors) { | ||
return pipeable_1.pipe(errors, groupByKey, R.mapWithIndex(format), R.compact, R.toArray, A.map(function (_a) { | ||
exports.formatValidationErrors = function (errors, options) { | ||
return pipeable_1.pipe(errors, groupByKey, R.mapWithIndex(function (path, errors) { return format(path, errors, options); }), R.compact, R.toArray, A.map(function (_a) { | ||
var _key = _a[0], error = _a[1]; | ||
@@ -160,4 +163,4 @@ return error; | ||
*/ | ||
exports.reporter = function (validation) { | ||
return pipeable_1.pipe(validation, E.mapLeft(exports.formatValidationErrors), E.fold(function (errors) { return errors; }, function () { return []; })); | ||
exports.reporter = function (validation, options) { | ||
return pipeable_1.pipe(validation, E.mapLeft(function (errors) { return exports.formatValidationErrors(errors, options); }), E.fold(function (errors) { return errors; }, function () { return []; })); | ||
}; | ||
@@ -164,0 +167,0 @@ var prettyReporter = { report: exports.reporter }; |
Sorry, the diff of this file is not supported yet
24406
477