Comparing version 0.14.0 to 0.15.0
@@ -1,364 +0,249 @@ | ||
'use strict'; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
var _typeof2 = require('babel-runtime/helpers/typeof'); | ||
var _typeof3 = _interopRequireDefault(_typeof2); | ||
var _toConsumableArray2 = require('babel-runtime/helpers/toConsumableArray'); | ||
var _toConsumableArray3 = _interopRequireDefault(_toConsumableArray2); | ||
var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck'); | ||
var _classCallCheck3 = _interopRequireDefault(_classCallCheck2); | ||
var _createClass2 = require('babel-runtime/helpers/createClass'); | ||
var _createClass3 = _interopRequireDefault(_createClass2); | ||
exports.bool = bool; | ||
exports.custom = custom; | ||
exports.func = func; | ||
var _isObject = require('./isObject'); | ||
var _isObject2 = _interopRequireDefault(_isObject); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
var Builder = function () { | ||
function Builder(type, defaultValue) { | ||
(0, _classCallCheck3.default)(this, Builder); | ||
this.checks = []; | ||
this.currentConfig = {}; | ||
this.currentOptions = {}; | ||
this.deprecatedMessage = ''; | ||
this.errorMessage = ''; | ||
this.isNullable = false; | ||
this.isRequired = false; | ||
if ("production" !== process.env.NODE_ENV) { | ||
this.invariant(typeof defaultValue !== 'undefined', 'A default value for type "' + type + '" is required.'); | ||
this.addCheck(this.checkType); | ||
} | ||
this.defaultValue = defaultValue; | ||
this.type = type; | ||
} | ||
(0, _createClass3.default)(Builder, [{ | ||
key: 'addCheck', | ||
value: function addCheck(checker) { | ||
for (var _len = arguments.length, args = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) { | ||
args[_key - 1] = arguments[_key]; | ||
} | ||
if ("production" !== process.env.NODE_ENV) { | ||
this.checks.push({ | ||
args: args, | ||
func: checker | ||
}); | ||
} | ||
return this; | ||
} | ||
}, { | ||
key: 'and', | ||
value: function and() { | ||
for (var _len2 = arguments.length, keys = Array(_len2), _key2 = 0; _key2 < _len2; _key2++) { | ||
keys[_key2] = arguments[_key2]; | ||
} | ||
if ("production" !== process.env.NODE_ENV) { | ||
this.invariant(keys.length > 0, 'AND requires a list of option names.'); | ||
} | ||
return this.addCheck(this.checkAnd, keys); | ||
} | ||
}, { | ||
key: 'checkAnd', | ||
value: function checkAnd(path, value, otherKeys) { | ||
if ("production" !== process.env.NODE_ENV) { | ||
var _keys = [this.key(path)].concat((0, _toConsumableArray3.default)(otherKeys)); | ||
var options = this.currentOptions; | ||
var undefs = _keys.filter(function (key) { | ||
return typeof options[key] === 'undefined' || options[key] === null; | ||
}); | ||
if (undefs.length === _keys.length) { | ||
return; | ||
"use strict"; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
} | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var isObject_1 = __importDefault(require("./isObject")); | ||
var Builder = (function () { | ||
function Builder(type, defaultValue) { | ||
this.checks = []; | ||
this.currentConfig = {}; | ||
this.currentOptions = {}; | ||
this.deprecatedMessage = ''; | ||
this.errorMessage = ''; | ||
this.isNullable = false; | ||
this.isRequired = false; | ||
if (process.env.NODE_ENV !== 'production') { | ||
this.invariant(typeof defaultValue !== 'undefined', "A default value for type \"" + type + "\" is required."); | ||
this.addCheck(this.checkType); | ||
} | ||
this.invariant(undefs.length === 0, 'All of these options must be defined: ' + _keys.join(', ')); | ||
} | ||
this.defaultValue = defaultValue; | ||
this.type = type; | ||
} | ||
}, { | ||
key: 'checkType', | ||
value: function checkType(path, value) { | ||
if ("production" !== process.env.NODE_ENV) { | ||
switch (this.type) { | ||
case 'array': | ||
this.invariant(Array.isArray(value), 'Must be an array.', path); | ||
break; | ||
case 'custom': | ||
case 'instance': | ||
case 'union': | ||
break; | ||
case 'object': | ||
case 'shape': | ||
this.invariant((0, _isObject2.default)(value), 'Must be a plain object.', path); | ||
break; | ||
default: | ||
this.invariant((typeof value === 'undefined' ? 'undefined' : (0, _typeof3.default)(value)) === this.type, 'Must be a ' + this.type + '.', path); | ||
break; | ||
Builder.prototype.addCheck = function (checker) { | ||
var args = []; | ||
for (var _i = 1; _i < arguments.length; _i++) { | ||
args[_i - 1] = arguments[_i]; | ||
} | ||
} | ||
} | ||
}, { | ||
key: 'custom', | ||
value: function custom(callback) { | ||
if ("production" !== process.env.NODE_ENV) { | ||
this.invariant(typeof callback === 'function', 'Custom blueprints require a validation function.'); | ||
} | ||
return this.addCheck(this.checkCustom, callback); | ||
} | ||
}, { | ||
key: 'checkCustom', | ||
value: function checkCustom(path, value, callback) { | ||
if ("production" !== process.env.NODE_ENV) { | ||
try { | ||
callback(value, this.currentOptions); | ||
} catch (error) { | ||
this.invariant(false, error.message, path); | ||
if (process.env.NODE_ENV !== 'production') { | ||
this.checks.push({ | ||
args: args, | ||
callback: checker, | ||
}); | ||
} | ||
} | ||
} | ||
}, { | ||
key: 'deprecate', | ||
value: function deprecate(message) { | ||
if ("production" !== process.env.NODE_ENV) { | ||
this.invariant(typeof message === 'string' && !!message, 'A non-empty string is required for deprecated messages.'); | ||
this.deprecatedMessage = message; | ||
} | ||
return this; | ||
} | ||
}, { | ||
key: 'invariant', | ||
value: function invariant(condition, message) { | ||
var path = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : ''; | ||
if ("production" !== process.env.NODE_ENV) { | ||
if (condition) { | ||
return; | ||
return this; | ||
}; | ||
Builder.prototype.and = function () { | ||
var keys = []; | ||
for (var _i = 0; _i < arguments.length; _i++) { | ||
keys[_i] = arguments[_i]; | ||
} | ||
var name = this.currentConfig.name; | ||
var prefix = ''; | ||
if (path) { | ||
if (name) { | ||
prefix += 'Invalid ' + name + ' option "' + path + '". '; | ||
} else { | ||
prefix += 'Invalid option "' + path + '". '; | ||
} | ||
} else if (name) { | ||
prefix += name + ': '; | ||
if (process.env.NODE_ENV !== 'production') { | ||
this.invariant(keys.length > 0, 'AND requires a list of option names.'); | ||
} | ||
throw new Error('' + prefix + (this.errorMessage || message)); | ||
} | ||
} | ||
}, { | ||
key: 'key', | ||
value: function key(path) { | ||
var index = path.lastIndexOf('.'); | ||
return index > 0 ? path.slice(index + 1) : path; | ||
} | ||
}, { | ||
key: 'message', | ||
value: function message(_message) { | ||
if ("production" !== process.env.NODE_ENV) { | ||
this.invariant(typeof _message === 'string' && !!_message, 'A non-empty string is required for custom messages.'); | ||
this.errorMessage = _message; | ||
} | ||
return this; | ||
} | ||
}, { | ||
key: 'nullable', | ||
value: function nullable() { | ||
var state = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : true; | ||
if ("production" !== process.env.NODE_ENV) { | ||
this.isNullable = state; | ||
} | ||
return this; | ||
} | ||
}, { | ||
key: 'only', | ||
value: function only() { | ||
if ("production" !== process.env.NODE_ENV) { | ||
this.invariant((0, _typeof3.default)(this.defaultValue) === this.type, 'Only requires a default ' + this.type + ' value.'); | ||
} | ||
return this.addCheck(this.checkOnly); | ||
} | ||
}, { | ||
key: 'checkOnly', | ||
value: function checkOnly(path, value) { | ||
if ("production" !== process.env.NODE_ENV) { | ||
this.invariant(value === this.defaultValue, 'Value may only be "' + String(this.defaultValue) + '".', path); | ||
} | ||
} | ||
}, { | ||
key: 'or', | ||
value: function or() { | ||
for (var _len3 = arguments.length, keys = Array(_len3), _key3 = 0; _key3 < _len3; _key3++) { | ||
keys[_key3] = arguments[_key3]; | ||
} | ||
if ("production" !== process.env.NODE_ENV) { | ||
this.invariant(keys.length > 0, 'OR requires a list of option names.'); | ||
} | ||
return this.addCheck(this.checkOr, keys); | ||
} | ||
}, { | ||
key: 'checkOr', | ||
value: function checkOr(path, value, otherKeys) { | ||
if ("production" !== process.env.NODE_ENV) { | ||
var _keys2 = [this.key(path)].concat((0, _toConsumableArray3.default)(otherKeys)); | ||
var options = this.currentOptions; | ||
var defs = _keys2.filter(function (key) { | ||
return typeof options[key] !== 'undefined' && options[key] !== null; | ||
}); | ||
this.invariant(defs.length > 0, 'At least one of these options must be defined: ' + _keys2.join(', ')); | ||
} | ||
} | ||
}, { | ||
key: 'required', | ||
value: function required() { | ||
var state = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : true; | ||
if ("production" !== process.env.NODE_ENV) { | ||
this.isRequired = state; | ||
} | ||
return this; | ||
} | ||
}, { | ||
key: 'runChecks', | ||
value: function runChecks(path, initialValue, options) { | ||
var _this = this; | ||
var config = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {}; | ||
this.currentConfig = config; | ||
this.currentOptions = options; | ||
var value = initialValue; | ||
if (typeof value === 'undefined') { | ||
if (!this.isRequired) { | ||
value = this.defaultValue; | ||
} else if ("production" !== process.env.NODE_ENV) { | ||
this.invariant(false, 'Field is required and must be defined.', path); | ||
return this.addCheck(this.checkAnd, keys); | ||
}; | ||
Builder.prototype.checkAnd = function (path, value, otherKeys) { | ||
if (process.env.NODE_ENV !== 'production') { | ||
var keys = [this.key(path)].concat(otherKeys); | ||
var options_1 = this.currentOptions; | ||
var undefs = keys.filter(function (key) { return typeof options_1[key] === 'undefined' || options_1[key] === null; }); | ||
if (undefs.length === keys.length) { | ||
return; | ||
} | ||
this.invariant(undefs.length === 0, "All of these options must be defined: " + keys.join(', ')); | ||
} | ||
} else if (this.deprecatedMessage) { | ||
if ("production" !== process.env.NODE_ENV) { | ||
console.info('Option "' + path + '" is deprecated. ' + this.deprecatedMessage); | ||
}; | ||
Builder.prototype.checkType = function (path, value) { | ||
if (process.env.NODE_ENV !== 'production') { | ||
switch (this.type) { | ||
case 'array': | ||
this.invariant(Array.isArray(value), 'Must be an array.', path); | ||
break; | ||
case 'custom': | ||
case 'instance': | ||
case 'union': | ||
break; | ||
case 'object': | ||
case 'shape': | ||
this.invariant(isObject_1.default(value), 'Must be a plain object.', path); | ||
break; | ||
default: | ||
this.invariant(typeof value === this.type, "Must be a " + this.type + ".", path); | ||
break; | ||
} | ||
} | ||
} | ||
if (value === null) { | ||
if (this.isNullable) { | ||
return value; | ||
} else if ("production" !== process.env.NODE_ENV) { | ||
this.invariant(false, 'Null is not allowed.', path); | ||
}; | ||
Builder.prototype.custom = function (callback) { | ||
if (process.env.NODE_ENV !== 'production') { | ||
this.invariant(typeof callback === 'function', 'Custom blueprints require a validation function.'); | ||
} | ||
} | ||
if ("production" !== process.env.NODE_ENV) { | ||
this.checks.forEach(function (checker) { | ||
var _checker$func; | ||
(_checker$func = checker.func).call.apply(_checker$func, [_this, path, value].concat((0, _toConsumableArray3.default)(checker.args))); | ||
}); | ||
} | ||
return value; | ||
} | ||
}, { | ||
key: 'typeAlias', | ||
value: function typeAlias() { | ||
return this.type[0].toUpperCase() + this.type.slice(1); | ||
} | ||
}, { | ||
key: 'xor', | ||
value: function xor() { | ||
for (var _len4 = arguments.length, keys = Array(_len4), _key4 = 0; _key4 < _len4; _key4++) { | ||
keys[_key4] = arguments[_key4]; | ||
} | ||
if ("production" !== process.env.NODE_ENV) { | ||
this.invariant(keys.length > 0, 'XOR requires a list of option names.'); | ||
} | ||
return this.addCheck(this.checkXor, keys); | ||
} | ||
}, { | ||
key: 'checkXor', | ||
value: function checkXor(path, value, otherKeys) { | ||
if ("production" !== process.env.NODE_ENV) { | ||
var _keys3 = [this.key(path)].concat((0, _toConsumableArray3.default)(otherKeys)); | ||
var options = this.currentOptions; | ||
var defs = _keys3.filter(function (key) { | ||
return typeof options[key] !== 'undefined' && options[key] !== null; | ||
}); | ||
this.invariant(defs.length === 1, 'Only one of these options may be defined: ' + _keys3.join(', ')); | ||
} | ||
} | ||
}]); | ||
return Builder; | ||
}(); /** | ||
* @copyright 2017, Miles Johnson | ||
* @license https://opensource.org/licenses/MIT | ||
* | ||
*/ | ||
return this.addCheck(this.checkCustom, callback); | ||
}; | ||
Builder.prototype.checkCustom = function (path, value, callback) { | ||
if (process.env.NODE_ENV !== 'production') { | ||
try { | ||
callback(value, this.currentOptions); | ||
} | ||
catch (error) { | ||
this.invariant(false, error.message, path); | ||
} | ||
} | ||
}; | ||
Builder.prototype.deprecate = function (message) { | ||
if (process.env.NODE_ENV !== 'production') { | ||
this.invariant(typeof message === 'string' && !!message, 'A non-empty string is required for deprecated messages.'); | ||
this.deprecatedMessage = message; | ||
} | ||
return this; | ||
}; | ||
Builder.prototype.invariant = function (condition, message, path) { | ||
if (path === void 0) { path = ''; } | ||
if (process.env.NODE_ENV !== 'production') { | ||
if (condition) { | ||
return; | ||
} | ||
var name = this.currentConfig.name; | ||
var prefix = ''; | ||
if (path) { | ||
if (name) { | ||
prefix += "Invalid " + name + " option \"" + path + "\". "; | ||
} | ||
else { | ||
prefix += "Invalid option \"" + path + "\". "; | ||
} | ||
} | ||
else if (name) { | ||
prefix += name + ": "; | ||
} | ||
throw new Error("" + prefix + (this.errorMessage || message)); | ||
} | ||
}; | ||
Builder.prototype.key = function (path) { | ||
var index = path.lastIndexOf('.'); | ||
return index > 0 ? path.slice(index + 1) : path; | ||
}; | ||
Builder.prototype.message = function (message) { | ||
if (process.env.NODE_ENV !== 'production') { | ||
this.invariant(typeof message === 'string' && !!message, 'A non-empty string is required for custom messages.'); | ||
this.errorMessage = message; | ||
} | ||
return this; | ||
}; | ||
Builder.prototype.nullable = function (state) { | ||
if (state === void 0) { state = true; } | ||
if (process.env.NODE_ENV !== 'production') { | ||
this.isNullable = state; | ||
} | ||
return this; | ||
}; | ||
Builder.prototype.only = function () { | ||
if (process.env.NODE_ENV !== 'production') { | ||
this.invariant(typeof this.defaultValue === this.type, "Only requires a default " + this.type + " value."); | ||
} | ||
return this.addCheck(this.checkOnly); | ||
}; | ||
Builder.prototype.checkOnly = function (path, value) { | ||
if (process.env.NODE_ENV !== 'production') { | ||
this.invariant(value === this.defaultValue, "Value may only be \"" + String(this.defaultValue) + "\".", path); | ||
} | ||
}; | ||
Builder.prototype.or = function () { | ||
var keys = []; | ||
for (var _i = 0; _i < arguments.length; _i++) { | ||
keys[_i] = arguments[_i]; | ||
} | ||
if (process.env.NODE_ENV !== 'production') { | ||
this.invariant(keys.length > 0, 'OR requires a list of option names.'); | ||
} | ||
return this.addCheck(this.checkOr, keys); | ||
}; | ||
Builder.prototype.checkOr = function (path, value, otherKeys) { | ||
if (process.env.NODE_ENV !== 'production') { | ||
var keys = [this.key(path)].concat(otherKeys); | ||
var options_2 = this.currentOptions; | ||
var defs = keys.filter(function (key) { return typeof options_2[key] !== 'undefined' && options_2[key] !== null; }); | ||
this.invariant(defs.length > 0, "At least one of these options must be defined: " + keys.join(', ')); | ||
} | ||
}; | ||
Builder.prototype.required = function (state) { | ||
if (state === void 0) { state = true; } | ||
if (process.env.NODE_ENV !== 'production') { | ||
this.isRequired = state; | ||
} | ||
return this; | ||
}; | ||
Builder.prototype.runChecks = function (path, initialValue, options, config) { | ||
var _this = this; | ||
if (config === void 0) { config = {}; } | ||
this.currentConfig = config; | ||
this.currentOptions = options; | ||
var value = initialValue; | ||
if (typeof value === 'undefined') { | ||
if (!this.isRequired) { | ||
value = this.defaultValue; | ||
} | ||
else if (process.env.NODE_ENV !== 'production') { | ||
this.invariant(false, 'Field is required and must be defined.', path); | ||
} | ||
} | ||
else if (this.deprecatedMessage) { | ||
if (process.env.NODE_ENV !== 'production') { | ||
console.info("Option \"" + path + "\" is deprecated. " + this.deprecatedMessage); | ||
} | ||
} | ||
if (value === null) { | ||
if (this.isNullable) { | ||
return value; | ||
} | ||
else if (process.env.NODE_ENV !== 'production') { | ||
this.invariant(false, 'Null is not allowed.', path); | ||
} | ||
} | ||
if (process.env.NODE_ENV !== 'production') { | ||
this.checks.forEach(function (checker) { | ||
(_a = checker.callback).call.apply(_a, [_this, path, value].concat(checker.args)); | ||
var _a; | ||
}); | ||
} | ||
return value; | ||
}; | ||
Builder.prototype.typeAlias = function () { | ||
return this.type; | ||
}; | ||
Builder.prototype.xor = function () { | ||
var keys = []; | ||
for (var _i = 0; _i < arguments.length; _i++) { | ||
keys[_i] = arguments[_i]; | ||
} | ||
if (process.env.NODE_ENV !== 'production') { | ||
this.invariant(keys.length > 0, 'XOR requires a list of option names.'); | ||
} | ||
return this.addCheck(this.checkXor, keys); | ||
}; | ||
Builder.prototype.checkXor = function (path, value, otherKeys) { | ||
if (process.env.NODE_ENV !== 'production') { | ||
var keys = [this.key(path)].concat(otherKeys); | ||
var options_3 = this.currentOptions; | ||
var defs = keys.filter(function (key) { return typeof options_3[key] !== 'undefined' && options_3[key] !== null; }); | ||
this.invariant(defs.length === 1, "Only one of these options may be defined: " + keys.join(', ')); | ||
} | ||
}; | ||
return Builder; | ||
}()); | ||
exports.default = Builder; | ||
function bool() { | ||
var defaultValue = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false; | ||
return new Builder('boolean', defaultValue); | ||
function bool(defaultValue) { | ||
if (defaultValue === void 0) { defaultValue = false; } | ||
return new Builder('boolean', defaultValue); | ||
} | ||
function custom(callback) { | ||
var defaultValue = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null; | ||
return new Builder('custom', defaultValue).custom(callback); | ||
exports.bool = bool; | ||
function custom(callback, defaultValue) { | ||
if (defaultValue === void 0) { defaultValue = null; } | ||
return new Builder('custom', defaultValue).custom(callback); | ||
} | ||
function func() { | ||
var defaultValue = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null; | ||
return new Builder('function', defaultValue).nullable(); | ||
} | ||
exports.custom = custom; | ||
function func(defaultValue) { | ||
if (defaultValue === void 0) { defaultValue = null; } | ||
return new Builder('function', defaultValue).nullable(); | ||
} | ||
exports.func = func; |
@@ -1,130 +0,84 @@ | ||
'use strict'; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
var _keys = require('babel-runtime/core-js/object/keys'); | ||
var _keys2 = _interopRequireDefault(_keys); | ||
var _getPrototypeOf = require('babel-runtime/core-js/object/get-prototype-of'); | ||
var _getPrototypeOf2 = _interopRequireDefault(_getPrototypeOf); | ||
var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck'); | ||
var _classCallCheck3 = _interopRequireDefault(_classCallCheck2); | ||
var _createClass2 = require('babel-runtime/helpers/createClass'); | ||
var _createClass3 = _interopRequireDefault(_createClass2); | ||
var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn'); | ||
var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2); | ||
var _get2 = require('babel-runtime/helpers/get'); | ||
var _get3 = _interopRequireDefault(_get2); | ||
var _inherits2 = require('babel-runtime/helpers/inherits'); | ||
var _inherits3 = _interopRequireDefault(_inherits2); | ||
exports.array = array; | ||
exports.object = object; | ||
var _Builder2 = require('./Builder'); | ||
var _Builder3 = _interopRequireDefault(_Builder2); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
var CollectionBuilder = function (_Builder) { | ||
(0, _inherits3.default)(CollectionBuilder, _Builder); | ||
function CollectionBuilder(type) { | ||
var contents = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null; | ||
var defaultValue = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null; | ||
(0, _classCallCheck3.default)(this, CollectionBuilder); | ||
var _this = (0, _possibleConstructorReturn3.default)(this, (CollectionBuilder.__proto__ || (0, _getPrototypeOf2.default)(CollectionBuilder)).call(this, type, defaultValue)); | ||
if ("production" !== process.env.NODE_ENV) { | ||
if (contents) { | ||
if (contents instanceof _Builder3.default) { | ||
_this.contents = contents; | ||
_this.addCheck(_this.checkContents, contents); | ||
} else { | ||
_this.invariant(false, 'A blueprint is required for ' + type + ' contents.'); | ||
"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 __()); | ||
}; | ||
})(); | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
} | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var Builder_1 = __importDefault(require("./Builder")); | ||
var CollectionBuilder = (function (_super) { | ||
__extends(CollectionBuilder, _super); | ||
function CollectionBuilder(type, contents, defaultValue) { | ||
if (contents === void 0) { contents = null; } | ||
if (defaultValue === void 0) { defaultValue = null; } | ||
var _this = _super.call(this, type, defaultValue) || this; | ||
_this.contents = null; | ||
if (process.env.NODE_ENV !== 'production') { | ||
if (contents) { | ||
if (contents instanceof Builder_1.default) { | ||
_this.contents = contents; | ||
_this.addCheck(_this.checkContents, contents); | ||
} | ||
else { | ||
_this.invariant(false, "A blueprint is required for " + type + " contents."); | ||
} | ||
} | ||
} | ||
} | ||
return _this; | ||
} | ||
return _this; | ||
} | ||
(0, _createClass3.default)(CollectionBuilder, [{ | ||
key: 'checkContents', | ||
value: function checkContents(path, value, contents) { | ||
var _this2 = this; | ||
if ("production" !== process.env.NODE_ENV) { | ||
if (this.type === 'array') { | ||
value.forEach(function (item, i) { | ||
contents.runChecks(path + '[' + i + ']', item, _this2.currentOptions, _this2.currentConfig); | ||
}); | ||
} else if (this.type === 'object') { | ||
(0, _keys2.default)(value).forEach(function (key) { | ||
contents.runChecks(path + '.' + key, value[key], _this2.currentOptions, _this2.currentConfig); | ||
}); | ||
CollectionBuilder.prototype.checkContents = function (path, value, contents) { | ||
var _this = this; | ||
if (process.env.NODE_ENV !== 'production') { | ||
if (this.type === 'array') { | ||
value.forEach(function (item, i) { | ||
contents.runChecks(path + "[" + i + "]", item, _this.currentOptions, _this.currentConfig); | ||
}); | ||
} | ||
else if (this.type === 'object') { | ||
Object.keys(value).forEach(function (key) { | ||
contents.runChecks(path + "." + key, value[key], _this.currentOptions, _this.currentConfig); | ||
}); | ||
} | ||
} | ||
} | ||
} | ||
}, { | ||
key: 'notEmpty', | ||
value: function notEmpty() { | ||
return this.addCheck(this.checkNotEmpty); | ||
} | ||
}, { | ||
key: 'checkNotEmpty', | ||
value: function checkNotEmpty(path, value) { | ||
if ("production" !== process.env.NODE_ENV) { | ||
if (this.type === 'array') { | ||
this.invariant(value.length > 0, 'Array cannot be empty.', path); | ||
} else if (this.type === 'object') { | ||
this.invariant((0, _keys2.default)(value).length > 0, 'Object cannot be empty.', path); | ||
}; | ||
CollectionBuilder.prototype.notEmpty = function () { | ||
return this.addCheck(this.checkNotEmpty); | ||
}; | ||
CollectionBuilder.prototype.checkNotEmpty = function (path, value) { | ||
if (process.env.NODE_ENV !== 'production') { | ||
if (this.type === 'array') { | ||
this.invariant(value.length > 0, 'Array cannot be empty.', path); | ||
} | ||
else if (this.type === 'object') { | ||
this.invariant(Object.keys(value).length > 0, 'Object cannot be empty.', path); | ||
} | ||
} | ||
} | ||
} | ||
}, { | ||
key: 'typeAlias', | ||
value: function typeAlias() { | ||
var contents = this.contents; | ||
var alias = (0, _get3.default)(CollectionBuilder.prototype.__proto__ || (0, _getPrototypeOf2.default)(CollectionBuilder.prototype), 'typeAlias', this).call(this); | ||
return contents ? alias + '<' + contents.typeAlias() + '>' : alias; | ||
} | ||
}]); | ||
return CollectionBuilder; | ||
}(_Builder3.default); /** | ||
* @copyright 2017, Miles Johnson | ||
* @license https://opensource.org/licenses/MIT | ||
* | ||
*/ | ||
}; | ||
CollectionBuilder.prototype.typeAlias = function () { | ||
var contents = this.contents; | ||
var alias = _super.prototype.typeAlias.call(this); | ||
return contents ? alias + "<" + contents.typeAlias() + ">" : alias; | ||
}; | ||
return CollectionBuilder; | ||
}(Builder_1.default)); | ||
exports.default = CollectionBuilder; | ||
function array() { | ||
var contents = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null; | ||
var defaultValue = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : []; | ||
return new CollectionBuilder('array', contents, defaultValue); | ||
function array(contents, defaultValue) { | ||
if (contents === void 0) { contents = null; } | ||
if (defaultValue === void 0) { defaultValue = []; } | ||
return new CollectionBuilder('array', contents, defaultValue); | ||
} | ||
function object() { | ||
var contents = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null; | ||
var defaultValue = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
return new CollectionBuilder('object', contents, defaultValue); | ||
} | ||
exports.array = array; | ||
function object(contents, defaultValue) { | ||
if (contents === void 0) { contents = null; } | ||
if (defaultValue === void 0) { defaultValue = {}; } | ||
return new CollectionBuilder('object', contents, defaultValue); | ||
} | ||
exports.object = object; |
@@ -1,48 +0,23 @@ | ||
'use strict'; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.default = exports.union = exports.string = exports.shape = exports.regex = exports.object = exports.number = exports.instance = exports.func = exports.date = exports.custom = exports.bool = exports.array = undefined; | ||
var _Options = require('./Options'); | ||
Object.defineProperty(exports, 'default', { | ||
enumerable: true, | ||
get: function get() { | ||
return _interopRequireDefault(_Options).default; | ||
} | ||
}); | ||
var _Builder = require('./Builder'); | ||
var _CollectionBuilder = require('./CollectionBuilder'); | ||
var _InstanceBuilder = require('./InstanceBuilder'); | ||
var _NumberBuilder = require('./NumberBuilder'); | ||
var _ShapeBuilder = require('./ShapeBuilder'); | ||
var _StringBuilder = require('./StringBuilder'); | ||
var _UnionBuilder = require('./UnionBuilder'); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
exports.array = _CollectionBuilder.array; | ||
exports.bool = _Builder.bool; | ||
exports.custom = _Builder.custom; | ||
exports.date = _InstanceBuilder.date; | ||
exports.func = _Builder.func; | ||
exports.instance = _InstanceBuilder.instance; | ||
exports.number = _NumberBuilder.number; | ||
exports.object = _CollectionBuilder.object; | ||
exports.regex = _InstanceBuilder.regex; | ||
exports.shape = _ShapeBuilder.shape; | ||
exports.string = _StringBuilder.string; | ||
exports.union = _UnionBuilder.union; /** | ||
* @copyright 2017, Miles Johnson | ||
* @license https://opensource.org/licenses/MIT | ||
* | ||
*/ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var Builder_1 = require("./Builder"); | ||
exports.bool = Builder_1.bool; | ||
exports.custom = Builder_1.custom; | ||
exports.func = Builder_1.func; | ||
var CollectionBuilder_1 = require("./CollectionBuilder"); | ||
exports.array = CollectionBuilder_1.array; | ||
exports.object = CollectionBuilder_1.object; | ||
var InstanceBuilder_1 = require("./InstanceBuilder"); | ||
exports.instance = InstanceBuilder_1.instance; | ||
exports.date = InstanceBuilder_1.date; | ||
exports.regex = InstanceBuilder_1.regex; | ||
var NumberBuilder_1 = require("./NumberBuilder"); | ||
exports.number = NumberBuilder_1.number; | ||
var ShapeBuilder_1 = require("./ShapeBuilder"); | ||
exports.shape = ShapeBuilder_1.shape; | ||
var StringBuilder_1 = require("./StringBuilder"); | ||
exports.string = StringBuilder_1.string; | ||
var UnionBuilder_1 = require("./UnionBuilder"); | ||
exports.union = UnionBuilder_1.union; | ||
var Options_1 = require("./Options"); | ||
exports.default = Options_1.default; |
@@ -1,104 +0,63 @@ | ||
'use strict'; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
var _getPrototypeOf = require('babel-runtime/core-js/object/get-prototype-of'); | ||
var _getPrototypeOf2 = _interopRequireDefault(_getPrototypeOf); | ||
var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck'); | ||
var _classCallCheck3 = _interopRequireDefault(_classCallCheck2); | ||
var _createClass2 = require('babel-runtime/helpers/createClass'); | ||
var _createClass3 = _interopRequireDefault(_createClass2); | ||
var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn'); | ||
var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2); | ||
var _inherits2 = require('babel-runtime/helpers/inherits'); | ||
var _inherits3 = _interopRequireDefault(_inherits2); | ||
exports.instance = instance; | ||
exports.regex = regex; | ||
exports.date = date; | ||
var _Builder2 = require('./Builder'); | ||
var _Builder3 = _interopRequireDefault(_Builder2); | ||
var _isObject = require('./isObject'); | ||
var _isObject2 = _interopRequireDefault(_isObject); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
/** | ||
* @copyright 2017, Miles Johnson | ||
* @license https://opensource.org/licenses/MIT | ||
* | ||
*/ | ||
var InstanceBuilder = function (_Builder) { | ||
(0, _inherits3.default)(InstanceBuilder, _Builder); | ||
function InstanceBuilder() { | ||
var refClass = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null; | ||
(0, _classCallCheck3.default)(this, InstanceBuilder); | ||
var _this = (0, _possibleConstructorReturn3.default)(this, (InstanceBuilder.__proto__ || (0, _getPrototypeOf2.default)(InstanceBuilder)).call(this, 'instance', null)); | ||
_this.nullable(); | ||
if ("production" !== process.env.NODE_ENV) { | ||
if (refClass) { | ||
_this.invariant(typeof refClass === 'function', 'A class reference is required.'); | ||
} | ||
_this.refClass = refClass; | ||
_this.addCheck(_this.checkInstance, refClass); | ||
"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 __()); | ||
}; | ||
})(); | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
} | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var Builder_1 = __importDefault(require("./Builder")); | ||
var isObject_1 = __importDefault(require("./isObject")); | ||
var InstanceBuilder = (function (_super) { | ||
__extends(InstanceBuilder, _super); | ||
function InstanceBuilder(refClass) { | ||
if (refClass === void 0) { refClass = null; } | ||
var _this = _super.call(this, 'instance', null) || this; | ||
_this.refClass = null; | ||
_this.nullable(); | ||
if (process.env.NODE_ENV !== 'production') { | ||
if (refClass) { | ||
_this.invariant(typeof refClass === 'function', 'A class reference is required.'); | ||
} | ||
_this.refClass = refClass; | ||
_this.addCheck(_this.checkInstance, refClass); | ||
} | ||
return _this; | ||
} | ||
return _this; | ||
} | ||
(0, _createClass3.default)(InstanceBuilder, [{ | ||
key: 'checkInstance', | ||
value: function checkInstance(path, value, refClass) { | ||
if ("production" !== process.env.NODE_ENV) { | ||
if (refClass) { | ||
this.invariant(value instanceof refClass, 'Must be an instance of "' + this.typeAlias() + '".', path); | ||
} else { | ||
this.invariant((0, _isObject2.default)(value) && value.constructor !== Object, 'Must be a class instance.', path); | ||
InstanceBuilder.prototype.checkInstance = function (path, value, refClass) { | ||
if (process.env.NODE_ENV !== 'production') { | ||
if (refClass) { | ||
this.invariant(typeof refClass === 'function' && value instanceof refClass, "Must be an instance of \"" + this.typeAlias() + "\".", path); | ||
} | ||
else { | ||
this.invariant(isObject_1.default(value) && value.constructor !== Object, 'Must be a class instance.', path); | ||
} | ||
} | ||
} | ||
} | ||
}, { | ||
key: 'typeAlias', | ||
value: function typeAlias() { | ||
var refClass = this.refClass; | ||
return refClass ? refClass.name || refClass.constructor.name : 'Class'; | ||
} | ||
}]); | ||
return InstanceBuilder; | ||
}(_Builder3.default); | ||
}; | ||
InstanceBuilder.prototype.typeAlias = function () { | ||
var refClass = this.refClass; | ||
return refClass ? refClass.name || refClass.constructor.name : 'class'; | ||
}; | ||
return InstanceBuilder; | ||
}(Builder_1.default)); | ||
exports.default = InstanceBuilder; | ||
function instance() { | ||
var refClass = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null; | ||
return new InstanceBuilder(refClass); | ||
function instance(refClass) { | ||
if (refClass === void 0) { refClass = null; } | ||
return new InstanceBuilder(refClass); | ||
} | ||
exports.instance = instance; | ||
function regex() { | ||
return instance(RegExp); | ||
return instance(RegExp); | ||
} | ||
exports.regex = regex; | ||
function date() { | ||
return instance(Date); | ||
} | ||
return instance(Date); | ||
} | ||
exports.date = date; |
@@ -1,23 +0,6 @@ | ||
'use strict'; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
var _typeof2 = require('babel-runtime/helpers/typeof'); | ||
var _typeof3 = _interopRequireDefault(_typeof2); | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
function isObject(value) { | ||
return typeof value === 'object' && value !== null && !Array.isArray(value); | ||
} | ||
exports.default = isObject; | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
/** | ||
* @copyright 2017, Miles Johnson | ||
* @license https://opensource.org/licenses/MIT | ||
* | ||
*/ | ||
function isObject(value) { | ||
return (typeof value === 'undefined' ? 'undefined' : (0, _typeof3.default)(value)) === 'object' && value !== null && !Array.isArray(value); | ||
} |
@@ -1,155 +0,99 @@ | ||
'use strict'; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
var _getPrototypeOf = require('babel-runtime/core-js/object/get-prototype-of'); | ||
var _getPrototypeOf2 = _interopRequireDefault(_getPrototypeOf); | ||
var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck'); | ||
var _classCallCheck3 = _interopRequireDefault(_classCallCheck2); | ||
var _createClass2 = require('babel-runtime/helpers/createClass'); | ||
var _createClass3 = _interopRequireDefault(_createClass2); | ||
var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn'); | ||
var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2); | ||
var _inherits2 = require('babel-runtime/helpers/inherits'); | ||
var _inherits3 = _interopRequireDefault(_inherits2); | ||
exports.number = number; | ||
var _Builder2 = require('./Builder'); | ||
var _Builder3 = _interopRequireDefault(_Builder2); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
"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 __()); | ||
}; | ||
})(); | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
} | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var Builder_1 = __importDefault(require("./Builder")); | ||
function isNumber(value) { | ||
return typeof value === 'number'; | ||
} /** | ||
* @copyright 2017, Miles Johnson | ||
* @license https://opensource.org/licenses/MIT | ||
* | ||
*/ | ||
var NumberBuilder = function (_Builder) { | ||
(0, _inherits3.default)(NumberBuilder, _Builder); | ||
function NumberBuilder() { | ||
var defaultValue = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0; | ||
(0, _classCallCheck3.default)(this, NumberBuilder); | ||
return (0, _possibleConstructorReturn3.default)(this, (NumberBuilder.__proto__ || (0, _getPrototypeOf2.default)(NumberBuilder)).call(this, 'number', defaultValue)); | ||
} | ||
(0, _createClass3.default)(NumberBuilder, [{ | ||
key: 'between', | ||
value: function between(min, max) { | ||
var inclusive = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false; | ||
if ("production" !== process.env.NODE_ENV) { | ||
this.invariant(isNumber(min) && isNumber(max), 'Between requires a minimum and maximum number.'); | ||
} | ||
return this.addCheck(this.checkBetween, min, max, inclusive); | ||
return typeof value === 'number'; | ||
} | ||
var NumberBuilder = (function (_super) { | ||
__extends(NumberBuilder, _super); | ||
function NumberBuilder(defaultValue) { | ||
if (defaultValue === void 0) { defaultValue = 0; } | ||
return _super.call(this, 'number', defaultValue) || this; | ||
} | ||
}, { | ||
key: 'checkBetween', | ||
value: function checkBetween(path, value, min, max) { | ||
var inclusive = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : false; | ||
if ("production" !== process.env.NODE_ENV) { | ||
this.invariant(isNumber(value) && (inclusive ? value >= min && value <= max : value > min && value < max), 'Number must be between ' + min + ' and ' + max + (inclusive ? ' inclusive' : '') + '.', path); | ||
} | ||
} | ||
}, { | ||
key: 'gt', | ||
value: function gt(min) { | ||
var inclusive = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false; | ||
if ("production" !== process.env.NODE_ENV) { | ||
this.invariant(isNumber(min), 'Greater-than requires a minimum number.'); | ||
} | ||
return this.addCheck(this.checkGreaterThan, min, inclusive); | ||
} | ||
}, { | ||
key: 'gte', | ||
value: function gte(min) { | ||
return this.gt(min, true); | ||
} | ||
}, { | ||
key: 'checkGreaterThan', | ||
value: function checkGreaterThan(path, value, min) { | ||
var inclusive = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : false; | ||
if ("production" !== process.env.NODE_ENV) { | ||
if (inclusive) { | ||
this.invariant(isNumber(value) && value >= min, 'Number must be greater than or equal to ' + min + '.', path); | ||
} else { | ||
this.invariant(isNumber(value) && value > min, 'Number must be greater than ' + min + '.', path); | ||
NumberBuilder.prototype.between = function (min, max, inclusive) { | ||
if (inclusive === void 0) { inclusive = false; } | ||
if (process.env.NODE_ENV !== 'production') { | ||
this.invariant(isNumber(min) && isNumber(max), 'Between requires a minimum and maximum number.'); | ||
} | ||
} | ||
} | ||
}, { | ||
key: 'lt', | ||
value: function lt(max) { | ||
var inclusive = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false; | ||
if ("production" !== process.env.NODE_ENV) { | ||
this.invariant(isNumber(max), 'Less-than requires a maximum number.'); | ||
} | ||
return this.addCheck(this.checkLessThan, max, inclusive); | ||
} | ||
}, { | ||
key: 'lte', | ||
value: function lte(max) { | ||
return this.lt(max, true); | ||
} | ||
}, { | ||
key: 'checkLessThan', | ||
value: function checkLessThan(path, value, max) { | ||
var inclusive = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : false; | ||
if ("production" !== process.env.NODE_ENV) { | ||
if (inclusive) { | ||
this.invariant(isNumber(value) && value <= max, 'Number must be less than or equal to ' + max + '.', path); | ||
} else { | ||
this.invariant(isNumber(value) && value < max, 'Number must be less than ' + max + '.', path); | ||
return this.addCheck(this.checkBetween, min, max, inclusive); | ||
}; | ||
NumberBuilder.prototype.checkBetween = function (path, value, min, max, inclusive) { | ||
if (inclusive === void 0) { inclusive = false; } | ||
if (process.env.NODE_ENV !== 'production') { | ||
this.invariant(isNumber(value) && (inclusive ? value >= min && value <= max : value > min && value < max), "Number must be between " + min + " and " + max + (inclusive ? ' inclusive' : '') + ".", path); | ||
} | ||
} | ||
} | ||
}, { | ||
key: 'oneOf', | ||
value: function oneOf(list) { | ||
if ("production" !== process.env.NODE_ENV) { | ||
this.invariant(Array.isArray(list) && list.length > 0 && list.every(isNumber), 'One of requires a non-empty array of numbers.'); | ||
} | ||
return this.addCheck(this.checkOneOf, list); | ||
} | ||
}, { | ||
key: 'checkOneOf', | ||
value: function checkOneOf(path, value, list) { | ||
if ("production" !== process.env.NODE_ENV) { | ||
this.invariant(list.indexOf(value) >= 0, 'Number must be one of: ' + list.join(', '), path); | ||
} | ||
} | ||
}]); | ||
return NumberBuilder; | ||
}(_Builder3.default); | ||
}; | ||
NumberBuilder.prototype.gt = function (min, inclusive) { | ||
if (inclusive === void 0) { inclusive = false; } | ||
if (process.env.NODE_ENV !== 'production') { | ||
this.invariant(isNumber(min), 'Greater-than requires a minimum number.'); | ||
} | ||
return this.addCheck(this.checkGreaterThan, min, inclusive); | ||
}; | ||
NumberBuilder.prototype.gte = function (min) { | ||
return this.gt(min, true); | ||
}; | ||
NumberBuilder.prototype.checkGreaterThan = function (path, value, min, inclusive) { | ||
if (inclusive === void 0) { inclusive = false; } | ||
if (process.env.NODE_ENV !== 'production') { | ||
if (inclusive) { | ||
this.invariant(isNumber(value) && value >= min, "Number must be greater than or equal to " + min + ".", path); | ||
} | ||
else { | ||
this.invariant(isNumber(value) && value > min, "Number must be greater than " + min + ".", path); | ||
} | ||
} | ||
}; | ||
NumberBuilder.prototype.lt = function (max, inclusive) { | ||
if (inclusive === void 0) { inclusive = false; } | ||
if (process.env.NODE_ENV !== 'production') { | ||
this.invariant(isNumber(max), 'Less-than requires a maximum number.'); | ||
} | ||
return this.addCheck(this.checkLessThan, max, inclusive); | ||
}; | ||
NumberBuilder.prototype.lte = function (max) { | ||
return this.lt(max, true); | ||
}; | ||
NumberBuilder.prototype.checkLessThan = function (path, value, max, inclusive) { | ||
if (inclusive === void 0) { inclusive = false; } | ||
if (process.env.NODE_ENV !== 'production') { | ||
if (inclusive) { | ||
this.invariant(isNumber(value) && value <= max, "Number must be less than or equal to " + max + ".", path); | ||
} | ||
else { | ||
this.invariant(isNumber(value) && value < max, "Number must be less than " + max + ".", path); | ||
} | ||
} | ||
}; | ||
NumberBuilder.prototype.oneOf = function (list) { | ||
if (process.env.NODE_ENV !== 'production') { | ||
this.invariant(Array.isArray(list) && list.length > 0 && list.every(function (item) { return isNumber(item); }), 'One of requires a non-empty array of numbers.'); | ||
} | ||
return this.addCheck(this.checkOneOf, list); | ||
}; | ||
NumberBuilder.prototype.checkOneOf = function (path, value, list) { | ||
if (process.env.NODE_ENV !== 'production') { | ||
this.invariant(list.indexOf(value) >= 0, "Number must be one of: " + list.join(', '), path); | ||
} | ||
}; | ||
return NumberBuilder; | ||
}(Builder_1.default)); | ||
exports.default = NumberBuilder; | ||
function number() { | ||
var defaultValue = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0; | ||
return new NumberBuilder(defaultValue); | ||
} | ||
function number(defaultValue) { | ||
if (defaultValue === void 0) { defaultValue = 0; } | ||
return new NumberBuilder(defaultValue); | ||
} | ||
exports.number = number; |
@@ -1,88 +0,62 @@ | ||
'use strict'; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
var _assign = require('babel-runtime/core-js/object/assign'); | ||
var _assign2 = _interopRequireDefault(_assign); | ||
var _keys = require('babel-runtime/core-js/object/keys'); | ||
var _keys2 = _interopRequireDefault(_keys); | ||
var _extends2 = require('babel-runtime/helpers/extends'); | ||
var _extends3 = _interopRequireDefault(_extends2); | ||
exports.default = Options; | ||
var _Builder = require('./Builder'); | ||
var _Builder2 = _interopRequireDefault(_Builder); | ||
var _isObject = require('./isObject'); | ||
var _isObject2 = _interopRequireDefault(_isObject); | ||
var _typeOf = require('./typeOf'); | ||
var _typeOf2 = _interopRequireDefault(_typeOf); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
function buildAndCheckOptions(baseOptions, blueprint) { | ||
var config = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; | ||
var parentPath = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : ''; | ||
var unknownOptions = (0, _extends3.default)({}, baseOptions); | ||
var options = {}; | ||
(0, _keys2.default)(blueprint).forEach(function (key) { | ||
var builder = blueprint[key]; | ||
var path = parentPath ? parentPath + '.' + key : key; | ||
if (builder instanceof _Builder2.default) { | ||
options[key] = builder.runChecks(path, baseOptions[key], baseOptions, config); | ||
} else if ((0, _isObject2.default)(builder)) { | ||
options[key] = buildAndCheckOptions(baseOptions[key] || {}, builder, config, path); | ||
} else if ("production" !== process.env.NODE_ENV) { | ||
throw new Error('Unknown blueprint option. Must be a builder or plain object.'); | ||
"use strict"; | ||
var __assign = (this && this.__assign) || Object.assign || function(t) { | ||
for (var s, i = 1, n = arguments.length; i < n; i++) { | ||
s = arguments[i]; | ||
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) | ||
t[p] = s[p]; | ||
} | ||
delete unknownOptions[key]; | ||
}); | ||
if (config.unknown) { | ||
(0, _assign2.default)(options, unknownOptions); | ||
} else if ("production" !== process.env.NODE_ENV) { | ||
var unknownKeys = (0, _keys2.default)(unknownOptions); | ||
if (unknownKeys.length > 0) { | ||
throw new Error('Unknown options: ' + unknownKeys.join(', ') + '.'); | ||
return t; | ||
}; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
} | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var Builder_1 = __importDefault(require("./Builder")); | ||
var isObject_1 = __importDefault(require("./isObject")); | ||
var typeOf_1 = __importDefault(require("./typeOf")); | ||
function buildAndCheckOptions(baseOptions, blueprint, config, parentPath) { | ||
if (config === void 0) { config = {}; } | ||
if (parentPath === void 0) { parentPath = ''; } | ||
var unknownOptions = __assign({}, baseOptions); | ||
var options = {}; | ||
Object.keys(blueprint).forEach(function (key) { | ||
var builder = blueprint[key]; | ||
var path = parentPath ? parentPath + "." + key : key; | ||
if (builder instanceof Builder_1.default) { | ||
options[key] = builder.runChecks(path, baseOptions[key], baseOptions, config); | ||
} | ||
else if (isObject_1.default(builder)) { | ||
options[key] = buildAndCheckOptions(baseOptions[key] || {}, builder, config, path); | ||
} | ||
else if (process.env.NODE_ENV !== 'production') { | ||
throw new Error('Unknown blueprint option. Must be a builder or plain object.'); | ||
} | ||
delete unknownOptions[key]; | ||
}); | ||
if (config.unknown) { | ||
Object.assign(options, unknownOptions); | ||
} | ||
} | ||
return options; | ||
} /** | ||
* @copyright 2017, Miles Johnson | ||
* @license https://opensource.org/licenses/MIT | ||
* | ||
*/ | ||
function Options(options, blueprint) { | ||
var config = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; | ||
if ("production" !== process.env.NODE_ENV) { | ||
if (!(0, _isObject2.default)(options)) { | ||
throw new TypeError('Options require a plain object, found ' + (0, _typeOf2.default)(options) + '.'); | ||
} else if (!(0, _isObject2.default)(config)) { | ||
throw new TypeError('Option configuration must be a plain object.'); | ||
} else if (!(0, _isObject2.default)(blueprint)) { | ||
throw new TypeError('An options blueprint is required.'); | ||
else if (process.env.NODE_ENV !== 'production') { | ||
var unknownKeys = Object.keys(unknownOptions); | ||
if (unknownKeys.length > 0) { | ||
throw new Error("Unknown options: " + unknownKeys.join(', ') + "."); | ||
} | ||
} | ||
} | ||
return buildAndCheckOptions(options, blueprint, config); | ||
} | ||
return options; | ||
} | ||
function parseOptions(options, blueprint, config) { | ||
if (config === void 0) { config = {}; } | ||
if (process.env.NODE_ENV !== 'production') { | ||
if (!isObject_1.default(options)) { | ||
throw new TypeError("Options require a plain object, found " + typeOf_1.default(options) + "."); | ||
} | ||
else if (!isObject_1.default(config)) { | ||
throw new TypeError('Option configuration must be a plain object.'); | ||
} | ||
else if (!isObject_1.default(blueprint)) { | ||
throw new TypeError('An options blueprint is required.'); | ||
} | ||
} | ||
return buildAndCheckOptions(options, blueprint, config); | ||
} | ||
exports.default = parseOptions; |
@@ -1,92 +0,49 @@ | ||
'use strict'; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
var _keys = require('babel-runtime/core-js/object/keys'); | ||
var _keys2 = _interopRequireDefault(_keys); | ||
var _getPrototypeOf = require('babel-runtime/core-js/object/get-prototype-of'); | ||
var _getPrototypeOf2 = _interopRequireDefault(_getPrototypeOf); | ||
var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck'); | ||
var _classCallCheck3 = _interopRequireDefault(_classCallCheck2); | ||
var _createClass2 = require('babel-runtime/helpers/createClass'); | ||
var _createClass3 = _interopRequireDefault(_createClass2); | ||
var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn'); | ||
var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2); | ||
var _inherits2 = require('babel-runtime/helpers/inherits'); | ||
var _inherits3 = _interopRequireDefault(_inherits2); | ||
exports.shape = shape; | ||
var _Builder2 = require('./Builder'); | ||
var _Builder3 = _interopRequireDefault(_Builder2); | ||
var _isObject = require('./isObject'); | ||
var _isObject2 = _interopRequireDefault(_isObject); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
/** | ||
* @copyright 2017, Miles Johnson | ||
* @license https://opensource.org/licenses/MIT | ||
* | ||
*/ | ||
var ShapeBuilder = function (_Builder) { | ||
(0, _inherits3.default)(ShapeBuilder, _Builder); | ||
function ShapeBuilder(contents) { | ||
var defaultValue = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
(0, _classCallCheck3.default)(this, ShapeBuilder); | ||
var _this = (0, _possibleConstructorReturn3.default)(this, (ShapeBuilder.__proto__ || (0, _getPrototypeOf2.default)(ShapeBuilder)).call(this, 'shape', defaultValue)); | ||
if ("production" !== process.env.NODE_ENV) { | ||
_this.invariant((0, _isObject2.default)(contents) && (0, _keys2.default)(contents).length > 0 && (0, _keys2.default)(contents).every(function (key) { | ||
return contents[key] instanceof _Builder3.default; | ||
}), 'A non-empty object of properties to blueprints are required for a shape.'); | ||
_this.addCheck(_this.checkContents, contents); | ||
"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 __()); | ||
}; | ||
})(); | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
} | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var Builder_1 = __importDefault(require("./Builder")); | ||
var isObject_1 = __importDefault(require("./isObject")); | ||
var ShapeBuilder = (function (_super) { | ||
__extends(ShapeBuilder, _super); | ||
function ShapeBuilder(contents, defaultValue) { | ||
if (defaultValue === void 0) { defaultValue = {}; } | ||
var _this = _super.call(this, 'shape', defaultValue) || this; | ||
if (process.env.NODE_ENV !== 'production') { | ||
_this.invariant(isObject_1.default(contents) && | ||
Object.keys(contents).length > 0 && | ||
Object.keys(contents).every(function (key) { return contents[key] instanceof Builder_1.default; }), 'A non-empty object of properties to blueprints are required for a shape.'); | ||
_this.addCheck(_this.checkContents, contents); | ||
} | ||
return _this; | ||
} | ||
return _this; | ||
} | ||
(0, _createClass3.default)(ShapeBuilder, [{ | ||
key: 'checkContents', | ||
value: function checkContents(path, object, contents) { | ||
var _this2 = this; | ||
if ("production" !== process.env.NODE_ENV) { | ||
(0, _keys2.default)(contents).forEach(function (key) { | ||
var builder = contents[key]; | ||
if (builder.isRequired || typeof object[key] !== 'undefined') { | ||
builder.runChecks(path + '.' + key, object[key], object, _this2.currentConfig); | ||
} | ||
}); | ||
} | ||
} | ||
}]); | ||
return ShapeBuilder; | ||
}(_Builder3.default); | ||
ShapeBuilder.prototype.checkContents = function (path, object, contents) { | ||
var _this = this; | ||
if (process.env.NODE_ENV !== 'production') { | ||
Object.keys(contents).forEach(function (key) { | ||
var builder = contents[key]; | ||
if (builder.isRequired || typeof object[key] !== 'undefined') { | ||
builder.runChecks(path + "." + key, object[key], object, _this.currentConfig); | ||
} | ||
}); | ||
} | ||
}; | ||
return ShapeBuilder; | ||
}(Builder_1.default)); | ||
exports.default = ShapeBuilder; | ||
function shape(contents) { | ||
var defaultValue = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
return new ShapeBuilder(contents, defaultValue); | ||
} | ||
function shape(contents, defaultValue) { | ||
if (defaultValue === void 0) { defaultValue = {}; } | ||
return new ShapeBuilder(contents, defaultValue); | ||
} | ||
exports.shape = shape; |
@@ -1,139 +0,86 @@ | ||
'use strict'; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
var _getPrototypeOf = require('babel-runtime/core-js/object/get-prototype-of'); | ||
var _getPrototypeOf2 = _interopRequireDefault(_getPrototypeOf); | ||
var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck'); | ||
var _classCallCheck3 = _interopRequireDefault(_classCallCheck2); | ||
var _createClass2 = require('babel-runtime/helpers/createClass'); | ||
var _createClass3 = _interopRequireDefault(_createClass2); | ||
var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn'); | ||
var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2); | ||
var _inherits2 = require('babel-runtime/helpers/inherits'); | ||
var _inherits3 = _interopRequireDefault(_inherits2); | ||
exports.string = string; | ||
var _Builder2 = require('./Builder'); | ||
var _Builder3 = _interopRequireDefault(_Builder2); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
"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 __()); | ||
}; | ||
})(); | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
} | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var Builder_1 = __importDefault(require("./Builder")); | ||
function isString(value) { | ||
return typeof value === 'string' && value !== ''; | ||
} /** | ||
* @copyright 2017, Miles Johnson | ||
* @license https://opensource.org/licenses/MIT | ||
* | ||
*/ | ||
var StringBuilder = function (_Builder) { | ||
(0, _inherits3.default)(StringBuilder, _Builder); | ||
function StringBuilder() { | ||
var defaultValue = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : ''; | ||
(0, _classCallCheck3.default)(this, StringBuilder); | ||
var _this = (0, _possibleConstructorReturn3.default)(this, (StringBuilder.__proto__ || (0, _getPrototypeOf2.default)(StringBuilder)).call(this, 'string', defaultValue)); | ||
_this.allowEmpty = false; | ||
if ("production" !== process.env.NODE_ENV) { | ||
_this.addCheck(_this.checkNotEmpty); | ||
return typeof value === 'string' && value !== ''; | ||
} | ||
var StringBuilder = (function (_super) { | ||
__extends(StringBuilder, _super); | ||
function StringBuilder(defaultValue) { | ||
if (defaultValue === void 0) { defaultValue = ''; } | ||
var _this = _super.call(this, 'string', defaultValue) || this; | ||
_this.allowEmpty = false; | ||
if (process.env.NODE_ENV !== 'production') { | ||
_this.addCheck(_this.checkNotEmpty); | ||
} | ||
return _this; | ||
} | ||
return _this; | ||
} | ||
(0, _createClass3.default)(StringBuilder, [{ | ||
key: 'contains', | ||
value: function contains(token) { | ||
var index = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0; | ||
if ("production" !== process.env.NODE_ENV) { | ||
this.invariant(isString(token), 'Contains requires a non-empty string.'); | ||
} | ||
return this.addCheck(this.checkContains, token, index); | ||
} | ||
}, { | ||
key: 'checkContains', | ||
value: function checkContains(path, value, token) { | ||
var index = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 0; | ||
if ("production" !== process.env.NODE_ENV) { | ||
this.invariant(value.indexOf(token, index) >= 0, 'String does not include "' + token + '".', path); | ||
} | ||
} | ||
}, { | ||
key: 'match', | ||
value: function match(pattern) { | ||
if ("production" !== process.env.NODE_ENV) { | ||
this.invariant(pattern instanceof RegExp, 'Match requires a regular expression to match against.'); | ||
} | ||
return this.addCheck(this.checkMatch, pattern); | ||
} | ||
}, { | ||
key: 'checkMatch', | ||
value: function checkMatch(path, value, pattern) { | ||
if ("production" !== process.env.NODE_ENV) { | ||
this.invariant(value.match(pattern), 'String does not match pattern "' + pattern.source + '".', path); | ||
} | ||
} | ||
}, { | ||
key: 'empty', | ||
value: function empty() { | ||
if ("production" !== process.env.NODE_ENV) { | ||
this.allowEmpty = true; | ||
} | ||
return this; | ||
} | ||
}, { | ||
key: 'checkNotEmpty', | ||
value: function checkNotEmpty(path, value) { | ||
if ("production" !== process.env.NODE_ENV) { | ||
if (!this.allowEmpty) { | ||
this.invariant(isString(value), 'String cannot be empty.', path); | ||
StringBuilder.prototype.contains = function (token, index) { | ||
if (index === void 0) { index = 0; } | ||
if (process.env.NODE_ENV !== 'production') { | ||
this.invariant(isString(token), 'Contains requires a non-empty string.'); | ||
} | ||
} | ||
} | ||
}, { | ||
key: 'oneOf', | ||
value: function oneOf(list) { | ||
if ("production" !== process.env.NODE_ENV) { | ||
this.invariant(Array.isArray(list) && list.length > 0 && list.every(isString), 'One of requires a non-empty array of strings.'); | ||
} | ||
return this.addCheck(this.checkOneOf, list); | ||
} | ||
}, { | ||
key: 'checkOneOf', | ||
value: function checkOneOf(path, value, list) { | ||
if ("production" !== process.env.NODE_ENV) { | ||
this.invariant(list.indexOf(value) >= 0, 'String must be one of: ' + list.join(', '), path); | ||
} | ||
} | ||
}]); | ||
return StringBuilder; | ||
}(_Builder3.default); | ||
return this.addCheck(this.checkContains, token, index); | ||
}; | ||
StringBuilder.prototype.checkContains = function (path, value, token, index) { | ||
if (index === void 0) { index = 0; } | ||
if (process.env.NODE_ENV !== 'production') { | ||
this.invariant(value.indexOf(token, index) >= 0, "String does not include \"" + token + "\".", path); | ||
} | ||
}; | ||
StringBuilder.prototype.match = function (pattern) { | ||
if (process.env.NODE_ENV !== 'production') { | ||
this.invariant(pattern instanceof RegExp, 'Match requires a regular expression to match against.'); | ||
} | ||
return this.addCheck(this.checkMatch, pattern); | ||
}; | ||
StringBuilder.prototype.checkMatch = function (path, value, pattern) { | ||
if (process.env.NODE_ENV !== 'production') { | ||
this.invariant(value.match(pattern), "String does not match pattern \"" + pattern.source + "\".", path); | ||
} | ||
}; | ||
StringBuilder.prototype.empty = function () { | ||
if (process.env.NODE_ENV !== 'production') { | ||
this.allowEmpty = true; | ||
} | ||
return this; | ||
}; | ||
StringBuilder.prototype.checkNotEmpty = function (path, value) { | ||
if (process.env.NODE_ENV !== 'production') { | ||
if (!this.allowEmpty) { | ||
this.invariant(isString(value), 'String cannot be empty.', path); | ||
} | ||
} | ||
}; | ||
StringBuilder.prototype.oneOf = function (list) { | ||
if (process.env.NODE_ENV !== 'production') { | ||
this.invariant(Array.isArray(list) && list.length > 0 && list.every(function (item) { return isString(item); }), 'One of requires a non-empty array of strings.'); | ||
} | ||
return this.addCheck(this.checkOneOf, list); | ||
}; | ||
StringBuilder.prototype.checkOneOf = function (path, value, list) { | ||
if (process.env.NODE_ENV !== 'production') { | ||
this.invariant(list.indexOf(value) >= 0, "String must be one of: " + list.join(', '), path); | ||
} | ||
}; | ||
return StringBuilder; | ||
}(Builder_1.default)); | ||
exports.default = StringBuilder; | ||
function string() { | ||
var defaultValue = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : ''; | ||
return new StringBuilder(defaultValue); | ||
} | ||
function string(defaultValue) { | ||
if (defaultValue === void 0) { defaultValue = ''; } | ||
return new StringBuilder(defaultValue); | ||
} | ||
exports.string = string; |
@@ -1,31 +0,16 @@ | ||
'use strict'; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
var _typeof2 = require('babel-runtime/helpers/typeof'); | ||
var _typeof3 = _interopRequireDefault(_typeof2); | ||
"use strict"; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
} | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var isObject_1 = __importDefault(require("./isObject")); | ||
function typeOf(value) { | ||
if (Array.isArray(value)) { | ||
return 'array'; | ||
} | ||
else if (isObject_1.default(value)) { | ||
return value.constructor === Object ? 'object' : 'instance'; | ||
} | ||
return typeof value; | ||
} | ||
exports.default = typeOf; | ||
var _isObject = require('./isObject'); | ||
var _isObject2 = _interopRequireDefault(_isObject); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
function typeOf(value) { | ||
if (Array.isArray(value)) { | ||
return 'array'; | ||
} else if ((0, _isObject2.default)(value)) { | ||
return value.constructor === Object ? 'object' : 'instance'; | ||
} | ||
return typeof value === 'undefined' ? 'undefined' : (0, _typeof3.default)(value); | ||
} /** | ||
* @copyright 2017, Miles Johnson | ||
* @license https://opensource.org/licenses/MIT | ||
* | ||
*/ |
@@ -1,1 +0,2 @@ | ||
'use strict'; | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); |
@@ -1,118 +0,76 @@ | ||
'use strict'; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
var _getPrototypeOf = require('babel-runtime/core-js/object/get-prototype-of'); | ||
var _getPrototypeOf2 = _interopRequireDefault(_getPrototypeOf); | ||
var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck'); | ||
var _classCallCheck3 = _interopRequireDefault(_classCallCheck2); | ||
var _createClass2 = require('babel-runtime/helpers/createClass'); | ||
var _createClass3 = _interopRequireDefault(_createClass2); | ||
var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn'); | ||
var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2); | ||
var _inherits2 = require('babel-runtime/helpers/inherits'); | ||
var _inherits3 = _interopRequireDefault(_inherits2); | ||
exports.union = union; | ||
var _Builder2 = require('./Builder'); | ||
var _Builder3 = _interopRequireDefault(_Builder2); | ||
var _typeOf = require('./typeOf'); | ||
var _typeOf2 = _interopRequireDefault(_typeOf); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
/** | ||
* @copyright 2017, Miles Johnson | ||
* @license https://opensource.org/licenses/MIT | ||
* | ||
*/ | ||
var UnionBuilder = function (_Builder) { | ||
(0, _inherits3.default)(UnionBuilder, _Builder); | ||
function UnionBuilder(builders) { | ||
var defaultValue = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null; | ||
(0, _classCallCheck3.default)(this, UnionBuilder); | ||
var _this = (0, _possibleConstructorReturn3.default)(this, (UnionBuilder.__proto__ || (0, _getPrototypeOf2.default)(UnionBuilder)).call(this, 'union', defaultValue)); | ||
if ("production" !== process.env.NODE_ENV) { | ||
_this.invariant(Array.isArray(builders) && builders.length > 0 && builders.every(function (builder) { | ||
return builder instanceof _Builder3.default; | ||
}), 'A non-empty array of blueprints are required for a union.'); | ||
_this.builders = builders; | ||
_this.addCheck(_this.checkUnions, builders); | ||
"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 __()); | ||
}; | ||
})(); | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
} | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var Builder_1 = __importDefault(require("./Builder")); | ||
var typeOf_1 = __importDefault(require("./typeOf")); | ||
var UnionBuilder = (function (_super) { | ||
__extends(UnionBuilder, _super); | ||
function UnionBuilder(builders, defaultValue) { | ||
if (defaultValue === void 0) { defaultValue = null; } | ||
var _this = _super.call(this, 'union', defaultValue) || this; | ||
_this.builders = []; | ||
if (process.env.NODE_ENV !== 'production') { | ||
_this.invariant(Array.isArray(builders) && | ||
builders.length > 0 && | ||
builders.every(function (builder) { return builder instanceof Builder_1.default; }), 'A non-empty array of blueprints are required for a union.'); | ||
_this.builders = builders; | ||
_this.addCheck(_this.checkUnions, builders); | ||
} | ||
return _this; | ||
} | ||
return _this; | ||
} | ||
(0, _createClass3.default)(UnionBuilder, [{ | ||
key: 'checkUnions', | ||
value: function checkUnions(path, value, builders) { | ||
var _this2 = this; | ||
if ("production" !== process.env.NODE_ENV) { | ||
var usage = {}; | ||
var keys = []; | ||
var type = (0, _typeOf2.default)(value); | ||
builders.forEach(function (builder) { | ||
if (usage[builder.type]) { | ||
_this2.invariant(false, 'Multiple instances of "' + builder.type + '" are not supported.', path); | ||
} else if (builder.type === 'union') { | ||
_this2.invariant(false, 'Nested unions are not supported.', path); | ||
} else { | ||
usage[builder.type] = true; | ||
keys.push(builder.typeAlias()); | ||
} | ||
}); | ||
if (usage.shape && usage.object) { | ||
this.invariant(false, 'Objects and shapes within the same union are not supported.', path); | ||
UnionBuilder.prototype.checkUnions = function (path, value, builders) { | ||
var _this = this; | ||
if (process.env.NODE_ENV !== 'production') { | ||
var usage_1 = {}; | ||
var keys_1 = []; | ||
var type_1 = typeOf_1.default(value); | ||
builders.forEach(function (builder) { | ||
if (usage_1[builder.type]) { | ||
_this.invariant(false, "Multiple instances of \"" + builder.type + "\" are not supported.", path); | ||
} | ||
else if (builder.type === 'union') { | ||
_this.invariant(false, 'Nested unions are not supported.', path); | ||
} | ||
else { | ||
usage_1[builder.type] = true; | ||
keys_1.push(builder.typeAlias()); | ||
} | ||
}); | ||
if (usage_1.shape && usage_1.object) { | ||
this.invariant(false, 'Objects and shapes within the same union are not supported.', path); | ||
} | ||
var checked_1 = false; | ||
builders.forEach(function (builder) { | ||
if (type_1 === builder.type || | ||
(type_1 === 'object' && builder.type === 'shape') || | ||
builder.type === 'custom') { | ||
checked_1 = true; | ||
builder.runChecks(path, value, _this.currentOptions, _this.currentConfig); | ||
} | ||
}); | ||
this.invariant(checked_1, "Type must be one of: " + keys_1.join(', '), path); | ||
} | ||
var checked = false; | ||
builders.forEach(function (builder) { | ||
if (type === builder.type || type === 'object' && builder.type === 'shape' || builder.type === 'custom') { | ||
checked = true; | ||
builder.runChecks(path, value, _this2.currentOptions, _this2.currentConfig); | ||
} | ||
}); | ||
this.invariant(checked, 'Type must be one of: ' + keys.join(', '), path); | ||
} | ||
} | ||
}, { | ||
key: 'typeAlias', | ||
value: function typeAlias() { | ||
return this.builders.map(function (builder) { | ||
return builder.typeAlias(); | ||
}).join(' | '); | ||
} | ||
}]); | ||
return UnionBuilder; | ||
}(_Builder3.default); | ||
}; | ||
UnionBuilder.prototype.typeAlias = function () { | ||
return this.builders.map(function (builder) { return builder.typeAlias(); }).join(' | '); | ||
}; | ||
return UnionBuilder; | ||
}(Builder_1.default)); | ||
exports.default = UnionBuilder; | ||
function union(builders) { | ||
var defaultValue = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null; | ||
return new UnionBuilder(builders, defaultValue); | ||
} | ||
function union(builders, defaultValue) { | ||
if (defaultValue === void 0) { defaultValue = null; } | ||
return new UnionBuilder(builders, defaultValue); | ||
} | ||
exports.union = union; |
{ | ||
"name": "optimal", | ||
"version": "0.14.0", | ||
"version": "0.15.0", | ||
"description": "Options object builder and validator.", | ||
"main": "./lib/index.js", | ||
"browser": "./lib/bundle.js", | ||
"module": "./esm/index.js", | ||
"scripts": { | ||
"babel": "build-lib ./src -d ./lib", | ||
"coverage": "run-coverage", | ||
"eslint": "run-linter ./bin ./src ./tests", | ||
"flow": "type-check", | ||
"jest": "run-tests", | ||
"posttest": "yarn run flow", | ||
"pretest": "yarn run eslint", | ||
"preversion": "yarn test && yarn run babel && yarn run rollup", | ||
"rollup": "NODE_ENV=production bundle-lib", | ||
"test": "yarn run jest" | ||
"build": "beemo typescript", | ||
"build:dts": "beemo run-script generate-dts --name optimal/lib", | ||
"coverage": "yarn run jest --coverage", | ||
"eslint": "beemo eslint", | ||
"type": "beemo typescript --noEmit", | ||
"jest": "beemo jest", | ||
"prettier": "beemo prettier", | ||
"posttest": "yarn run eslint --silent", | ||
"pretest": "yarn run type --silent", | ||
"preversion": "yarn test", | ||
"test": "yarn run jest --silent", | ||
"postversion": "yarn run build && yarn run build:dts" | ||
}, | ||
@@ -43,16 +45,17 @@ "repository": { | ||
"dependencies": { | ||
"babel-runtime": "^6.25.0" | ||
"babel-runtime": "^6.26.0" | ||
}, | ||
"devDependencies": { | ||
"@milesj/build-tool-config": "^0.17.4" | ||
"@milesj/build-tool-config": "^0.75.1" | ||
}, | ||
"babel": { | ||
"extends": "./node_modules/@milesj/build-tool-config/babel.json5" | ||
}, | ||
"eslintConfig": { | ||
"extends": "./node_modules/@milesj/build-tool-config/eslint.json5" | ||
}, | ||
"jest": { | ||
"preset": "@milesj/build-tool-config" | ||
"beemo": { | ||
"module": "@milesj/build-tool-config", | ||
"drivers": [ | ||
"babel", | ||
"eslint", | ||
"jest", | ||
"prettier", | ||
"typescript" | ||
] | ||
} | ||
} |
@@ -1,50 +0,33 @@ | ||
# Optimal v0.14.0 | ||
# Optimal | ||
[![Build Status](https://travis-ci.org/milesj/optimal.svg?branch=master)](https://travis-ci.org/milesj/optimal) | ||
Options object builder and validator. | ||
Optimal, a system for building and validating options and configuration objects. | ||
## Usage | ||
* Recursively builds and validates nested structures. | ||
* Supports common data types. | ||
* Autofills missing fields with default values. | ||
* Allows or restricts unknown fields. | ||
* Mark fields as nullable or required. | ||
* Utilize complex operators like AND, OR, and XOR. | ||
Pass a plain object and a blueprint to `Options`. The blueprint defines every property, | ||
its type, and its value within the options object. | ||
## Documentation | ||
The plain object is then validated, built, and returned. | ||
* [Options Class](#options-class) | ||
* [Blueprint](#predicate-blueprint) | ||
* [Customization](#customization) | ||
* [Predicates](#predicates) | ||
* [Array](#array) | ||
* [Bool](#bool) | ||
* [Custom](#custom) | ||
* [Date](#date) | ||
* [Function](#func) | ||
* [Instance](#instance) | ||
* [Number](#number) | ||
* [Object](#object) | ||
* [Regex](#regex) | ||
* [Shape](#shape) | ||
* [String](#string) | ||
* [Union](#union) | ||
```js | ||
import Options, { bool, string, number, func } from 'optimal'; | ||
const options = new Options({ | ||
bool: false, | ||
number: 10, | ||
object: { | ||
foo: 'A', | ||
}, | ||
}, { | ||
bool: bool(true), | ||
string: string('foo'), | ||
number: number(5).between(0, 10), | ||
func: func(), | ||
object: { | ||
foo: string('a').oneOf(['a', 'b', 'c']), | ||
bar: string('b'), | ||
}, | ||
})); | ||
/* | ||
{ | ||
bool: false, | ||
string: 'foo', | ||
number: 10, | ||
func: null, | ||
object: { | ||
foo: 'A', | ||
bar: 'b', | ||
}, | ||
} | ||
*/ | ||
const { | ||
string, // foo | ||
number, // 10 | ||
} = options; | ||
``` | ||
TODO |
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
329863
17
981
34
1
Updatedbabel-runtime@^6.26.0