UNPKG

jaywalk

Version:
85 lines 2.89 kB
"use strict"; 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 assert = require('assert'); var utils_1 = require('../utils'); var rule_1 = require('./rule'); var Any = (function (_super) { __extends(Any, _super); function Any(options) { if (options === void 0) { options = {}; } _super.call(this, options); this.type = 'Any'; this.required = true; this.uses = []; if (options.default != null) { this.default = options.default; } if (options.required != null) { this.required = options.required; assert.ok(typeof this.required === 'boolean', "Expected \"required\" to be a boolean"); } if (options.uses != null) { this.uses = options.uses; } this._tests.push(toDefaultTest(this.default)); this._tests.push(toRequiredTest(this.required)); } Any.prototype._isType = function (value, path, context) { if (value == null) { if (this.required === false || this.default != null) { return 1; } throw context.error(path, 'Any', 'required', this.required, value); } return 1; }; Any.prototype._compile = function () { return utils_1.compose([ _super.prototype._compile.call(this) ].concat(this.uses.map(function (type) { return type._compile(); }))); }; Any.prototype._extend = function (options) { var result = _super.prototype._extend.call(this, options); if (options.uses) { result.uses = this.uses.concat(options.uses); } return result; }; Any.prototype.toJSON = function () { var json = _super.prototype.toJSON.call(this); json.uses = this.uses.map(function (x) { return x.toJSON(); }); return json; }; return Any; }(rule_1.Rule)); exports.Any = Any; function toRequiredTest(required) { if (!required) { return function (value, path, context, next) { if (value == null) { return value; } return next(value); }; } return function (value, path, context, next) { if (value == null) { throw context.error(path, 'Any', 'required', required, value); } return next(value); }; } function toDefaultTest(defaulted) { if (defaulted == null) { return utils_1.toNext; } var defaultValue = utils_1.toValue(defaulted); return function (value, path, context, next) { return next(value == null ? defaultValue(path, context) : value); }; } //# sourceMappingURL=any.js.map