Comparing version 1.0.0 to 1.1.0
# Changelog | ||
# 1.1.0 - 09/02/18 | ||
#### 🚀 New | ||
- Switched to Babel 7 as the transpiler. Bumped IE requirement to v11, and Node requirement to v8.9. | ||
# 1.0.0 - 06/07/18 | ||
#### 🎉 Release | ||
- Initial release. |
@@ -0,1 +1,5 @@ | ||
/** | ||
* @copyright 2017, Miles Johnson | ||
* @license https://opensource.org/licenses/MIT | ||
*/ | ||
import { SupportedType, CheckerCallback, CustomCallback, OptimalOptions, Struct } from './types'; | ||
@@ -17,21 +21,81 @@ export interface Check { | ||
constructor(type: SupportedType, defaultValue: T); | ||
/** | ||
* Add a checking function with optional arguments. | ||
*/ | ||
addCheck(checker: CheckerCallback, ...args: any[]): this; | ||
/** | ||
* Map a list of names that must be defined alongside this field. | ||
*/ | ||
and(...keys: string[]): this; | ||
/** | ||
* Validate that all fields have been defined. | ||
*/ | ||
checkAnd(path: string, value: any, otherKeys: string[]): void; | ||
/** | ||
* Validate the type of value. | ||
*/ | ||
checkType(path: string, value: any): void; | ||
/** | ||
* Set a callback to run custom logic. | ||
*/ | ||
custom(callback: CustomCallback): this; | ||
/** | ||
* Validate the value using a custom callback. | ||
*/ | ||
checkCustom(path: string, value: any, callback: CustomCallback): void; | ||
/** | ||
* Set a message to log when this field is present. | ||
*/ | ||
deprecate(message: string): this; | ||
/** | ||
* Throw an error if the condition is falsy. | ||
*/ | ||
invariant(condition: boolean, message: string, path?: string): void; | ||
/** | ||
* Return the current key from a path. | ||
*/ | ||
key(path: string): string; | ||
/** | ||
* Set a custom error message for all checks. | ||
*/ | ||
message(message: string): this; | ||
/** | ||
* Allow null values. | ||
*/ | ||
nullable(state?: boolean): this; | ||
/** | ||
* Mark a field as only the default value can be used. | ||
*/ | ||
only(): this; | ||
/** | ||
* Validate the value matches only the default value. | ||
*/ | ||
checkOnly(path: string, value: any): void; | ||
/** | ||
* Map a list of field names that must have at least 1 defined. | ||
*/ | ||
or(...keys: string[]): this; | ||
/** | ||
* Validate that at least 1 field is defined. | ||
*/ | ||
checkOr(path: string, value: any, otherKeys: string[]): void; | ||
/** | ||
* Disallow undefined values. | ||
*/ | ||
required(state?: boolean): this; | ||
/** | ||
* Run all validation checks that have been enqueued. | ||
*/ | ||
runChecks(path: string, initialValue: any, struct: Struct, options?: OptimalOptions): any; | ||
/** | ||
* Return a human readable type name. | ||
*/ | ||
typeAlias(): string; | ||
/** | ||
* Map a list of field names that must not be defined alongside this field. | ||
*/ | ||
xor(...keys: string[]): this; | ||
/** | ||
* Validate that only 1 field is defined. | ||
*/ | ||
checkXor(path: string, value: any, otherKeys: string[]): void; | ||
@@ -38,0 +102,0 @@ } |
"use strict"; | ||
/** | ||
* @copyright 2017, Miles Johnson | ||
* @license https://opensource.org/licenses/MIT | ||
*/ | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
@@ -7,3 +11,3 @@ return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
var isObject_1 = __importDefault(require("./isObject")); | ||
var Builder = (function () { | ||
var Builder = /** @class */ (function () { | ||
function Builder(type, defaultValue) { | ||
@@ -17,3 +21,3 @@ this.checks = []; | ||
this.options = {}; | ||
if (process.env.NODE_ENV !== 'production') { | ||
if (__DEV__) { | ||
this.invariant(typeof defaultValue !== 'undefined', "A default value for type \"" + type + "\" is required."); | ||
@@ -25,2 +29,5 @@ this.addCheck(this.checkType); | ||
} | ||
/** | ||
* Add a checking function with optional arguments. | ||
*/ | ||
Builder.prototype.addCheck = function (checker) { | ||
@@ -31,3 +38,3 @@ var args = []; | ||
} | ||
if (process.env.NODE_ENV !== 'production') { | ||
if (__DEV__) { | ||
this.checks.push({ | ||
@@ -40,2 +47,5 @@ args: args, | ||
}; | ||
/** | ||
* Map a list of names that must be defined alongside this field. | ||
*/ | ||
Builder.prototype.and = function () { | ||
@@ -46,3 +56,3 @@ var keys = []; | ||
} | ||
if (process.env.NODE_ENV !== 'production') { | ||
if (__DEV__) { | ||
this.invariant(keys.length > 0, 'AND requires a list of field names.'); | ||
@@ -52,7 +62,11 @@ } | ||
}; | ||
/** | ||
* Validate that all fields have been defined. | ||
*/ | ||
Builder.prototype.checkAnd = function (path, value, otherKeys) { | ||
if (process.env.NODE_ENV !== 'production') { | ||
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) { | ||
@@ -64,4 +78,7 @@ return; | ||
}; | ||
/** | ||
* Validate the type of value. | ||
*/ | ||
Builder.prototype.checkType = function (path, value) { | ||
if (process.env.NODE_ENV !== 'production') { | ||
if (__DEV__) { | ||
switch (this.type) { | ||
@@ -74,2 +91,3 @@ case 'array': | ||
case 'union': | ||
// Handle in the sub-class | ||
break; | ||
@@ -81,2 +99,3 @@ case 'object': | ||
default: | ||
// eslint-disable-next-line valid-typeof | ||
this.invariant(typeof value === this.type, "Must be a " + this.type + ".", path); | ||
@@ -87,4 +106,7 @@ break; | ||
}; | ||
/** | ||
* Set a callback to run custom logic. | ||
*/ | ||
Builder.prototype.custom = function (callback) { | ||
if (process.env.NODE_ENV !== 'production') { | ||
if (__DEV__) { | ||
this.invariant(typeof callback === 'function', 'Custom blueprints require a validation function.'); | ||
@@ -94,4 +116,7 @@ } | ||
}; | ||
/** | ||
* Validate the value using a custom callback. | ||
*/ | ||
Builder.prototype.checkCustom = function (path, value, callback) { | ||
if (process.env.NODE_ENV !== 'production') { | ||
if (__DEV__) { | ||
try { | ||
@@ -105,4 +130,7 @@ callback(value, this.currentStruct); | ||
}; | ||
/** | ||
* Set a message to log when this field is present. | ||
*/ | ||
Builder.prototype.deprecate = function (message) { | ||
if (process.env.NODE_ENV !== 'production') { | ||
if (__DEV__) { | ||
this.invariant(typeof message === 'string' && !!message, 'A non-empty string is required for deprecated messages.'); | ||
@@ -113,5 +141,8 @@ this.deprecatedMessage = message; | ||
}; | ||
/** | ||
* Throw an error if the condition is falsy. | ||
*/ | ||
Builder.prototype.invariant = function (condition, message, path) { | ||
if (path === void 0) { path = ''; } | ||
if (process.env.NODE_ENV !== 'production') { | ||
if (__DEV__) { | ||
if (condition) { | ||
@@ -136,2 +167,5 @@ return; | ||
}; | ||
/** | ||
* Return the current key from a path. | ||
*/ | ||
Builder.prototype.key = function (path) { | ||
@@ -141,4 +175,7 @@ var index = path.lastIndexOf('.'); | ||
}; | ||
/** | ||
* Set a custom error message for all checks. | ||
*/ | ||
Builder.prototype.message = function (message) { | ||
if (process.env.NODE_ENV !== 'production') { | ||
if (__DEV__) { | ||
this.invariant(typeof message === 'string' && !!message, 'A non-empty string is required for custom messages.'); | ||
@@ -149,5 +186,8 @@ this.errorMessage = message; | ||
}; | ||
/** | ||
* Allow null values. | ||
*/ | ||
Builder.prototype.nullable = function (state) { | ||
if (state === void 0) { state = true; } | ||
if (process.env.NODE_ENV !== 'production') { | ||
if (__DEV__) { | ||
this.isNullable = state; | ||
@@ -157,13 +197,24 @@ } | ||
}; | ||
/** | ||
* Mark a field as only the default value can be used. | ||
*/ | ||
Builder.prototype.only = function () { | ||
if (process.env.NODE_ENV !== 'production') { | ||
this.invariant(typeof this.defaultValue === this.type, "Only requires a default " + this.type + " value."); | ||
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 (process.env.NODE_ENV !== 'production') { | ||
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 () { | ||
@@ -174,3 +225,3 @@ var keys = []; | ||
} | ||
if (process.env.NODE_ENV !== 'production') { | ||
if (__DEV__) { | ||
this.invariant(keys.length > 0, 'OR requires a list of field names.'); | ||
@@ -180,4 +231,7 @@ } | ||
}; | ||
/** | ||
* Validate that at least 1 field is defined. | ||
*/ | ||
Builder.prototype.checkOr = function (path, value, otherKeys) { | ||
if (process.env.NODE_ENV !== 'production') { | ||
if (__DEV__) { | ||
var keys = [this.key(path)].concat(otherKeys); | ||
@@ -189,5 +243,8 @@ var struct_2 = this.currentStruct; | ||
}; | ||
/** | ||
* Disallow undefined values. | ||
*/ | ||
Builder.prototype.required = function (state) { | ||
if (state === void 0) { state = true; } | ||
if (process.env.NODE_ENV !== 'production') { | ||
if (__DEV__) { | ||
this.isRequired = state; | ||
@@ -197,2 +254,5 @@ } | ||
}; | ||
/** | ||
* Run all validation checks that have been enqueued. | ||
*/ | ||
Builder.prototype.runChecks = function (path, initialValue, struct, options) { | ||
@@ -204,2 +264,3 @@ var _this = this; | ||
var value = initialValue; | ||
// Handle undefined | ||
if (typeof value === 'undefined') { | ||
@@ -209,3 +270,3 @@ if (!this.isRequired) { | ||
} | ||
else if (process.env.NODE_ENV !== 'production') { | ||
else if (__DEV__) { | ||
this.invariant(false, 'Field is required and must be defined.', path); | ||
@@ -215,6 +276,8 @@ } | ||
else if (this.deprecatedMessage) { | ||
if (process.env.NODE_ENV !== 'production') { | ||
if (__DEV__) { | ||
// eslint-disable-next-line no-console | ||
console.info("Field \"" + path + "\" is deprecated. " + this.deprecatedMessage); | ||
} | ||
} | ||
// Handle null | ||
if (value === null) { | ||
@@ -224,7 +287,8 @@ if (this.isNullable) { | ||
} | ||
else if (process.env.NODE_ENV !== 'production') { | ||
if (__DEV__) { | ||
this.invariant(false, 'Null is not allowed.', path); | ||
} | ||
} | ||
if (process.env.NODE_ENV !== 'production') { | ||
// Run all checks against the value | ||
if (__DEV__) { | ||
this.checks.forEach(function (checker) { | ||
@@ -237,5 +301,11 @@ var _a; | ||
}; | ||
/** | ||
* 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 () { | ||
@@ -246,3 +316,3 @@ var keys = []; | ||
} | ||
if (process.env.NODE_ENV !== 'production') { | ||
if (__DEV__) { | ||
this.invariant(keys.length > 0, 'XOR requires a list of field names.'); | ||
@@ -252,4 +322,7 @@ } | ||
}; | ||
/** | ||
* Validate that only 1 field is defined. | ||
*/ | ||
Builder.prototype.checkXor = function (path, value, otherKeys) { | ||
if (process.env.NODE_ENV !== 'production') { | ||
if (__DEV__) { | ||
var keys = [this.key(path)].concat(otherKeys); | ||
@@ -256,0 +329,0 @@ var struct_3 = this.currentStruct; |
@@ -0,1 +1,5 @@ | ||
/** | ||
* @copyright 2017, Miles Johnson | ||
* @license https://opensource.org/licenses/MIT | ||
*/ | ||
import Builder from './Builder'; | ||
@@ -8,2 +12,5 @@ export default class CollectionBuilder<T, TDefault> extends Builder<TDefault | null> { | ||
checkNotEmpty(path: string, value: any): void; | ||
/** | ||
* If contents are defined, return the type name using generics syntax. | ||
*/ | ||
typeAlias(): string; | ||
@@ -10,0 +17,0 @@ } |
"use strict"; | ||
/** | ||
* @copyright 2017, Miles Johnson | ||
* @license https://opensource.org/licenses/MIT | ||
*/ | ||
var __extends = (this && this.__extends) || (function () { | ||
var extendStatics = Object.setPrototypeOf || | ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || | ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; | ||
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) { | ||
@@ -17,3 +24,3 @@ extendStatics(d, b); | ||
var Builder_1 = __importDefault(require("./Builder")); | ||
var CollectionBuilder = (function (_super) { | ||
var CollectionBuilder = /** @class */ (function (_super) { | ||
__extends(CollectionBuilder, _super); | ||
@@ -25,3 +32,3 @@ function CollectionBuilder(type, contents, defaultValue) { | ||
_this.contents = null; | ||
if (process.env.NODE_ENV !== 'production') { | ||
if (__DEV__) { | ||
if (contents) { | ||
@@ -41,3 +48,3 @@ if (contents instanceof Builder_1.default) { | ||
var _this = this; | ||
if (process.env.NODE_ENV !== 'production') { | ||
if (__DEV__) { | ||
if (this.type === 'array') { | ||
@@ -59,3 +66,3 @@ value.forEach(function (item, i) { | ||
CollectionBuilder.prototype.checkNotEmpty = function (path, value) { | ||
if (process.env.NODE_ENV !== 'production') { | ||
if (__DEV__) { | ||
if (this.type === 'array') { | ||
@@ -69,2 +76,5 @@ this.invariant(value.length > 0, 'Array cannot be empty.', path); | ||
}; | ||
/** | ||
* If contents are defined, return the type name using generics syntax. | ||
*/ | ||
CollectionBuilder.prototype.typeAlias = function () { | ||
@@ -71,0 +81,0 @@ var contents = this.contents; |
@@ -0,1 +1,5 @@ | ||
/** | ||
* @copyright 2017, Miles Johnson | ||
* @license https://opensource.org/licenses/MIT | ||
*/ | ||
import optimal from './optimal'; | ||
@@ -2,0 +6,0 @@ import Builder, { bool, custom, func } from './Builder'; |
"use strict"; | ||
/** | ||
* @copyright 2017, Miles Johnson | ||
* @license https://opensource.org/licenses/MIT | ||
*/ | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
@@ -3,0 +7,0 @@ return (mod && mod.__esModule) ? mod : { "default": mod }; |
@@ -0,1 +1,5 @@ | ||
/** | ||
* @copyright 2017, Miles Johnson | ||
* @license https://opensource.org/licenses/MIT | ||
*/ | ||
import Builder from './Builder'; | ||
@@ -7,2 +11,5 @@ export declare type Constructor<T> = new (...args: any[]) => T; | ||
checkInstance(path: string, value: any, refClass: T | null): void; | ||
/** | ||
* If reference class is defined, return the class name if available. | ||
*/ | ||
typeAlias(): string; | ||
@@ -9,0 +16,0 @@ } |
"use strict"; | ||
/** | ||
* @copyright 2017, Miles Johnson | ||
* @license https://opensource.org/licenses/MIT | ||
*/ | ||
var __extends = (this && this.__extends) || (function () { | ||
var extendStatics = Object.setPrototypeOf || | ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || | ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; | ||
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) { | ||
@@ -18,3 +25,3 @@ extendStatics(d, b); | ||
var isObject_1 = __importDefault(require("./isObject")); | ||
var InstanceBuilder = (function (_super) { | ||
var InstanceBuilder = /** @class */ (function (_super) { | ||
__extends(InstanceBuilder, _super); | ||
@@ -25,4 +32,5 @@ function InstanceBuilder(refClass) { | ||
_this.refClass = null; | ||
// Nullable by default | ||
_this.nullable(); | ||
if (process.env.NODE_ENV !== 'production') { | ||
if (__DEV__) { | ||
if (refClass) { | ||
@@ -37,3 +45,3 @@ _this.invariant(typeof refClass === 'function', 'A class reference is required.'); | ||
InstanceBuilder.prototype.checkInstance = function (path, value, refClass) { | ||
if (process.env.NODE_ENV !== 'production') { | ||
if (__DEV__) { | ||
if (refClass) { | ||
@@ -47,2 +55,5 @@ this.invariant(typeof refClass === 'function' && value instanceof refClass, "Must be an instance of \"" + this.typeAlias() + "\".", path); | ||
}; | ||
/** | ||
* If reference class is defined, return the class name if available. | ||
*/ | ||
InstanceBuilder.prototype.typeAlias = function () { | ||
@@ -49,0 +60,0 @@ var refClass = this.refClass; |
@@ -0,1 +1,8 @@ | ||
/** | ||
* @copyright 2017, Miles Johnson | ||
* @license https://opensource.org/licenses/MIT | ||
*/ | ||
/** | ||
* Return true if the value is a plain object. | ||
*/ | ||
export default function isObject(value: any): boolean; |
"use strict"; | ||
/** | ||
* @copyright 2017, Miles Johnson | ||
* @license https://opensource.org/licenses/MIT | ||
*/ | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
/** | ||
* Return true if the value is a plain object. | ||
*/ | ||
function isObject(value) { | ||
@@ -4,0 +11,0 @@ return typeof value === 'object' && value !== null && !Array.isArray(value); |
@@ -0,1 +1,5 @@ | ||
/** | ||
* @copyright 2017, Miles Johnson | ||
* @license https://opensource.org/licenses/MIT | ||
*/ | ||
import Builder from './Builder'; | ||
@@ -2,0 +6,0 @@ export default class NumberBuilder extends Builder<number | null> { |
"use strict"; | ||
/** | ||
* @copyright 2017, Miles Johnson | ||
* @license https://opensource.org/licenses/MIT | ||
*/ | ||
var __extends = (this && this.__extends) || (function () { | ||
var extendStatics = Object.setPrototypeOf || | ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || | ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; | ||
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) { | ||
@@ -20,3 +27,3 @@ extendStatics(d, b); | ||
} | ||
var NumberBuilder = (function (_super) { | ||
var NumberBuilder = /** @class */ (function (_super) { | ||
__extends(NumberBuilder, _super); | ||
@@ -29,3 +36,3 @@ function NumberBuilder(defaultValue) { | ||
if (inclusive === void 0) { inclusive = false; } | ||
if (process.env.NODE_ENV !== 'production') { | ||
if (__DEV__) { | ||
this.invariant(isNumber(min) && isNumber(max), 'Between requires a minimum and maximum number.'); | ||
@@ -37,3 +44,3 @@ } | ||
if (inclusive === void 0) { inclusive = false; } | ||
if (process.env.NODE_ENV !== 'production') { | ||
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); | ||
@@ -44,3 +51,3 @@ } | ||
if (inclusive === void 0) { inclusive = false; } | ||
if (process.env.NODE_ENV !== 'production') { | ||
if (__DEV__) { | ||
this.invariant(isNumber(min), 'Greater-than requires a minimum number.'); | ||
@@ -55,3 +62,3 @@ } | ||
if (inclusive === void 0) { inclusive = false; } | ||
if (process.env.NODE_ENV !== 'production') { | ||
if (__DEV__) { | ||
if (inclusive) { | ||
@@ -67,3 +74,3 @@ this.invariant(isNumber(value) && value >= min, "Number must be greater than or equal to " + min + ".", path); | ||
if (inclusive === void 0) { inclusive = false; } | ||
if (process.env.NODE_ENV !== 'production') { | ||
if (__DEV__) { | ||
this.invariant(isNumber(max), 'Less-than requires a maximum number.'); | ||
@@ -78,3 +85,3 @@ } | ||
if (inclusive === void 0) { inclusive = false; } | ||
if (process.env.NODE_ENV !== 'production') { | ||
if (__DEV__) { | ||
if (inclusive) { | ||
@@ -89,3 +96,3 @@ this.invariant(isNumber(value) && value <= max, "Number must be less than or equal to " + max + ".", path); | ||
NumberBuilder.prototype.oneOf = function (list) { | ||
if (process.env.NODE_ENV !== 'production') { | ||
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.'); | ||
@@ -96,3 +103,3 @@ } | ||
NumberBuilder.prototype.checkOneOf = function (path, value, list) { | ||
if (process.env.NODE_ENV !== 'production') { | ||
if (__DEV__) { | ||
this.invariant(list.indexOf(value) >= 0, "Number must be one of: " + list.join(', '), path); | ||
@@ -99,0 +106,0 @@ } |
@@ -0,2 +1,6 @@ | ||
/** | ||
* @copyright 2017, Miles Johnson | ||
* @license https://opensource.org/licenses/MIT | ||
*/ | ||
import { Blueprint, OptimalOptions, Struct } from './types'; | ||
export default function optimal<T extends Struct>(struct: Struct, blueprint: Blueprint, options?: OptimalOptions): T; |
"use strict"; | ||
var __assign = (this && this.__assign) || Object.assign || function(t) { | ||
for (var s, i = 1, n = arguments.length; i < n; i++) { | ||
s = arguments[i]; | ||
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) | ||
t[p] = s[p]; | ||
} | ||
return t; | ||
/** | ||
* @copyright 2017, Miles Johnson | ||
* @license https://opensource.org/licenses/MIT | ||
*/ | ||
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); | ||
}; | ||
@@ -22,20 +29,26 @@ var __importDefault = (this && this.__importDefault) || function (mod) { | ||
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 (process.env.NODE_ENV !== 'production') { | ||
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); | ||
} | ||
else if (process.env.NODE_ENV !== 'production') { | ||
else if (__DEV__) { | ||
var unknownKeys = Object.keys(unknownFields); | ||
@@ -50,3 +63,3 @@ if (unknownKeys.length > 0) { | ||
if (options === void 0) { options = {}; } | ||
if (process.env.NODE_ENV !== 'production') { | ||
if (__DEV__) { | ||
if (!isObject_1.default(struct)) { | ||
@@ -53,0 +66,0 @@ throw new TypeError("Optimal requires a plain object, found " + typeOf_1.default(struct) + "."); |
@@ -0,1 +1,5 @@ | ||
/** | ||
* @copyright 2017, Miles Johnson | ||
* @license https://opensource.org/licenses/MIT | ||
*/ | ||
import Builder from './Builder'; | ||
@@ -2,0 +6,0 @@ import { Blueprint } from './types'; |
"use strict"; | ||
/** | ||
* @copyright 2017, Miles Johnson | ||
* @license https://opensource.org/licenses/MIT | ||
*/ | ||
var __extends = (this && this.__extends) || (function () { | ||
var extendStatics = Object.setPrototypeOf || | ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || | ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; | ||
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) { | ||
@@ -18,3 +25,3 @@ extendStatics(d, b); | ||
var isObject_1 = __importDefault(require("./isObject")); | ||
var ShapeBuilder = (function (_super) { | ||
var ShapeBuilder = /** @class */ (function (_super) { | ||
__extends(ShapeBuilder, _super); | ||
@@ -24,3 +31,3 @@ function ShapeBuilder(contents, defaultValue) { | ||
var _this = _super.call(this, 'shape', defaultValue) || this; | ||
if (process.env.NODE_ENV !== 'production') { | ||
if (__DEV__) { | ||
_this.invariant(isObject_1.default(contents) && | ||
@@ -35,5 +42,6 @@ Object.keys(contents).length > 0 && | ||
var _this = this; | ||
if (process.env.NODE_ENV !== 'production') { | ||
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 && | ||
@@ -40,0 +48,0 @@ (builder.isRequired || typeof object[key] !== 'undefined')) { |
@@ -0,1 +1,5 @@ | ||
/** | ||
* @copyright 2017, Miles Johnson | ||
* @license https://opensource.org/licenses/MIT | ||
*/ | ||
import Builder from './Builder'; | ||
@@ -2,0 +6,0 @@ export default class StringBuilder extends Builder<string | null> { |
"use strict"; | ||
/** | ||
* @copyright 2017, Miles Johnson | ||
* @license https://opensource.org/licenses/MIT | ||
*/ | ||
var __extends = (this && this.__extends) || (function () { | ||
var extendStatics = Object.setPrototypeOf || | ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || | ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; | ||
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) { | ||
@@ -20,3 +27,3 @@ extendStatics(d, b); | ||
} | ||
var StringBuilder = (function (_super) { | ||
var StringBuilder = /** @class */ (function (_super) { | ||
__extends(StringBuilder, _super); | ||
@@ -27,3 +34,4 @@ function StringBuilder(defaultValue) { | ||
_this.allowEmpty = false; | ||
if (process.env.NODE_ENV !== 'production') { | ||
// Not empty by default | ||
if (__DEV__) { | ||
_this.addCheck(_this.checkNotEmpty); | ||
@@ -35,3 +43,3 @@ } | ||
if (index === void 0) { index = 0; } | ||
if (process.env.NODE_ENV !== 'production') { | ||
if (__DEV__) { | ||
this.invariant(isString(token), 'Contains requires a non-empty string.'); | ||
@@ -43,3 +51,3 @@ } | ||
if (index === void 0) { index = 0; } | ||
if (process.env.NODE_ENV !== 'production') { | ||
if (__DEV__) { | ||
this.invariant(value.indexOf(token, index) >= 0, "String does not include \"" + token + "\".", path); | ||
@@ -49,3 +57,3 @@ } | ||
StringBuilder.prototype.match = function (pattern) { | ||
if (process.env.NODE_ENV !== 'production') { | ||
if (__DEV__) { | ||
this.invariant(pattern instanceof RegExp, 'Match requires a regular expression to match against.'); | ||
@@ -56,3 +64,3 @@ } | ||
StringBuilder.prototype.checkMatch = function (path, value, pattern) { | ||
if (process.env.NODE_ENV !== 'production') { | ||
if (__DEV__) { | ||
this.invariant(value.match(pattern), "String does not match pattern \"" + pattern.source + "\".", path); | ||
@@ -62,3 +70,3 @@ } | ||
StringBuilder.prototype.empty = function () { | ||
if (process.env.NODE_ENV !== 'production') { | ||
if (__DEV__) { | ||
this.allowEmpty = true; | ||
@@ -69,3 +77,3 @@ } | ||
StringBuilder.prototype.checkNotEmpty = function (path, value) { | ||
if (process.env.NODE_ENV !== 'production') { | ||
if (__DEV__) { | ||
if (!this.allowEmpty) { | ||
@@ -77,3 +85,3 @@ this.invariant(isString(value), 'String cannot be empty.', path); | ||
StringBuilder.prototype.oneOf = function (list) { | ||
if (process.env.NODE_ENV !== 'production') { | ||
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.'); | ||
@@ -84,3 +92,3 @@ } | ||
StringBuilder.prototype.checkOneOf = function (path, value, list) { | ||
if (process.env.NODE_ENV !== 'production') { | ||
if (__DEV__) { | ||
this.invariant(list.indexOf(value) >= 0, "String must be one of: " + list.join(', '), path); | ||
@@ -87,0 +95,0 @@ } |
@@ -0,2 +1,6 @@ | ||
/** | ||
* @copyright 2017, Miles Johnson | ||
* @license https://opensource.org/licenses/MIT | ||
*/ | ||
import { SupportedType } from './types'; | ||
export default function typeOf(value: any): SupportedType; |
"use strict"; | ||
/** | ||
* @copyright 2017, Miles Johnson | ||
* @license https://opensource.org/licenses/MIT | ||
*/ | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
@@ -7,2 +11,3 @@ return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
var isObject_1 = __importDefault(require("./isObject")); | ||
// Not supported: Shape, Custom | ||
function typeOf(value) { | ||
@@ -20,2 +25,3 @@ if (Array.isArray(value)) { | ||
case 'string': | ||
// @ts-ignore | ||
return typeof value; | ||
@@ -22,0 +28,0 @@ default: |
@@ -0,1 +1,5 @@ | ||
/** | ||
* @copyright 2017, Miles Johnson | ||
* @license https://opensource.org/licenses/MIT | ||
*/ | ||
import Builder from './Builder'; | ||
@@ -2,0 +6,0 @@ export interface Blueprint { |
"use strict"; | ||
/** | ||
* @copyright 2017, Miles Johnson | ||
* @license https://opensource.org/licenses/MIT | ||
*/ | ||
Object.defineProperty(exports, "__esModule", { value: true }); |
@@ -0,1 +1,5 @@ | ||
/** | ||
* @copyright 2017, Miles Johnson | ||
* @license https://opensource.org/licenses/MIT | ||
*/ | ||
import Builder from './Builder'; | ||
@@ -6,4 +10,7 @@ export default class UnionBuilder extends Builder<any> { | ||
checkUnions(path: string, value: any, builders: Builder<any>[]): void; | ||
/** | ||
* Return the type name using generics syntax. | ||
*/ | ||
typeAlias(): string; | ||
} | ||
export declare function union(builders: Builder<any>[], defaultValue?: any | null): UnionBuilder; |
"use strict"; | ||
/** | ||
* @copyright 2017, Miles Johnson | ||
* @license https://opensource.org/licenses/MIT | ||
*/ | ||
var __extends = (this && this.__extends) || (function () { | ||
var extendStatics = Object.setPrototypeOf || | ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || | ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; | ||
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) { | ||
@@ -18,3 +25,3 @@ extendStatics(d, b); | ||
var typeOf_1 = __importDefault(require("./typeOf")); | ||
var UnionBuilder = (function (_super) { | ||
var UnionBuilder = /** @class */ (function (_super) { | ||
__extends(UnionBuilder, _super); | ||
@@ -25,3 +32,3 @@ function UnionBuilder(builders, defaultValue) { | ||
_this.builders = []; | ||
if (process.env.NODE_ENV !== 'production') { | ||
if (__DEV__) { | ||
_this.invariant(Array.isArray(builders) && | ||
@@ -37,6 +44,7 @@ builders.length > 0 && | ||
var _this = this; | ||
if (process.env.NODE_ENV !== 'production') { | ||
if (__DEV__) { | ||
var usage_1 = {}; | ||
var keys_1 = []; | ||
var type_1 = typeOf_1.default(value); | ||
// Verify structure and usage | ||
builders.forEach(function (builder) { | ||
@@ -57,2 +65,3 @@ if (usage_1[builder.type]) { | ||
} | ||
// Run checks on value | ||
var checked_1 = false; | ||
@@ -70,2 +79,5 @@ builders.forEach(function (builder) { | ||
}; | ||
/** | ||
* Return the type name using generics syntax. | ||
*/ | ||
UnionBuilder.prototype.typeAlias = function () { | ||
@@ -72,0 +84,0 @@ return this.builders.map(function (builder) { return builder.typeAlias(); }).join(' | '); |
{ | ||
"name": "optimal", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "A system for building and validating defined object structures.", | ||
@@ -8,5 +8,3 @@ "main": "./lib/index.js", | ||
"scripts": { | ||
"babel": "beemo typescript --declaration", | ||
"build": "yarn run babel", | ||
"clean": "rimraf ./{lib,esm}/", | ||
"build": "beemo babel && beemo typescript --declarationOnly", | ||
"coverage": "yarn run jest --coverage", | ||
@@ -18,5 +16,4 @@ "docs": "gitbook build --debug", | ||
"jest": "beemo jest", | ||
"package": "yarn run clean && yarn run build && yarn test", | ||
"prettier": "beemo prettier", | ||
"prerelease": "yarn run package", | ||
"prerelease": "yarn test && yarn run build", | ||
"release": "np --yolo --no-yarn", | ||
@@ -53,3 +50,3 @@ "pretest": "yarn run type", | ||
"devDependencies": { | ||
"@milesj/build-tool-config": "^0.100.1" | ||
"@milesj/build-tools": "^0.0.8" | ||
}, | ||
@@ -67,4 +64,7 @@ "beemo": { | ||
"browserslist": [ | ||
"ie 10" | ||
] | ||
"ie 11" | ||
], | ||
"dependencies": { | ||
"@babel/runtime": "^7.0.0" | ||
} | ||
} |
@@ -18,4 +18,4 @@ # Optimal | ||
- Node 6.5 (server) | ||
- IE 10+ (browser) | ||
- Node 8.9 (server) | ||
- IE 11+ (browser) | ||
@@ -22,0 +22,0 @@ ## Installation |
Sorry, the diff of this file is not supported yet
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
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
32
1295
0
280900
1
+ Added@babel/runtime@^7.0.0
+ Added@babel/runtime@7.26.0(transitive)
+ Addedregenerator-runtime@0.14.1(transitive)