jaywalk
Version:
Runtime type validation
37 lines • 1.33 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 any_1 = require('./any');
var utils_1 = require('../utils');
var Literal = (function (_super) {
__extends(Literal, _super);
function Literal(options) {
_super.call(this, options);
this.type = 'Literal';
this.value = options.value;
this._tests.push(toValueTest(this.value));
}
Literal.prototype._isType = function (value, path, context) {
var _this = this;
return utils_1.wrapIsType(this, value, path, context, _super.prototype._isType, function (value) {
if (value === _this.value) {
return 1;
}
throw context.error(path, 'Literal', 'value', _this.value, value);
});
};
return Literal;
}(any_1.Any));
exports.Literal = Literal;
function toValueTest(validValue) {
return function isString(value, path, context, next) {
if (value !== validValue) {
throw context.error(path, 'Literal', 'value', validValue, value);
}
return next(value);
};
}
//# sourceMappingURL=literal.js.map