jaywalk
Version:
Runtime type validation
44 lines • 1.58 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 validator = require('validator');
var string_1 = require('./string');
var utils_1 = require('../utils');
var Uuid = (function (_super) {
__extends(Uuid, _super);
function Uuid(options) {
if (options === void 0) { options = {}; }
_super.call(this, options);
this.type = 'Uuid';
if (options.version != null) {
this.version = options.version;
}
this._tests.push(toUuidTest(this.version));
}
Uuid.prototype._isType = function (value, path, context) {
var _this = this;
return utils_1.wrapIsType(this, value, path, context, _super.prototype._isType, function (value) {
if (validator.isUUID(value, _this.version)) {
return 1;
}
throw context.error(path, 'Uuid', 'type', 'Uuid', value);
});
};
Uuid.prototype._extend = function (options) {
return _super.prototype._extend.call(this, options);
};
return Uuid;
}(string_1.String));
exports.Uuid = Uuid;
function toUuidTest(version) {
return function (value, path, context, next) {
if (!validator.isUUID(value, version)) {
throw context.error(path, 'Uuid', 'type', 'Uuid', value);
}
return next(value);
};
}
//# sourceMappingURL=uuid.js.map