jaywalk
Version:
Runtime type validation
32 lines • 1.1 kB
JavaScript
;
var __extends = (this && this.__extends) || function (d, b) {
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
var Promise = require('any-promise');
var rule_1 = require('./rule');
var Test = (function (_super) {
__extends(Test, _super);
function Test(options) {
_super.call(this, options);
this.type = 'Test';
this.name = options.name;
this.test = options.test;
this._tests.push(toTestTest(this.name, this.test));
}
return Test;
}(rule_1.Rule));
exports.Test = Test;
function toTestTest(name, test) {
return function (value, path, context, next) {
return new Promise(function (resolve) { return resolve(test(value)); })
.then(function (passed) {
if (!passed) {
return Promise.reject(context.error(path, 'Test', 'test', name, value));
}
return next(value);
});
};
}
//# sourceMappingURL=test.js.map