io-ts-reporters
Advanced tools
Comparing version 1.0.0 to 1.1.0
@@ -5,11 +5,15 @@ { | ||
"typings": "./target/src/index.d.ts", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"scripts": { | ||
"lint": "tslint --project tsconfig.json", | ||
"lint": "npm run typecheck && npm run lint:only", | ||
"lint:only": "xo", | ||
"typecheck": "tsc --noEmit", | ||
"compile": "rm -rf ./target/* && tsc", | ||
"test": "npm run compile && npm run lint && node ./target/tests/index.js", | ||
"test": "npm run lint && npm run test:unit", | ||
"test:unit": "ava", | ||
"prepublishOnly": "npm run compile && npm run lint" | ||
}, | ||
"files": [ | ||
"target/src" | ||
"target/src", | ||
"src" | ||
], | ||
@@ -21,5 +25,9 @@ "dependencies": { | ||
"devDependencies": { | ||
"tslint": "^5.8.0", | ||
"typescript": "^3.5.3" | ||
"@types/tape": "^4.2.34", | ||
"ava": "^3.8.2", | ||
"io-ts-types": "^0.5.6", | ||
"ts-node": "^8.6.2", | ||
"typescript": "^3.5.3", | ||
"xo": "^0.30.0" | ||
} | ||
} |
@@ -0,3 +1,3 @@ | ||
import * as E from 'fp-ts/lib/Either'; | ||
import * as t from 'io-ts'; | ||
export declare const formatValidationError: (error: t.ValidationError) => import("fp-ts/lib/Option").Option<string>; | ||
export declare const reporter: <T>(validation: import("fp-ts/lib/Either").Either<t.Errors, T>) => string[]; | ||
export declare const reporter: <T>(validation: E.Either<t.Errors, T>) => string[]; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var array = require("fp-ts/lib/Array"); | ||
var Either_1 = require("fp-ts/lib/Either"); | ||
var Option_1 = require("fp-ts/lib/Option"); | ||
exports.reporter = void 0; | ||
var A = require("fp-ts/lib/Array"); | ||
var E = require("fp-ts/lib/Either"); | ||
var NEA = require("fp-ts/lib/NonEmptyArray"); | ||
var O = require("fp-ts/lib/Option"); | ||
var R = require("fp-ts/lib/Record"); | ||
var pipeable_1 = require("fp-ts/lib/pipeable"); | ||
var jsToString = function (value) { return (value === undefined ? 'undefined' : JSON.stringify(value)); }; | ||
exports.formatValidationError = function (error) { | ||
var path = error.context | ||
var t = require("io-ts"); | ||
var utils_1 = require("./utils"); | ||
var isUnionType = function (_a) { | ||
var type = _a.type; | ||
return type instanceof t.UnionType; | ||
}; | ||
var jsToString = function (value) { | ||
return value === undefined ? 'undefined' : JSON.stringify(value); | ||
}; | ||
var keyPath = function (ctx) { | ||
// The context entry with an empty key is the original | ||
// type ("default context"), not a type error. | ||
return ctx | ||
.map(function (c) { return c.key; }) | ||
// The context entry with an empty key is the original type ("default | ||
// context"), not an type error. | ||
.filter(function (key) { return key.length > 0; }) | ||
.filter(Boolean) | ||
.join('.'); | ||
// The actual error is last in context | ||
var maybeErrorContext = array.last( | ||
}; | ||
// The actual error is last in context | ||
var getErrorFromCtx = function (validation) { | ||
// https://github.com/gcanti/fp-ts/pull/544/files | ||
error.context); | ||
return pipeable_1.pipe(maybeErrorContext, Option_1.map(function (errorContext) { | ||
var expectedType = errorContext.type.name; | ||
return ( | ||
// https://github.com/elm-lang/core/blob/18c9e84e975ed22649888bfad15d1efdb0128ab2/src/Native/Json.js#L199 | ||
// tslint:disable-next-line:prefer-template | ||
"Expecting " + expectedType | ||
+ (path === '' ? '' : " at " + path) | ||
+ (" but instead got: " + jsToString(error.value) + ".")); | ||
return A.last(validation.context); | ||
}; | ||
var getValidationContext = function (validation) { | ||
// https://github.com/gcanti/fp-ts/pull/544/files | ||
return validation.context; | ||
}; | ||
var errorMessageSimple = function (expectedType, path, error) { | ||
// https://github.com/elm-lang/core/blob/18c9e84e975ed22649888bfad15d1efdb0128ab2/src/Native/Json.js#L199 | ||
return [ | ||
"Expecting " + expectedType, | ||
path === '' ? '' : "at " + path, | ||
"but instead got: " + jsToString(error.value), | ||
error.message ? "(" + error.message + ")" : '' | ||
] | ||
.filter(Boolean) | ||
.join(' '); | ||
}; | ||
var errorMessageUnion = function (expectedTypes, path, value) { | ||
// https://github.com/elm-lang/core/blob/18c9e84e975ed22649888bfad15d1efdb0128ab2/src/Native/Json.js#L199 | ||
return [ | ||
'Expecting one of:\n', | ||
expectedTypes.map(function (type) { return " " + type; }).join('\n'), | ||
path === '' ? '\n' : "\nat " + path + " ", | ||
"but instead got: " + jsToString(value) | ||
] | ||
.filter(Boolean) | ||
.join(''); | ||
}; | ||
// Find the union type in the list of ContextEntry | ||
// The next ContextEntry should be the type of this branch of the union | ||
var findExpectedType = function (ctx) { | ||
return pipeable_1.pipe(ctx, A.findIndex(isUnionType), O.chain(function (n) { return A.lookup(n + 1, ctx); })); | ||
}; | ||
var formatValidationErrorOfUnion = function (path, errors) { | ||
var expectedTypes = pipeable_1.pipe(errors, A.map(getValidationContext), A.map(findExpectedType), A.compact); | ||
var value = pipeable_1.pipe(expectedTypes, A.head, O.map(function (v) { return v.actual; }), O.getOrElse(function () { return undefined; })); | ||
var expected = expectedTypes.map(function (_a) { | ||
var type = _a.type; | ||
return type.name; | ||
}); | ||
return expected.length > 0 | ||
? O.some(errorMessageUnion(expected, path, value)) | ||
: O.none; | ||
}; | ||
var formatValidationError = function (path, error) { | ||
return pipeable_1.pipe(error, getErrorFromCtx, O.map(function (errorContext) { | ||
return errorMessageSimple(errorContext.type.name, path, error); | ||
})); | ||
}; | ||
exports.reporter = function (validation) { return (pipeable_1.pipe(validation, Either_1.fold(function (errors) { return array.compact(errors.map(exports.formatValidationError)); }, function () { return []; }))); }; | ||
var format = function (path, errors) { | ||
return NEA.tail(errors).length > 0 | ||
? formatValidationErrorOfUnion(path, errors) | ||
: formatValidationError(path, NEA.head(errors)); | ||
}; | ||
var groupByKey = NEA.groupBy(function (error) { | ||
return pipeable_1.pipe(error.context, utils_1.takeUntil(isUnionType), keyPath); | ||
}); | ||
exports.reporter = function (validation) { | ||
return pipeable_1.pipe(validation, E.mapLeft(groupByKey), E.mapLeft(R.mapWithIndex(format)), E.mapLeft(R.compact), E.mapLeft(R.toArray), E.mapLeft(A.map(function (_a) { | ||
var _key = _a[0], error = _a[1]; | ||
return error; | ||
})), E.fold(function (errors) { return errors; }, function () { return []; })); | ||
}; | ||
//# sourceMappingURL=index.js.map |
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
15371
11
239
6
2