container.ts
Version:
Modular application framework
659 lines • 22.8 kB
JavaScript
"use strict";
var __extends = (this && this.__extends) || (function () {
var extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
var validator = require("validator");
var error_1 = require("../error");
var Validate = require("./Validate");
// TODO(MEDIUM): Improve operator field documentation/testing.
/** Field error codes. */
var EFieldError;
(function (EFieldError) {
EFieldError[EFieldError["InvalidAnd"] = 0] = "InvalidAnd";
EFieldError[EFieldError["InvalidOr"] = 1] = "InvalidOr";
EFieldError[EFieldError["InvalidNot"] = 2] = "InvalidNot";
EFieldError[EFieldError["InvalidOptional"] = 3] = "InvalidOptional";
})(EFieldError = exports.EFieldError || (exports.EFieldError = {}));
/** Field error chain class. */
var FieldError = /** @class */ (function (_super) {
__extends(FieldError, _super);
function FieldError(code, value, cause) {
return _super.call(this, { name: EFieldError[code], value: value }, cause) || this;
}
return FieldError;
}(error_1.ErrorChain));
exports.FieldError = FieldError;
/**
* Fields have validate and format methods.
* Validate method takes string input and returns typed output.
* Format method takes typed input and returns string output.
* Optional context available for additional validation/formatting information.
*/
var Field = /** @class */ (function () {
function Field() {
}
Field.prototype.format = function (value, context) {
return validator.toString(value);
};
Field.prototype.and = function () {
var fields = [];
for (var _i = 0; _i < arguments.length; _i++) {
fields[_i] = arguments[_i];
}
return new (AndField.bind.apply(AndField, [void 0, this].concat(fields)))();
};
Field.prototype.or = function () {
var fields = [];
for (var _i = 0; _i < arguments.length; _i++) {
fields[_i] = arguments[_i];
}
return new (OrField.bind.apply(OrField, [void 0, this].concat(fields)))();
};
Field.prototype.not = function () {
var fields = [];
for (var _i = 0; _i < arguments.length; _i++) {
fields[_i] = arguments[_i];
}
return this.and(this, new (NotField.bind.apply(NotField, [void 0].concat(fields)))());
};
return Field;
}());
exports.Field = Field;
/** Operator field helper. */
var OperatorField = /** @class */ (function (_super) {
__extends(OperatorField, _super);
function OperatorField() {
var fields = [];
for (var _i = 0; _i < arguments.length; _i++) {
fields[_i] = arguments[_i];
}
var _this = _super.call(this) || this;
_this.fields = fields;
return _this;
}
return OperatorField;
}(Field));
exports.OperatorField = OperatorField;
/** And field wrapper, all input fields used to validate/format values. */
var AndField = /** @class */ (function (_super) {
__extends(AndField, _super);
function AndField() {
return _super !== null && _super.apply(this, arguments) || this;
}
AndField.prototype.validate = function (value, context) {
var validated;
try {
validated = this.fields
.map(function (f) { return f.validate(value, context); })
.reduce(function (p, c) { return ((p != null) ? p : c); }, null);
}
catch (error) {
throw new FieldError(EFieldError.InvalidAnd, value, error);
}
if (validated == null) {
throw new FieldError(EFieldError.InvalidAnd, value);
}
return validated;
};
AndField.prototype.format = function (value, context) {
var formatted;
try {
formatted = this.fields
.map(function (f) { return f.format(value, context); })
.reduce(function (p, c) { return ((p != null) ? p : c); }, null);
}
catch (error) {
throw new FieldError(EFieldError.InvalidAnd, value, error);
}
if (formatted == null) {
throw new FieldError(EFieldError.InvalidAnd, value);
}
return formatted;
};
return AndField;
}(OperatorField));
exports.AndField = AndField;
/** Or field wrapper, at least one input field used to validate/format values. */
var OrField = /** @class */ (function (_super) {
__extends(OrField, _super);
function OrField() {
return _super !== null && _super.apply(this, arguments) || this;
}
OrField.prototype.validate = function (value, context) {
var validated;
try {
validated = this.fields
.map(function (f) {
try {
return f.validate(value, context);
}
catch (error) {
return null;
}
})
.reduce(function (p, c) { return ((p != null) ? p : c); }, null);
}
catch (error) {
throw new FieldError(EFieldError.InvalidOr, value, error);
}
if (validated == null) {
throw new FieldError(EFieldError.InvalidOr);
}
return validated;
};
OrField.prototype.format = function (value, context) {
var formatted;
try {
formatted = this.fields
.map(function (f) {
try {
return f.format(value, context);
}
catch (error) {
return null;
}
})
.reduce(function (p, c) { return ((p != null) ? p : c); }, null);
}
catch (error) {
throw new FieldError(EFieldError.InvalidOr, value, error);
}
if (formatted == null) {
throw new FieldError(EFieldError.InvalidOr);
}
return formatted;
};
return OrField;
}(OperatorField));
exports.OrField = OrField;
/** Not field wrapper, all input fields expected to throw error/fail to format values. */
var NotField = /** @class */ (function (_super) {
__extends(NotField, _super);
function NotField() {
return _super !== null && _super.apply(this, arguments) || this;
}
NotField.prototype.validate = function (value, context) {
var validated;
try {
validated = this.fields
.map(function (f) {
try {
return f.validate(value, context);
}
catch (error) {
return null;
}
})
.reduce(function (p, c) { return ((p != null) ? p : c); }, null);
}
catch (error) {
throw new FieldError(EFieldError.InvalidNot, value, error);
}
if (validated != null) {
throw new FieldError(EFieldError.InvalidNot, validated);
}
return validated;
};
NotField.prototype.format = function (value, context) {
var formatted;
try {
formatted = this.fields
.map(function (f) {
try {
return f.format(value, context);
}
catch (error) {
return null;
}
})
.reduce(function (p, c) { return ((p != null) ? p : c); }, null);
}
catch (error) {
throw new FieldError(EFieldError.InvalidNot, value, error);
}
if (formatted != null) {
throw new FieldError(EFieldError.InvalidNot, formatted);
}
return formatted;
};
return NotField;
}(OperatorField));
exports.NotField = NotField;
/**
* Optional field wrapper, if value is defined uses field to validate/format.
* If value is undefined default or null value is returned.
*/
var OptionalField = /** @class */ (function (_super) {
__extends(OptionalField, _super);
function OptionalField(field, defaultValue, context) {
var _this = _super.call(this) || this;
_this.field = field;
_this.defaultValue = defaultValue;
_this.formatDefault = _this.format(defaultValue, context);
return _this;
}
OptionalField.prototype.validate = function (value, context) {
try {
if (value == null) {
if (this.formatDefault == null) {
return null;
}
return this.field.validate(this.formatDefault, context);
}
return this.field.validate(value, context);
}
catch (error) {
throw new FieldError(EFieldError.InvalidOptional, value, error);
}
};
OptionalField.prototype.format = function (value, context) {
try {
if (value == null) {
if (this.defaultValue == null) {
return null;
}
return this.field.format(this.defaultValue, context);
}
return this.field.format(value, context);
}
catch (error) {
throw new FieldError(EFieldError.InvalidOptional, value, error);
}
};
return OptionalField;
}(Field));
exports.OptionalField = OptionalField;
var BooleanField = /** @class */ (function (_super) {
__extends(BooleanField, _super);
function BooleanField(options) {
if (options === void 0) { options = {}; }
var _this = _super.call(this) || this;
_this.options = options;
return _this;
}
BooleanField.prototype.validate = function (value) {
return Validate.isBoolean(value, this.options);
};
return BooleanField;
}(Field));
exports.BooleanField = BooleanField;
var IntegerField = /** @class */ (function (_super) {
__extends(IntegerField, _super);
function IntegerField(options) {
if (options === void 0) { options = {}; }
var _this = _super.call(this) || this;
_this.options = options;
return _this;
}
IntegerField.prototype.validate = function (value) {
return Validate.isInteger(value, this.options);
};
return IntegerField;
}(Field));
exports.IntegerField = IntegerField;
var FloatField = /** @class */ (function (_super) {
__extends(FloatField, _super);
function FloatField(options) {
if (options === void 0) { options = {}; }
var _this = _super.call(this) || this;
_this.options = options;
return _this;
}
FloatField.prototype.validate = function (value) {
return Validate.isFloat(value, this.options);
};
return FloatField;
}(Field));
exports.FloatField = FloatField;
var HexadecimalField = /** @class */ (function (_super) {
__extends(HexadecimalField, _super);
function HexadecimalField() {
return _super !== null && _super.apply(this, arguments) || this;
}
HexadecimalField.prototype.validate = function (value) {
return Validate.isHexadecimal(value);
};
HexadecimalField.prototype.format = function (value) {
return "0x" + value.toString(16);
};
return HexadecimalField;
}(Field));
exports.HexadecimalField = HexadecimalField;
var StringField = /** @class */ (function (_super) {
__extends(StringField, _super);
function StringField(options) {
if (options === void 0) { options = {}; }
var _this = _super.call(this) || this;
_this.options = options;
return _this;
}
StringField.prototype.validate = function (value) {
return Validate.isString(value, this.options);
};
return StringField;
}(Field));
exports.StringField = StringField;
var AsciiField = /** @class */ (function (_super) {
__extends(AsciiField, _super);
function AsciiField(options) {
if (options === void 0) { options = {}; }
var _this = _super.call(this) || this;
_this.options = options;
return _this;
}
AsciiField.prototype.validate = function (value) {
return Validate.isAscii(value, this.options);
};
return AsciiField;
}(Field));
exports.AsciiField = AsciiField;
var Base64Field = /** @class */ (function (_super) {
__extends(Base64Field, _super);
function Base64Field(options) {
if (options === void 0) { options = {}; }
var _this = _super.call(this) || this;
_this.options = options;
return _this;
}
Base64Field.prototype.validate = function (value) {
return Validate.isBase64(value, this.options);
};
return Base64Field;
}(Field));
exports.Base64Field = Base64Field;
var AlphaField = /** @class */ (function (_super) {
__extends(AlphaField, _super);
function AlphaField(options) {
if (options === void 0) { options = {}; }
var _this = _super.call(this) || this;
_this.options = options;
return _this;
}
AlphaField.prototype.validate = function (value) {
return Validate.isAlpha(value, this.options);
};
return AlphaField;
}(Field));
exports.AlphaField = AlphaField;
var AlphanumericField = /** @class */ (function (_super) {
__extends(AlphanumericField, _super);
function AlphanumericField(options) {
if (options === void 0) { options = {}; }
var _this = _super.call(this) || this;
_this.options = options;
return _this;
}
AlphanumericField.prototype.validate = function (value) {
return Validate.isAlphanumeric(value, this.options);
};
return AlphanumericField;
}(Field));
exports.AlphanumericField = AlphanumericField;
var CreditCardField = /** @class */ (function (_super) {
__extends(CreditCardField, _super);
function CreditCardField() {
return _super !== null && _super.apply(this, arguments) || this;
}
CreditCardField.prototype.validate = function (value) {
return Validate.isCreditCard(value);
};
return CreditCardField;
}(Field));
exports.CreditCardField = CreditCardField;
var EmailField = /** @class */ (function (_super) {
__extends(EmailField, _super);
function EmailField(options) {
if (options === void 0) { options = {}; }
var _this = _super.call(this) || this;
_this.options = options;
return _this;
}
EmailField.prototype.validate = function (value) {
return Validate.isEmail(value, this.options);
};
return EmailField;
}(Field));
exports.EmailField = EmailField;
var DomainField = /** @class */ (function (_super) {
__extends(DomainField, _super);
function DomainField(options) {
if (options === void 0) { options = {}; }
var _this = _super.call(this) || this;
_this.options = options;
return _this;
}
DomainField.prototype.validate = function (value) {
return Validate.isDomain(value, this.options);
};
return DomainField;
}(Field));
exports.DomainField = DomainField;
var HexColourField = /** @class */ (function (_super) {
__extends(HexColourField, _super);
function HexColourField() {
return _super !== null && _super.apply(this, arguments) || this;
}
HexColourField.prototype.validate = function (value) {
return Validate.isHexColour(value);
};
return HexColourField;
}(Field));
exports.HexColourField = HexColourField;
var IpField = /** @class */ (function (_super) {
__extends(IpField, _super);
function IpField(options) {
if (options === void 0) { options = {}; }
var _this = _super.call(this) || this;
_this.options = options;
return _this;
}
IpField.prototype.validate = function (value) {
return Validate.isIp(value, this.options);
};
return IpField;
}(Field));
exports.IpField = IpField;
var JsonField = /** @class */ (function (_super) {
__extends(JsonField, _super);
function JsonField() {
return _super !== null && _super.apply(this, arguments) || this;
}
JsonField.prototype.validate = function (value) {
return Validate.isJson(value);
};
JsonField.prototype.format = function (value) {
return JSON.stringify(value);
};
return JsonField;
}(Field));
exports.JsonField = JsonField;
var MacAddressField = /** @class */ (function (_super) {
__extends(MacAddressField, _super);
function MacAddressField() {
return _super !== null && _super.apply(this, arguments) || this;
}
MacAddressField.prototype.validate = function (value) {
return Validate.isMacAddress(value);
};
return MacAddressField;
}(Field));
exports.MacAddressField = MacAddressField;
var Md5Field = /** @class */ (function (_super) {
__extends(Md5Field, _super);
function Md5Field() {
return _super !== null && _super.apply(this, arguments) || this;
}
Md5Field.prototype.validate = function (value) {
return Validate.isMd5(value);
};
return Md5Field;
}(Field));
exports.Md5Field = Md5Field;
var MobilePhoneField = /** @class */ (function (_super) {
__extends(MobilePhoneField, _super);
function MobilePhoneField(options) {
if (options === void 0) { options = {}; }
var _this = _super.call(this) || this;
_this.options = options;
return _this;
}
MobilePhoneField.prototype.validate = function (value) {
return Validate.isMobilePhone(value, this.options);
};
return MobilePhoneField;
}(Field));
exports.MobilePhoneField = MobilePhoneField;
var MongoIdField = /** @class */ (function (_super) {
__extends(MongoIdField, _super);
function MongoIdField() {
return _super !== null && _super.apply(this, arguments) || this;
}
MongoIdField.prototype.validate = function (value) {
return Validate.isMongoId(value);
};
return MongoIdField;
}(Field));
exports.MongoIdField = MongoIdField;
var PostalCodeField = /** @class */ (function (_super) {
__extends(PostalCodeField, _super);
function PostalCodeField(options) {
if (options === void 0) { options = {}; }
var _this = _super.call(this) || this;
_this.options = options;
return _this;
}
PostalCodeField.prototype.validate = function (value) {
return Validate.isPostalCode(value, this.options);
};
return PostalCodeField;
}(Field));
exports.PostalCodeField = PostalCodeField;
var UrlField = /** @class */ (function (_super) {
__extends(UrlField, _super);
function UrlField(options) {
if (options === void 0) { options = { require_host: true }; }
var _this = _super.call(this) || this;
_this.options = options;
return _this;
}
UrlField.prototype.validate = function (value) {
return Validate.isUrl(value, this.options);
};
return UrlField;
}(Field));
exports.UrlField = UrlField;
var UuidField = /** @class */ (function (_super) {
__extends(UuidField, _super);
function UuidField(options) {
if (options === void 0) { options = {}; }
var _this = _super.call(this) || this;
_this.options = options;
return _this;
}
UuidField.prototype.validate = function (value) {
return Validate.isUuid(value, this.options);
};
return UuidField;
}(Field));
exports.UuidField = UuidField;
var PortField = /** @class */ (function (_super) {
__extends(PortField, _super);
function PortField() {
return _super !== null && _super.apply(this, arguments) || this;
}
PortField.prototype.validate = function (value) {
return Validate.isPort(value);
};
return PortField;
}(Field));
exports.PortField = PortField;
var LanguageField = /** @class */ (function (_super) {
__extends(LanguageField, _super);
function LanguageField() {
return _super !== null && _super.apply(this, arguments) || this;
}
LanguageField.prototype.validate = function (value) {
return Validate.isLanguage(value);
};
return LanguageField;
}(Field));
exports.LanguageField = LanguageField;
var CountryField = /** @class */ (function (_super) {
__extends(CountryField, _super);
function CountryField() {
return _super !== null && _super.apply(this, arguments) || this;
}
CountryField.prototype.validate = function (value) {
return Validate.isCountry(value);
};
return CountryField;
}(Field));
exports.CountryField = CountryField;
var LocaleField = /** @class */ (function (_super) {
__extends(LocaleField, _super);
function LocaleField(options) {
if (options === void 0) { options = {}; }
var _this = _super.call(this) || this;
_this.options = options;
return _this;
}
LocaleField.prototype.validate = function (value) {
return Validate.isLocale(value, this.options);
};
return LocaleField;
}(Field));
exports.LocaleField = LocaleField;
var TimeZoneField = /** @class */ (function (_super) {
__extends(TimeZoneField, _super);
function TimeZoneField() {
return _super !== null && _super.apply(this, arguments) || this;
}
TimeZoneField.prototype.validate = function (value) {
return Validate.isTimeZone(value);
};
return TimeZoneField;
}(Field));
exports.TimeZoneField = TimeZoneField;
var DateField = /** @class */ (function (_super) {
__extends(DateField, _super);
function DateField(options) {
if (options === void 0) { options = {}; }
var _this = _super.call(this) || this;
_this.options = options;
return _this;
}
DateField.prototype.validate = function (value) {
return Validate.isDate(value, this.options);
};
DateField.prototype.format = function (value) {
return value.format();
};
return DateField;
}(Field));
exports.DateField = DateField;
var DurationField = /** @class */ (function (_super) {
__extends(DurationField, _super);
function DurationField(options) {
if (options === void 0) { options = {}; }
var _this = _super.call(this) || this;
_this.options = options;
return _this;
}
DurationField.prototype.validate = function (value) {
return Validate.isDuration(value, this.options);
};
DurationField.prototype.format = function (value) {
return value.toISOString();
};
return DurationField;
}(Field));
exports.DurationField = DurationField;
//# sourceMappingURL=Field.js.map