| 1 | "use strict";
|
|---|
| 2 | var __extends = (this && this.__extends) || (function () {
|
|---|
| 3 | var extendStatics = function (d, b) {
|
|---|
| 4 | extendStatics = Object.setPrototypeOf ||
|
|---|
| 5 | ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
|---|
| 6 | function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
|
|---|
| 7 | return extendStatics(d, b);
|
|---|
| 8 | };
|
|---|
| 9 | return function (d, b) {
|
|---|
| 10 | extendStatics(d, b);
|
|---|
| 11 | function __() { this.constructor = d; }
|
|---|
| 12 | d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|---|
| 13 | };
|
|---|
| 14 | })();
|
|---|
| 15 | Object.defineProperty(exports, "__esModule", { value: true });
|
|---|
| 16 | exports.DetailContext = exports.NoopContext = exports.VError = void 0;
|
|---|
| 17 | /**
|
|---|
| 18 | * Error thrown by validation. Besides an informative message, it includes the path to the
|
|---|
| 19 | * property which triggered the failure.
|
|---|
| 20 | */
|
|---|
| 21 | var VError = /** @class */ (function (_super) {
|
|---|
| 22 | __extends(VError, _super);
|
|---|
| 23 | function VError(path, message) {
|
|---|
| 24 | var _this = _super.call(this, message) || this;
|
|---|
| 25 | _this.path = path;
|
|---|
| 26 | // See https://github.com/Microsoft/TypeScript/wiki/Breaking-Changes#extending-built-ins-like-error-array-and-map-may-no-longer-work for info about this workaround.
|
|---|
| 27 | Object.setPrototypeOf(_this, VError.prototype);
|
|---|
| 28 | return _this;
|
|---|
| 29 | }
|
|---|
| 30 | return VError;
|
|---|
| 31 | }(Error));
|
|---|
| 32 | exports.VError = VError;
|
|---|
| 33 | /**
|
|---|
| 34 | * Fast implementation of IContext used for first-pass validation. If that fails, we can validate
|
|---|
| 35 | * using DetailContext to collect error messages. That's faster for the common case when messages
|
|---|
| 36 | * normally pass validation.
|
|---|
| 37 | */
|
|---|
| 38 | var NoopContext = /** @class */ (function () {
|
|---|
| 39 | function NoopContext() {
|
|---|
| 40 | }
|
|---|
| 41 | NoopContext.prototype.fail = function (relPath, message, score) {
|
|---|
| 42 | return false;
|
|---|
| 43 | };
|
|---|
| 44 | NoopContext.prototype.unionResolver = function () { return this; };
|
|---|
| 45 | NoopContext.prototype.createContext = function () { return this; };
|
|---|
| 46 | NoopContext.prototype.resolveUnion = function (ur) { };
|
|---|
| 47 | return NoopContext;
|
|---|
| 48 | }());
|
|---|
| 49 | exports.NoopContext = NoopContext;
|
|---|
| 50 | /**
|
|---|
| 51 | * Complete implementation of IContext that collects meaningfull errors.
|
|---|
| 52 | */
|
|---|
| 53 | var DetailContext = /** @class */ (function () {
|
|---|
| 54 | function DetailContext() {
|
|---|
| 55 | // Stack of property names and associated messages for reporting helpful error messages.
|
|---|
| 56 | this._propNames = [""];
|
|---|
| 57 | this._messages = [null];
|
|---|
| 58 | // Score is used to choose the best union member whose DetailContext to use for reporting.
|
|---|
| 59 | // Higher score means better match (or rather less severe mismatch).
|
|---|
| 60 | this._score = 0;
|
|---|
| 61 | }
|
|---|
| 62 | DetailContext.prototype.fail = function (relPath, message, score) {
|
|---|
| 63 | this._propNames.push(relPath);
|
|---|
| 64 | this._messages.push(message);
|
|---|
| 65 | this._score += score;
|
|---|
| 66 | return false;
|
|---|
| 67 | };
|
|---|
| 68 | DetailContext.prototype.unionResolver = function () {
|
|---|
| 69 | return new DetailUnionResolver();
|
|---|
| 70 | };
|
|---|
| 71 | DetailContext.prototype.resolveUnion = function (unionResolver) {
|
|---|
| 72 | var _a, _b;
|
|---|
| 73 | var u = unionResolver;
|
|---|
| 74 | var best = null;
|
|---|
| 75 | for (var _i = 0, _c = u.contexts; _i < _c.length; _i++) {
|
|---|
| 76 | var ctx = _c[_i];
|
|---|
| 77 | if (!best || ctx._score >= best._score) {
|
|---|
| 78 | best = ctx;
|
|---|
| 79 | }
|
|---|
| 80 | }
|
|---|
| 81 | if (best && best._score > 0) {
|
|---|
| 82 | (_a = this._propNames).push.apply(_a, best._propNames);
|
|---|
| 83 | (_b = this._messages).push.apply(_b, best._messages);
|
|---|
| 84 | }
|
|---|
| 85 | };
|
|---|
| 86 | DetailContext.prototype.getError = function (path) {
|
|---|
| 87 | var msgParts = [];
|
|---|
| 88 | for (var i = this._propNames.length - 1; i >= 0; i--) {
|
|---|
| 89 | var p = this._propNames[i];
|
|---|
| 90 | path += (typeof p === "number") ? "[" + p + "]" : (p ? "." + p : "");
|
|---|
| 91 | var m = this._messages[i];
|
|---|
| 92 | if (m) {
|
|---|
| 93 | msgParts.push(path + " " + m);
|
|---|
| 94 | }
|
|---|
| 95 | }
|
|---|
| 96 | return new VError(path, msgParts.join("; "));
|
|---|
| 97 | };
|
|---|
| 98 | DetailContext.prototype.getErrorDetail = function (path) {
|
|---|
| 99 | var details = [];
|
|---|
| 100 | for (var i = this._propNames.length - 1; i >= 0; i--) {
|
|---|
| 101 | var p = this._propNames[i];
|
|---|
| 102 | path += (typeof p === "number") ? "[" + p + "]" : (p ? "." + p : "");
|
|---|
| 103 | var message = this._messages[i];
|
|---|
| 104 | if (message) {
|
|---|
| 105 | details.push({ path: path, message: message });
|
|---|
| 106 | }
|
|---|
| 107 | }
|
|---|
| 108 | var detail = null;
|
|---|
| 109 | for (var i = details.length - 1; i >= 0; i--) {
|
|---|
| 110 | if (detail) {
|
|---|
| 111 | details[i].nested = [detail];
|
|---|
| 112 | }
|
|---|
| 113 | detail = details[i];
|
|---|
| 114 | }
|
|---|
| 115 | return detail;
|
|---|
| 116 | };
|
|---|
| 117 | return DetailContext;
|
|---|
| 118 | }());
|
|---|
| 119 | exports.DetailContext = DetailContext;
|
|---|
| 120 | var DetailUnionResolver = /** @class */ (function () {
|
|---|
| 121 | function DetailUnionResolver() {
|
|---|
| 122 | this.contexts = [];
|
|---|
| 123 | }
|
|---|
| 124 | DetailUnionResolver.prototype.createContext = function () {
|
|---|
| 125 | var ctx = new DetailContext();
|
|---|
| 126 | this.contexts.push(ctx);
|
|---|
| 127 | return ctx;
|
|---|
| 128 | };
|
|---|
| 129 | return DetailUnionResolver;
|
|---|
| 130 | }());
|
|---|