UNPKG

js-props-validator

Version:
340 lines (258 loc) 13.3 kB
'use strict'; var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol ? "symbol" : typeof obj; }; var _get = function get(object, property, receiver) { if (object === null) object = Function.prototype; var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { return get(parent, property, receiver); } } else if ("value" in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } }; var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); Object.defineProperty(exports, "__esModule", { value: true }); var _underscore = require('underscore'); var _underscore2 = _interopRequireDefault(_underscore); var _underscoreDeepExtend = require('underscore-deep-extend'); var _underscoreDeepExtend2 = _interopRequireDefault(_underscoreDeepExtend); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } _underscore2.default.mixin({ deepExtend: (0, _underscoreDeepExtend2.default)(_underscore2.default) }); var AbstractType = function () { function AbstractType(isOptional, defaultValue, typeName) { _classCallCheck(this, AbstractType); this._isOptional = isOptional ? true : false; this.defaultValue = defaultValue; this._typeName = typeName; } _createClass(AbstractType, [{ key: '_getCheckValue', value: function _getCheckValue(value) { return !value && this.defaultValue !== undefined ? this.defaultValue : value; } }, { key: 'isOptional', value: function isOptional(_isOptional) { this._isOptional = true; return this; } }, { key: 'validate', value: function validate(value, valueName) { var result = this.validateValue(value); var self = this; if (!result) { throw (valueName ? valueName + ': ' : '') + 'The given value is not valid. Type ' + JSON.stringify(this, null, 2) + '. Value: ' + JSON.stringify(value, null, 2); } return result; } }, { key: 'validateValue', value: function validateValue(value, valueName) { var result = this._validateValue(value); if (!result) { console.warn((valueName ? valueName + ': ' : '') + 'The given value is not valid. Type ' + JSON.stringify(this, null, 2) + '. Value: ' + JSON.stringify(value, null, 2)); } return result; } }, { key: '_validateValue', value: function _validateValue(value) { return this._isOptional || value !== undefined; } }, { key: 'validateValueFalse', value: function validateValueFalse(value, valueName) { return !this.validateValue(value, valueName); } }, { key: 'valueOrDefault', value: function valueOrDefault(value) { return this._getCheckValue(); } }, { key: 'withDefault', value: function withDefault(defaultValue) { this.defaultValue = defaultValue; this._isOptional = true; return this; } }]); return AbstractType; }(); var AnyProp = function (_AbstractType) { _inherits(AnyProp, _AbstractType); function AnyProp(ofType, isOptional, defaultValue) { _classCallCheck(this, AnyProp); var _this = _possibleConstructorReturn(this, Object.getPrototypeOf(AnyProp).call(this, isOptional, defaultValue)); _this.ofType = ofType; return _this; } _createClass(AnyProp, [{ key: '_validateValue', value: function _validateValue(value) { var _this2 = this; return _get(Object.getPrototypeOf(AnyProp.prototype), '_validateValue', this).call(this, value) && (this._getCheckValue(value) === undefined || !(this.ofType && _underscore2.default.isFunction(this.ofType)) || this.ofType(value)) && (this._getCheckValue(value) === undefined || !(this.ofType && _underscore2.default.isArray(this.ofType)) || _underscore2.default.findIndex(this.ofType, function (type) { return type.validateValue(_this2._getCheckValue(value)); }, this) > -1); } }]); return AnyProp; }(AbstractType); var ArrayProp = function (_AbstractType2) { _inherits(ArrayProp, _AbstractType2); function ArrayProp(ofType, isOptional, defaultValue) { _classCallCheck(this, ArrayProp); var _this3 = _possibleConstructorReturn(this, Object.getPrototypeOf(ArrayProp).call(this, isOptional, defaultValue, 'Array')); _this3.ofType = ofType; return _this3; } _createClass(ArrayProp, [{ key: '_validateValue', value: function _validateValue(value) { return _get(Object.getPrototypeOf(ArrayProp.prototype), '_validateValue', this).call(this, value) && (this._getCheckValue(value) === undefined || _underscore2.default.isArray(this._getCheckValue(value))) && (this._getCheckValue(value) === undefined || !this.ofType || _underscore2.default.findIndex(this._getCheckValue(value), this.ofType.validateValueFalse, this.ofType) === -1); } }]); return ArrayProp; }(AbstractType); var BoolProp = function (_AbstractType3) { _inherits(BoolProp, _AbstractType3); function BoolProp(isOptional, defaultValue) { _classCallCheck(this, BoolProp); return _possibleConstructorReturn(this, Object.getPrototypeOf(BoolProp).call(this, isOptional, defaultValue, 'Boolean')); } _createClass(BoolProp, [{ key: '_validateValue', value: function _validateValue(value) { return _get(Object.getPrototypeOf(BoolProp.prototype), '_validateValue', this).call(this, value) && (this._getCheckValue(value) === undefined || _underscore2.default.isBoolean(this._getCheckValue(value))); } }]); return BoolProp; }(AbstractType); var EnumProp = function (_AbstractType4) { _inherits(EnumProp, _AbstractType4); function EnumProp(values, isOptional, defaultValue) { _classCallCheck(this, EnumProp); var _this5 = _possibleConstructorReturn(this, Object.getPrototypeOf(EnumProp).call(this, isOptional, defaultValue, 'Enumeration of ' + values)); new ArrayProp().validate(values, 'Parameter "values"'); _this5.values = values; return _this5; } _createClass(EnumProp, [{ key: '_validateValue', value: function _validateValue(value) { return _get(Object.getPrototypeOf(EnumProp.prototype), '_validateValue', this).call(this, value) && (this._getCheckValue(value) === undefined || _underscore2.default.indexOf(this.values, this._getCheckValue(value)) > -1); } }]); return EnumProp; }(AbstractType); var FuncProp = function (_AbstractType5) { _inherits(FuncProp, _AbstractType5); function FuncProp(isOptional, defaultValue) { _classCallCheck(this, FuncProp); return _possibleConstructorReturn(this, Object.getPrototypeOf(FuncProp).call(this, isOptional, defaultValue, 'Function')); } _createClass(FuncProp, [{ key: '_validateValue', value: function _validateValue(value) { return _get(Object.getPrototypeOf(FuncProp.prototype), '_validateValue', this).call(this, value) && (this._getCheckValue(value) === undefined || _underscore2.default.isFunction(this._getCheckValue(value))); } }]); return FuncProp; }(AbstractType); var NumberProp = function (_AbstractType6) { _inherits(NumberProp, _AbstractType6); function NumberProp(isOptional, defaultValue) { _classCallCheck(this, NumberProp); return _possibleConstructorReturn(this, Object.getPrototypeOf(NumberProp).call(this, isOptional, defaultValue, 'Number')); } _createClass(NumberProp, [{ key: '_validateValue', value: function _validateValue(value) { return _get(Object.getPrototypeOf(NumberProp.prototype), '_validateValue', this).call(this, value) && (this._getCheckValue(value) === undefined || _underscore2.default.isNumber(this._getCheckValue(value))); } }]); return NumberProp; }(AbstractType); var ObjectProp = function (_AbstractType7) { _inherits(ObjectProp, _AbstractType7); function ObjectProp(ofType, isOptional, defaultValue) { _classCallCheck(this, ObjectProp); var _this8 = _possibleConstructorReturn(this, Object.getPrototypeOf(ObjectProp).call(this, isOptional, defaultValue, 'Object')); _this8.ofType = ofType; return _this8; } _createClass(ObjectProp, [{ key: '_validateValue', value: function _validateValue(value) { var _this9 = this; var checkFields = function checkFields(key) { return _this9.ofType[key].validateValueFalse(_this9._getCheckValue(value[key]), key); }; var checkFieldValues = function checkFieldValues(value) { return _this9.ofType.validateValueFalse(value); }; return _get(Object.getPrototypeOf(ObjectProp.prototype), '_validateValue', this).call(this, value) && (this._getCheckValue(value) === undefined || _underscore2.default.isObject(this._getCheckValue(value))) && (this._getCheckValue(value) === undefined || !(this.ofType instanceof AbstractType) || _underscore2.default.findIndex(_underscore2.default.values(this._getCheckValue(value), this), checkFieldValues, this) === -1) && (this._getCheckValue(value) === undefined || !(this.ofType && _underscore2.default.isObject(this.ofType) && !(this.ofType instanceof AbstractType)) || _underscore2.default.chain(this.ofType).keys().findIndex(checkFields, this).value() === -1); } }, { key: 'valueOrDefault', value: function valueOrDefault(value) { var _this10 = this; if (this.ofType && _underscore2.default.isObject(this.ofType) && !this.defaultValue) { var _ret = function () { var v = value ? _underscore2.default.deepExtend({}, value) : {}; _underscore2.default.each(_this10.ofType, function (type, key) { if (type.defaultValue) { v[key] = type.valueOrDefault(v[key]); } }); return { v: v }; }(); if ((typeof _ret === 'undefined' ? 'undefined' : _typeof(_ret)) === "object") return _ret.v; } else { return _get(Object.getPrototypeOf(ObjectProp.prototype), 'valueOrDefault', this).call(this, value); } } }]); return ObjectProp; }(AbstractType); var StringProp = function (_AbstractType8) { _inherits(StringProp, _AbstractType8); function StringProp(isOptional, defaultValue) { _classCallCheck(this, StringProp); return _possibleConstructorReturn(this, Object.getPrototypeOf(StringProp).call(this, isOptional, defaultValue, 'String')); } _createClass(StringProp, [{ key: '_validateValue', value: function _validateValue(value) { return _get(Object.getPrototypeOf(StringProp.prototype), '_validateValue', this).call(this, value) && (value === undefined || _underscore2.default.isString(this._getCheckValue(value))); } }]); return StringProp; }(AbstractType); exports.default = { any: function any(ofType, isOptional, defaultValue) { return new AnyProp(ofType, isOptional, defaultValue); }, array: function array(ofType, isOptional, defaultValue) { return new ArrayProp(ofType, isOptional, defaultValue); }, bool: function bool(isOptional, defaultValue) { return new BoolProp(isOptional, defaultValue); }, func: function func(isOptional, defaultValue) { return new FuncProp(isOptional, defaultValue); }, number: function number(isOptional, defaultValue) { return new NumberProp(isOptional, defaultValue); }, object: function object(ofType, isOptional, defaultValue) { return new ObjectProp(ofType, isOptional, defaultValue); }, oneOf: function oneOf(values, isOptional, defaultValue) { return new EnumProp(values, isOptional, defaultValue); }, string: function string(isOptional, defaultValue) { return new StringProp(isOptional, defaultValue); } };