|
"use strict"; |
|
var __spreadArrays = (this && this.__spreadArrays) || function () { |
|
for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length; |
|
for (var r = Array(s), k = 0, i = 0; i < il; i++) |
|
for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++) |
|
r[k] = a[j]; |
|
return r; |
|
}; |
|
Object.defineProperty(exports, "__esModule", { value: true }); |
|
exports.Checker = exports.createCheckers = void 0; |
|
var types_1 = require("./types"); |
|
var util_1 = require("./util"); |
|
|
|
|
|
|
|
var types_2 = require("./types"); |
|
Object.defineProperty(exports, "TArray", { enumerable: true, get: function () { return types_2.TArray; } }); |
|
Object.defineProperty(exports, "TEnumType", { enumerable: true, get: function () { return types_2.TEnumType; } }); |
|
Object.defineProperty(exports, "TEnumLiteral", { enumerable: true, get: function () { return types_2.TEnumLiteral; } }); |
|
Object.defineProperty(exports, "TFunc", { enumerable: true, get: function () { return types_2.TFunc; } }); |
|
Object.defineProperty(exports, "TIface", { enumerable: true, get: function () { return types_2.TIface; } }); |
|
Object.defineProperty(exports, "TLiteral", { enumerable: true, get: function () { return types_2.TLiteral; } }); |
|
Object.defineProperty(exports, "TName", { enumerable: true, get: function () { return types_2.TName; } }); |
|
Object.defineProperty(exports, "TOptional", { enumerable: true, get: function () { return types_2.TOptional; } }); |
|
Object.defineProperty(exports, "TParam", { enumerable: true, get: function () { return types_2.TParam; } }); |
|
Object.defineProperty(exports, "TParamList", { enumerable: true, get: function () { return types_2.TParamList; } }); |
|
Object.defineProperty(exports, "TProp", { enumerable: true, get: function () { return types_2.TProp; } }); |
|
Object.defineProperty(exports, "TTuple", { enumerable: true, get: function () { return types_2.TTuple; } }); |
|
Object.defineProperty(exports, "TType", { enumerable: true, get: function () { return types_2.TType; } }); |
|
Object.defineProperty(exports, "TUnion", { enumerable: true, get: function () { return types_2.TUnion; } }); |
|
Object.defineProperty(exports, "TIntersection", { enumerable: true, get: function () { return types_2.TIntersection; } }); |
|
Object.defineProperty(exports, "array", { enumerable: true, get: function () { return types_2.array; } }); |
|
Object.defineProperty(exports, "enumlit", { enumerable: true, get: function () { return types_2.enumlit; } }); |
|
Object.defineProperty(exports, "enumtype", { enumerable: true, get: function () { return types_2.enumtype; } }); |
|
Object.defineProperty(exports, "func", { enumerable: true, get: function () { return types_2.func; } }); |
|
Object.defineProperty(exports, "iface", { enumerable: true, get: function () { return types_2.iface; } }); |
|
Object.defineProperty(exports, "lit", { enumerable: true, get: function () { return types_2.lit; } }); |
|
Object.defineProperty(exports, "name", { enumerable: true, get: function () { return types_2.name; } }); |
|
Object.defineProperty(exports, "opt", { enumerable: true, get: function () { return types_2.opt; } }); |
|
Object.defineProperty(exports, "param", { enumerable: true, get: function () { return types_2.param; } }); |
|
Object.defineProperty(exports, "tuple", { enumerable: true, get: function () { return types_2.tuple; } }); |
|
Object.defineProperty(exports, "union", { enumerable: true, get: function () { return types_2.union; } }); |
|
Object.defineProperty(exports, "intersection", { enumerable: true, get: function () { return types_2.intersection; } }); |
|
Object.defineProperty(exports, "BasicType", { enumerable: true, get: function () { return types_2.BasicType; } }); |
|
var util_2 = require("./util"); |
|
Object.defineProperty(exports, "VError", { enumerable: true, get: function () { return util_2.VError; } }); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function createCheckers() { |
|
var typeSuite = []; |
|
for (var _i = 0; _i < arguments.length; _i++) { |
|
typeSuite[_i] = arguments[_i]; |
|
} |
|
var fullSuite = Object.assign.apply(Object, __spreadArrays([{}, types_1.basicTypes], typeSuite)); |
|
var checkers = {}; |
|
for (var _a = 0, typeSuite_1 = typeSuite; _a < typeSuite_1.length; _a++) { |
|
var suite_1 = typeSuite_1[_a]; |
|
for (var _b = 0, _c = Object.keys(suite_1); _b < _c.length; _b++) { |
|
var name = _c[_b]; |
|
checkers[name] = new Checker(fullSuite, suite_1[name]); |
|
} |
|
} |
|
return checkers; |
|
} |
|
exports.createCheckers = createCheckers; |
|
|
|
|
|
|
|
|
|
var Checker = (function () { |
|
|
|
function Checker(suite, ttype, _path) { |
|
if (_path === void 0) { _path = 'value'; } |
|
this.suite = suite; |
|
this.ttype = ttype; |
|
this._path = _path; |
|
this.props = new Map(); |
|
if (ttype instanceof types_1.TIface) { |
|
for (var _i = 0, _a = ttype.props; _i < _a.length; _i++) { |
|
var p = _a[_i]; |
|
this.props.set(p.name, p.ttype); |
|
} |
|
} |
|
this.checkerPlain = this.ttype.getChecker(suite, false); |
|
this.checkerStrict = this.ttype.getChecker(suite, true); |
|
} |
|
|
|
|
|
|
|
|
|
Checker.prototype.setReportedPath = function (path) { |
|
this._path = path; |
|
}; |
|
|
|
|
|
|
|
Checker.prototype.check = function (value) { return this._doCheck(this.checkerPlain, value); }; |
|
|
|
|
|
|
|
|
|
Checker.prototype.test = function (value) { |
|
return this.checkerPlain(value, new util_1.NoopContext()); |
|
}; |
|
|
|
|
|
|
|
|
|
Checker.prototype.validate = function (value) { |
|
return this._doValidate(this.checkerPlain, value); |
|
}; |
|
|
|
|
|
|
|
|
|
|
|
Checker.prototype.strictCheck = function (value) { return this._doCheck(this.checkerStrict, value); }; |
|
|
|
|
|
|
|
|
|
Checker.prototype.strictTest = function (value) { |
|
return this.checkerStrict(value, new util_1.NoopContext()); |
|
}; |
|
|
|
|
|
|
|
|
|
Checker.prototype.strictValidate = function (value) { |
|
return this._doValidate(this.checkerStrict, value); |
|
}; |
|
|
|
|
|
|
|
|
|
Checker.prototype.getProp = function (prop) { |
|
var ttype = this.props.get(prop); |
|
if (!ttype) { |
|
throw new Error("Type has no property " + prop); |
|
} |
|
return new Checker(this.suite, ttype, this._path + "." + prop); |
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Checker.prototype.methodArgs = function (methodName) { |
|
var tfunc = this._getMethod(methodName); |
|
return new Checker(this.suite, tfunc.paramList); |
|
}; |
|
|
|
|
|
|
|
|
|
Checker.prototype.methodResult = function (methodName) { |
|
var tfunc = this._getMethod(methodName); |
|
return new Checker(this.suite, tfunc.result); |
|
}; |
|
|
|
|
|
|
|
Checker.prototype.getArgs = function () { |
|
if (!(this.ttype instanceof types_1.TFunc)) { |
|
throw new Error("getArgs() applied to non-function"); |
|
} |
|
return new Checker(this.suite, this.ttype.paramList); |
|
}; |
|
|
|
|
|
|
|
Checker.prototype.getResult = function () { |
|
if (!(this.ttype instanceof types_1.TFunc)) { |
|
throw new Error("getResult() applied to non-function"); |
|
} |
|
return new Checker(this.suite, this.ttype.result); |
|
}; |
|
|
|
|
|
|
|
Checker.prototype.getType = function () { |
|
return this.ttype; |
|
}; |
|
|
|
|
|
|
|
Checker.prototype._doCheck = function (checkerFunc, value) { |
|
var noopCtx = new util_1.NoopContext(); |
|
if (!checkerFunc(value, noopCtx)) { |
|
var detailCtx = new util_1.DetailContext(); |
|
checkerFunc(value, detailCtx); |
|
throw detailCtx.getError(this._path); |
|
} |
|
}; |
|
Checker.prototype._doValidate = function (checkerFunc, value) { |
|
var noopCtx = new util_1.NoopContext(); |
|
if (checkerFunc(value, noopCtx)) { |
|
return null; |
|
} |
|
var detailCtx = new util_1.DetailContext(); |
|
checkerFunc(value, detailCtx); |
|
return detailCtx.getErrorDetail(this._path); |
|
}; |
|
Checker.prototype._getMethod = function (methodName) { |
|
var ttype = this.props.get(methodName); |
|
if (!ttype) { |
|
throw new Error("Type has no property " + methodName); |
|
} |
|
if (!(ttype instanceof types_1.TFunc)) { |
|
throw new Error("Property " + methodName + " is not a method"); |
|
} |
|
return ttype; |
|
}; |
|
return Checker; |
|
}()); |
|
exports.Checker = Checker; |
|
|