Comparing version 3.1.1 to 3.2.0
@@ -0,1 +1,17 @@ | ||
## 3.2.0 - 2019-04-20 | ||
#### 🚀 Updates | ||
- Added a `prefix` option to `optimal`, which customizes the initial object path prefix for errors. | ||
- Builder methods can now return a value to mutate the value being checked. | ||
#### 🐞 Fixes | ||
- Shapes within arrays, objects, or unions are now returned with the full object. | ||
#### 🛠 Internals | ||
- Updated `ShapeBuilder` to call `optimal` internally, as the APIs are very similar. | ||
- Removed `@babel/runtime-corejs3` as it wasn't saving much space. | ||
### 3.1.1 - 2019-04-13 | ||
@@ -2,0 +18,0 @@ |
@@ -1,5 +0,10 @@ | ||
import _forEachInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/for-each"; | ||
import _assertThisInitialized from "@babel/runtime-corejs3/helpers/esm/assertThisInitialized"; | ||
import _inheritsLoose from "@babel/runtime-corejs3/helpers/esm/inheritsLoose"; | ||
import _defineProperty from "@babel/runtime-corejs3/helpers/esm/defineProperty"; | ||
import "core-js/modules/es.array.concat"; | ||
import "core-js/modules/web.dom-collections.for-each"; | ||
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; } | ||
function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; subClass.__proto__ = superClass; } | ||
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } | ||
import Builder from './Builder'; | ||
@@ -45,7 +50,11 @@ | ||
var nextValue = [].concat(value); | ||
if ("production" !== process.env.NODE_ENV) { | ||
_forEachInstanceProperty(value).call(value, function (item, i) { | ||
contents.runChecks(path + "[" + i + "]", item, _this2.currentStruct, _this2.options); | ||
value.forEach(function (item, i) { | ||
nextValue[i] = contents.runChecks(path + "[" + i + "]", item, _this2.currentStruct, _this2.options); | ||
}); | ||
} | ||
return nextValue; | ||
}; | ||
@@ -52,0 +61,0 @@ |
@@ -0,10 +1,10 @@ | ||
import "core-js/modules/es.array.concat"; | ||
import "core-js/modules/es.array.filter"; | ||
import "core-js/modules/es.array.join"; | ||
import "core-js/modules/es.array.slice"; | ||
import "core-js/modules/es.function.name"; | ||
import _forEachInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/for-each"; | ||
import _sliceInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/slice"; | ||
import _lastIndexOfInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/last-index-of"; | ||
import _Array$isArray from "@babel/runtime-corejs3/core-js-stable/array/is-array"; | ||
import _filterInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/filter"; | ||
import _concatInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/concat"; | ||
import _defineProperty from "@babel/runtime-corejs3/helpers/esm/defineProperty"; | ||
import "core-js/modules/web.dom-collections.for-each"; | ||
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } | ||
import isObject from './isObject'; | ||
@@ -28,3 +28,3 @@ | ||
_defineProperty(this, "noPrefix", false); | ||
_defineProperty(this, "noErrorPrefix", false); | ||
@@ -75,9 +75,5 @@ _defineProperty(this, "options", {}); | ||
if ("production" !== process.env.NODE_ENV) { | ||
var _context; | ||
var keys = _concatInstanceProperty(_context = [this.key(path)]).call(_context, otherKeys); | ||
var keys = [this.key(path)].concat(otherKeys); | ||
var struct = this.currentStruct; | ||
var undefs = _filterInstanceProperty(keys).call(keys, function (key) { | ||
var undefs = keys.filter(function (key) { | ||
return typeof struct[key] === 'undefined' || struct[key] === null; | ||
@@ -98,3 +94,3 @@ }); | ||
case 'array': | ||
this.invariant(_Array$isArray(value), 'Must be an array.', path); | ||
this.invariant(Array.isArray(value), 'Must be an array.', path); | ||
break; | ||
@@ -176,3 +172,3 @@ | ||
if (prefix && !this.noPrefix) { | ||
if (prefix && !this.noErrorPrefix) { | ||
throw new Error(prefix + ". " + error); | ||
@@ -190,5 +186,4 @@ } else { | ||
_proto.key = function key(path) { | ||
var index = _lastIndexOfInstanceProperty(path).call(path, '.'); | ||
return index > 0 ? _sliceInstanceProperty(path).call(path, index + 1) : path; | ||
var index = path.lastIndexOf('.'); | ||
return index > 0 ? path.slice(index + 1) : path; | ||
}; | ||
@@ -249,12 +244,7 @@ | ||
if ("production" !== process.env.NODE_ENV) { | ||
var _context2; | ||
var keys = _concatInstanceProperty(_context2 = [this.key(path)]).call(_context2, otherKeys); | ||
var keys = [this.key(path)].concat(otherKeys); | ||
var struct = this.currentStruct; | ||
var defs = _filterInstanceProperty(keys).call(keys, function (key) { | ||
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: " + keys.join(', ')); | ||
@@ -310,8 +300,10 @@ } | ||
if ("production" !== process.env.NODE_ENV) { | ||
var _context3; | ||
this.checks.forEach(function (checker) { | ||
var _checker$callback; | ||
_forEachInstanceProperty(_context3 = this.checks).call(_context3, function (checker) { | ||
var _checker$callback, _context4; | ||
var result = (_checker$callback = checker.callback).call.apply(_checker$callback, [_this, path, value].concat(checker.args)); | ||
(_checker$callback = checker.callback).call.apply(_checker$callback, _concatInstanceProperty(_context4 = [_this, path, value]).call(_context4, checker.args)); | ||
if (typeof result !== 'undefined') { | ||
value = result; | ||
} | ||
}); | ||
@@ -341,12 +333,7 @@ } | ||
if ("production" !== process.env.NODE_ENV) { | ||
var _context5; | ||
var keys = _concatInstanceProperty(_context5 = [this.key(path)]).call(_context5, otherKeys); | ||
var keys = [this.key(path)].concat(otherKeys); | ||
var struct = this.currentStruct; | ||
var defs = _filterInstanceProperty(keys).call(keys, function (key) { | ||
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: " + keys.join(', ')); | ||
@@ -353,0 +340,0 @@ } |
import "core-js/modules/es.function.name"; | ||
import "core-js/modules/es.regexp.constructor"; | ||
import "core-js/modules/es.regexp.to-string"; | ||
import _assertThisInitialized from "@babel/runtime-corejs3/helpers/esm/assertThisInitialized"; | ||
import _inheritsLoose from "@babel/runtime-corejs3/helpers/esm/inheritsLoose"; | ||
import _defineProperty from "@babel/runtime-corejs3/helpers/esm/defineProperty"; | ||
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; } | ||
function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; subClass.__proto__ = superClass; } | ||
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } | ||
import Builder from './Builder'; | ||
@@ -8,0 +12,0 @@ import isObject from './isObject'; |
import "core-js/modules/es.function.name"; | ||
import _Object$getPrototypeOf from "@babel/runtime-corejs3/core-js-stable/object/get-prototype-of"; | ||
import "core-js/modules/es.object.get-prototype-of"; | ||
export default function instanceOf(object, contract) { | ||
@@ -23,3 +23,3 @@ if (!object || typeof object !== 'object') { | ||
current = _Object$getPrototypeOf(current); | ||
current = Object.getPrototypeOf(current); | ||
} | ||
@@ -26,0 +26,0 @@ |
@@ -1,4 +0,3 @@ | ||
import _Array$isArray from "@babel/runtime-corejs3/core-js-stable/array/is-array"; | ||
export default function isObject(value) { | ||
return !!value && typeof value === 'object' && !_Array$isArray(value); | ||
return !!value && typeof value === 'object' && !Array.isArray(value); | ||
} |
@@ -0,6 +1,7 @@ | ||
import "core-js/modules/es.array.includes"; | ||
import "core-js/modules/es.array.join"; | ||
import _includesInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/includes"; | ||
import _everyInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/every"; | ||
import _Array$isArray from "@babel/runtime-corejs3/core-js-stable/array/is-array"; | ||
import _inheritsLoose from "@babel/runtime-corejs3/helpers/esm/inheritsLoose"; | ||
import "core-js/modules/es.string.includes"; | ||
function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; subClass.__proto__ = superClass; } | ||
import Builder from './Builder'; | ||
@@ -105,3 +106,3 @@ | ||
if ("production" !== process.env.NODE_ENV) { | ||
this.invariant(_Array$isArray(list) && list.length > 0 && _everyInstanceProperty(list).call(list, function (item) { | ||
this.invariant(Array.isArray(list) && list.length > 0 && list.every(function (item) { | ||
return isNumber(item); | ||
@@ -117,3 +118,3 @@ }), 'One of requires a non-empty array of numbers.'); | ||
if ("production" !== process.env.NODE_ENV) { | ||
this.invariant(_includesInstanceProperty(list).call(list, value), "Number must be one of: " + list.join(', '), path); | ||
this.invariant(list.includes(value), "Number must be one of: " + list.join(', '), path); | ||
} | ||
@@ -120,0 +121,0 @@ }; |
@@ -1,6 +0,11 @@ | ||
import _Object$keys from "@babel/runtime-corejs3/core-js-stable/object/keys"; | ||
import _forEachInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/for-each"; | ||
import _assertThisInitialized from "@babel/runtime-corejs3/helpers/esm/assertThisInitialized"; | ||
import _inheritsLoose from "@babel/runtime-corejs3/helpers/esm/inheritsLoose"; | ||
import _defineProperty from "@babel/runtime-corejs3/helpers/esm/defineProperty"; | ||
import "core-js/modules/es.object.assign"; | ||
import "core-js/modules/es.object.keys"; | ||
import "core-js/modules/web.dom-collections.for-each"; | ||
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; } | ||
function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; subClass.__proto__ = superClass; } | ||
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } | ||
import Builder from './Builder'; | ||
@@ -47,9 +52,11 @@ import { builder } from './InstanceBuilder'; | ||
var nextValue = Object.assign({}, value); | ||
if ("production" !== process.env.NODE_ENV) { | ||
var _context; | ||
_forEachInstanceProperty(_context = _Object$keys(value)).call(_context, function (key) { | ||
contents.runChecks(path + "." + key, value[key], _this2.currentStruct, _this2.options); | ||
Object.keys(value).forEach(function (key) { | ||
nextValue[key] = contents.runChecks(path + "." + key, value[key], _this2.currentStruct, _this2.options); | ||
}); | ||
} | ||
return nextValue; | ||
}; | ||
@@ -63,3 +70,3 @@ | ||
if ("production" !== process.env.NODE_ENV) { | ||
this.invariant(_Object$keys(value).length > 0, 'Object cannot be empty.', path); | ||
this.invariant(Object.keys(value).length > 0, 'Object cannot be empty.', path); | ||
} | ||
@@ -66,0 +73,0 @@ }; |
import "core-js/modules/es.array.join"; | ||
import "core-js/modules/es.function.name"; | ||
import _endsWithInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/ends-with"; | ||
import _Object$keys from "@babel/runtime-corejs3/core-js-stable/object/keys"; | ||
import _forEachInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/for-each"; | ||
import _Object$assign from "@babel/runtime-corejs3/core-js-stable/object/assign"; | ||
import "core-js/modules/es.object.assign"; | ||
import "core-js/modules/es.object.keys"; | ||
import "core-js/modules/es.string.ends-with"; | ||
import "core-js/modules/web.dom-collections.for-each"; | ||
import Builder from './Builder'; | ||
@@ -12,4 +12,2 @@ import isObject from './isObject'; | ||
function buildAndCheck(blueprint, struct, options, parentPath) { | ||
var _context; | ||
if (options === void 0) { | ||
@@ -23,9 +21,5 @@ options = {}; | ||
var unknownFields = _Object$assign({}, struct); | ||
var unknownFields = Object.assign({}, struct); | ||
var builtStruct = {}; | ||
_forEachInstanceProperty(_context = _Object$keys(blueprint)).call(_context, function (baseKey) { | ||
var _context2; | ||
Object.keys(blueprint).forEach(function (baseKey) { | ||
var key = baseKey; | ||
@@ -36,3 +30,3 @@ var value = struct[key]; | ||
if (builder instanceof Builder || isObject(builder) && _endsWithInstanceProperty(_context2 = builder.constructor.name).call(_context2, 'Builder')) { | ||
if (builder instanceof Builder || isObject(builder) && builder.constructor.name.endsWith('Builder')) { | ||
builtStruct[key] = builder.runChecks(path, value, struct, options); | ||
@@ -47,8 +41,9 @@ } else if ("production" !== process.env.NODE_ENV) { | ||
if (options.unknown) { | ||
_Object$assign(builtStruct, unknownFields); | ||
Object.assign(builtStruct, unknownFields); | ||
} else if ("production" !== process.env.NODE_ENV) { | ||
var unknownKeys = _Object$keys(unknownFields); | ||
var unknownKeys = Object.keys(unknownFields); | ||
if (unknownKeys.length > 0) { | ||
throw new Error("Unknown fields: " + unknownKeys.join(', ') + "."); | ||
var message = parentPath ? "Unknown \"" + parentPath + "\" fields" : 'Unknown fields'; | ||
throw new Error(message + ": " + unknownKeys.join(', ') + "."); | ||
} | ||
@@ -75,3 +70,3 @@ } | ||
return buildAndCheck(blueprint, struct, options); | ||
return buildAndCheck(blueprint, struct, options, options.prefix); | ||
} |
@@ -1,11 +0,13 @@ | ||
import "core-js/modules/es.array.join"; | ||
import _forEachInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/for-each"; | ||
import _Object$assign from "@babel/runtime-corejs3/core-js-stable/object/assign"; | ||
import _everyInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/every"; | ||
import _Object$keys from "@babel/runtime-corejs3/core-js-stable/object/keys"; | ||
import _assertThisInitialized from "@babel/runtime-corejs3/helpers/esm/assertThisInitialized"; | ||
import _inheritsLoose from "@babel/runtime-corejs3/helpers/esm/inheritsLoose"; | ||
import _defineProperty from "@babel/runtime-corejs3/helpers/esm/defineProperty"; | ||
import "core-js/modules/es.object.assign"; | ||
import "core-js/modules/es.object.keys"; | ||
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; } | ||
function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; subClass.__proto__ = superClass; } | ||
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } | ||
import Builder from './Builder'; | ||
import isObject from './isObject'; | ||
import optimal from './optimal'; | ||
@@ -25,5 +27,3 @@ var ShapeBuilder = function (_Builder) { | ||
if ("production" !== process.env.NODE_ENV) { | ||
var _context; | ||
_this.invariant(isObject(contents) && _Object$keys(contents).length > 0 && _everyInstanceProperty(_context = _Object$keys(contents)).call(_context, function (key) { | ||
_this.invariant(isObject(contents) && Object.keys(contents).length > 0 && Object.keys(contents).every(function (key) { | ||
return contents[key] instanceof Builder; | ||
@@ -45,5 +45,2 @@ }), 'A non-empty object of properties to blueprints are required for a shape.'); | ||
_proto.runChecks = function runChecks(path, initialValue, struct, options) { | ||
var _context2, | ||
_this2 = this; | ||
if (options === void 0) { | ||
@@ -53,3 +50,2 @@ options = {}; | ||
var value = {}; | ||
var object = initialValue || this.defaultValue || {}; | ||
@@ -61,26 +57,6 @@ | ||
var unknownFields = _Object$assign({}, object); | ||
_forEachInstanceProperty(_context2 = _Object$keys(this.contents)).call(_context2, function (baseKey) { | ||
var key = baseKey; | ||
var builder = _this2.contents[key]; | ||
if (builder instanceof Builder) { | ||
value[key] = builder.runChecks(path + "." + key, object[key], object, options); | ||
} else { | ||
value[key] = object[key]; | ||
} | ||
delete unknownFields[key]; | ||
}); | ||
if (this.isExact) { | ||
var unknownKeys = _Object$keys(unknownFields); | ||
if (unknownKeys.length > 0) { | ||
throw new Error("Unknown shape fields: " + unknownKeys.join(', ') + "."); | ||
} | ||
} | ||
return value; | ||
return optimal(object, this.contents, Object.assign({}, options, { | ||
prefix: path, | ||
unknown: !this.isExact | ||
})); | ||
}; | ||
@@ -87,0 +63,0 @@ |
@@ -0,9 +1,10 @@ | ||
import "core-js/modules/es.array.includes"; | ||
import "core-js/modules/es.array.join"; | ||
import "core-js/modules/es.regexp.constructor"; | ||
import "core-js/modules/es.regexp.to-string"; | ||
import "core-js/modules/es.string.includes"; | ||
import "core-js/modules/es.string.match"; | ||
import _everyInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/every"; | ||
import _Array$isArray from "@babel/runtime-corejs3/core-js-stable/array/is-array"; | ||
import _includesInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/includes"; | ||
import _inheritsLoose from "@babel/runtime-corejs3/helpers/esm/inheritsLoose"; | ||
function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; subClass.__proto__ = superClass; } | ||
import Builder from './Builder'; | ||
@@ -46,3 +47,3 @@ | ||
this.invariant(_includesInstanceProperty(value).call(value, token, index), "String does not include \"" + token + "\".", path); | ||
this.invariant(value.includes(token, index), "String does not include \"" + token + "\".", path); | ||
} | ||
@@ -81,3 +82,3 @@ }; | ||
if ("production" !== process.env.NODE_ENV) { | ||
this.invariant(_Array$isArray(list) && list.length > 0 && _everyInstanceProperty(list).call(list, function (item) { | ||
this.invariant(Array.isArray(list) && list.length > 0 && list.every(function (item) { | ||
return isString(item); | ||
@@ -93,3 +94,3 @@ }), 'One of requires a non-empty array of strings.'); | ||
if ("production" !== process.env.NODE_ENV) { | ||
this.invariant(_includesInstanceProperty(list).call(list, value), "String must be one of: " + list.join(', '), path); | ||
this.invariant(list.includes(value), "String must be one of: " + list.join(', '), path); | ||
} | ||
@@ -96,0 +97,0 @@ }; |
@@ -1,7 +0,5 @@ | ||
import _everyInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/every"; | ||
import _Array$isArray from "@babel/runtime-corejs3/core-js-stable/array/is-array"; | ||
import isObject from './isObject'; | ||
export default function typeOf(value) { | ||
if (_Array$isArray(value)) { | ||
return _everyInstanceProperty(value).call(value, function (item) { | ||
if (Array.isArray(value)) { | ||
return value.every(function (item) { | ||
return typeof item === typeof value[0]; | ||
@@ -8,0 +6,0 @@ }) ? 'array' : 'union'; |
@@ -0,12 +1,17 @@ | ||
import "core-js/modules/es.array.iterator"; | ||
import "core-js/modules/es.array.join"; | ||
import _trimInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/trim"; | ||
import _forEachInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/for-each"; | ||
import _someInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/some"; | ||
import _Set from "@babel/runtime-corejs3/core-js-stable/set"; | ||
import _mapInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/map"; | ||
import _everyInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/every"; | ||
import _Array$isArray from "@babel/runtime-corejs3/core-js-stable/array/is-array"; | ||
import _assertThisInitialized from "@babel/runtime-corejs3/helpers/esm/assertThisInitialized"; | ||
import _inheritsLoose from "@babel/runtime-corejs3/helpers/esm/inheritsLoose"; | ||
import _defineProperty from "@babel/runtime-corejs3/helpers/esm/defineProperty"; | ||
import "core-js/modules/es.array.map"; | ||
import "core-js/modules/es.object.to-string"; | ||
import "core-js/modules/es.set"; | ||
import "core-js/modules/es.string.iterator"; | ||
import "core-js/modules/es.string.trim"; | ||
import "core-js/modules/web.dom-collections.for-each"; | ||
import "core-js/modules/web.dom-collections.iterator"; | ||
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; } | ||
function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; subClass.__proto__ = superClass; } | ||
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } | ||
import Builder from './Builder'; | ||
@@ -26,3 +31,3 @@ import typeOf from './typeOf'; | ||
if ("production" !== process.env.NODE_ENV) { | ||
_this.invariant(_Array$isArray(builders) && builders.length > 0 && _everyInstanceProperty(builders).call(builders, function (builder) { | ||
_this.invariant(Array.isArray(builders) && builders.length > 0 && builders.every(function (builder) { | ||
return builder instanceof Builder; | ||
@@ -44,11 +49,11 @@ }), 'A non-empty array of blueprints are required for a union.'); | ||
var nextValue = value; | ||
if ("production" !== process.env.NODE_ENV) { | ||
var keys = _mapInstanceProperty(builders).call(builders, function (builder) { | ||
var keys = builders.map(function (builder) { | ||
return builder.typeAlias(); | ||
}).join(', '); | ||
var type = typeOf(value); | ||
var errors = new _Set(); | ||
var passed = _someInstanceProperty(builders).call(builders, function (builder) { | ||
var errors = new Set(); | ||
var passed = builders.some(function (builder) { | ||
if (builder.type === 'union') { | ||
@@ -60,4 +65,4 @@ _this2.invariant(false, 'Nested unions are not supported.', path); | ||
if (type === builder.type || type === 'object' && builder.type === 'shape' || builder.type === 'custom') { | ||
builder.noPrefix = true; | ||
builder.runChecks(path, value, _this2.currentStruct, _this2.options); | ||
builder.noErrorPrefix = true; | ||
nextValue = builder.runChecks(path, value, _this2.currentStruct, _this2.options); | ||
return true; | ||
@@ -71,3 +76,2 @@ } | ||
}); | ||
var message = "Type must be one of: " + keys + "."; | ||
@@ -77,4 +81,3 @@ | ||
message += " Received " + type + " with the following invalidations:\n"; | ||
_forEachInstanceProperty(errors).call(errors, function (error) { | ||
errors.forEach(function (error) { | ||
message += error; | ||
@@ -84,10 +87,10 @@ }); | ||
this.invariant(passed, _trimInstanceProperty(message).call(message), path); | ||
this.invariant(passed, message.trim(), path); | ||
} | ||
return nextValue; | ||
}; | ||
_proto.typeAlias = function typeAlias() { | ||
var _context; | ||
return _mapInstanceProperty(_context = this.builders).call(_context, function (builder) { | ||
return this.builders.map(function (builder) { | ||
return builder.typeAlias(); | ||
@@ -94,0 +97,0 @@ }).join(' | '); |
@@ -6,3 +6,3 @@ import Builder from './Builder'; | ||
constructor(contents?: Builder<T> | null, defaultValue?: ArrayOf<T>); | ||
checkContents(path: string, value: ArrayOf<T>, contents: Builder<T>): void; | ||
checkContents(path: string, value: ArrayOf<T>, contents: Builder<T>): T[]; | ||
notEmpty(): this; | ||
@@ -9,0 +9,0 @@ checkNotEmpty(path: string, value: ArrayOf<T>): void; |
"use strict"; | ||
var _interopRequireDefault = require("@babel/runtime-corejs3/helpers/interopRequireDefault"); | ||
require("core-js/modules/es.array.concat"); | ||
require("core-js/modules/web.dom-collections.for-each"); | ||
exports.__esModule = true; | ||
@@ -9,14 +11,14 @@ exports.array = array; | ||
var _forEach = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/for-each")); | ||
var _Builder2 = _interopRequireDefault(require("./Builder")); | ||
var _assertThisInitialized2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/assertThisInitialized")); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
var _inheritsLoose2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/inheritsLoose")); | ||
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; } | ||
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/defineProperty")); | ||
function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; subClass.__proto__ = superClass; } | ||
var _Builder2 = _interopRequireDefault(require("./Builder")); | ||
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } | ||
var ArrayBuilder = function (_Builder) { | ||
(0, _inheritsLoose2.default)(ArrayBuilder, _Builder); | ||
_inheritsLoose(ArrayBuilder, _Builder); | ||
@@ -35,4 +37,5 @@ function ArrayBuilder(contents, defaultValue) { | ||
_this = _Builder.call(this, 'array', defaultValue) || this; | ||
(0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "contents", null); | ||
_defineProperty(_assertThisInitialized(_this), "contents", null); | ||
if ("production" !== process.env.NODE_ENV) { | ||
@@ -58,7 +61,11 @@ if (contents) { | ||
var nextValue = [].concat(value); | ||
if ("production" !== process.env.NODE_ENV) { | ||
(0, _forEach.default)(value).call(value, function (item, i) { | ||
contents.runChecks(path + "[" + i + "]", item, _this2.currentStruct, _this2.options); | ||
value.forEach(function (item, i) { | ||
nextValue[i] = contents.runChecks(path + "[" + i + "]", item, _this2.currentStruct, _this2.options); | ||
}); | ||
} | ||
return nextValue; | ||
}; | ||
@@ -65,0 +72,0 @@ |
@@ -14,3 +14,3 @@ import { SupportedType, CheckerCallback, CustomCallback, OptimalOptions, FuncOf } from './types'; | ||
isRequired: boolean; | ||
noPrefix: boolean; | ||
noErrorPrefix: boolean; | ||
options: OptimalOptions; | ||
@@ -17,0 +17,0 @@ type: SupportedType; |
"use strict"; | ||
var _interopRequireDefault = require("@babel/runtime-corejs3/helpers/interopRequireDefault"); | ||
require("core-js/modules/es.array.concat"); | ||
require("core-js/modules/es.array.filter"); | ||
require("core-js/modules/es.array.join"); | ||
require("core-js/modules/es.array.slice"); | ||
require("core-js/modules/es.function.name"); | ||
require("core-js/modules/web.dom-collections.for-each"); | ||
exports.__esModule = true; | ||
@@ -15,31 +21,30 @@ exports.bool = bool; | ||
var _forEach = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/for-each")); | ||
var _isObject = _interopRequireDefault(require("./isObject")); | ||
var _slice = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/slice")); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
var _lastIndexOf = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/last-index-of")); | ||
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } | ||
var _isArray = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/array/is-array")); | ||
var Builder = function () { | ||
function Builder(type, defaultValue) { | ||
_defineProperty(this, "checks", []); | ||
var _filter = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/filter")); | ||
_defineProperty(this, "currentStruct", {}); | ||
var _concat = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/concat")); | ||
_defineProperty(this, "defaultValue", void 0); | ||
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/defineProperty")); | ||
_defineProperty(this, "deprecatedMessage", ''); | ||
var _isObject = _interopRequireDefault(require("./isObject")); | ||
_defineProperty(this, "errorMessage", ''); | ||
var Builder = function () { | ||
function Builder(type, defaultValue) { | ||
(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, "noPrefix", false); | ||
(0, _defineProperty2.default)(this, "options", {}); | ||
(0, _defineProperty2.default)(this, "type", void 0); | ||
_defineProperty(this, "isNullable", false); | ||
_defineProperty(this, "isRequired", false); | ||
_defineProperty(this, "noErrorPrefix", false); | ||
_defineProperty(this, "options", {}); | ||
_defineProperty(this, "type", void 0); | ||
if ("production" !== process.env.NODE_ENV) { | ||
@@ -85,7 +90,5 @@ this.invariant(typeof defaultValue !== 'undefined', "A default value for type \"" + type + "\" is required."); | ||
if ("production" !== process.env.NODE_ENV) { | ||
var _context; | ||
var keys = (0, _concat.default)(_context = [this.key(path)]).call(_context, otherKeys); | ||
var keys = [this.key(path)].concat(otherKeys); | ||
var struct = this.currentStruct; | ||
var undefs = (0, _filter.default)(keys).call(keys, function (key) { | ||
var undefs = keys.filter(function (key) { | ||
return typeof struct[key] === 'undefined' || struct[key] === null; | ||
@@ -106,3 +109,3 @@ }); | ||
case 'array': | ||
this.invariant((0, _isArray.default)(value), 'Must be an array.', path); | ||
this.invariant(Array.isArray(value), 'Must be an array.', path); | ||
break; | ||
@@ -184,3 +187,3 @@ | ||
if (prefix && !this.noPrefix) { | ||
if (prefix && !this.noErrorPrefix) { | ||
throw new Error(prefix + ". " + error); | ||
@@ -198,4 +201,4 @@ } else { | ||
_proto.key = function key(path) { | ||
var index = (0, _lastIndexOf.default)(path).call(path, '.'); | ||
return index > 0 ? (0, _slice.default)(path).call(path, index + 1) : path; | ||
var index = path.lastIndexOf('.'); | ||
return index > 0 ? path.slice(index + 1) : path; | ||
}; | ||
@@ -256,7 +259,5 @@ | ||
if ("production" !== process.env.NODE_ENV) { | ||
var _context2; | ||
var keys = (0, _concat.default)(_context2 = [this.key(path)]).call(_context2, otherKeys); | ||
var keys = [this.key(path)].concat(otherKeys); | ||
var struct = this.currentStruct; | ||
var defs = (0, _filter.default)(keys).call(keys, function (key) { | ||
var defs = keys.filter(function (key) { | ||
return typeof struct[key] !== 'undefined' && struct[key] !== null; | ||
@@ -314,8 +315,10 @@ }); | ||
if ("production" !== process.env.NODE_ENV) { | ||
var _context3; | ||
this.checks.forEach(function (checker) { | ||
var _checker$callback; | ||
(0, _forEach.default)(_context3 = this.checks).call(_context3, function (checker) { | ||
var _checker$callback, _context4; | ||
var result = (_checker$callback = checker.callback).call.apply(_checker$callback, [_this, path, value].concat(checker.args)); | ||
(_checker$callback = checker.callback).call.apply(_checker$callback, (0, _concat.default)(_context4 = [_this, path, value]).call(_context4, checker.args)); | ||
if (typeof result !== 'undefined') { | ||
value = result; | ||
} | ||
}); | ||
@@ -345,7 +348,5 @@ } | ||
if ("production" !== process.env.NODE_ENV) { | ||
var _context5; | ||
var keys = (0, _concat.default)(_context5 = [this.key(path)]).call(_context5, otherKeys); | ||
var keys = [this.key(path)].concat(otherKeys); | ||
var struct = this.currentStruct; | ||
var defs = (0, _filter.default)(keys).call(keys, function (key) { | ||
var defs = keys.filter(function (key) { | ||
return typeof struct[key] !== 'undefined' && struct[key] !== null; | ||
@@ -352,0 +353,0 @@ }); |
"use strict"; | ||
var _Object$keys = require("@babel/runtime-corejs3/core-js-stable/object/keys"); | ||
require("core-js/modules/es.object.get-own-property-descriptor"); | ||
var _forEachInstanceProperty = require("@babel/runtime-corejs3/core-js-stable/instance/for-each"); | ||
require("core-js/modules/es.object.keys"); | ||
var _context; | ||
require("core-js/modules/web.dom-collections.for-each"); | ||
var _interopRequireWildcard = require("@babel/runtime-corejs3/helpers/interopRequireWildcard"); | ||
var _interopRequireDefault = require("@babel/runtime-corejs3/helpers/interopRequireDefault"); | ||
exports.__esModule = true; | ||
@@ -91,3 +87,3 @@ var _exportNames = { | ||
_forEachInstanceProperty(_context = _Object$keys(_types)).call(_context, function (key) { | ||
Object.keys(_types).forEach(function (key) { | ||
if (key === "default" || key === "__esModule") return; | ||
@@ -98,2 +94,6 @@ if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return; | ||
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } } | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
/** | ||
@@ -100,0 +100,0 @@ * @copyright 2017-2019, Miles Johnson |
"use strict"; | ||
var _interopRequireDefault = require("@babel/runtime-corejs3/helpers/interopRequireDefault"); | ||
require("core-js/modules/es.function.name"); | ||
@@ -18,8 +16,2 @@ | ||
var _assertThisInitialized2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/assertThisInitialized")); | ||
var _inheritsLoose2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/inheritsLoose")); | ||
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/defineProperty")); | ||
var _Builder2 = _interopRequireDefault(require("./Builder")); | ||
@@ -31,4 +23,12 @@ | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; } | ||
function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; subClass.__proto__ = superClass; } | ||
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } | ||
var InstanceBuilder = function (_Builder) { | ||
(0, _inheritsLoose2.default)(InstanceBuilder, _Builder); | ||
_inheritsLoose(InstanceBuilder, _Builder); | ||
@@ -47,5 +47,7 @@ function InstanceBuilder(refClass, loose) { | ||
_this = _Builder.call(this, 'instance', null) || this; | ||
(0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "loose", false); | ||
(0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "refClass", null); | ||
_defineProperty(_assertThisInitialized(_this), "loose", false); | ||
_defineProperty(_assertThisInitialized(_this), "refClass", null); | ||
_this.nullable(); | ||
@@ -52,0 +54,0 @@ |
"use strict"; | ||
var _interopRequireDefault = require("@babel/runtime-corejs3/helpers/interopRequireDefault"); | ||
require("core-js/modules/es.function.name"); | ||
require("core-js/modules/es.object.get-prototype-of"); | ||
exports.__esModule = true; | ||
exports.default = instanceOf; | ||
var _getPrototypeOf = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/object/get-prototype-of")); | ||
function instanceOf(object, contract) { | ||
@@ -32,3 +30,3 @@ if (!object || typeof object !== 'object') { | ||
current = (0, _getPrototypeOf.default)(current); | ||
current = Object.getPrototypeOf(current); | ||
} | ||
@@ -35,0 +33,0 @@ |
"use strict"; | ||
var _interopRequireDefault = require("@babel/runtime-corejs3/helpers/interopRequireDefault"); | ||
exports.__esModule = true; | ||
exports.default = isObject; | ||
var _isArray = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/array/is-array")); | ||
function isObject(value) { | ||
return !!value && typeof value === 'object' && !(0, _isArray.default)(value); | ||
return !!value && typeof value === 'object' && !Array.isArray(value); | ||
} |
"use strict"; | ||
var _interopRequireDefault = require("@babel/runtime-corejs3/helpers/interopRequireDefault"); | ||
require("core-js/modules/es.array.includes"); | ||
require("core-js/modules/es.array.join"); | ||
require("core-js/modules/es.string.includes"); | ||
exports.__esModule = true; | ||
@@ -11,12 +13,8 @@ exports.number = number; | ||
var _includes = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/includes")); | ||
var _Builder2 = _interopRequireDefault(require("./Builder")); | ||
var _every = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/every")); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
var _isArray = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/array/is-array")); | ||
function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; subClass.__proto__ = superClass; } | ||
var _inheritsLoose2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/inheritsLoose")); | ||
var _Builder2 = _interopRequireDefault(require("./Builder")); | ||
function isNumber(value) { | ||
@@ -27,3 +25,3 @@ return typeof value === 'number'; | ||
var NumberBuilder = function (_Builder) { | ||
(0, _inheritsLoose2.default)(NumberBuilder, _Builder); | ||
_inheritsLoose(NumberBuilder, _Builder); | ||
@@ -120,3 +118,3 @@ function NumberBuilder(defaultValue) { | ||
if ("production" !== process.env.NODE_ENV) { | ||
this.invariant((0, _isArray.default)(list) && list.length > 0 && (0, _every.default)(list).call(list, function (item) { | ||
this.invariant(Array.isArray(list) && list.length > 0 && list.every(function (item) { | ||
return isNumber(item); | ||
@@ -132,3 +130,3 @@ }), 'One of requires a non-empty array of numbers.'); | ||
if ("production" !== process.env.NODE_ENV) { | ||
this.invariant((0, _includes.default)(list).call(list, value), "Number must be one of: " + list.join(', '), path); | ||
this.invariant(list.includes(value), "Number must be one of: " + list.join(', '), path); | ||
} | ||
@@ -135,0 +133,0 @@ }; |
@@ -6,3 +6,3 @@ import Builder from './Builder'; | ||
constructor(contents?: Builder<T> | null, defaultValue?: ObjectOf<T>); | ||
checkContents(path: string, value: ObjectOf<T>, contents: Builder<T>): void; | ||
checkContents(path: string, value: ObjectOf<T>, contents: Builder<T>): ObjectOf<T>; | ||
notEmpty(): this; | ||
@@ -9,0 +9,0 @@ checkNotEmpty(path: string, value: ObjectOf<T>): void; |
"use strict"; | ||
var _interopRequireDefault = require("@babel/runtime-corejs3/helpers/interopRequireDefault"); | ||
require("core-js/modules/es.object.assign"); | ||
require("core-js/modules/es.object.keys"); | ||
require("core-js/modules/web.dom-collections.for-each"); | ||
exports.__esModule = true; | ||
@@ -10,18 +14,16 @@ exports.object = object; | ||
var _keys = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/object/keys")); | ||
var _Builder2 = _interopRequireDefault(require("./Builder")); | ||
var _forEach = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/for-each")); | ||
var _InstanceBuilder = require("./InstanceBuilder"); | ||
var _assertThisInitialized2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/assertThisInitialized")); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
var _inheritsLoose2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/inheritsLoose")); | ||
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; } | ||
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/defineProperty")); | ||
function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; subClass.__proto__ = superClass; } | ||
var _Builder2 = _interopRequireDefault(require("./Builder")); | ||
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } | ||
var _InstanceBuilder = require("./InstanceBuilder"); | ||
var ObjectBuilder = function (_Builder) { | ||
(0, _inheritsLoose2.default)(ObjectBuilder, _Builder); | ||
_inheritsLoose(ObjectBuilder, _Builder); | ||
@@ -40,4 +42,5 @@ function ObjectBuilder(contents, defaultValue) { | ||
_this = _Builder.call(this, 'object', defaultValue) || this; | ||
(0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "contents", null); | ||
_defineProperty(_assertThisInitialized(_this), "contents", null); | ||
if ("production" !== process.env.NODE_ENV) { | ||
@@ -63,9 +66,11 @@ if (contents) { | ||
var nextValue = Object.assign({}, value); | ||
if ("production" !== process.env.NODE_ENV) { | ||
var _context; | ||
(0, _forEach.default)(_context = (0, _keys.default)(value)).call(_context, function (key) { | ||
contents.runChecks(path + "." + key, value[key], _this2.currentStruct, _this2.options); | ||
Object.keys(value).forEach(function (key) { | ||
nextValue[key] = contents.runChecks(path + "." + key, value[key], _this2.currentStruct, _this2.options); | ||
}); | ||
} | ||
return nextValue; | ||
}; | ||
@@ -79,3 +84,3 @@ | ||
if ("production" !== process.env.NODE_ENV) { | ||
this.invariant((0, _keys.default)(value).length > 0, 'Object cannot be empty.', path); | ||
this.invariant(Object.keys(value).length > 0, 'Object cannot be empty.', path); | ||
} | ||
@@ -82,0 +87,0 @@ }; |
"use strict"; | ||
var _interopRequireDefault = require("@babel/runtime-corejs3/helpers/interopRequireDefault"); | ||
require("core-js/modules/es.array.join"); | ||
@@ -9,12 +7,12 @@ | ||
exports.__esModule = true; | ||
exports.default = optimal; | ||
require("core-js/modules/es.object.assign"); | ||
var _endsWith = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/ends-with")); | ||
require("core-js/modules/es.object.keys"); | ||
var _keys = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/object/keys")); | ||
require("core-js/modules/es.string.ends-with"); | ||
var _forEach = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/for-each")); | ||
require("core-js/modules/web.dom-collections.for-each"); | ||
var _assign = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/object/assign")); | ||
exports.__esModule = true; | ||
exports.default = optimal; | ||
@@ -27,5 +25,5 @@ var _Builder = _interopRequireDefault(require("./Builder")); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
function buildAndCheck(blueprint, struct, options, parentPath) { | ||
var _context; | ||
if (options === void 0) { | ||
@@ -39,7 +37,5 @@ options = {}; | ||
var unknownFields = (0, _assign.default)({}, struct); | ||
var unknownFields = Object.assign({}, struct); | ||
var builtStruct = {}; | ||
(0, _forEach.default)(_context = (0, _keys.default)(blueprint)).call(_context, function (baseKey) { | ||
var _context2; | ||
Object.keys(blueprint).forEach(function (baseKey) { | ||
var key = baseKey; | ||
@@ -50,3 +46,3 @@ var value = struct[key]; | ||
if (builder instanceof _Builder.default || (0, _isObject.default)(builder) && (0, _endsWith.default)(_context2 = builder.constructor.name).call(_context2, 'Builder')) { | ||
if (builder instanceof _Builder.default || (0, _isObject.default)(builder) && builder.constructor.name.endsWith('Builder')) { | ||
builtStruct[key] = builder.runChecks(path, value, struct, options); | ||
@@ -61,8 +57,9 @@ } else if ("production" !== process.env.NODE_ENV) { | ||
if (options.unknown) { | ||
(0, _assign.default)(builtStruct, unknownFields); | ||
Object.assign(builtStruct, unknownFields); | ||
} else if ("production" !== process.env.NODE_ENV) { | ||
var unknownKeys = (0, _keys.default)(unknownFields); | ||
var unknownKeys = Object.keys(unknownFields); | ||
if (unknownKeys.length > 0) { | ||
throw new Error("Unknown fields: " + unknownKeys.join(', ') + "."); | ||
var message = parentPath ? "Unknown \"" + parentPath + "\" fields" : 'Unknown fields'; | ||
throw new Error(message + ": " + unknownKeys.join(', ') + "."); | ||
} | ||
@@ -89,3 +86,3 @@ } | ||
return buildAndCheck(blueprint, struct, options); | ||
return buildAndCheck(blueprint, struct, options, options.prefix); | ||
} |
"use strict"; | ||
var _interopRequireDefault = require("@babel/runtime-corejs3/helpers/interopRequireDefault"); | ||
require("core-js/modules/es.object.assign"); | ||
require("core-js/modules/es.array.join"); | ||
require("core-js/modules/es.object.keys"); | ||
@@ -11,22 +11,18 @@ exports.__esModule = true; | ||
var _forEach = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/for-each")); | ||
var _Builder2 = _interopRequireDefault(require("./Builder")); | ||
var _assign = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/object/assign")); | ||
var _isObject = _interopRequireDefault(require("./isObject")); | ||
var _every = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/every")); | ||
var _optimal = _interopRequireDefault(require("./optimal")); | ||
var _keys = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/object/keys")); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
var _assertThisInitialized2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/assertThisInitialized")); | ||
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; } | ||
var _inheritsLoose2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/inheritsLoose")); | ||
function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; subClass.__proto__ = superClass; } | ||
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/defineProperty")); | ||
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } | ||
var _Builder2 = _interopRequireDefault(require("./Builder")); | ||
var _isObject = _interopRequireDefault(require("./isObject")); | ||
var ShapeBuilder = function (_Builder) { | ||
(0, _inheritsLoose2.default)(ShapeBuilder, _Builder); | ||
_inheritsLoose(ShapeBuilder, _Builder); | ||
@@ -37,9 +33,9 @@ function ShapeBuilder(contents) { | ||
_this = _Builder.call(this, 'shape', {}) || this; | ||
(0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "contents", void 0); | ||
(0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "isExact", false); | ||
_defineProperty(_assertThisInitialized(_this), "contents", void 0); | ||
_defineProperty(_assertThisInitialized(_this), "isExact", false); | ||
if ("production" !== process.env.NODE_ENV) { | ||
var _context; | ||
_this.invariant((0, _isObject.default)(contents) && (0, _keys.default)(contents).length > 0 && (0, _every.default)(_context = (0, _keys.default)(contents)).call(_context, function (key) { | ||
_this.invariant((0, _isObject.default)(contents) && Object.keys(contents).length > 0 && Object.keys(contents).every(function (key) { | ||
return contents[key] instanceof _Builder2.default; | ||
@@ -61,5 +57,2 @@ }), 'A non-empty object of properties to blueprints are required for a shape.'); | ||
_proto.runChecks = function runChecks(path, initialValue, struct, options) { | ||
var _context2, | ||
_this2 = this; | ||
if (options === void 0) { | ||
@@ -69,3 +62,2 @@ options = {}; | ||
var value = {}; | ||
var object = initialValue || this.defaultValue || {}; | ||
@@ -77,25 +69,6 @@ | ||
var unknownFields = (0, _assign.default)({}, object); | ||
(0, _forEach.default)(_context2 = (0, _keys.default)(this.contents)).call(_context2, function (baseKey) { | ||
var key = baseKey; | ||
var builder = _this2.contents[key]; | ||
if (builder instanceof _Builder2.default) { | ||
value[key] = builder.runChecks(path + "." + key, object[key], object, options); | ||
} else { | ||
value[key] = object[key]; | ||
} | ||
delete unknownFields[key]; | ||
}); | ||
if (this.isExact) { | ||
var unknownKeys = (0, _keys.default)(unknownFields); | ||
if (unknownKeys.length > 0) { | ||
throw new Error("Unknown shape fields: " + unknownKeys.join(', ') + "."); | ||
} | ||
} | ||
return value; | ||
return (0, _optimal.default)(object, this.contents, Object.assign({}, options, { | ||
prefix: path, | ||
unknown: !this.isExact | ||
})); | ||
}; | ||
@@ -102,0 +75,0 @@ |
"use strict"; | ||
var _interopRequireDefault = require("@babel/runtime-corejs3/helpers/interopRequireDefault"); | ||
require("core-js/modules/es.array.includes"); | ||
@@ -11,2 +11,4 @@ require("core-js/modules/es.array.join"); | ||
require("core-js/modules/es.string.includes"); | ||
require("core-js/modules/es.string.match"); | ||
@@ -18,12 +20,8 @@ | ||
var _every = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/every")); | ||
var _Builder2 = _interopRequireDefault(require("./Builder")); | ||
var _isArray = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/array/is-array")); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
var _includes = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/includes")); | ||
function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; subClass.__proto__ = superClass; } | ||
var _inheritsLoose2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/inheritsLoose")); | ||
var _Builder2 = _interopRequireDefault(require("./Builder")); | ||
function isString(value) { | ||
@@ -34,3 +32,3 @@ return typeof value === 'string' && value !== ''; | ||
var StringBuilder = function (_Builder) { | ||
(0, _inheritsLoose2.default)(StringBuilder, _Builder); | ||
_inheritsLoose(StringBuilder, _Builder); | ||
@@ -65,3 +63,3 @@ function StringBuilder(defaultValue) { | ||
this.invariant((0, _includes.default)(value).call(value, token, index), "String does not include \"" + token + "\".", path); | ||
this.invariant(value.includes(token, index), "String does not include \"" + token + "\".", path); | ||
} | ||
@@ -100,3 +98,3 @@ }; | ||
if ("production" !== process.env.NODE_ENV) { | ||
this.invariant((0, _isArray.default)(list) && list.length > 0 && (0, _every.default)(list).call(list, function (item) { | ||
this.invariant(Array.isArray(list) && list.length > 0 && list.every(function (item) { | ||
return isString(item); | ||
@@ -112,3 +110,3 @@ }), 'One of requires a non-empty array of strings.'); | ||
if ("production" !== process.env.NODE_ENV) { | ||
this.invariant((0, _includes.default)(list).call(list, value), "String must be one of: " + list.join(', '), path); | ||
this.invariant(list.includes(value), "String must be one of: " + list.join(', '), path); | ||
} | ||
@@ -115,0 +113,0 @@ }; |
"use strict"; | ||
var _interopRequireDefault = require("@babel/runtime-corejs3/helpers/interopRequireDefault"); | ||
exports.__esModule = true; | ||
exports.default = typeOf; | ||
var _every = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/every")); | ||
var _isObject = _interopRequireDefault(require("./isObject")); | ||
var _isArray = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/array/is-array")); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
var _isObject = _interopRequireDefault(require("./isObject")); | ||
function typeOf(value) { | ||
if ((0, _isArray.default)(value)) { | ||
return (0, _every.default)(value).call(value, function (item) { | ||
if (Array.isArray(value)) { | ||
return value.every(function (item) { | ||
return typeof item === typeof value[0]; | ||
@@ -18,0 +14,0 @@ }) ? 'array' : 'union'; |
@@ -15,4 +15,5 @@ import Builder from './Builder'; | ||
name?: string; | ||
prefix?: string; | ||
unknown?: boolean; | ||
} | ||
export declare type SupportedType = 'array' | 'boolean' | 'custom' | 'function' | 'instance' | 'number' | 'object' | 'shape' | 'string' | 'union' | 'unknown'; |
@@ -5,5 +5,5 @@ import Builder from './Builder'; | ||
constructor(builders: Builder<unknown>[], defaultValue: any); | ||
checkUnions(path: string, value: any, builders: Builder<unknown>[]): void; | ||
checkUnions(path: string, value: any, builders: Builder<unknown>[]): any; | ||
/** | ||
* Return the type name using generics syntax. | ||
* Return the type name using pipe syntax. | ||
*/ | ||
@@ -10,0 +10,0 @@ typeAlias(): string; |
"use strict"; | ||
var _interopRequireDefault = require("@babel/runtime-corejs3/helpers/interopRequireDefault"); | ||
require("core-js/modules/es.array.iterator"); | ||
require("core-js/modules/es.array.join"); | ||
exports.__esModule = true; | ||
exports.union = union; | ||
exports.default = void 0; | ||
require("core-js/modules/es.array.map"); | ||
var _trim = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/trim")); | ||
require("core-js/modules/es.object.to-string"); | ||
var _forEach = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/for-each")); | ||
require("core-js/modules/es.set"); | ||
var _some = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/some")); | ||
require("core-js/modules/es.string.iterator"); | ||
var _set = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/set")); | ||
require("core-js/modules/es.string.trim"); | ||
var _map = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/map")); | ||
require("core-js/modules/web.dom-collections.for-each"); | ||
var _every = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/every")); | ||
require("core-js/modules/web.dom-collections.iterator"); | ||
var _isArray = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/array/is-array")); | ||
exports.__esModule = true; | ||
exports.union = union; | ||
exports.default = void 0; | ||
var _assertThisInitialized2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/assertThisInitialized")); | ||
var _Builder2 = _interopRequireDefault(require("./Builder")); | ||
var _inheritsLoose2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/inheritsLoose")); | ||
var _typeOf = _interopRequireDefault(require("./typeOf")); | ||
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/defineProperty")); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
var _Builder2 = _interopRequireDefault(require("./Builder")); | ||
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; } | ||
var _typeOf = _interopRequireDefault(require("./typeOf")); | ||
function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; subClass.__proto__ = superClass; } | ||
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } | ||
var UnionBuilder = function (_Builder) { | ||
(0, _inheritsLoose2.default)(UnionBuilder, _Builder); | ||
_inheritsLoose(UnionBuilder, _Builder); | ||
@@ -42,6 +44,7 @@ function UnionBuilder(builders, defaultValue) { | ||
_this = _Builder.call(this, 'union', defaultValue) || this; | ||
(0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "builders", []); | ||
_defineProperty(_assertThisInitialized(_this), "builders", []); | ||
if ("production" !== process.env.NODE_ENV) { | ||
_this.invariant((0, _isArray.default)(builders) && builders.length > 0 && (0, _every.default)(builders).call(builders, function (builder) { | ||
_this.invariant(Array.isArray(builders) && builders.length > 0 && builders.every(function (builder) { | ||
return builder instanceof _Builder2.default; | ||
@@ -63,9 +66,11 @@ }), 'A non-empty array of blueprints are required for a union.'); | ||
var nextValue = value; | ||
if ("production" !== process.env.NODE_ENV) { | ||
var keys = (0, _map.default)(builders).call(builders, function (builder) { | ||
var keys = builders.map(function (builder) { | ||
return builder.typeAlias(); | ||
}).join(', '); | ||
var type = (0, _typeOf.default)(value); | ||
var errors = new _set.default(); | ||
var passed = (0, _some.default)(builders).call(builders, function (builder) { | ||
var errors = new Set(); | ||
var passed = builders.some(function (builder) { | ||
if (builder.type === 'union') { | ||
@@ -77,4 +82,4 @@ _this2.invariant(false, 'Nested unions are not supported.', path); | ||
if (type === builder.type || type === 'object' && builder.type === 'shape' || builder.type === 'custom') { | ||
builder.noPrefix = true; | ||
builder.runChecks(path, value, _this2.currentStruct, _this2.options); | ||
builder.noErrorPrefix = true; | ||
nextValue = builder.runChecks(path, value, _this2.currentStruct, _this2.options); | ||
return true; | ||
@@ -92,3 +97,3 @@ } | ||
message += " Received " + type + " with the following invalidations:\n"; | ||
(0, _forEach.default)(errors).call(errors, function (error) { | ||
errors.forEach(function (error) { | ||
message += error; | ||
@@ -98,10 +103,10 @@ }); | ||
this.invariant(passed, (0, _trim.default)(message).call(message), path); | ||
this.invariant(passed, message.trim(), path); | ||
} | ||
return nextValue; | ||
}; | ||
_proto.typeAlias = function typeAlias() { | ||
var _context; | ||
return (0, _map.default)(_context = this.builders).call(_context, function (builder) { | ||
return this.builders.map(function (builder) { | ||
return builder.typeAlias(); | ||
@@ -108,0 +113,0 @@ }).join(' | '); |
{ | ||
"name": "optimal", | ||
"version": "3.1.1", | ||
"version": "3.2.0", | ||
"description": "A system for building and validating defined object structures.", | ||
@@ -45,7 +45,4 @@ "main": "./lib/index.js", | ||
"homepage": "https://github.com/milesj/optimal#readme", | ||
"dependencies": { | ||
"@babel/runtime-corejs3": "^7.4.3" | ||
}, | ||
"devDependencies": { | ||
"@milesj/build-tools": "^0.39.1" | ||
"@milesj/build-tools": "^0.42.0" | ||
}, | ||
@@ -52,0 +49,0 @@ "beemo": { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
0
101159
2184
- Removed@babel/runtime-corejs3@^7.4.3
- Removed@babel/runtime-corejs3@7.26.0(transitive)
- Removedcore-js-pure@3.39.0(transitive)
- Removedregenerator-runtime@0.14.1(transitive)