Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

optimal

Package Overview
Dependencies
Maintainers
1
Versions
68
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

optimal - npm Package Compare versions

Comparing version 1.1.0 to 1.1.1

6

CHANGELOG.md
# Changelog
# 1.1.1 - 09/02/18
#### 🐞 Fixed
- Fixed an issue with built declaration files.
# 1.1.0 - 09/02/18

@@ -4,0 +10,0 @@

634

lib/Builder.js
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.bool = bool;
exports.custom = custom;
exports.func = func;
exports.default = void 0;
var _typeof2 = _interopRequireDefault(require("@babel/runtime/helpers/typeof"));
var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _isObject = _interopRequireDefault(require("./isObject"));
/**

@@ -6,318 +29,321 @@ * @copyright 2017, Miles Johnson

*/
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 = /** @class */ (function () {
function Builder(type, defaultValue) {
this.checks = [];
this.currentStruct = {};
this.deprecatedMessage = '';
this.errorMessage = '';
this.isNullable = false;
this.isRequired = false;
this.options = {};
if (__DEV__) {
this.invariant(typeof defaultValue !== 'undefined', "A default value for type \"" + type + "\" is required.");
this.addCheck(this.checkType);
var Builder = function () {
function Builder(type, defaultValue) {
(0, _classCallCheck2.default)(this, Builder);
(0, _defineProperty2.default)(this, "checks", []);
(0, _defineProperty2.default)(this, "currentStruct", {});
(0, _defineProperty2.default)(this, "defaultValue", void 0);
(0, _defineProperty2.default)(this, "deprecatedMessage", '');
(0, _defineProperty2.default)(this, "errorMessage", '');
(0, _defineProperty2.default)(this, "isNullable", false);
(0, _defineProperty2.default)(this, "isRequired", false);
(0, _defineProperty2.default)(this, "options", {});
(0, _defineProperty2.default)(this, "type", void 0);
if ("production" !== process.env.NODE_ENV) {
this.invariant(typeof defaultValue !== 'undefined', "A default value for type \"".concat(type, "\" is required."));
this.addCheck(this.checkType);
}
this.defaultValue = defaultValue;
this.type = type;
}
(0, _createClass2.default)(Builder, [{
key: "addCheck",
value: function addCheck(checker) {
for (var _len = arguments.length, args = new 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,
callback: checker
});
}
return this;
}
}, {
key: "and",
value: function and() {
for (var _len2 = arguments.length, keys = new 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 field 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, _toConsumableArray2.default)(otherKeys));
var struct = this.currentStruct;
var undefs = keys.filter(function (key) {
return typeof struct[key] === 'undefined' || struct[key] === null;
});
if (undefs.length === keys.length) {
return;
}
this.defaultValue = defaultValue;
this.type = type;
this.invariant(undefs.length === 0, "All of these fields must be defined: ".concat(keys.join(', ')));
}
}
/**
* Add a checking function with optional arguments.
*/
Builder.prototype.addCheck = function (checker) {
var args = [];
for (var _i = 1; _i < arguments.length; _i++) {
args[_i - 1] = arguments[_i];
}, {
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, _isObject.default)(value), 'Must be a plain object.', path);
break;
default:
this.invariant((0, _typeof2.default)(value) === this.type, "Must be a ".concat(this.type, "."), path);
break;
}
if (__DEV__) {
this.checks.push({
args: args,
callback: checker,
});
}
}
}, {
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.currentStruct);
} catch (error) {
this.invariant(false, error.message, path);
}
return this;
};
/**
* Map a list of names that must be defined alongside this field.
*/
Builder.prototype.and = function () {
var keys = [];
for (var _i = 0; _i < arguments.length; _i++) {
keys[_i] = arguments[_i];
}
}
}, {
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;
}
if (__DEV__) {
this.invariant(keys.length > 0, 'AND requires a list of field names.');
var name = this.options.name;
var prefix = '';
if (path) {
if (name) {
prefix += "Invalid ".concat(name, " field \"").concat(path, "\". ");
} else {
prefix += "Invalid field \"".concat(path, "\". ");
}
} else if (name) {
prefix += "".concat(name, ": ");
}
return this.addCheck(this.checkAnd, keys);
};
/**
* Validate that all fields have been defined.
*/
Builder.prototype.checkAnd = function (path, value, otherKeys) {
if (__DEV__) {
var keys = [this.key(path)].concat(otherKeys);
var struct_1 = this.currentStruct;
var undefs = keys.filter(function (key) { return typeof struct_1[key] === 'undefined' || struct_1[key] === null; });
// Only error once one of the struct is defined
if (undefs.length === keys.length) {
return;
}
this.invariant(undefs.length === 0, "All of these fields must be defined: " + keys.join(', '));
throw new Error("".concat(prefix).concat(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, _typeof2.default)(this.defaultValue) === this.type, "Only requires a default ".concat(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 \"".concat(String(this.defaultValue), "\"."), path);
}
}
}, {
key: "or",
value: function or() {
for (var _len3 = arguments.length, keys = new 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 field names.');
}
return this.addCheck(this.checkOr, keys);
}
}, {
key: "checkOr",
value: function checkOr(path, value, otherKeys) {
if ("production" !== process.env.NODE_ENV) {
var keys = [this.key(path)].concat((0, _toConsumableArray2.default)(otherKeys));
var struct = this.currentStruct;
var defs = keys.filter(function (key) {
return typeof struct[key] !== 'undefined' && struct[key] !== null;
});
this.invariant(defs.length > 0, "At least one of these fields must be defined: ".concat(keys.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, struct) {
var _this = this;
var options = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
this.currentStruct = struct;
this.options = 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);
}
};
/**
* Validate the type of value.
*/
Builder.prototype.checkType = function (path, value) {
if (__DEV__) {
switch (this.type) {
case 'array':
this.invariant(Array.isArray(value), 'Must be an array.', path);
break;
case 'custom':
case 'instance':
case 'union':
// Handle in the sub-class
break;
case 'object':
case 'shape':
this.invariant(isObject_1.default(value), 'Must be a plain object.', path);
break;
default:
// eslint-disable-next-line valid-typeof
this.invariant(typeof value === this.type, "Must be a " + this.type + ".", path);
break;
}
} else if (this.deprecatedMessage) {
if ("production" !== process.env.NODE_ENV) {
console.info("Field \"".concat(path, "\" is deprecated. ").concat(this.deprecatedMessage));
}
};
/**
* Set a callback to run custom logic.
*/
Builder.prototype.custom = function (callback) {
if (__DEV__) {
this.invariant(typeof callback === 'function', 'Custom blueprints require a validation function.');
}
if (value === null) {
if (this.isNullable) {
return value;
}
return this.addCheck(this.checkCustom, callback);
};
/**
* Validate the value using a custom callback.
*/
Builder.prototype.checkCustom = function (path, value, callback) {
if (__DEV__) {
try {
callback(value, this.currentStruct);
}
catch (error) {
this.invariant(false, error.message, path);
}
if ("production" !== process.env.NODE_ENV) {
this.invariant(false, 'Null is not allowed.', path);
}
};
/**
* Set a message to log when this field is present.
*/
Builder.prototype.deprecate = function (message) {
if (__DEV__) {
this.invariant(typeof message === 'string' && !!message, 'A non-empty string is required for deprecated messages.');
this.deprecatedMessage = message;
}
return this;
};
/**
* Throw an error if the condition is falsy.
*/
Builder.prototype.invariant = function (condition, message, path) {
if (path === void 0) { path = ''; }
if (__DEV__) {
if (condition) {
return;
}
var name_1 = this.options.name;
var prefix = '';
if (path) {
if (name_1) {
prefix += "Invalid " + name_1 + " field \"" + path + "\". ";
}
else {
prefix += "Invalid field \"" + path + "\". ";
}
}
else if (name_1) {
prefix += name_1 + ": ";
}
throw new Error("" + prefix + (this.errorMessage || message));
}
};
/**
* Return the current key from a path.
*/
Builder.prototype.key = function (path) {
var index = path.lastIndexOf('.');
return index > 0 ? path.slice(index + 1) : path;
};
/**
* Set a custom error message for all checks.
*/
Builder.prototype.message = function (message) {
if (__DEV__) {
this.invariant(typeof message === 'string' && !!message, 'A non-empty string is required for custom messages.');
this.errorMessage = message;
}
return this;
};
/**
* Allow null values.
*/
Builder.prototype.nullable = function (state) {
if (state === void 0) { state = true; }
if (__DEV__) {
this.isNullable = state;
}
return this;
};
/**
* Mark a field as only the default value can be used.
*/
Builder.prototype.only = function () {
if (__DEV__) {
this.invariant(
// eslint-disable-next-line valid-typeof
typeof this.defaultValue === this.type, "Only requires a default " + this.type + " value.");
}
return this.addCheck(this.checkOnly);
};
/**
* Validate the value matches only the default value.
*/
Builder.prototype.checkOnly = function (path, value) {
if (__DEV__) {
this.invariant(value === this.defaultValue, "Value may only be \"" + String(this.defaultValue) + "\".", path);
}
};
/**
* Map a list of field names that must have at least 1 defined.
*/
Builder.prototype.or = function () {
var keys = [];
for (var _i = 0; _i < arguments.length; _i++) {
keys[_i] = arguments[_i];
}
if (__DEV__) {
this.invariant(keys.length > 0, 'OR requires a list of field names.');
}
return this.addCheck(this.checkOr, keys);
};
/**
* Validate that at least 1 field is defined.
*/
Builder.prototype.checkOr = function (path, value, otherKeys) {
if (__DEV__) {
var keys = [this.key(path)].concat(otherKeys);
var struct_2 = this.currentStruct;
var defs = keys.filter(function (key) { return typeof struct_2[key] !== 'undefined' && struct_2[key] !== null; });
this.invariant(defs.length > 0, "At least one of these fields must be defined: " + keys.join(', '));
}
};
/**
* Disallow undefined values.
*/
Builder.prototype.required = function (state) {
if (state === void 0) { state = true; }
if (__DEV__) {
this.isRequired = state;
}
return this;
};
/**
* Run all validation checks that have been enqueued.
*/
Builder.prototype.runChecks = function (path, initialValue, struct, options) {
var _this = this;
if (options === void 0) { options = {}; }
this.currentStruct = struct;
this.options = options;
var value = initialValue;
// Handle undefined
if (typeof value === 'undefined') {
if (!this.isRequired) {
value = this.defaultValue;
}
else if (__DEV__) {
this.invariant(false, 'Field is required and must be defined.', path);
}
}
else if (this.deprecatedMessage) {
if (__DEV__) {
// eslint-disable-next-line no-console
console.info("Field \"" + path + "\" is deprecated. " + this.deprecatedMessage);
}
}
// Handle null
if (value === null) {
if (this.isNullable) {
return value;
}
if (__DEV__) {
this.invariant(false, 'Null is not allowed.', path);
}
}
// Run all checks against the value
if (__DEV__) {
this.checks.forEach(function (checker) {
var _a;
(_a = checker.callback).call.apply(_a, [_this, path, value].concat(checker.args));
});
}
return value;
};
/**
* Return a human readable type name.
*/
Builder.prototype.typeAlias = function () {
return this.type;
};
/**
* Map a list of field names that must not be defined alongside this field.
*/
Builder.prototype.xor = function () {
var keys = [];
for (var _i = 0; _i < arguments.length; _i++) {
keys[_i] = arguments[_i];
}
if (__DEV__) {
this.invariant(keys.length > 0, 'XOR requires a list of field names.');
}
return this.addCheck(this.checkXor, keys);
};
/**
* Validate that only 1 field is defined.
*/
Builder.prototype.checkXor = function (path, value, otherKeys) {
if (__DEV__) {
var keys = [this.key(path)].concat(otherKeys);
var struct_3 = this.currentStruct;
var defs = keys.filter(function (key) { return typeof struct_3[key] !== 'undefined' && struct_3[key] !== null; });
this.invariant(defs.length === 1, "Only one of these fields may be defined: " + keys.join(', '));
}
};
return Builder;
}());
}
if ("production" !== process.env.NODE_ENV) {
this.checks.forEach(function (checker) {
var _checker$callback;
(_checker$callback = checker.callback).call.apply(_checker$callback, [_this, path, value].concat((0, _toConsumableArray2.default)(checker.args)));
});
}
return value;
}
}, {
key: "typeAlias",
value: function typeAlias() {
return this.type;
}
}, {
key: "xor",
value: function xor() {
for (var _len4 = arguments.length, keys = new 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 field names.');
}
return this.addCheck(this.checkXor, keys);
}
}, {
key: "checkXor",
value: function checkXor(path, value, otherKeys) {
if ("production" !== process.env.NODE_ENV) {
var keys = [this.key(path)].concat((0, _toConsumableArray2.default)(otherKeys));
var struct = this.currentStruct;
var defs = keys.filter(function (key) {
return typeof struct[key] !== 'undefined' && struct[key] !== null;
});
this.invariant(defs.length === 1, "Only one of these fields may be defined: ".concat(keys.join(', ')));
}
}
}]);
return Builder;
}();
exports.default = Builder;
function bool(defaultValue) {
if (defaultValue === void 0) { defaultValue = false; }
return new Builder('boolean', defaultValue);
function bool() {
var defaultValue = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
return new Builder('boolean', defaultValue);
}
exports.bool = bool;
function custom(callback, defaultValue) {
if (defaultValue === void 0) { defaultValue = null; }
return new Builder('custom', defaultValue).custom(callback);
function custom(callback) {
var defaultValue = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
return new Builder('custom', defaultValue).custom(callback);
}
exports.custom = custom;
function func(defaultValue) {
if (defaultValue === void 0) { defaultValue = null; }
return new Builder('function', defaultValue).nullable();
}
exports.func = func;
function func() {
var defaultValue = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null;
return new Builder('function', defaultValue).nullable();
}
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.array = array;
exports.object = object;
exports.default = void 0;
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn"));
var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf"));
var _get2 = _interopRequireDefault(require("@babel/runtime/helpers/get"));
var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));
var _assertThisInitialized2 = _interopRequireDefault(require("@babel/runtime/helpers/assertThisInitialized"));
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _Builder2 = _interopRequireDefault(require("./Builder"));
/**

@@ -6,90 +34,85 @@ * @copyright 2017, Miles Johnson

*/
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
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 extendStatics(d, b);
var CollectionBuilder = function (_Builder) {
(0, _inherits2.default)(CollectionBuilder, _Builder);
function CollectionBuilder(type) {
var _this;
var contents = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
var defaultValue = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null;
(0, _classCallCheck2.default)(this, CollectionBuilder);
_this = (0, _possibleConstructorReturn2.default)(this, (0, _getPrototypeOf2.default)(CollectionBuilder).call(this, type, defaultValue));
(0, _defineProperty2.default)((0, _assertThisInitialized2.default)((0, _assertThisInitialized2.default)(_this)), "contents", null);
if ("production" !== process.env.NODE_ENV) {
if (contents) {
if (contents instanceof _Builder2.default) {
_this.contents = contents;
_this.addCheck(_this.checkContents, contents);
} else {
_this.invariant(false, "A blueprint is required for ".concat(type, " contents."));
}
}
}
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 = /** @class */ (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 (__DEV__) {
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;
}
(0, _createClass2.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("".concat(path, "[").concat(i, "]"), item, _this2.currentStruct, _this2.options);
});
} else if (this.type === 'object') {
Object.keys(value).forEach(function (key) {
contents.runChecks("".concat(path, ".").concat(key), value[key], _this2.currentStruct, _this2.options);
});
}
return _this;
}
}
CollectionBuilder.prototype.checkContents = function (path, value, contents) {
var _this = this;
if (__DEV__) {
if (this.type === 'array') {
value.forEach(function (item, i) {
contents.runChecks(path + "[" + i + "]", item, _this.currentStruct, _this.options);
});
}
else if (this.type === 'object') {
Object.keys(value).forEach(function (key) {
contents.runChecks(path + "." + key, value[key], _this.currentStruct, _this.options);
});
}
}, {
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(Object.keys(value).length > 0, 'Object cannot be empty.', path);
}
};
CollectionBuilder.prototype.notEmpty = function () {
return this.addCheck(this.checkNotEmpty);
};
CollectionBuilder.prototype.checkNotEmpty = function (path, value) {
if (__DEV__) {
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);
}
}
};
/**
* If contents are defined, return the type name using generics syntax.
*/
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));
}
}
}, {
key: "typeAlias",
value: function typeAlias() {
var contents = this.contents;
var alias = (0, _get2.default)((0, _getPrototypeOf2.default)(CollectionBuilder.prototype), "typeAlias", this).call(this);
return contents ? "".concat(alias, "<").concat(contents.typeAlias(), ">") : alias;
}
}]);
return CollectionBuilder;
}(_Builder2.default);
exports.default = CollectionBuilder;
function array(contents, defaultValue) {
if (contents === void 0) { contents = null; }
if (defaultValue === void 0) { defaultValue = []; }
return new CollectionBuilder('array', contents, defaultValue);
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);
}
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;
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);
}
"use strict";
var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
var _exportNames = {
Builder: true,
bool: true,
custom: true,
func: true,
CollectionBuilder: true,
array: true,
object: true,
InstanceBuilder: true,
instance: true,
date: true,
regex: true,
NumberBuilder: true,
number: true,
ShapeBuilder: true,
shape: true,
StringBuilder: true,
string: true,
UnionBuilder: true,
union: true
};
Object.defineProperty(exports, "Builder", {
enumerable: true,
get: function get() {
return _Builder.default;
}
});
Object.defineProperty(exports, "bool", {
enumerable: true,
get: function get() {
return _Builder.bool;
}
});
Object.defineProperty(exports, "custom", {
enumerable: true,
get: function get() {
return _Builder.custom;
}
});
Object.defineProperty(exports, "func", {
enumerable: true,
get: function get() {
return _Builder.func;
}
});
Object.defineProperty(exports, "CollectionBuilder", {
enumerable: true,
get: function get() {
return _CollectionBuilder.default;
}
});
Object.defineProperty(exports, "array", {
enumerable: true,
get: function get() {
return _CollectionBuilder.array;
}
});
Object.defineProperty(exports, "object", {
enumerable: true,
get: function get() {
return _CollectionBuilder.object;
}
});
Object.defineProperty(exports, "InstanceBuilder", {
enumerable: true,
get: function get() {
return _InstanceBuilder.default;
}
});
Object.defineProperty(exports, "instance", {
enumerable: true,
get: function get() {
return _InstanceBuilder.instance;
}
});
Object.defineProperty(exports, "date", {
enumerable: true,
get: function get() {
return _InstanceBuilder.date;
}
});
Object.defineProperty(exports, "regex", {
enumerable: true,
get: function get() {
return _InstanceBuilder.regex;
}
});
Object.defineProperty(exports, "NumberBuilder", {
enumerable: true,
get: function get() {
return _NumberBuilder.default;
}
});
Object.defineProperty(exports, "number", {
enumerable: true,
get: function get() {
return _NumberBuilder.number;
}
});
Object.defineProperty(exports, "ShapeBuilder", {
enumerable: true,
get: function get() {
return _ShapeBuilder.default;
}
});
Object.defineProperty(exports, "shape", {
enumerable: true,
get: function get() {
return _ShapeBuilder.shape;
}
});
Object.defineProperty(exports, "StringBuilder", {
enumerable: true,
get: function get() {
return _StringBuilder.default;
}
});
Object.defineProperty(exports, "string", {
enumerable: true,
get: function get() {
return _StringBuilder.string;
}
});
Object.defineProperty(exports, "UnionBuilder", {
enumerable: true,
get: function get() {
return _UnionBuilder.default;
}
});
Object.defineProperty(exports, "union", {
enumerable: true,
get: function get() {
return _UnionBuilder.union;
}
});
exports.default = void 0;
var _optimal = _interopRequireDefault(require("./optimal"));
var _Builder = _interopRequireWildcard(require("./Builder"));
var _CollectionBuilder = _interopRequireWildcard(require("./CollectionBuilder"));
var _InstanceBuilder = _interopRequireWildcard(require("./InstanceBuilder"));
var _NumberBuilder = _interopRequireWildcard(require("./NumberBuilder"));
var _ShapeBuilder = _interopRequireWildcard(require("./ShapeBuilder"));
var _StringBuilder = _interopRequireWildcard(require("./StringBuilder"));
var _UnionBuilder = _interopRequireWildcard(require("./UnionBuilder"));
var _types = require("./types");
Object.keys(_types).forEach(function (key) {
if (key === "default" || key === "__esModule") return;
if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
Object.defineProperty(exports, key, {
enumerable: true,
get: function get() {
return _types[key];
}
});
});
/**

@@ -6,40 +180,3 @@ * @copyright 2017, Miles Johnson

*/
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
result["default"] = mod;
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
var optimal_1 = __importDefault(require("./optimal"));
var Builder_1 = __importStar(require("./Builder"));
exports.Builder = Builder_1.default;
exports.bool = Builder_1.bool;
exports.custom = Builder_1.custom;
exports.func = Builder_1.func;
var CollectionBuilder_1 = __importStar(require("./CollectionBuilder"));
exports.CollectionBuilder = CollectionBuilder_1.default;
exports.array = CollectionBuilder_1.array;
exports.object = CollectionBuilder_1.object;
var InstanceBuilder_1 = __importStar(require("./InstanceBuilder"));
exports.InstanceBuilder = InstanceBuilder_1.default;
exports.instance = InstanceBuilder_1.instance;
exports.date = InstanceBuilder_1.date;
exports.regex = InstanceBuilder_1.regex;
var NumberBuilder_1 = __importStar(require("./NumberBuilder"));
exports.NumberBuilder = NumberBuilder_1.default;
exports.number = NumberBuilder_1.number;
var ShapeBuilder_1 = __importStar(require("./ShapeBuilder"));
exports.ShapeBuilder = ShapeBuilder_1.default;
exports.shape = ShapeBuilder_1.shape;
var StringBuilder_1 = __importStar(require("./StringBuilder"));
exports.StringBuilder = StringBuilder_1.default;
exports.string = StringBuilder_1.string;
var UnionBuilder_1 = __importStar(require("./UnionBuilder"));
exports.UnionBuilder = UnionBuilder_1.default;
exports.union = UnionBuilder_1.union;
exports.default = optimal_1.default;
var _default = _optimal.default;
exports.default = _default;
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.instance = instance;
exports.regex = regex;
exports.date = date;
exports.default = void 0;
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn"));
var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf"));
var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));
var _assertThisInitialized2 = _interopRequireDefault(require("@babel/runtime/helpers/assertThisInitialized"));
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _Builder2 = _interopRequireDefault(require("./Builder"));
var _isObject = _interopRequireDefault(require("./isObject"));
/**

@@ -6,70 +35,62 @@ * @copyright 2017, Miles Johnson

*/
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
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 extendStatics(d, b);
var InstanceBuilder = function (_Builder) {
(0, _inherits2.default)(InstanceBuilder, _Builder);
function InstanceBuilder() {
var _this;
var refClass = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null;
(0, _classCallCheck2.default)(this, InstanceBuilder);
_this = (0, _possibleConstructorReturn2.default)(this, (0, _getPrototypeOf2.default)(InstanceBuilder).call(this, 'instance', null));
(0, _defineProperty2.default)((0, _assertThisInitialized2.default)((0, _assertThisInitialized2.default)(_this)), "refClass", 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);
}
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 = /** @class */ (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;
// Nullable by default
_this.nullable();
if (__DEV__) {
if (refClass) {
_this.invariant(typeof refClass === 'function', 'A class reference is required.');
}
_this.refClass = refClass;
_this.addCheck(_this.checkInstance, refClass);
return _this;
}
(0, _createClass2.default)(InstanceBuilder, [{
key: "checkInstance",
value: function checkInstance(path, value, refClass) {
if ("production" !== process.env.NODE_ENV) {
if (refClass) {
this.invariant(typeof refClass === 'function' && value instanceof refClass, "Must be an instance of \"".concat(this.typeAlias(), "\"."), path);
} else {
this.invariant((0, _isObject.default)(value) && value.constructor !== Object, 'Must be a class instance.', path);
}
return _this;
}
}
InstanceBuilder.prototype.checkInstance = function (path, value, refClass) {
if (__DEV__) {
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);
}
}
};
/**
* If reference class is defined, return the class name if available.
*/
InstanceBuilder.prototype.typeAlias = function () {
var refClass = this.refClass;
return refClass ? refClass.name || refClass.constructor.name : 'class';
};
return InstanceBuilder;
}(Builder_1.default));
}, {
key: "typeAlias",
value: function typeAlias() {
var refClass = this.refClass;
return refClass ? refClass.name || refClass.constructor.name : 'class';
}
}]);
return InstanceBuilder;
}(_Builder2.default);
exports.default = InstanceBuilder;
function instance(refClass) {
if (refClass === void 0) { refClass = null; }
return new InstanceBuilder(refClass);
function instance() {
var refClass = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null;
return new InstanceBuilder(refClass);
}
exports.instance = instance;
function regex() {
return instance(RegExp);
return instance(RegExp);
}
exports.regex = regex;
function date() {
return instance(Date);
}
exports.date = date;
return instance(Date);
}
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = isObject;
var _typeof2 = _interopRequireDefault(require("@babel/runtime/helpers/typeof"));
/**

@@ -6,9 +16,4 @@ * @copyright 2017, Miles Johnson

*/
Object.defineProperty(exports, "__esModule", { value: true });
/**
* Return true if the value is a plain object.
*/
function isObject(value) {
return typeof value === 'object' && value !== null && !Array.isArray(value);
}
exports.default = isObject;
return (0, _typeof2.default)(value) === 'object' && value !== null && !Array.isArray(value);
}
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.number = number;
exports.default = void 0;
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn"));
var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf"));
var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));
var _Builder2 = _interopRequireDefault(require("./Builder"));
/**

@@ -6,102 +27,120 @@ * @copyright 2017, Miles Johnson

*/
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
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 extendStatics(d, b);
}
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';
return typeof value === 'number';
}
var NumberBuilder = /** @class */ (function (_super) {
__extends(NumberBuilder, _super);
function NumberBuilder(defaultValue) {
if (defaultValue === void 0) { defaultValue = 0; }
return _super.call(this, 'number', defaultValue) || this;
var NumberBuilder = function (_Builder) {
(0, _inherits2.default)(NumberBuilder, _Builder);
function NumberBuilder() {
var defaultValue = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0;
(0, _classCallCheck2.default)(this, NumberBuilder);
return (0, _possibleConstructorReturn2.default)(this, (0, _getPrototypeOf2.default)(NumberBuilder).call(this, 'number', defaultValue));
}
(0, _createClass2.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);
}
NumberBuilder.prototype.between = function (min, max, inclusive) {
if (inclusive === void 0) { inclusive = false; }
if (__DEV__) {
this.invariant(isNumber(min) && isNumber(max), 'Between requires a minimum and maximum number.');
}, {
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 ".concat(min, " and ").concat(max).concat(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 ".concat(min, "."), path);
} else {
this.invariant(isNumber(value) && value > min, "Number must be greater than ".concat(min, "."), 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 (__DEV__) {
this.invariant(isNumber(value) && (inclusive ? value >= min && value <= max : value > min && value < max), "Number must be between " + min + " and " + max + (inclusive ? ' inclusive' : '') + ".", path);
}
}
}, {
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 ".concat(max, "."), path);
} else {
this.invariant(isNumber(value) && value < max, "Number must be less than ".concat(max, "."), path);
}
};
NumberBuilder.prototype.gt = function (min, inclusive) {
if (inclusive === void 0) { inclusive = false; }
if (__DEV__) {
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 (__DEV__) {
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 (__DEV__) {
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 (__DEV__) {
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 (__DEV__) {
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 (__DEV__) {
this.invariant(list.indexOf(value) >= 0, "Number must be one of: " + list.join(', '), path);
}
};
return NumberBuilder;
}(Builder_1.default));
}
}
}, {
key: "oneOf",
value: function oneOf(list) {
if ("production" !== process.env.NODE_ENV) {
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);
}
}, {
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: ".concat(list.join(', ')), path);
}
}
}]);
return NumberBuilder;
}(_Builder2.default);
exports.default = NumberBuilder;
function number(defaultValue) {
if (defaultValue === void 0) { defaultValue = 0; }
return new NumberBuilder(defaultValue);
}
exports.number = number;
function number() {
var defaultValue = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0;
return new NumberBuilder(defaultValue);
}
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = optimal;
var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread"));
var _Builder = _interopRequireDefault(require("./Builder"));
var _isObject = _interopRequireDefault(require("./isObject"));
var _typeOf = _interopRequireDefault(require("./typeOf"));
/**

@@ -6,71 +22,49 @@ * @copyright 2017, Miles Johnson

*/
var __assign = (this && this.__assign) || function () {
__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];
}
return t;
};
return __assign.apply(this, arguments);
};
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 buildAndCheck(struct, blueprint, options, parentPath) {
if (options === void 0) { options = {}; }
if (parentPath === void 0) { parentPath = ''; }
var unknownFields = __assign({}, struct);
var builtStruct = {};
// Validate using the blueprint
Object.keys(blueprint).forEach(function (key) {
var builder = blueprint[key];
var path = parentPath ? parentPath + "." + key : key;
// Run validation checks
if (builder instanceof Builder_1.default) {
builtStruct[key] = builder.runChecks(path, struct[key], struct, options);
// Builder is a plain object, so let's recursively try again
}
else if (isObject_1.default(builder)) {
builtStruct[key] = buildAndCheck(struct[key] || {}, builder, options, path);
// Oops
}
else if (__DEV__) {
throw new Error('Unknown blueprint. Must be a builder or plain object.');
}
// Delete the prop and mark it as known
delete unknownFields[key];
});
// Handle unknown options
if (options.unknown) {
Object.assign(builtStruct, unknownFields);
function buildAndCheck(struct, blueprint) {
var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
var parentPath = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : '';
var unknownFields = (0, _objectSpread2.default)({}, struct);
var builtStruct = {};
Object.keys(blueprint).forEach(function (key) {
var builder = blueprint[key];
var path = parentPath ? "".concat(parentPath, ".").concat(key) : key;
if (builder instanceof _Builder.default) {
builtStruct[key] = builder.runChecks(path, struct[key], struct, options);
} else if ((0, _isObject.default)(builder)) {
builtStruct[key] = buildAndCheck(struct[key] || {}, builder, options, path);
} else if ("production" !== process.env.NODE_ENV) {
throw new Error('Unknown blueprint. Must be a builder or plain object.');
}
else if (__DEV__) {
var unknownKeys = Object.keys(unknownFields);
if (unknownKeys.length > 0) {
throw new Error("Unknown fields: " + unknownKeys.join(', ') + ".");
}
delete unknownFields[key];
});
if (options.unknown) {
Object.assign(builtStruct, unknownFields);
} else if ("production" !== process.env.NODE_ENV) {
var unknownKeys = Object.keys(unknownFields);
if (unknownKeys.length > 0) {
throw new Error("Unknown fields: ".concat(unknownKeys.join(', '), "."));
}
return builtStruct;
}
return builtStruct;
}
function optimal(struct, blueprint, options) {
if (options === void 0) { options = {}; }
if (__DEV__) {
if (!isObject_1.default(struct)) {
throw new TypeError("Optimal requires a plain object, found " + typeOf_1.default(struct) + ".");
}
else if (!isObject_1.default(options)) {
throw new TypeError('Optimal options must be a plain object.');
}
else if (!isObject_1.default(blueprint)) {
throw new TypeError('A blueprint is required.');
}
function optimal(struct, blueprint) {
var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
if ("production" !== process.env.NODE_ENV) {
if (!(0, _isObject.default)(struct)) {
throw new TypeError("Optimal requires a plain object, found ".concat((0, _typeOf.default)(struct), "."));
} else if (!(0, _isObject.default)(options)) {
throw new TypeError('Optimal options must be a plain object.');
} else if (!(0, _isObject.default)(blueprint)) {
throw new TypeError('A blueprint is required.');
}
return buildAndCheck(struct, blueprint, options);
}
exports.default = optimal;
}
return buildAndCheck(struct, blueprint, options);
}
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.shape = shape;
exports.default = void 0;
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn"));
var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf"));
var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));
var _Builder2 = _interopRequireDefault(require("./Builder"));
var _isObject = _interopRequireDefault(require("./isObject"));
/**

@@ -6,54 +29,47 @@ * @copyright 2017, Miles Johnson

*/
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
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 extendStatics(d, b);
var ShapeBuilder = function (_Builder) {
(0, _inherits2.default)(ShapeBuilder, _Builder);
function ShapeBuilder(contents) {
var _this;
var defaultValue = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
(0, _classCallCheck2.default)(this, ShapeBuilder);
_this = (0, _possibleConstructorReturn2.default)(this, (0, _getPrototypeOf2.default)(ShapeBuilder).call(this, 'shape', defaultValue));
if ("production" !== process.env.NODE_ENV) {
_this.invariant((0, _isObject.default)(contents) && Object.keys(contents).length > 0 && Object.keys(contents).every(function (key) {
return contents[key] instanceof _Builder2.default;
}), 'A non-empty object of properties to blueprints are required for a shape.');
_this.addCheck(_this.checkContents, contents);
}
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 = /** @class */ (function (_super) {
__extends(ShapeBuilder, _super);
function ShapeBuilder(contents, defaultValue) {
if (defaultValue === void 0) { defaultValue = {}; }
var _this = _super.call(this, 'shape', defaultValue) || this;
if (__DEV__) {
_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, _createClass2.default)(ShapeBuilder, [{
key: "checkContents",
value: function checkContents(path, object, contents) {
var _this2 = this;
if ("production" !== process.env.NODE_ENV) {
Object.keys(contents).forEach(function (key) {
var builder = contents[key];
if (builder instanceof _Builder2.default && (builder.isRequired || typeof object[key] !== 'undefined')) {
builder.runChecks("".concat(path, ".").concat(key), object[key], object, _this2.options);
}
});
}
}
ShapeBuilder.prototype.checkContents = function (path, object, contents) {
var _this = this;
if (__DEV__) {
Object.keys(contents).forEach(function (key) {
var builder = contents[key];
// Fields should be optional by default unless explicitly required
if (builder instanceof Builder_1.default &&
(builder.isRequired || typeof object[key] !== 'undefined')) {
builder.runChecks(path + "." + key, object[key], object, _this.options);
}
});
}
};
return ShapeBuilder;
}(Builder_1.default));
}]);
return ShapeBuilder;
}(_Builder2.default);
exports.default = ShapeBuilder;
function shape(contents, defaultValue) {
if (defaultValue === void 0) { defaultValue = {}; }
return new ShapeBuilder(contents, defaultValue);
}
exports.shape = shape;
function shape(contents) {
var defaultValue = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
return new ShapeBuilder(contents, defaultValue);
}
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.string = string;
exports.default = void 0;
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn"));
var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf"));
var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));
var _assertThisInitialized2 = _interopRequireDefault(require("@babel/runtime/helpers/assertThisInitialized"));
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _Builder2 = _interopRequireDefault(require("./Builder"));
/**

@@ -6,90 +31,105 @@ * @copyright 2017, Miles Johnson

*/
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
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 extendStatics(d, b);
}
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 !== '';
return typeof value === 'string' && value !== '';
}
var StringBuilder = /** @class */ (function (_super) {
__extends(StringBuilder, _super);
function StringBuilder(defaultValue) {
if (defaultValue === void 0) { defaultValue = ''; }
var _this = _super.call(this, 'string', defaultValue) || this;
_this.allowEmpty = false;
// Not empty by default
if (__DEV__) {
_this.addCheck(_this.checkNotEmpty);
var StringBuilder = function (_Builder) {
(0, _inherits2.default)(StringBuilder, _Builder);
function StringBuilder() {
var _this;
var defaultValue = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
(0, _classCallCheck2.default)(this, StringBuilder);
_this = (0, _possibleConstructorReturn2.default)(this, (0, _getPrototypeOf2.default)(StringBuilder).call(this, 'string', defaultValue));
(0, _defineProperty2.default)((0, _assertThisInitialized2.default)((0, _assertThisInitialized2.default)(_this)), "allowEmpty", false);
if ("production" !== process.env.NODE_ENV) {
_this.addCheck(_this.checkNotEmpty);
}
return _this;
}
(0, _createClass2.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 \"".concat(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 \"".concat(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);
}
return _this;
}
}
StringBuilder.prototype.contains = function (token, index) {
if (index === void 0) { index = 0; }
if (__DEV__) {
this.invariant(isString(token), 'Contains requires a non-empty string.');
}
return this.addCheck(this.checkContains, token, index);
};
StringBuilder.prototype.checkContains = function (path, value, token, index) {
if (index === void 0) { index = 0; }
if (__DEV__) {
this.invariant(value.indexOf(token, index) >= 0, "String does not include \"" + token + "\".", path);
}
};
StringBuilder.prototype.match = function (pattern) {
if (__DEV__) {
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 (__DEV__) {
this.invariant(value.match(pattern), "String does not match pattern \"" + pattern.source + "\".", path);
}
};
StringBuilder.prototype.empty = function () {
if (__DEV__) {
this.allowEmpty = true;
}
return this;
};
StringBuilder.prototype.checkNotEmpty = function (path, value) {
if (__DEV__) {
if (!this.allowEmpty) {
this.invariant(isString(value), 'String cannot be empty.', path);
}
}
};
StringBuilder.prototype.oneOf = function (list) {
if (__DEV__) {
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 (__DEV__) {
this.invariant(list.indexOf(value) >= 0, "String must be one of: " + list.join(', '), path);
}
};
return StringBuilder;
}(Builder_1.default));
}, {
key: "oneOf",
value: function oneOf(list) {
if ("production" !== process.env.NODE_ENV) {
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);
}
}, {
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: ".concat(list.join(', ')), path);
}
}
}]);
return StringBuilder;
}(_Builder2.default);
exports.default = StringBuilder;
function string(defaultValue) {
if (defaultValue === void 0) { defaultValue = ''; }
return new StringBuilder(defaultValue);
}
exports.string = string;
function string() {
var defaultValue = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
return new StringBuilder(defaultValue);
}
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = typeOf;
var _typeof2 = _interopRequireDefault(require("@babel/runtime/helpers/typeof"));
var _isObject = _interopRequireDefault(require("./isObject"));
/**

@@ -6,26 +18,23 @@ * @copyright 2017, Miles Johnson

*/
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"));
// Not supported: Shape, Custom
function typeOf(value) {
if (Array.isArray(value)) {
return value.every(function (item) { return typeof item === typeof value[0]; }) ? 'array' : 'union';
}
if (isObject_1.default(value)) {
return value.constructor === Object ? 'object' : 'instance';
}
switch (typeof value) {
case 'boolean':
case 'function':
case 'number':
case 'string':
// @ts-ignore
return typeof value;
default:
return 'unknown';
}
}
exports.default = typeOf;
if (Array.isArray(value)) {
return value.every(function (item) {
return (0, _typeof2.default)(item) === (0, _typeof2.default)(value[0]);
}) ? 'array' : 'union';
}
if ((0, _isObject.default)(value)) {
return value.constructor === Object ? 'object' : 'instance';
}
switch ((0, _typeof2.default)(value)) {
case 'boolean':
case 'function':
case 'number':
case 'string':
return (0, _typeof2.default)(value);
default:
return 'unknown';
}
}

@@ -1,6 +0,1 @@

"use strict";
/**
* @copyright 2017, Miles Johnson
* @license https://opensource.org/licenses/MIT
*/
Object.defineProperty(exports, "__esModule", { value: true });
"use strict";
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.union = union;
exports.default = void 0;
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn"));
var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf"));
var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));
var _assertThisInitialized2 = _interopRequireDefault(require("@babel/runtime/helpers/assertThisInitialized"));
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _Builder2 = _interopRequireDefault(require("./Builder"));
var _typeOf = _interopRequireDefault(require("./typeOf"));
/**

@@ -6,84 +33,76 @@ * @copyright 2017, Miles Johnson

*/
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
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 extendStatics(d, b);
var UnionBuilder = function (_Builder) {
(0, _inherits2.default)(UnionBuilder, _Builder);
function UnionBuilder(builders) {
var _this;
var defaultValue = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
(0, _classCallCheck2.default)(this, UnionBuilder);
_this = (0, _possibleConstructorReturn2.default)(this, (0, _getPrototypeOf2.default)(UnionBuilder).call(this, 'union', defaultValue));
(0, _defineProperty2.default)((0, _assertThisInitialized2.default)((0, _assertThisInitialized2.default)(_this)), "builders", []);
if ("production" !== process.env.NODE_ENV) {
_this.invariant(Array.isArray(builders) && builders.length > 0 && builders.every(function (builder) {
return builder instanceof _Builder2.default;
}), 'A non-empty array of blueprints are required for a union.');
_this.builders = builders;
_this.addCheck(_this.checkUnions, builders);
}
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 = /** @class */ (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 (__DEV__) {
_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;
}
(0, _createClass2.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, _typeOf.default)(value);
builders.forEach(function (builder) {
if (usage[builder.type]) {
_this2.invariant(false, "Multiple instances of \"".concat(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);
}
return _this;
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.currentStruct, _this2.options);
}
});
this.invariant(checked, "Type must be one of: ".concat(keys.join(', ')), path);
}
}
UnionBuilder.prototype.checkUnions = function (path, value, builders) {
var _this = this;
if (__DEV__) {
var usage_1 = {};
var keys_1 = [];
var type_1 = typeOf_1.default(value);
// Verify structure and usage
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);
}
// Run checks on value
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.currentStruct, _this.options);
}
});
this.invariant(checked_1, "Type must be one of: " + keys_1.join(', '), path);
}
};
/**
* Return the type name using generics syntax.
*/
UnionBuilder.prototype.typeAlias = function () {
return this.builders.map(function (builder) { return builder.typeAlias(); }).join(' | ');
};
return UnionBuilder;
}(Builder_1.default));
}, {
key: "typeAlias",
value: function typeAlias() {
return this.builders.map(function (builder) {
return builder.typeAlias();
}).join(' | ');
}
}]);
return UnionBuilder;
}(_Builder2.default);
exports.default = UnionBuilder;
function union(builders, defaultValue) {
if (defaultValue === void 0) { defaultValue = null; }
return new UnionBuilder(builders, defaultValue);
}
exports.union = union;
function union(builders) {
var defaultValue = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
return new UnionBuilder(builders, defaultValue);
}
{
"name": "optimal",
"version": "1.1.0",
"version": "1.1.1",
"description": "A system for building and validating defined object structures.",

@@ -8,3 +8,3 @@ "main": "./lib/index.js",

"scripts": {
"build": "beemo babel && beemo typescript --declarationOnly",
"build": "beemo babel && beemo typescript --emitDeclarationOnly --no-clean",
"coverage": "yarn run jest --coverage",

@@ -49,3 +49,3 @@ "docs": "gitbook build --debug",

"devDependencies": {
"@milesj/build-tools": "^0.0.8"
"@milesj/build-tools": "^0.0.9"
},

@@ -52,0 +52,0 @@ "beemo": {

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc