superstruct
Advanced tools
Comparing version 0.6.2 to 0.7.0
@@ -5,2 +5,8 @@ # Changelog | ||
### `0.7.0` — September 21, 2019 | ||
###### BREAKING | ||
* **The build process now outputs ES5 code.** Previously it was outputting ES6 code, which posed problems for some builders. This change shouldn't really affect anyone negatively, but it's being released as a breaking version just in case. | ||
--- | ||
@@ -7,0 +13,0 @@ |
1090
lib/index.es.js
@@ -0,1 +1,133 @@ | ||
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; | ||
} | ||
function _objectSpread(target) { | ||
for (var i = 1; i < arguments.length; i++) { | ||
var source = arguments[i] != null ? arguments[i] : {}; | ||
var ownKeys = Object.keys(source); | ||
if (typeof Object.getOwnPropertySymbols === 'function') { | ||
ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) { | ||
return Object.getOwnPropertyDescriptor(source, sym).enumerable; | ||
})); | ||
} | ||
ownKeys.forEach(function (key) { | ||
_defineProperty(target, key, source[key]); | ||
}); | ||
} | ||
return target; | ||
} | ||
function _inheritsLoose(subClass, superClass) { | ||
subClass.prototype = Object.create(superClass.prototype); | ||
subClass.prototype.constructor = subClass; | ||
subClass.__proto__ = superClass; | ||
} | ||
function _getPrototypeOf(o) { | ||
_getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { | ||
return o.__proto__ || Object.getPrototypeOf(o); | ||
}; | ||
return _getPrototypeOf(o); | ||
} | ||
function _setPrototypeOf(o, p) { | ||
_setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { | ||
o.__proto__ = p; | ||
return o; | ||
}; | ||
return _setPrototypeOf(o, p); | ||
} | ||
function isNativeReflectConstruct() { | ||
if (typeof Reflect === "undefined" || !Reflect.construct) return false; | ||
if (Reflect.construct.sham) return false; | ||
if (typeof Proxy === "function") return true; | ||
try { | ||
Date.prototype.toString.call(Reflect.construct(Date, [], function () {})); | ||
return true; | ||
} catch (e) { | ||
return false; | ||
} | ||
} | ||
function _construct(Parent, args, Class) { | ||
if (isNativeReflectConstruct()) { | ||
_construct = Reflect.construct; | ||
} else { | ||
_construct = function _construct(Parent, args, Class) { | ||
var a = [null]; | ||
a.push.apply(a, args); | ||
var Constructor = Function.bind.apply(Parent, a); | ||
var instance = new Constructor(); | ||
if (Class) _setPrototypeOf(instance, Class.prototype); | ||
return instance; | ||
}; | ||
} | ||
return _construct.apply(null, arguments); | ||
} | ||
function _isNativeFunction(fn) { | ||
return Function.toString.call(fn).indexOf("[native code]") !== -1; | ||
} | ||
function _wrapNativeSuper(Class) { | ||
var _cache = typeof Map === "function" ? new Map() : undefined; | ||
_wrapNativeSuper = function _wrapNativeSuper(Class) { | ||
if (Class === null || !_isNativeFunction(Class)) return Class; | ||
if (typeof Class !== "function") { | ||
throw new TypeError("Super expression must either be null or a function"); | ||
} | ||
if (typeof _cache !== "undefined") { | ||
if (_cache.has(Class)) return _cache.get(Class); | ||
_cache.set(Class, Wrapper); | ||
} | ||
function Wrapper() { | ||
return _construct(Class, arguments, _getPrototypeOf(this).constructor); | ||
} | ||
Wrapper.prototype = Object.create(Class.prototype, { | ||
constructor: { | ||
value: Wrapper, | ||
enumerable: false, | ||
writable: true, | ||
configurable: true | ||
} | ||
}); | ||
return _setPrototypeOf(Wrapper, Class); | ||
}; | ||
return _wrapNativeSuper(Class); | ||
} | ||
function _assertThisInitialized(self) { | ||
if (self === void 0) { | ||
throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); | ||
} | ||
return self; | ||
} | ||
/** | ||
@@ -6,34 +138,50 @@ * Define a struct error. | ||
*/ | ||
var StructError = | ||
/*#__PURE__*/ | ||
function (_TypeError) { | ||
_inheritsLoose(StructError, _TypeError); | ||
class StructError extends TypeError { | ||
static format(attrs) { | ||
const { type, path, value } = attrs; | ||
const message = `Expected a value of type \`${type}\`${path.length ? ` for \`${path.join('.')}\`` : ''} but received \`${JSON.stringify(value)}\`.`; | ||
StructError.format = function format(attrs) { | ||
var type = attrs.type, | ||
path = attrs.path, | ||
value = attrs.value; | ||
var message = "Expected a value of type `" + type + "`" + (path.length ? " for `" + path.join('.') + "`" : '') + " but received `" + JSON.stringify(value) + "`."; | ||
return message; | ||
} | ||
}; | ||
constructor(attrs) { | ||
const message = StructError.format(attrs); | ||
super(message); | ||
function StructError(attrs) { | ||
var _this; | ||
const { data, path, value, reason, type, errors = [] } = attrs; | ||
this.data = data; | ||
this.path = path; | ||
this.value = value; | ||
this.reason = reason; | ||
this.type = type; | ||
this.errors = errors; | ||
var message = StructError.format(attrs); | ||
_this = _TypeError.call(this, message) || this; | ||
var data = attrs.data, | ||
path = attrs.path, | ||
value = attrs.value, | ||
reason = attrs.reason, | ||
type = attrs.type, | ||
_attrs$errors = attrs.errors, | ||
errors = _attrs$errors === void 0 ? [] : _attrs$errors; | ||
_this.data = data; | ||
_this.path = path; | ||
_this.value = value; | ||
_this.reason = reason; | ||
_this.type = type; | ||
_this.errors = errors; | ||
if (!errors.length) { | ||
errors.push(this); | ||
errors.push(_assertThisInitialized(_this)); | ||
} | ||
if (Error.captureStackTrace) { | ||
Error.captureStackTrace(this, this.constructor); | ||
Error.captureStackTrace(_assertThisInitialized(_this), _this.constructor); | ||
} else { | ||
this.stack = new Error().stack; | ||
_this.stack = new Error().stack; | ||
} | ||
return _this; | ||
} | ||
} | ||
return StructError; | ||
}(_wrapNativeSuper(TypeError)); | ||
var toString = Object.prototype.toString; | ||
@@ -174,5 +322,3 @@ | ||
*/ | ||
const IS_STRUCT = '@@__STRUCT__@@'; | ||
var IS_STRUCT = '@@__STRUCT__@@'; | ||
/** | ||
@@ -184,3 +330,3 @@ * A private string to refer to a struct's kind. | ||
const KIND = '@@__KIND__@@'; | ||
var KIND = '@@__KIND__@@'; | ||
@@ -197,3 +343,2 @@ /** | ||
} | ||
/** | ||
@@ -211,16 +356,2 @@ * Resolve `defaults`, for an optional `value`. | ||
var _extends = Object.assign || function (target) { | ||
for (var i = 1; i < arguments.length; i++) { | ||
var source = arguments[i]; | ||
for (var key in source) { | ||
if (Object.prototype.hasOwnProperty.call(source, key)) { | ||
target[key] = source[key]; | ||
} | ||
} | ||
} | ||
return target; | ||
}; | ||
/** | ||
@@ -232,10 +363,7 @@ * Kind. | ||
class Kind { | ||
constructor(name, type, validate) { | ||
this.name = name; | ||
this.type = type; | ||
this.validate = validate; | ||
} | ||
} | ||
var Kind = function Kind(name, type, validate) { | ||
this.name = name; | ||
this.type = type; | ||
this.validate = validate; | ||
}; | ||
/** | ||
@@ -249,3 +377,4 @@ * Any. | ||
function any(schema, defaults$$1, options) { | ||
function any(schema, defaults, options) { | ||
if (isStruct(schema)) { | ||
@@ -262,3 +391,3 @@ return schema[KIND]; | ||
{ | ||
return schema.length > 1 ? tuple(schema, defaults$$1, options) : list(schema, defaults$$1, options); | ||
return schema.length > 1 ? tuple(schema, defaults, options) : list(schema, defaults, options); | ||
} | ||
@@ -268,3 +397,3 @@ | ||
{ | ||
return func(schema, defaults$$1, options); | ||
return func(schema, defaults, options); | ||
} | ||
@@ -274,3 +403,3 @@ | ||
{ | ||
return object(schema, defaults$$1, options); | ||
return object(schema, defaults, options); | ||
} | ||
@@ -280,4 +409,4 @@ | ||
{ | ||
let required = true; | ||
let type; | ||
var required = true; | ||
var type; | ||
@@ -290,9 +419,10 @@ if (schema.endsWith('?')) { | ||
if (schema.includes('|')) { | ||
const scalars = schema.split(/\s*\|\s*/g); | ||
type = union(scalars, defaults$$1, options); | ||
var scalars = schema.split(/\s*\|\s*/g); | ||
type = union(scalars, defaults, options); | ||
} else if (schema.includes('&')) { | ||
const scalars = schema.split(/\s*&\s*/g); | ||
type = intersection(scalars, defaults$$1, options); | ||
var _scalars = schema.split(/\s*&\s*/g); | ||
type = intersection(_scalars, defaults, options); | ||
} else { | ||
type = scalar(schema, defaults$$1, options); | ||
type = scalar(schema, defaults, options); | ||
} | ||
@@ -309,8 +439,7 @@ | ||
if (process.env.NODE_ENV !== 'production') { | ||
throw new Error(`A schema definition must be an object, array, string or function, but you passed: ${schema}`); | ||
throw new Error("A schema definition must be an object, array, string or function, but you passed: " + schema); | ||
} else { | ||
throw new Error(`Invalid schema: ${schema}`); | ||
throw new Error("Invalid schema: " + schema); | ||
} | ||
} | ||
/** | ||
@@ -324,21 +453,25 @@ * Dict. | ||
function dict(schema, defaults$$1, options) { | ||
function dict(schema, defaults, options) { | ||
if (kindOf(schema) !== 'array' || schema.length !== 2) { | ||
if (process.env.NODE_ENV !== 'production') { | ||
throw new Error(`Dict structs must be defined as an array with two elements, but you passed: ${schema}`); | ||
throw new Error("Dict structs must be defined as an array with two elements, but you passed: " + schema); | ||
} else { | ||
throw new Error(`Invalid schema: ${schema}`); | ||
throw new Error("Invalid schema: " + schema); | ||
} | ||
} | ||
const obj = scalar('object', undefined, options); | ||
const keys = any(schema[0], undefined, options); | ||
const values = any(schema[1], undefined, options); | ||
const name = 'dict'; | ||
const type = `dict<${keys.type},${values.type}>`; | ||
const validate = value => { | ||
const resolved = resolveDefaults(defaults$$1); | ||
value = resolved ? _extends({}, resolved, value) : value; | ||
const [error] = obj.validate(value); | ||
var obj = scalar('object', undefined, options); | ||
var keys = any(schema[0], undefined, options); | ||
var values = any(schema[1], undefined, options); | ||
var name = 'dict'; | ||
var type = "dict<" + keys.type + "," + values.type + ">"; | ||
var validate = function validate(value) { | ||
var resolved = resolveDefaults(defaults); | ||
value = resolved ? _objectSpread({}, resolved, value) : value; | ||
var _obj$validate = obj.validate(value), | ||
error = _obj$validate[0]; | ||
if (error) { | ||
@@ -349,36 +482,53 @@ error.type = type; | ||
const ret = {}; | ||
const errors = []; | ||
var ret = {}; | ||
var errors = []; | ||
for (let k in value) { | ||
const v = value[k]; | ||
const [e, r] = keys.validate(k); | ||
var _loop = function _loop(_k) { | ||
var v = value[_k]; | ||
var _keys$validate = keys.validate(_k), | ||
e = _keys$validate[0], | ||
r = _keys$validate[1]; | ||
if (e) { | ||
const allE = e.errors || [e]; | ||
allE.forEach(singleE => { | ||
singleE.path = [k].concat(singleE.path); | ||
var allE = e.errors || [e]; | ||
allE.forEach(function (singleE) { | ||
singleE.path = [_k].concat(singleE.path); | ||
singleE.data = value; | ||
errors.push(singleE); | ||
}); | ||
continue; | ||
k = _k; | ||
return "continue"; | ||
} | ||
k = r; | ||
const [e2, r2] = values.validate(v); | ||
_k = r; | ||
var _values$validate = values.validate(v), | ||
e2 = _values$validate[0], | ||
r2 = _values$validate[1]; | ||
if (e2) { | ||
const allE2 = e2.errors || [e2]; | ||
allE2.forEach(singleE => { | ||
singleE.path = [k].concat(singleE.path); | ||
var allE2 = e2.errors || [e2]; | ||
allE2.forEach(function (singleE) { | ||
singleE.path = [_k].concat(singleE.path); | ||
singleE.data = value; | ||
errors.push(singleE); | ||
}); | ||
continue; | ||
k = _k; | ||
return "continue"; | ||
} | ||
ret[k] = r2; | ||
ret[_k] = r2; | ||
k = _k; | ||
}; | ||
for (var k in value) { | ||
var _ret = _loop(k); | ||
if (_ret === "continue") continue; | ||
} | ||
return errors.length ? [_extends({}, errors[0], { errors })] : [undefined, ret]; | ||
return errors.length ? [_objectSpread({}, errors[0], { | ||
errors: errors | ||
})] : [undefined, ret]; | ||
}; | ||
@@ -388,3 +538,2 @@ | ||
} | ||
/** | ||
@@ -398,13 +547,14 @@ * Enum. | ||
function en(schema, defaults$$1, options) { | ||
function en(schema, defaults, options) { | ||
if (kindOf(schema) !== 'array') { | ||
if (process.env.NODE_ENV !== 'production') { | ||
throw new Error(`Enum structs must be defined as an array, but you passed: ${schema}`); | ||
throw new Error("Enum structs must be defined as an array, but you passed: " + schema); | ||
} else { | ||
throw new Error(`Invalid schema: ${schema}`); | ||
throw new Error("Invalid schema: " + schema); | ||
} | ||
} | ||
const name = 'enum'; | ||
const type = schema.map(s => { | ||
var name = 'enum'; | ||
var type = schema.map(function (s) { | ||
try { | ||
@@ -417,4 +567,13 @@ return JSON.stringify(s); | ||
const validate = (value = resolveDefaults(defaults$$1)) => { | ||
return schema.includes(value) ? [undefined, value] : [{ data: value, path: [], value, type }]; | ||
var validate = function validate(value) { | ||
if (value === void 0) { | ||
value = resolveDefaults(defaults); | ||
} | ||
return schema.includes(value) ? [undefined, value] : [{ | ||
data: value, | ||
path: [], | ||
value: value, | ||
type: type | ||
}]; | ||
}; | ||
@@ -424,3 +583,2 @@ | ||
} | ||
/** | ||
@@ -434,8 +592,8 @@ * Enums. | ||
function enums(schema, defaults$$1, options) { | ||
const e = en(schema, undefined, options); | ||
const l = list([e], defaults$$1, options); | ||
function enums(schema, defaults, options) { | ||
var e = en(schema, undefined, options); | ||
var l = list([e], defaults, options); | ||
return l; | ||
} | ||
/** | ||
@@ -449,18 +607,27 @@ * Function. | ||
function func(schema, defaults$$1, options) { | ||
function func(schema, defaults, options) { | ||
if (kindOf(schema) !== 'function') { | ||
if (process.env.NODE_ENV !== 'production') { | ||
throw new Error(`Function structs must be defined as a function, but you passed: ${schema}`); | ||
throw new Error("Function structs must be defined as a function, but you passed: " + schema); | ||
} else { | ||
throw new Error(`Invalid schema: ${schema}`); | ||
throw new Error("Invalid schema: " + schema); | ||
} | ||
} | ||
const name = 'function'; | ||
const type = '<function>'; | ||
const validate = (value = resolveDefaults(defaults$$1), data) => { | ||
const result = schema(value, data); | ||
let failure = { path: [], reason: null }; | ||
let isValid; | ||
var name = 'function'; | ||
var type = '<function>'; | ||
var validate = function validate(value, data) { | ||
if (value === void 0) { | ||
value = resolveDefaults(defaults); | ||
} | ||
var result = schema(value, data); | ||
var failure = { | ||
path: [], | ||
reason: null | ||
}; | ||
var isValid; | ||
switch (kindOf(result)) { | ||
@@ -472,2 +639,3 @@ case 'boolean': | ||
} | ||
case 'string': | ||
@@ -479,14 +647,16 @@ { | ||
} | ||
case 'object': | ||
{ | ||
isValid = false; | ||
failure = _extends({}, failure, result); | ||
failure = _objectSpread({}, failure, result); | ||
break; | ||
} | ||
default: | ||
{ | ||
if (process.env.NODE_ENV !== 'production') { | ||
throw new Error(`Validator functions must return a boolean, an error reason string or an error reason object, but you passed: ${schema}`); | ||
throw new Error("Validator functions must return a boolean, an error reason string or an error reason object, but you passed: " + schema); | ||
} else { | ||
throw new Error(`Invalid result: ${result}`); | ||
throw new Error("Invalid result: " + result); | ||
} | ||
@@ -496,3 +666,7 @@ } | ||
return isValid ? [undefined, value] : [_extends({ type, value, data: value }, failure)]; | ||
return isValid ? [undefined, value] : [_objectSpread({ | ||
type: type, | ||
value: value, | ||
data: value | ||
}, failure)]; | ||
}; | ||
@@ -502,3 +676,2 @@ | ||
} | ||
/** | ||
@@ -512,7 +685,18 @@ * Instance. | ||
function instance(schema, defaults$$1, options) { | ||
const name = 'instance'; | ||
const type = `instance<${schema.name}>`; | ||
const validate = (value = resolveDefaults(defaults$$1)) => { | ||
return value instanceof schema ? [undefined, value] : [{ data: value, path: [], value, type }]; | ||
function instance(schema, defaults, options) { | ||
var name = 'instance'; | ||
var type = "instance<" + schema.name + ">"; | ||
var validate = function validate(value) { | ||
if (value === void 0) { | ||
value = resolveDefaults(defaults); | ||
} | ||
return value instanceof schema ? [undefined, value] : [{ | ||
data: value, | ||
path: [], | ||
value: value, | ||
type: type | ||
}]; | ||
}; | ||
@@ -522,3 +706,2 @@ | ||
} | ||
/** | ||
@@ -532,27 +715,34 @@ * Interface. | ||
function inter(schema, defaults$$1, options) { | ||
function inter(schema, defaults, options) { | ||
if (kindOf(schema) !== 'object') { | ||
if (process.env.NODE_ENV !== 'production') { | ||
throw new Error(`Interface structs must be defined as an object, but you passed: ${schema}`); | ||
throw new Error("Interface structs must be defined as an object, but you passed: " + schema); | ||
} else { | ||
throw new Error(`Invalid schema: ${schema}`); | ||
throw new Error("Invalid schema: " + schema); | ||
} | ||
} | ||
const obj = scalar('object', undefined, options); | ||
const ks = []; | ||
const properties = {}; | ||
var obj = scalar('object', undefined, options); | ||
var ks = []; | ||
var properties = {}; | ||
for (const key in schema) { | ||
for (var key in schema) { | ||
ks.push(key); | ||
const s = schema[key]; | ||
const kind = any(s, undefined, options); | ||
var s = schema[key]; | ||
var kind = any(s, undefined, options); | ||
properties[key] = kind; | ||
} | ||
const name = 'interface'; | ||
const type = `{${ks.join()}}`; | ||
const validate = (value = resolveDefaults(defaults$$1)) => { | ||
const [error] = obj.validate(value); | ||
var name = 'interface'; | ||
var type = "{" + ks.join() + "}"; | ||
var validate = function validate(value) { | ||
if (value === void 0) { | ||
value = resolveDefaults(defaults); | ||
} | ||
var _obj$validate2 = obj.validate(value), | ||
error = _obj$validate2[0]; | ||
if (error) { | ||
@@ -563,32 +753,42 @@ error.type = type; | ||
const errors = []; | ||
const ret = value; | ||
var errors = []; | ||
var ret = value; | ||
for (const key in properties) { | ||
let v = value[key]; | ||
const kind = properties[key]; | ||
var _loop2 = function _loop2(_key) { | ||
var v = value[_key]; | ||
var kind = properties[_key]; | ||
if (v === undefined) { | ||
const d = defaults$$1 && defaults$$1[key]; | ||
var d = defaults && defaults[_key]; | ||
v = resolveDefaults(d, value); | ||
} | ||
const [e, r] = kind.validate(v, value); | ||
var _kind$validate = kind.validate(v, value), | ||
e = _kind$validate[0], | ||
r = _kind$validate[1]; | ||
if (e) { | ||
const allE = e.errors || [e]; | ||
allE.forEach(singleE => { | ||
singleE.path = [key].concat(singleE.path); | ||
var allE = e.errors || [e]; | ||
allE.forEach(function (singleE) { | ||
singleE.path = [_key].concat(singleE.path); | ||
singleE.data = value; | ||
errors.push(singleE); | ||
}); | ||
continue; | ||
return "continue"; | ||
} | ||
if (key in value || r !== undefined) { | ||
ret[key] = r; | ||
if (_key in value || r !== undefined) { | ||
ret[_key] = r; | ||
} | ||
}; | ||
for (var _key in properties) { | ||
var _ret2 = _loop2(_key); | ||
if (_ret2 === "continue") continue; | ||
} | ||
return errors.length ? [_extends({}, errors[0], { errors })] : [undefined, ret]; | ||
return errors.length ? [_objectSpread({}, errors[0], { | ||
errors: errors | ||
})] : [undefined, ret]; | ||
}; | ||
@@ -598,3 +798,2 @@ | ||
} | ||
/** | ||
@@ -608,16 +807,18 @@ * Lazy. | ||
function lazy(schema, defaults$$1, options) { | ||
function lazy(schema, defaults, options) { | ||
if (kindOf(schema) !== 'function') { | ||
if (process.env.NODE_ENV !== 'production') { | ||
throw new Error(`Lazy structs must be defined as an function that returns a schema, but you passed: ${schema}`); | ||
throw new Error("Lazy structs must be defined as an function that returns a schema, but you passed: " + schema); | ||
} else { | ||
throw new Error(`Invalid schema: ${schema}`); | ||
throw new Error("Invalid schema: " + schema); | ||
} | ||
} | ||
let kind; | ||
let struct; | ||
const name = 'lazy'; | ||
const type = `lazy...`; | ||
const compile = value => { | ||
var kind; | ||
var struct; | ||
var name = 'lazy'; | ||
var type = "lazy..."; | ||
var compile = function compile(value) { | ||
struct = schema(); | ||
@@ -633,3 +834,2 @@ kind.name = struct.kind; | ||
} | ||
/** | ||
@@ -643,25 +843,33 @@ * Dynamic. | ||
function dynamic(createSchema, defaults$$1, options) { | ||
function dynamic(createSchema, defaults, options) { | ||
if (kindOf(createSchema) !== 'function') { | ||
if (process.env.NODE_ENV !== 'production') { | ||
throw new Error(`Dynamic structs must be defined as a function, but you passed: ${createSchema}`); | ||
throw new Error("Dynamic structs must be defined as a function, but you passed: " + createSchema); | ||
} else { | ||
throw new Error(`Invalid schema: ${createSchema}`); | ||
throw new Error("Invalid schema: " + createSchema); | ||
} | ||
} | ||
const name = 'dynamic'; | ||
const type = 'dynamic...'; | ||
const validate = (value = resolveDefaults(defaults$$1), data) => { | ||
const schema = createSchema(value, data); | ||
var name = 'dynamic'; | ||
var type = 'dynamic...'; | ||
var validate = function validate(value, data) { | ||
if (value === void 0) { | ||
value = resolveDefaults(defaults); | ||
} | ||
var schema = createSchema(value, data); | ||
if (kindOf(schema) !== 'function') { | ||
if (process.env.NODE_ENV !== 'production') { | ||
throw new Error(`Dynamic structs must return a schema, but you passed: ${schema}`); | ||
throw new Error("Dynamic structs must return a schema, but you passed: " + schema); | ||
} else { | ||
throw new Error(`Invalid schema: ${schema}`); | ||
throw new Error("Invalid schema: " + schema); | ||
} | ||
} | ||
const [error, result] = schema.validate(value); | ||
var _schema$validate = schema.validate(value), | ||
error = _schema$validate[0], | ||
result = _schema$validate[1]; | ||
@@ -677,3 +885,2 @@ if (error) { | ||
} | ||
/** | ||
@@ -687,18 +894,26 @@ * List. | ||
function list(schema, defaults$$1, options) { | ||
function list(schema, defaults, options) { | ||
if (kindOf(schema) !== 'array' || schema.length !== 1) { | ||
if (process.env.NODE_ENV !== 'production') { | ||
throw new Error(`List structs must be defined as an array with a single element, but you passed: ${schema}`); | ||
throw new Error("List structs must be defined as an array with a single element, but you passed: " + schema); | ||
} else { | ||
throw new Error(`Invalid schema: ${schema}`); | ||
throw new Error("Invalid schema: " + schema); | ||
} | ||
} | ||
const array = scalar('array', undefined, options); | ||
const element = any(schema[0], undefined, options); | ||
const name = 'list'; | ||
const type = `[${element.type}]`; | ||
const validate = (value = resolveDefaults(defaults$$1)) => { | ||
const [error, result] = array.validate(value); | ||
var array = scalar('array', undefined, options); | ||
var element = any(schema[0], undefined, options); | ||
var name = 'list'; | ||
var type = "[" + element.type + "]"; | ||
var validate = function validate(value) { | ||
if (value === void 0) { | ||
value = resolveDefaults(defaults); | ||
} | ||
var _array$validate = array.validate(value), | ||
error = _array$validate[0], | ||
result = _array$validate[1]; | ||
if (error) { | ||
@@ -710,12 +925,15 @@ error.type = type; | ||
value = result; | ||
const errors = []; | ||
const ret = []; | ||
var errors = []; | ||
var ret = []; | ||
for (let i = 0; i < value.length; i++) { | ||
const v = value[i]; | ||
const [e, r] = element.validate(v); | ||
var _loop3 = function _loop3(i) { | ||
var v = value[i]; | ||
var _element$validate = element.validate(v), | ||
e = _element$validate[0], | ||
r = _element$validate[1]; | ||
if (e) { | ||
const allE = e.errors || [e]; | ||
allE.forEach(singleE => { | ||
var allE = e.errors || [e]; | ||
allE.forEach(function (singleE) { | ||
singleE.path = [i].concat(singleE.path); | ||
@@ -725,9 +943,17 @@ singleE.data = value; | ||
}); | ||
continue; | ||
return "continue"; | ||
} | ||
ret[i] = r; | ||
}; | ||
for (var i = 0; i < value.length; i++) { | ||
var _ret3 = _loop3(i); | ||
if (_ret3 === "continue") continue; | ||
} | ||
return errors.length ? [_extends({}, errors[0], { errors })] : [undefined, ret]; | ||
return errors.length ? [_objectSpread({}, errors[0], { | ||
errors: errors | ||
})] : [undefined, ret]; | ||
}; | ||
@@ -737,3 +963,2 @@ | ||
} | ||
/** | ||
@@ -747,7 +972,18 @@ * Literal. | ||
function literal(schema, defaults$$1, options) { | ||
const name = 'literal'; | ||
const type = `literal: ${JSON.stringify(schema)}`; | ||
const validate = (value = resolveDefaults(defaults$$1)) => { | ||
return value === schema ? [undefined, value] : [{ data: value, path: [], value, type }]; | ||
function literal(schema, defaults, options) { | ||
var name = 'literal'; | ||
var type = "literal: " + JSON.stringify(schema); | ||
var validate = function validate(value) { | ||
if (value === void 0) { | ||
value = resolveDefaults(defaults); | ||
} | ||
return value === schema ? [undefined, value] : [{ | ||
data: value, | ||
path: [], | ||
value: value, | ||
type: type | ||
}]; | ||
}; | ||
@@ -757,3 +993,2 @@ | ||
} | ||
/** | ||
@@ -767,27 +1002,34 @@ * Object. | ||
function object(schema, defaults$$1, options) { | ||
function object(schema, defaults, options) { | ||
if (kindOf(schema) !== 'object') { | ||
if (process.env.NODE_ENV !== 'production') { | ||
throw new Error(`Object structs must be defined as an object, but you passed: ${schema}`); | ||
throw new Error("Object structs must be defined as an object, but you passed: " + schema); | ||
} else { | ||
throw new Error(`Invalid schema: ${schema}`); | ||
throw new Error("Invalid schema: " + schema); | ||
} | ||
} | ||
const obj = scalar('object', undefined, options); | ||
const ks = []; | ||
const properties = {}; | ||
var obj = scalar('object', undefined, options); | ||
var ks = []; | ||
var properties = {}; | ||
for (const key in schema) { | ||
for (var key in schema) { | ||
ks.push(key); | ||
const s = schema[key]; | ||
const kind = any(s, undefined, options); | ||
var s = schema[key]; | ||
var kind = any(s, undefined, options); | ||
properties[key] = kind; | ||
} | ||
const name = 'object'; | ||
const type = `{${ks.join()}}`; | ||
const validate = (value = resolveDefaults(defaults$$1)) => { | ||
const [error] = obj.validate(value); | ||
var name = 'object'; | ||
var type = "{" + ks.join() + "}"; | ||
var validate = function validate(value) { | ||
if (value === void 0) { | ||
value = resolveDefaults(defaults); | ||
} | ||
var _obj$validate3 = obj.validate(value), | ||
error = _obj$validate3[0]; | ||
if (error) { | ||
@@ -798,14 +1040,13 @@ error.type = type; | ||
const errors = []; | ||
const ret = {}; | ||
const valueKeys = Object.keys(value); | ||
const propertiesKeys = Object.keys(properties); | ||
const keys = new Set(valueKeys.concat(propertiesKeys)); | ||
var errors = []; | ||
var ret = {}; | ||
var valueKeys = Object.keys(value); | ||
var propertiesKeys = Object.keys(properties); | ||
var keys = new Set(valueKeys.concat(propertiesKeys)); | ||
keys.forEach(function (key) { | ||
var v = value[key]; | ||
var kind = properties[key]; | ||
keys.forEach(key => { | ||
let v = value[key]; | ||
const kind = properties[key]; | ||
if (v === undefined) { | ||
const d = defaults$$1 && defaults$$1[key]; | ||
var d = defaults && defaults[key]; | ||
v = resolveDefaults(d, value); | ||
@@ -815,12 +1056,18 @@ } | ||
if (!kind) { | ||
const e = { data: value, path: [key], value: v }; | ||
errors.push(e); | ||
var _e = { | ||
data: value, | ||
path: [key], | ||
value: v | ||
}; | ||
errors.push(_e); | ||
return; | ||
} | ||
const [e, r] = kind.validate(v, value); | ||
var _kind$validate2 = kind.validate(v, value), | ||
e = _kind$validate2[0], | ||
r = _kind$validate2[1]; | ||
if (e) { | ||
const allE = e.errors || [e]; | ||
allE.forEach(singleE => { | ||
var allE = e.errors || [e]; | ||
allE.forEach(function (singleE) { | ||
singleE.path = [key].concat(singleE.path); | ||
@@ -837,4 +1084,5 @@ singleE.data = value; | ||
}); | ||
return errors.length ? [_extends({}, errors[0], { errors })] : [undefined, ret]; | ||
return errors.length ? [_objectSpread({}, errors[0], { | ||
errors: errors | ||
})] : [undefined, ret]; | ||
}; | ||
@@ -844,3 +1092,2 @@ | ||
} | ||
/** | ||
@@ -854,6 +1101,6 @@ * Optional. | ||
function optional(schema, defaults$$1, options) { | ||
return union([schema, 'undefined'], defaults$$1, options); | ||
function optional(schema, defaults, options) { | ||
return union([schema, 'undefined'], defaults, options); | ||
} | ||
/** | ||
@@ -867,27 +1114,34 @@ * Partial. | ||
function partial(schema, defaults$$1, options) { | ||
function partial(schema, defaults, options) { | ||
if (kindOf(schema) !== 'object') { | ||
if (process.env.NODE_ENV !== 'production') { | ||
throw new Error(`Partial structs must be defined as an object, but you passed: ${schema}`); | ||
throw new Error("Partial structs must be defined as an object, but you passed: " + schema); | ||
} else { | ||
throw new Error(`Invalid schema: ${schema}`); | ||
throw new Error("Invalid schema: " + schema); | ||
} | ||
} | ||
const obj = scalar('object', undefined, options); | ||
const ks = []; | ||
const properties = {}; | ||
var obj = scalar('object', undefined, options); | ||
var ks = []; | ||
var properties = {}; | ||
for (const key in schema) { | ||
for (var key in schema) { | ||
ks.push(key); | ||
const s = schema[key]; | ||
const kind = any(s, undefined, options); | ||
var s = schema[key]; | ||
var kind = any(s, undefined, options); | ||
properties[key] = kind; | ||
} | ||
const name = 'partial'; | ||
const type = `{${ks.join()},...}`; | ||
const validate = (value = resolveDefaults(defaults$$1)) => { | ||
const [error] = obj.validate(value); | ||
var name = 'partial'; | ||
var type = "{" + ks.join() + ",...}"; | ||
var validate = function validate(value) { | ||
if (value === void 0) { | ||
value = resolveDefaults(defaults); | ||
} | ||
var _obj$validate4 = obj.validate(value), | ||
error = _obj$validate4[0]; | ||
if (error) { | ||
@@ -898,32 +1152,42 @@ error.type = type; | ||
const errors = []; | ||
const ret = {}; | ||
var errors = []; | ||
var ret = {}; | ||
for (const key in properties) { | ||
let v = value[key]; | ||
const kind = properties[key]; | ||
var _loop4 = function _loop4(_key2) { | ||
var v = value[_key2]; | ||
var kind = properties[_key2]; | ||
if (v === undefined) { | ||
const d = defaults$$1 && defaults$$1[key]; | ||
var d = defaults && defaults[_key2]; | ||
v = resolveDefaults(d, value); | ||
} | ||
const [e, r] = kind.validate(v, value); | ||
var _kind$validate3 = kind.validate(v, value), | ||
e = _kind$validate3[0], | ||
r = _kind$validate3[1]; | ||
if (e) { | ||
const allE = e.errors || [e]; | ||
allE.forEach(singleE => { | ||
singleE.path = [key].concat(singleE.path); | ||
var allE = e.errors || [e]; | ||
allE.forEach(function (singleE) { | ||
singleE.path = [_key2].concat(singleE.path); | ||
singleE.data = value; | ||
errors.push(singleE); | ||
}); | ||
continue; | ||
return "continue"; | ||
} | ||
if (key in value || r !== undefined) { | ||
ret[key] = r; | ||
if (_key2 in value || r !== undefined) { | ||
ret[_key2] = r; | ||
} | ||
}; | ||
for (var _key2 in properties) { | ||
var _ret4 = _loop4(_key2); | ||
if (_ret4 === "continue") continue; | ||
} | ||
return errors.length ? [_extends({}, errors[0], { errors })] : [undefined, ret]; | ||
return errors.length ? [_objectSpread({}, errors[0], { | ||
errors: errors | ||
})] : [undefined, ret]; | ||
}; | ||
@@ -933,3 +1197,2 @@ | ||
} | ||
/** | ||
@@ -943,28 +1206,32 @@ * Scalar. | ||
function scalar(schema, defaults$$1, options) { | ||
function scalar(schema, defaults, options) { | ||
if (kindOf(schema) !== 'string') { | ||
if (process.env.NODE_ENV !== 'production') { | ||
throw new Error(`Scalar structs must be defined as a string, but you passed: ${schema}`); | ||
throw new Error("Scalar structs must be defined as a string, but you passed: " + schema); | ||
} else { | ||
throw new Error(`Invalid schema: ${schema}`); | ||
throw new Error("Invalid schema: " + schema); | ||
} | ||
} | ||
const { types } = options; | ||
const fn = types[schema]; | ||
var types = options.types; | ||
var fn = types[schema]; | ||
if (kindOf(fn) !== 'function') { | ||
if (process.env.NODE_ENV !== 'production') { | ||
throw new Error(`No struct validator function found for type "${schema}".`); | ||
throw new Error("No struct validator function found for type \"" + schema + "\"."); | ||
} else { | ||
throw new Error(`Invalid type: ${schema}`); | ||
throw new Error("Invalid type: " + schema); | ||
} | ||
} | ||
const kind = func(fn, defaults$$1, options); | ||
const name = 'scalar'; | ||
const type = schema; | ||
const validate = value => { | ||
const [error, result] = kind.validate(value); | ||
var kind = func(fn, defaults, options); | ||
var name = 'scalar'; | ||
var type = schema; | ||
var validate = function validate(value) { | ||
var _kind$validate4 = kind.validate(value), | ||
error = _kind$validate4[0], | ||
result = _kind$validate4[1]; | ||
if (error) { | ||
@@ -980,3 +1247,2 @@ error.type = type; | ||
} | ||
/** | ||
@@ -990,18 +1256,29 @@ * Tuple. | ||
function tuple(schema, defaults$$1, options) { | ||
function tuple(schema, defaults, options) { | ||
if (kindOf(schema) !== 'array') { | ||
if (process.env.NODE_ENV !== 'production') { | ||
throw new Error(`Tuple structs must be defined as an array, but you passed: ${schema}`); | ||
throw new Error("Tuple structs must be defined as an array, but you passed: " + schema); | ||
} else { | ||
throw new Error(`Invalid schema: ${schema}`); | ||
throw new Error("Invalid schema: " + schema); | ||
} | ||
} | ||
const kinds = schema.map(s => any(s, undefined, options)); | ||
const array = scalar('array', undefined, options); | ||
const name = 'tuple'; | ||
const type = `[${kinds.map(k => k.type).join()}]`; | ||
const validate = (value = resolveDefaults(defaults$$1)) => { | ||
const [error] = array.validate(value); | ||
var kinds = schema.map(function (s) { | ||
return any(s, undefined, options); | ||
}); | ||
var array = scalar('array', undefined, options); | ||
var name = 'tuple'; | ||
var type = "[" + kinds.map(function (k) { | ||
return k.type; | ||
}).join() + "]"; | ||
var validate = function validate(value) { | ||
if (value === void 0) { | ||
value = resolveDefaults(defaults); | ||
} | ||
var _array$validate2 = array.validate(value), | ||
error = _array$validate2[0]; | ||
if (error) { | ||
@@ -1012,21 +1289,27 @@ error.type = type; | ||
const ret = []; | ||
const errors = []; | ||
const length = Math.max(value.length, kinds.length); | ||
var ret = []; | ||
var errors = []; | ||
var length = Math.max(value.length, kinds.length); | ||
for (let i = 0; i < length; i++) { | ||
const kind = kinds[i]; | ||
const v = value[i]; | ||
var _loop5 = function _loop5(i) { | ||
var kind = kinds[i]; | ||
var v = value[i]; | ||
if (!kind) { | ||
const e = { data: value, path: [i], value: v }; | ||
errors.push(e); | ||
continue; | ||
var _e2 = { | ||
data: value, | ||
path: [i], | ||
value: v | ||
}; | ||
errors.push(_e2); | ||
return "continue"; | ||
} | ||
const [e, r] = kind.validate(v); | ||
var _kind$validate5 = kind.validate(v), | ||
e = _kind$validate5[0], | ||
r = _kind$validate5[1]; | ||
if (e) { | ||
const allE = e.errors || [e]; | ||
allE.forEach(singleE => { | ||
var allE = e.errors || [e]; | ||
allE.forEach(function (singleE) { | ||
singleE.path = [i].concat(singleE.path); | ||
@@ -1036,9 +1319,17 @@ singleE.data = value; | ||
}); | ||
continue; | ||
return "continue"; | ||
} | ||
ret[i] = r; | ||
}; | ||
for (var i = 0; i < length; i++) { | ||
var _ret5 = _loop5(i); | ||
if (_ret5 === "continue") continue; | ||
} | ||
return errors.length ? [_extends({}, errors[0], { errors })] : [undefined, ret]; | ||
return errors.length ? [_objectSpread({}, errors[0], { | ||
errors: errors | ||
})] : [undefined, ret]; | ||
}; | ||
@@ -1048,3 +1339,2 @@ | ||
} | ||
/** | ||
@@ -1058,20 +1348,45 @@ * Union. | ||
function union(schema, defaults$$1, options) { | ||
function union(schema, defaults, options) { | ||
if (kindOf(schema) !== 'array') { | ||
if (process.env.NODE_ENV !== 'production') { | ||
throw new Error(`Union structs must be defined as an array, but you passed: ${schema}`); | ||
throw new Error("Union structs must be defined as an array, but you passed: " + schema); | ||
} else { | ||
throw new Error(`Invalid schema: ${schema}`); | ||
throw new Error("Invalid schema: " + schema); | ||
} | ||
} | ||
const kinds = schema.map(s => any(s, undefined, options)); | ||
const name = 'union'; | ||
const type = kinds.map(k => k.type).join(' | '); | ||
const validate = (value = resolveDefaults(defaults$$1)) => { | ||
const errors = []; | ||
var kinds = schema.map(function (s) { | ||
return any(s, undefined, options); | ||
}); | ||
var name = 'union'; | ||
var type = kinds.map(function (k) { | ||
return k.type; | ||
}).join(' | '); | ||
for (const k of kinds) { | ||
const [e, r] = k.validate(value); | ||
var validate = function validate(value) { | ||
if (value === void 0) { | ||
value = resolveDefaults(defaults); | ||
} | ||
var errors = []; | ||
for (var _iterator = kinds, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) { | ||
var _ref; | ||
if (_isArray) { | ||
if (_i >= _iterator.length) break; | ||
_ref = _iterator[_i++]; | ||
} else { | ||
_i = _iterator.next(); | ||
if (_i.done) break; | ||
_ref = _i.value; | ||
} | ||
var _k2 = _ref; | ||
var _k2$validate = _k2.validate(value), | ||
e = _k2$validate[0], | ||
r = _k2$validate[1]; | ||
if (!e) { | ||
@@ -1083,2 +1398,3 @@ return [undefined, r]; | ||
} | ||
errors[0].type = type; | ||
@@ -1090,3 +1406,2 @@ return errors; | ||
} | ||
/** | ||
@@ -1100,20 +1415,45 @@ * Intersection. | ||
function intersection(schema, defaults$$1, options) { | ||
function intersection(schema, defaults, options) { | ||
if (kindOf(schema) !== 'array') { | ||
if (process.env.NODE_ENV !== 'production') { | ||
throw new Error(`Intersection structs must be defined as an array, but you passed: ${schema}`); | ||
throw new Error("Intersection structs must be defined as an array, but you passed: " + schema); | ||
} else { | ||
throw new Error(`Invalid schema: ${schema}`); | ||
throw new Error("Invalid schema: " + schema); | ||
} | ||
} | ||
const types = schema.map(s => any(s, undefined, options)); | ||
const name = 'intersection'; | ||
const type = types.map(t => t.type).join(' & '); | ||
const validate = (value = resolveDefaults(defaults$$1)) => { | ||
let v = value; | ||
var types = schema.map(function (s) { | ||
return any(s, undefined, options); | ||
}); | ||
var name = 'intersection'; | ||
var type = types.map(function (t) { | ||
return t.type; | ||
}).join(' & '); | ||
for (const t of types) { | ||
const [e, r] = t.validate(v); | ||
var validate = function validate(value) { | ||
if (value === void 0) { | ||
value = resolveDefaults(defaults); | ||
} | ||
var v = value; | ||
for (var _iterator2 = types, _isArray2 = Array.isArray(_iterator2), _i2 = 0, _iterator2 = _isArray2 ? _iterator2 : _iterator2[Symbol.iterator]();;) { | ||
var _ref2; | ||
if (_isArray2) { | ||
if (_i2 >= _iterator2.length) break; | ||
_ref2 = _iterator2[_i2++]; | ||
} else { | ||
_i2 = _iterator2.next(); | ||
if (_i2.done) break; | ||
_ref2 = _i2.value; | ||
} | ||
var t = _ref2; | ||
var _t$validate = t.validate(v), | ||
e = _t$validate[0], | ||
r = _t$validate[1]; | ||
if (e) { | ||
@@ -1132,3 +1472,2 @@ e.type = type; | ||
} | ||
/** | ||
@@ -1140,22 +1479,22 @@ * Kinds. | ||
const Kinds = { | ||
any, | ||
dict, | ||
var Kinds = { | ||
any: any, | ||
dict: dict, | ||
enum: en, | ||
enums, | ||
enums: enums, | ||
function: func, | ||
instance, | ||
instance: instance, | ||
interface: inter, | ||
lazy, | ||
list, | ||
literal, | ||
object, | ||
optional, | ||
partial, | ||
scalar, | ||
tuple, | ||
union, | ||
intersection, | ||
dynamic | ||
lazy: lazy, | ||
list: list, | ||
literal: literal, | ||
object: object, | ||
optional: optional, | ||
partial: partial, | ||
scalar: scalar, | ||
tuple: tuple, | ||
union: union, | ||
intersection: intersection, | ||
dynamic: dynamic | ||
/** | ||
@@ -1175,4 +1514,3 @@ * Export. | ||
const TYPES = ['arguments', 'array', 'boolean', 'buffer', 'error', 'float32array', 'float64array', 'function', 'generatorfunction', 'int16array', 'int32array', 'int8array', 'map', 'null', 'number', 'object', 'promise', 'regexp', 'set', 'string', 'symbol', 'uint16array', 'uint32array', 'uint8array', 'uint8clampedarray', 'undefined', 'weakmap', 'weakset']; | ||
var TYPES = ['arguments', 'array', 'boolean', 'buffer', 'error', 'float32array', 'float64array', 'function', 'generatorfunction', 'int16array', 'int32array', 'int8array', 'map', 'null', 'number', 'object', 'promise', 'regexp', 'set', 'string', 'symbol', 'uint16array', 'uint32array', 'uint8array', 'uint8clampedarray', 'undefined', 'weakmap', 'weakset']; | ||
/** | ||
@@ -1184,10 +1522,12 @@ * The default types that Superstruct ships with. | ||
const Types = { | ||
any: value => value !== undefined | ||
var Types = { | ||
any: function any(value) { | ||
return value !== undefined; | ||
} | ||
}; | ||
TYPES.forEach(type => { | ||
Types[type] = value => kindOf(value) === type; | ||
TYPES.forEach(function (type) { | ||
Types[type] = function (value) { | ||
return kindOf(value) === type; | ||
}; | ||
}); | ||
/** | ||
@@ -1200,3 +1540,5 @@ * Handle the 'date' case specially, to throw out invalid `Date` objects. | ||
Types.date = value => kindOf(value) === 'date' && !isNaN(value); | ||
Types.date = function (value) { | ||
return kindOf(value) === 'date' && !isNaN(value); | ||
}; | ||
@@ -1210,5 +1552,8 @@ /** | ||
function superstruct(config = {}) { | ||
const types = _extends({}, Types, config.types || {}); | ||
function superstruct(config) { | ||
if (config === void 0) { | ||
config = {}; | ||
} | ||
var types = _objectSpread({}, Types, config.types || {}); | ||
/** | ||
@@ -1223,3 +1568,8 @@ * Create a `kind` struct with `schema`, `defaults` and `options`. | ||
function struct(schema, defaults$$1, options = {}) { | ||
function struct(schema, defaults, options) { | ||
if (options === void 0) { | ||
options = {}; | ||
} | ||
if (isStruct(schema)) { | ||
@@ -1229,3 +1579,5 @@ schema = schema.schema; | ||
const kind = Kinds.any(schema, defaults$$1, _extends({}, options, { types })); | ||
var kind = Kinds.any(schema, defaults, _objectSpread({}, options, { | ||
types: types | ||
})); | ||
@@ -1244,13 +1596,18 @@ function Struct(data) { | ||
Object.defineProperty(Struct, IS_STRUCT, { value: true }); | ||
Object.defineProperty(Struct, KIND, { value: kind }); | ||
Object.defineProperty(Struct, IS_STRUCT, { | ||
value: true | ||
}); | ||
Object.defineProperty(Struct, KIND, { | ||
value: kind | ||
}); | ||
Struct.kind = kind.name; | ||
Struct.type = kind.type; | ||
Struct.schema = schema; | ||
Struct.defaults = defaults$$1; | ||
Struct.defaults = defaults; | ||
Struct.options = options; | ||
Struct.assert = value => { | ||
const [error, result] = kind.validate(value); | ||
Struct.assert = function (value) { | ||
var _kind$validate = kind.validate(value), | ||
error = _kind$validate[0], | ||
result = _kind$validate[1]; | ||
@@ -1264,9 +1621,13 @@ if (error) { | ||
Struct.test = value => { | ||
const [error] = kind.validate(value); | ||
Struct.test = function (value) { | ||
var _kind$validate2 = kind.validate(value), | ||
error = _kind$validate2[0]; | ||
return !error; | ||
}; | ||
Struct.validate = value => { | ||
const [error, result] = kind.validate(value); | ||
Struct.validate = function (value) { | ||
var _kind$validate3 = kind.validate(value), | ||
error = _kind$validate3[0], | ||
result = _kind$validate3[1]; | ||
@@ -1282,3 +1643,2 @@ if (error) { | ||
} | ||
/** | ||
@@ -1288,12 +1648,14 @@ * Mix in a factory for each specific kind of struct. | ||
Object.keys(Kinds).forEach(name => { | ||
const kind = Kinds[name]; | ||
struct[name] = (schema, defaults$$1, options) => { | ||
const type = kind(schema, defaults$$1, _extends({}, options, { types })); | ||
const s = struct(type, defaults$$1, options); | ||
Object.keys(Kinds).forEach(function (name) { | ||
var kind = Kinds[name]; | ||
struct[name] = function (schema, defaults, options) { | ||
var type = kind(schema, defaults, _objectSpread({}, options, { | ||
types: types | ||
})); | ||
var s = struct(type, defaults, options); | ||
return s; | ||
}; | ||
}); | ||
/** | ||
@@ -1312,5 +1674,5 @@ * Return the struct factory. | ||
const struct = superstruct(); | ||
var struct = superstruct(); | ||
export { struct, superstruct, isStruct, StructError }; | ||
//# sourceMappingURL=index.es.js.map |
1090
lib/index.js
@@ -5,2 +5,134 @@ 'use strict'; | ||
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; | ||
} | ||
function _objectSpread(target) { | ||
for (var i = 1; i < arguments.length; i++) { | ||
var source = arguments[i] != null ? arguments[i] : {}; | ||
var ownKeys = Object.keys(source); | ||
if (typeof Object.getOwnPropertySymbols === 'function') { | ||
ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) { | ||
return Object.getOwnPropertyDescriptor(source, sym).enumerable; | ||
})); | ||
} | ||
ownKeys.forEach(function (key) { | ||
_defineProperty(target, key, source[key]); | ||
}); | ||
} | ||
return target; | ||
} | ||
function _inheritsLoose(subClass, superClass) { | ||
subClass.prototype = Object.create(superClass.prototype); | ||
subClass.prototype.constructor = subClass; | ||
subClass.__proto__ = superClass; | ||
} | ||
function _getPrototypeOf(o) { | ||
_getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { | ||
return o.__proto__ || Object.getPrototypeOf(o); | ||
}; | ||
return _getPrototypeOf(o); | ||
} | ||
function _setPrototypeOf(o, p) { | ||
_setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { | ||
o.__proto__ = p; | ||
return o; | ||
}; | ||
return _setPrototypeOf(o, p); | ||
} | ||
function isNativeReflectConstruct() { | ||
if (typeof Reflect === "undefined" || !Reflect.construct) return false; | ||
if (Reflect.construct.sham) return false; | ||
if (typeof Proxy === "function") return true; | ||
try { | ||
Date.prototype.toString.call(Reflect.construct(Date, [], function () {})); | ||
return true; | ||
} catch (e) { | ||
return false; | ||
} | ||
} | ||
function _construct(Parent, args, Class) { | ||
if (isNativeReflectConstruct()) { | ||
_construct = Reflect.construct; | ||
} else { | ||
_construct = function _construct(Parent, args, Class) { | ||
var a = [null]; | ||
a.push.apply(a, args); | ||
var Constructor = Function.bind.apply(Parent, a); | ||
var instance = new Constructor(); | ||
if (Class) _setPrototypeOf(instance, Class.prototype); | ||
return instance; | ||
}; | ||
} | ||
return _construct.apply(null, arguments); | ||
} | ||
function _isNativeFunction(fn) { | ||
return Function.toString.call(fn).indexOf("[native code]") !== -1; | ||
} | ||
function _wrapNativeSuper(Class) { | ||
var _cache = typeof Map === "function" ? new Map() : undefined; | ||
_wrapNativeSuper = function _wrapNativeSuper(Class) { | ||
if (Class === null || !_isNativeFunction(Class)) return Class; | ||
if (typeof Class !== "function") { | ||
throw new TypeError("Super expression must either be null or a function"); | ||
} | ||
if (typeof _cache !== "undefined") { | ||
if (_cache.has(Class)) return _cache.get(Class); | ||
_cache.set(Class, Wrapper); | ||
} | ||
function Wrapper() { | ||
return _construct(Class, arguments, _getPrototypeOf(this).constructor); | ||
} | ||
Wrapper.prototype = Object.create(Class.prototype, { | ||
constructor: { | ||
value: Wrapper, | ||
enumerable: false, | ||
writable: true, | ||
configurable: true | ||
} | ||
}); | ||
return _setPrototypeOf(Wrapper, Class); | ||
}; | ||
return _wrapNativeSuper(Class); | ||
} | ||
function _assertThisInitialized(self) { | ||
if (self === void 0) { | ||
throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); | ||
} | ||
return self; | ||
} | ||
/** | ||
@@ -11,34 +143,50 @@ * Define a struct error. | ||
*/ | ||
var StructError = | ||
/*#__PURE__*/ | ||
function (_TypeError) { | ||
_inheritsLoose(StructError, _TypeError); | ||
class StructError extends TypeError { | ||
static format(attrs) { | ||
const { type, path, value } = attrs; | ||
const message = `Expected a value of type \`${type}\`${path.length ? ` for \`${path.join('.')}\`` : ''} but received \`${JSON.stringify(value)}\`.`; | ||
StructError.format = function format(attrs) { | ||
var type = attrs.type, | ||
path = attrs.path, | ||
value = attrs.value; | ||
var message = "Expected a value of type `" + type + "`" + (path.length ? " for `" + path.join('.') + "`" : '') + " but received `" + JSON.stringify(value) + "`."; | ||
return message; | ||
} | ||
}; | ||
constructor(attrs) { | ||
const message = StructError.format(attrs); | ||
super(message); | ||
function StructError(attrs) { | ||
var _this; | ||
const { data, path, value, reason, type, errors = [] } = attrs; | ||
this.data = data; | ||
this.path = path; | ||
this.value = value; | ||
this.reason = reason; | ||
this.type = type; | ||
this.errors = errors; | ||
var message = StructError.format(attrs); | ||
_this = _TypeError.call(this, message) || this; | ||
var data = attrs.data, | ||
path = attrs.path, | ||
value = attrs.value, | ||
reason = attrs.reason, | ||
type = attrs.type, | ||
_attrs$errors = attrs.errors, | ||
errors = _attrs$errors === void 0 ? [] : _attrs$errors; | ||
_this.data = data; | ||
_this.path = path; | ||
_this.value = value; | ||
_this.reason = reason; | ||
_this.type = type; | ||
_this.errors = errors; | ||
if (!errors.length) { | ||
errors.push(this); | ||
errors.push(_assertThisInitialized(_this)); | ||
} | ||
if (Error.captureStackTrace) { | ||
Error.captureStackTrace(this, this.constructor); | ||
Error.captureStackTrace(_assertThisInitialized(_this), _this.constructor); | ||
} else { | ||
this.stack = new Error().stack; | ||
_this.stack = new Error().stack; | ||
} | ||
return _this; | ||
} | ||
} | ||
return StructError; | ||
}(_wrapNativeSuper(TypeError)); | ||
var toString = Object.prototype.toString; | ||
@@ -179,5 +327,3 @@ | ||
*/ | ||
const IS_STRUCT = '@@__STRUCT__@@'; | ||
var IS_STRUCT = '@@__STRUCT__@@'; | ||
/** | ||
@@ -189,3 +335,3 @@ * A private string to refer to a struct's kind. | ||
const KIND = '@@__KIND__@@'; | ||
var KIND = '@@__KIND__@@'; | ||
@@ -202,3 +348,2 @@ /** | ||
} | ||
/** | ||
@@ -216,16 +361,2 @@ * Resolve `defaults`, for an optional `value`. | ||
var _extends = Object.assign || function (target) { | ||
for (var i = 1; i < arguments.length; i++) { | ||
var source = arguments[i]; | ||
for (var key in source) { | ||
if (Object.prototype.hasOwnProperty.call(source, key)) { | ||
target[key] = source[key]; | ||
} | ||
} | ||
} | ||
return target; | ||
}; | ||
/** | ||
@@ -237,10 +368,7 @@ * Kind. | ||
class Kind { | ||
constructor(name, type, validate) { | ||
this.name = name; | ||
this.type = type; | ||
this.validate = validate; | ||
} | ||
} | ||
var Kind = function Kind(name, type, validate) { | ||
this.name = name; | ||
this.type = type; | ||
this.validate = validate; | ||
}; | ||
/** | ||
@@ -254,3 +382,4 @@ * Any. | ||
function any(schema, defaults$$1, options) { | ||
function any(schema, defaults, options) { | ||
if (isStruct(schema)) { | ||
@@ -267,3 +396,3 @@ return schema[KIND]; | ||
{ | ||
return schema.length > 1 ? tuple(schema, defaults$$1, options) : list(schema, defaults$$1, options); | ||
return schema.length > 1 ? tuple(schema, defaults, options) : list(schema, defaults, options); | ||
} | ||
@@ -273,3 +402,3 @@ | ||
{ | ||
return func(schema, defaults$$1, options); | ||
return func(schema, defaults, options); | ||
} | ||
@@ -279,3 +408,3 @@ | ||
{ | ||
return object(schema, defaults$$1, options); | ||
return object(schema, defaults, options); | ||
} | ||
@@ -285,4 +414,4 @@ | ||
{ | ||
let required = true; | ||
let type; | ||
var required = true; | ||
var type; | ||
@@ -295,9 +424,10 @@ if (schema.endsWith('?')) { | ||
if (schema.includes('|')) { | ||
const scalars = schema.split(/\s*\|\s*/g); | ||
type = union(scalars, defaults$$1, options); | ||
var scalars = schema.split(/\s*\|\s*/g); | ||
type = union(scalars, defaults, options); | ||
} else if (schema.includes('&')) { | ||
const scalars = schema.split(/\s*&\s*/g); | ||
type = intersection(scalars, defaults$$1, options); | ||
var _scalars = schema.split(/\s*&\s*/g); | ||
type = intersection(_scalars, defaults, options); | ||
} else { | ||
type = scalar(schema, defaults$$1, options); | ||
type = scalar(schema, defaults, options); | ||
} | ||
@@ -314,8 +444,7 @@ | ||
if (process.env.NODE_ENV !== 'production') { | ||
throw new Error(`A schema definition must be an object, array, string or function, but you passed: ${schema}`); | ||
throw new Error("A schema definition must be an object, array, string or function, but you passed: " + schema); | ||
} else { | ||
throw new Error(`Invalid schema: ${schema}`); | ||
throw new Error("Invalid schema: " + schema); | ||
} | ||
} | ||
/** | ||
@@ -329,21 +458,25 @@ * Dict. | ||
function dict(schema, defaults$$1, options) { | ||
function dict(schema, defaults, options) { | ||
if (kindOf(schema) !== 'array' || schema.length !== 2) { | ||
if (process.env.NODE_ENV !== 'production') { | ||
throw new Error(`Dict structs must be defined as an array with two elements, but you passed: ${schema}`); | ||
throw new Error("Dict structs must be defined as an array with two elements, but you passed: " + schema); | ||
} else { | ||
throw new Error(`Invalid schema: ${schema}`); | ||
throw new Error("Invalid schema: " + schema); | ||
} | ||
} | ||
const obj = scalar('object', undefined, options); | ||
const keys = any(schema[0], undefined, options); | ||
const values = any(schema[1], undefined, options); | ||
const name = 'dict'; | ||
const type = `dict<${keys.type},${values.type}>`; | ||
const validate = value => { | ||
const resolved = resolveDefaults(defaults$$1); | ||
value = resolved ? _extends({}, resolved, value) : value; | ||
const [error] = obj.validate(value); | ||
var obj = scalar('object', undefined, options); | ||
var keys = any(schema[0], undefined, options); | ||
var values = any(schema[1], undefined, options); | ||
var name = 'dict'; | ||
var type = "dict<" + keys.type + "," + values.type + ">"; | ||
var validate = function validate(value) { | ||
var resolved = resolveDefaults(defaults); | ||
value = resolved ? _objectSpread({}, resolved, value) : value; | ||
var _obj$validate = obj.validate(value), | ||
error = _obj$validate[0]; | ||
if (error) { | ||
@@ -354,36 +487,53 @@ error.type = type; | ||
const ret = {}; | ||
const errors = []; | ||
var ret = {}; | ||
var errors = []; | ||
for (let k in value) { | ||
const v = value[k]; | ||
const [e, r] = keys.validate(k); | ||
var _loop = function _loop(_k) { | ||
var v = value[_k]; | ||
var _keys$validate = keys.validate(_k), | ||
e = _keys$validate[0], | ||
r = _keys$validate[1]; | ||
if (e) { | ||
const allE = e.errors || [e]; | ||
allE.forEach(singleE => { | ||
singleE.path = [k].concat(singleE.path); | ||
var allE = e.errors || [e]; | ||
allE.forEach(function (singleE) { | ||
singleE.path = [_k].concat(singleE.path); | ||
singleE.data = value; | ||
errors.push(singleE); | ||
}); | ||
continue; | ||
k = _k; | ||
return "continue"; | ||
} | ||
k = r; | ||
const [e2, r2] = values.validate(v); | ||
_k = r; | ||
var _values$validate = values.validate(v), | ||
e2 = _values$validate[0], | ||
r2 = _values$validate[1]; | ||
if (e2) { | ||
const allE2 = e2.errors || [e2]; | ||
allE2.forEach(singleE => { | ||
singleE.path = [k].concat(singleE.path); | ||
var allE2 = e2.errors || [e2]; | ||
allE2.forEach(function (singleE) { | ||
singleE.path = [_k].concat(singleE.path); | ||
singleE.data = value; | ||
errors.push(singleE); | ||
}); | ||
continue; | ||
k = _k; | ||
return "continue"; | ||
} | ||
ret[k] = r2; | ||
ret[_k] = r2; | ||
k = _k; | ||
}; | ||
for (var k in value) { | ||
var _ret = _loop(k); | ||
if (_ret === "continue") continue; | ||
} | ||
return errors.length ? [_extends({}, errors[0], { errors })] : [undefined, ret]; | ||
return errors.length ? [_objectSpread({}, errors[0], { | ||
errors: errors | ||
})] : [undefined, ret]; | ||
}; | ||
@@ -393,3 +543,2 @@ | ||
} | ||
/** | ||
@@ -403,13 +552,14 @@ * Enum. | ||
function en(schema, defaults$$1, options) { | ||
function en(schema, defaults, options) { | ||
if (kindOf(schema) !== 'array') { | ||
if (process.env.NODE_ENV !== 'production') { | ||
throw new Error(`Enum structs must be defined as an array, but you passed: ${schema}`); | ||
throw new Error("Enum structs must be defined as an array, but you passed: " + schema); | ||
} else { | ||
throw new Error(`Invalid schema: ${schema}`); | ||
throw new Error("Invalid schema: " + schema); | ||
} | ||
} | ||
const name = 'enum'; | ||
const type = schema.map(s => { | ||
var name = 'enum'; | ||
var type = schema.map(function (s) { | ||
try { | ||
@@ -422,4 +572,13 @@ return JSON.stringify(s); | ||
const validate = (value = resolveDefaults(defaults$$1)) => { | ||
return schema.includes(value) ? [undefined, value] : [{ data: value, path: [], value, type }]; | ||
var validate = function validate(value) { | ||
if (value === void 0) { | ||
value = resolveDefaults(defaults); | ||
} | ||
return schema.includes(value) ? [undefined, value] : [{ | ||
data: value, | ||
path: [], | ||
value: value, | ||
type: type | ||
}]; | ||
}; | ||
@@ -429,3 +588,2 @@ | ||
} | ||
/** | ||
@@ -439,8 +597,8 @@ * Enums. | ||
function enums(schema, defaults$$1, options) { | ||
const e = en(schema, undefined, options); | ||
const l = list([e], defaults$$1, options); | ||
function enums(schema, defaults, options) { | ||
var e = en(schema, undefined, options); | ||
var l = list([e], defaults, options); | ||
return l; | ||
} | ||
/** | ||
@@ -454,18 +612,27 @@ * Function. | ||
function func(schema, defaults$$1, options) { | ||
function func(schema, defaults, options) { | ||
if (kindOf(schema) !== 'function') { | ||
if (process.env.NODE_ENV !== 'production') { | ||
throw new Error(`Function structs must be defined as a function, but you passed: ${schema}`); | ||
throw new Error("Function structs must be defined as a function, but you passed: " + schema); | ||
} else { | ||
throw new Error(`Invalid schema: ${schema}`); | ||
throw new Error("Invalid schema: " + schema); | ||
} | ||
} | ||
const name = 'function'; | ||
const type = '<function>'; | ||
const validate = (value = resolveDefaults(defaults$$1), data) => { | ||
const result = schema(value, data); | ||
let failure = { path: [], reason: null }; | ||
let isValid; | ||
var name = 'function'; | ||
var type = '<function>'; | ||
var validate = function validate(value, data) { | ||
if (value === void 0) { | ||
value = resolveDefaults(defaults); | ||
} | ||
var result = schema(value, data); | ||
var failure = { | ||
path: [], | ||
reason: null | ||
}; | ||
var isValid; | ||
switch (kindOf(result)) { | ||
@@ -477,2 +644,3 @@ case 'boolean': | ||
} | ||
case 'string': | ||
@@ -484,14 +652,16 @@ { | ||
} | ||
case 'object': | ||
{ | ||
isValid = false; | ||
failure = _extends({}, failure, result); | ||
failure = _objectSpread({}, failure, result); | ||
break; | ||
} | ||
default: | ||
{ | ||
if (process.env.NODE_ENV !== 'production') { | ||
throw new Error(`Validator functions must return a boolean, an error reason string or an error reason object, but you passed: ${schema}`); | ||
throw new Error("Validator functions must return a boolean, an error reason string or an error reason object, but you passed: " + schema); | ||
} else { | ||
throw new Error(`Invalid result: ${result}`); | ||
throw new Error("Invalid result: " + result); | ||
} | ||
@@ -501,3 +671,7 @@ } | ||
return isValid ? [undefined, value] : [_extends({ type, value, data: value }, failure)]; | ||
return isValid ? [undefined, value] : [_objectSpread({ | ||
type: type, | ||
value: value, | ||
data: value | ||
}, failure)]; | ||
}; | ||
@@ -507,3 +681,2 @@ | ||
} | ||
/** | ||
@@ -517,7 +690,18 @@ * Instance. | ||
function instance(schema, defaults$$1, options) { | ||
const name = 'instance'; | ||
const type = `instance<${schema.name}>`; | ||
const validate = (value = resolveDefaults(defaults$$1)) => { | ||
return value instanceof schema ? [undefined, value] : [{ data: value, path: [], value, type }]; | ||
function instance(schema, defaults, options) { | ||
var name = 'instance'; | ||
var type = "instance<" + schema.name + ">"; | ||
var validate = function validate(value) { | ||
if (value === void 0) { | ||
value = resolveDefaults(defaults); | ||
} | ||
return value instanceof schema ? [undefined, value] : [{ | ||
data: value, | ||
path: [], | ||
value: value, | ||
type: type | ||
}]; | ||
}; | ||
@@ -527,3 +711,2 @@ | ||
} | ||
/** | ||
@@ -537,27 +720,34 @@ * Interface. | ||
function inter(schema, defaults$$1, options) { | ||
function inter(schema, defaults, options) { | ||
if (kindOf(schema) !== 'object') { | ||
if (process.env.NODE_ENV !== 'production') { | ||
throw new Error(`Interface structs must be defined as an object, but you passed: ${schema}`); | ||
throw new Error("Interface structs must be defined as an object, but you passed: " + schema); | ||
} else { | ||
throw new Error(`Invalid schema: ${schema}`); | ||
throw new Error("Invalid schema: " + schema); | ||
} | ||
} | ||
const obj = scalar('object', undefined, options); | ||
const ks = []; | ||
const properties = {}; | ||
var obj = scalar('object', undefined, options); | ||
var ks = []; | ||
var properties = {}; | ||
for (const key in schema) { | ||
for (var key in schema) { | ||
ks.push(key); | ||
const s = schema[key]; | ||
const kind = any(s, undefined, options); | ||
var s = schema[key]; | ||
var kind = any(s, undefined, options); | ||
properties[key] = kind; | ||
} | ||
const name = 'interface'; | ||
const type = `{${ks.join()}}`; | ||
const validate = (value = resolveDefaults(defaults$$1)) => { | ||
const [error] = obj.validate(value); | ||
var name = 'interface'; | ||
var type = "{" + ks.join() + "}"; | ||
var validate = function validate(value) { | ||
if (value === void 0) { | ||
value = resolveDefaults(defaults); | ||
} | ||
var _obj$validate2 = obj.validate(value), | ||
error = _obj$validate2[0]; | ||
if (error) { | ||
@@ -568,32 +758,42 @@ error.type = type; | ||
const errors = []; | ||
const ret = value; | ||
var errors = []; | ||
var ret = value; | ||
for (const key in properties) { | ||
let v = value[key]; | ||
const kind = properties[key]; | ||
var _loop2 = function _loop2(_key) { | ||
var v = value[_key]; | ||
var kind = properties[_key]; | ||
if (v === undefined) { | ||
const d = defaults$$1 && defaults$$1[key]; | ||
var d = defaults && defaults[_key]; | ||
v = resolveDefaults(d, value); | ||
} | ||
const [e, r] = kind.validate(v, value); | ||
var _kind$validate = kind.validate(v, value), | ||
e = _kind$validate[0], | ||
r = _kind$validate[1]; | ||
if (e) { | ||
const allE = e.errors || [e]; | ||
allE.forEach(singleE => { | ||
singleE.path = [key].concat(singleE.path); | ||
var allE = e.errors || [e]; | ||
allE.forEach(function (singleE) { | ||
singleE.path = [_key].concat(singleE.path); | ||
singleE.data = value; | ||
errors.push(singleE); | ||
}); | ||
continue; | ||
return "continue"; | ||
} | ||
if (key in value || r !== undefined) { | ||
ret[key] = r; | ||
if (_key in value || r !== undefined) { | ||
ret[_key] = r; | ||
} | ||
}; | ||
for (var _key in properties) { | ||
var _ret2 = _loop2(_key); | ||
if (_ret2 === "continue") continue; | ||
} | ||
return errors.length ? [_extends({}, errors[0], { errors })] : [undefined, ret]; | ||
return errors.length ? [_objectSpread({}, errors[0], { | ||
errors: errors | ||
})] : [undefined, ret]; | ||
}; | ||
@@ -603,3 +803,2 @@ | ||
} | ||
/** | ||
@@ -613,16 +812,18 @@ * Lazy. | ||
function lazy(schema, defaults$$1, options) { | ||
function lazy(schema, defaults, options) { | ||
if (kindOf(schema) !== 'function') { | ||
if (process.env.NODE_ENV !== 'production') { | ||
throw new Error(`Lazy structs must be defined as an function that returns a schema, but you passed: ${schema}`); | ||
throw new Error("Lazy structs must be defined as an function that returns a schema, but you passed: " + schema); | ||
} else { | ||
throw new Error(`Invalid schema: ${schema}`); | ||
throw new Error("Invalid schema: " + schema); | ||
} | ||
} | ||
let kind; | ||
let struct; | ||
const name = 'lazy'; | ||
const type = `lazy...`; | ||
const compile = value => { | ||
var kind; | ||
var struct; | ||
var name = 'lazy'; | ||
var type = "lazy..."; | ||
var compile = function compile(value) { | ||
struct = schema(); | ||
@@ -638,3 +839,2 @@ kind.name = struct.kind; | ||
} | ||
/** | ||
@@ -648,25 +848,33 @@ * Dynamic. | ||
function dynamic(createSchema, defaults$$1, options) { | ||
function dynamic(createSchema, defaults, options) { | ||
if (kindOf(createSchema) !== 'function') { | ||
if (process.env.NODE_ENV !== 'production') { | ||
throw new Error(`Dynamic structs must be defined as a function, but you passed: ${createSchema}`); | ||
throw new Error("Dynamic structs must be defined as a function, but you passed: " + createSchema); | ||
} else { | ||
throw new Error(`Invalid schema: ${createSchema}`); | ||
throw new Error("Invalid schema: " + createSchema); | ||
} | ||
} | ||
const name = 'dynamic'; | ||
const type = 'dynamic...'; | ||
const validate = (value = resolveDefaults(defaults$$1), data) => { | ||
const schema = createSchema(value, data); | ||
var name = 'dynamic'; | ||
var type = 'dynamic...'; | ||
var validate = function validate(value, data) { | ||
if (value === void 0) { | ||
value = resolveDefaults(defaults); | ||
} | ||
var schema = createSchema(value, data); | ||
if (kindOf(schema) !== 'function') { | ||
if (process.env.NODE_ENV !== 'production') { | ||
throw new Error(`Dynamic structs must return a schema, but you passed: ${schema}`); | ||
throw new Error("Dynamic structs must return a schema, but you passed: " + schema); | ||
} else { | ||
throw new Error(`Invalid schema: ${schema}`); | ||
throw new Error("Invalid schema: " + schema); | ||
} | ||
} | ||
const [error, result] = schema.validate(value); | ||
var _schema$validate = schema.validate(value), | ||
error = _schema$validate[0], | ||
result = _schema$validate[1]; | ||
@@ -682,3 +890,2 @@ if (error) { | ||
} | ||
/** | ||
@@ -692,18 +899,26 @@ * List. | ||
function list(schema, defaults$$1, options) { | ||
function list(schema, defaults, options) { | ||
if (kindOf(schema) !== 'array' || schema.length !== 1) { | ||
if (process.env.NODE_ENV !== 'production') { | ||
throw new Error(`List structs must be defined as an array with a single element, but you passed: ${schema}`); | ||
throw new Error("List structs must be defined as an array with a single element, but you passed: " + schema); | ||
} else { | ||
throw new Error(`Invalid schema: ${schema}`); | ||
throw new Error("Invalid schema: " + schema); | ||
} | ||
} | ||
const array = scalar('array', undefined, options); | ||
const element = any(schema[0], undefined, options); | ||
const name = 'list'; | ||
const type = `[${element.type}]`; | ||
const validate = (value = resolveDefaults(defaults$$1)) => { | ||
const [error, result] = array.validate(value); | ||
var array = scalar('array', undefined, options); | ||
var element = any(schema[0], undefined, options); | ||
var name = 'list'; | ||
var type = "[" + element.type + "]"; | ||
var validate = function validate(value) { | ||
if (value === void 0) { | ||
value = resolveDefaults(defaults); | ||
} | ||
var _array$validate = array.validate(value), | ||
error = _array$validate[0], | ||
result = _array$validate[1]; | ||
if (error) { | ||
@@ -715,12 +930,15 @@ error.type = type; | ||
value = result; | ||
const errors = []; | ||
const ret = []; | ||
var errors = []; | ||
var ret = []; | ||
for (let i = 0; i < value.length; i++) { | ||
const v = value[i]; | ||
const [e, r] = element.validate(v); | ||
var _loop3 = function _loop3(i) { | ||
var v = value[i]; | ||
var _element$validate = element.validate(v), | ||
e = _element$validate[0], | ||
r = _element$validate[1]; | ||
if (e) { | ||
const allE = e.errors || [e]; | ||
allE.forEach(singleE => { | ||
var allE = e.errors || [e]; | ||
allE.forEach(function (singleE) { | ||
singleE.path = [i].concat(singleE.path); | ||
@@ -730,9 +948,17 @@ singleE.data = value; | ||
}); | ||
continue; | ||
return "continue"; | ||
} | ||
ret[i] = r; | ||
}; | ||
for (var i = 0; i < value.length; i++) { | ||
var _ret3 = _loop3(i); | ||
if (_ret3 === "continue") continue; | ||
} | ||
return errors.length ? [_extends({}, errors[0], { errors })] : [undefined, ret]; | ||
return errors.length ? [_objectSpread({}, errors[0], { | ||
errors: errors | ||
})] : [undefined, ret]; | ||
}; | ||
@@ -742,3 +968,2 @@ | ||
} | ||
/** | ||
@@ -752,7 +977,18 @@ * Literal. | ||
function literal(schema, defaults$$1, options) { | ||
const name = 'literal'; | ||
const type = `literal: ${JSON.stringify(schema)}`; | ||
const validate = (value = resolveDefaults(defaults$$1)) => { | ||
return value === schema ? [undefined, value] : [{ data: value, path: [], value, type }]; | ||
function literal(schema, defaults, options) { | ||
var name = 'literal'; | ||
var type = "literal: " + JSON.stringify(schema); | ||
var validate = function validate(value) { | ||
if (value === void 0) { | ||
value = resolveDefaults(defaults); | ||
} | ||
return value === schema ? [undefined, value] : [{ | ||
data: value, | ||
path: [], | ||
value: value, | ||
type: type | ||
}]; | ||
}; | ||
@@ -762,3 +998,2 @@ | ||
} | ||
/** | ||
@@ -772,27 +1007,34 @@ * Object. | ||
function object(schema, defaults$$1, options) { | ||
function object(schema, defaults, options) { | ||
if (kindOf(schema) !== 'object') { | ||
if (process.env.NODE_ENV !== 'production') { | ||
throw new Error(`Object structs must be defined as an object, but you passed: ${schema}`); | ||
throw new Error("Object structs must be defined as an object, but you passed: " + schema); | ||
} else { | ||
throw new Error(`Invalid schema: ${schema}`); | ||
throw new Error("Invalid schema: " + schema); | ||
} | ||
} | ||
const obj = scalar('object', undefined, options); | ||
const ks = []; | ||
const properties = {}; | ||
var obj = scalar('object', undefined, options); | ||
var ks = []; | ||
var properties = {}; | ||
for (const key in schema) { | ||
for (var key in schema) { | ||
ks.push(key); | ||
const s = schema[key]; | ||
const kind = any(s, undefined, options); | ||
var s = schema[key]; | ||
var kind = any(s, undefined, options); | ||
properties[key] = kind; | ||
} | ||
const name = 'object'; | ||
const type = `{${ks.join()}}`; | ||
const validate = (value = resolveDefaults(defaults$$1)) => { | ||
const [error] = obj.validate(value); | ||
var name = 'object'; | ||
var type = "{" + ks.join() + "}"; | ||
var validate = function validate(value) { | ||
if (value === void 0) { | ||
value = resolveDefaults(defaults); | ||
} | ||
var _obj$validate3 = obj.validate(value), | ||
error = _obj$validate3[0]; | ||
if (error) { | ||
@@ -803,14 +1045,13 @@ error.type = type; | ||
const errors = []; | ||
const ret = {}; | ||
const valueKeys = Object.keys(value); | ||
const propertiesKeys = Object.keys(properties); | ||
const keys = new Set(valueKeys.concat(propertiesKeys)); | ||
var errors = []; | ||
var ret = {}; | ||
var valueKeys = Object.keys(value); | ||
var propertiesKeys = Object.keys(properties); | ||
var keys = new Set(valueKeys.concat(propertiesKeys)); | ||
keys.forEach(function (key) { | ||
var v = value[key]; | ||
var kind = properties[key]; | ||
keys.forEach(key => { | ||
let v = value[key]; | ||
const kind = properties[key]; | ||
if (v === undefined) { | ||
const d = defaults$$1 && defaults$$1[key]; | ||
var d = defaults && defaults[key]; | ||
v = resolveDefaults(d, value); | ||
@@ -820,12 +1061,18 @@ } | ||
if (!kind) { | ||
const e = { data: value, path: [key], value: v }; | ||
errors.push(e); | ||
var _e = { | ||
data: value, | ||
path: [key], | ||
value: v | ||
}; | ||
errors.push(_e); | ||
return; | ||
} | ||
const [e, r] = kind.validate(v, value); | ||
var _kind$validate2 = kind.validate(v, value), | ||
e = _kind$validate2[0], | ||
r = _kind$validate2[1]; | ||
if (e) { | ||
const allE = e.errors || [e]; | ||
allE.forEach(singleE => { | ||
var allE = e.errors || [e]; | ||
allE.forEach(function (singleE) { | ||
singleE.path = [key].concat(singleE.path); | ||
@@ -842,4 +1089,5 @@ singleE.data = value; | ||
}); | ||
return errors.length ? [_extends({}, errors[0], { errors })] : [undefined, ret]; | ||
return errors.length ? [_objectSpread({}, errors[0], { | ||
errors: errors | ||
})] : [undefined, ret]; | ||
}; | ||
@@ -849,3 +1097,2 @@ | ||
} | ||
/** | ||
@@ -859,6 +1106,6 @@ * Optional. | ||
function optional(schema, defaults$$1, options) { | ||
return union([schema, 'undefined'], defaults$$1, options); | ||
function optional(schema, defaults, options) { | ||
return union([schema, 'undefined'], defaults, options); | ||
} | ||
/** | ||
@@ -872,27 +1119,34 @@ * Partial. | ||
function partial(schema, defaults$$1, options) { | ||
function partial(schema, defaults, options) { | ||
if (kindOf(schema) !== 'object') { | ||
if (process.env.NODE_ENV !== 'production') { | ||
throw new Error(`Partial structs must be defined as an object, but you passed: ${schema}`); | ||
throw new Error("Partial structs must be defined as an object, but you passed: " + schema); | ||
} else { | ||
throw new Error(`Invalid schema: ${schema}`); | ||
throw new Error("Invalid schema: " + schema); | ||
} | ||
} | ||
const obj = scalar('object', undefined, options); | ||
const ks = []; | ||
const properties = {}; | ||
var obj = scalar('object', undefined, options); | ||
var ks = []; | ||
var properties = {}; | ||
for (const key in schema) { | ||
for (var key in schema) { | ||
ks.push(key); | ||
const s = schema[key]; | ||
const kind = any(s, undefined, options); | ||
var s = schema[key]; | ||
var kind = any(s, undefined, options); | ||
properties[key] = kind; | ||
} | ||
const name = 'partial'; | ||
const type = `{${ks.join()},...}`; | ||
const validate = (value = resolveDefaults(defaults$$1)) => { | ||
const [error] = obj.validate(value); | ||
var name = 'partial'; | ||
var type = "{" + ks.join() + ",...}"; | ||
var validate = function validate(value) { | ||
if (value === void 0) { | ||
value = resolveDefaults(defaults); | ||
} | ||
var _obj$validate4 = obj.validate(value), | ||
error = _obj$validate4[0]; | ||
if (error) { | ||
@@ -903,32 +1157,42 @@ error.type = type; | ||
const errors = []; | ||
const ret = {}; | ||
var errors = []; | ||
var ret = {}; | ||
for (const key in properties) { | ||
let v = value[key]; | ||
const kind = properties[key]; | ||
var _loop4 = function _loop4(_key2) { | ||
var v = value[_key2]; | ||
var kind = properties[_key2]; | ||
if (v === undefined) { | ||
const d = defaults$$1 && defaults$$1[key]; | ||
var d = defaults && defaults[_key2]; | ||
v = resolveDefaults(d, value); | ||
} | ||
const [e, r] = kind.validate(v, value); | ||
var _kind$validate3 = kind.validate(v, value), | ||
e = _kind$validate3[0], | ||
r = _kind$validate3[1]; | ||
if (e) { | ||
const allE = e.errors || [e]; | ||
allE.forEach(singleE => { | ||
singleE.path = [key].concat(singleE.path); | ||
var allE = e.errors || [e]; | ||
allE.forEach(function (singleE) { | ||
singleE.path = [_key2].concat(singleE.path); | ||
singleE.data = value; | ||
errors.push(singleE); | ||
}); | ||
continue; | ||
return "continue"; | ||
} | ||
if (key in value || r !== undefined) { | ||
ret[key] = r; | ||
if (_key2 in value || r !== undefined) { | ||
ret[_key2] = r; | ||
} | ||
}; | ||
for (var _key2 in properties) { | ||
var _ret4 = _loop4(_key2); | ||
if (_ret4 === "continue") continue; | ||
} | ||
return errors.length ? [_extends({}, errors[0], { errors })] : [undefined, ret]; | ||
return errors.length ? [_objectSpread({}, errors[0], { | ||
errors: errors | ||
})] : [undefined, ret]; | ||
}; | ||
@@ -938,3 +1202,2 @@ | ||
} | ||
/** | ||
@@ -948,28 +1211,32 @@ * Scalar. | ||
function scalar(schema, defaults$$1, options) { | ||
function scalar(schema, defaults, options) { | ||
if (kindOf(schema) !== 'string') { | ||
if (process.env.NODE_ENV !== 'production') { | ||
throw new Error(`Scalar structs must be defined as a string, but you passed: ${schema}`); | ||
throw new Error("Scalar structs must be defined as a string, but you passed: " + schema); | ||
} else { | ||
throw new Error(`Invalid schema: ${schema}`); | ||
throw new Error("Invalid schema: " + schema); | ||
} | ||
} | ||
const { types } = options; | ||
const fn = types[schema]; | ||
var types = options.types; | ||
var fn = types[schema]; | ||
if (kindOf(fn) !== 'function') { | ||
if (process.env.NODE_ENV !== 'production') { | ||
throw new Error(`No struct validator function found for type "${schema}".`); | ||
throw new Error("No struct validator function found for type \"" + schema + "\"."); | ||
} else { | ||
throw new Error(`Invalid type: ${schema}`); | ||
throw new Error("Invalid type: " + schema); | ||
} | ||
} | ||
const kind = func(fn, defaults$$1, options); | ||
const name = 'scalar'; | ||
const type = schema; | ||
const validate = value => { | ||
const [error, result] = kind.validate(value); | ||
var kind = func(fn, defaults, options); | ||
var name = 'scalar'; | ||
var type = schema; | ||
var validate = function validate(value) { | ||
var _kind$validate4 = kind.validate(value), | ||
error = _kind$validate4[0], | ||
result = _kind$validate4[1]; | ||
if (error) { | ||
@@ -985,3 +1252,2 @@ error.type = type; | ||
} | ||
/** | ||
@@ -995,18 +1261,29 @@ * Tuple. | ||
function tuple(schema, defaults$$1, options) { | ||
function tuple(schema, defaults, options) { | ||
if (kindOf(schema) !== 'array') { | ||
if (process.env.NODE_ENV !== 'production') { | ||
throw new Error(`Tuple structs must be defined as an array, but you passed: ${schema}`); | ||
throw new Error("Tuple structs must be defined as an array, but you passed: " + schema); | ||
} else { | ||
throw new Error(`Invalid schema: ${schema}`); | ||
throw new Error("Invalid schema: " + schema); | ||
} | ||
} | ||
const kinds = schema.map(s => any(s, undefined, options)); | ||
const array = scalar('array', undefined, options); | ||
const name = 'tuple'; | ||
const type = `[${kinds.map(k => k.type).join()}]`; | ||
const validate = (value = resolveDefaults(defaults$$1)) => { | ||
const [error] = array.validate(value); | ||
var kinds = schema.map(function (s) { | ||
return any(s, undefined, options); | ||
}); | ||
var array = scalar('array', undefined, options); | ||
var name = 'tuple'; | ||
var type = "[" + kinds.map(function (k) { | ||
return k.type; | ||
}).join() + "]"; | ||
var validate = function validate(value) { | ||
if (value === void 0) { | ||
value = resolveDefaults(defaults); | ||
} | ||
var _array$validate2 = array.validate(value), | ||
error = _array$validate2[0]; | ||
if (error) { | ||
@@ -1017,21 +1294,27 @@ error.type = type; | ||
const ret = []; | ||
const errors = []; | ||
const length = Math.max(value.length, kinds.length); | ||
var ret = []; | ||
var errors = []; | ||
var length = Math.max(value.length, kinds.length); | ||
for (let i = 0; i < length; i++) { | ||
const kind = kinds[i]; | ||
const v = value[i]; | ||
var _loop5 = function _loop5(i) { | ||
var kind = kinds[i]; | ||
var v = value[i]; | ||
if (!kind) { | ||
const e = { data: value, path: [i], value: v }; | ||
errors.push(e); | ||
continue; | ||
var _e2 = { | ||
data: value, | ||
path: [i], | ||
value: v | ||
}; | ||
errors.push(_e2); | ||
return "continue"; | ||
} | ||
const [e, r] = kind.validate(v); | ||
var _kind$validate5 = kind.validate(v), | ||
e = _kind$validate5[0], | ||
r = _kind$validate5[1]; | ||
if (e) { | ||
const allE = e.errors || [e]; | ||
allE.forEach(singleE => { | ||
var allE = e.errors || [e]; | ||
allE.forEach(function (singleE) { | ||
singleE.path = [i].concat(singleE.path); | ||
@@ -1041,9 +1324,17 @@ singleE.data = value; | ||
}); | ||
continue; | ||
return "continue"; | ||
} | ||
ret[i] = r; | ||
}; | ||
for (var i = 0; i < length; i++) { | ||
var _ret5 = _loop5(i); | ||
if (_ret5 === "continue") continue; | ||
} | ||
return errors.length ? [_extends({}, errors[0], { errors })] : [undefined, ret]; | ||
return errors.length ? [_objectSpread({}, errors[0], { | ||
errors: errors | ||
})] : [undefined, ret]; | ||
}; | ||
@@ -1053,3 +1344,2 @@ | ||
} | ||
/** | ||
@@ -1063,20 +1353,45 @@ * Union. | ||
function union(schema, defaults$$1, options) { | ||
function union(schema, defaults, options) { | ||
if (kindOf(schema) !== 'array') { | ||
if (process.env.NODE_ENV !== 'production') { | ||
throw new Error(`Union structs must be defined as an array, but you passed: ${schema}`); | ||
throw new Error("Union structs must be defined as an array, but you passed: " + schema); | ||
} else { | ||
throw new Error(`Invalid schema: ${schema}`); | ||
throw new Error("Invalid schema: " + schema); | ||
} | ||
} | ||
const kinds = schema.map(s => any(s, undefined, options)); | ||
const name = 'union'; | ||
const type = kinds.map(k => k.type).join(' | '); | ||
const validate = (value = resolveDefaults(defaults$$1)) => { | ||
const errors = []; | ||
var kinds = schema.map(function (s) { | ||
return any(s, undefined, options); | ||
}); | ||
var name = 'union'; | ||
var type = kinds.map(function (k) { | ||
return k.type; | ||
}).join(' | '); | ||
for (const k of kinds) { | ||
const [e, r] = k.validate(value); | ||
var validate = function validate(value) { | ||
if (value === void 0) { | ||
value = resolveDefaults(defaults); | ||
} | ||
var errors = []; | ||
for (var _iterator = kinds, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) { | ||
var _ref; | ||
if (_isArray) { | ||
if (_i >= _iterator.length) break; | ||
_ref = _iterator[_i++]; | ||
} else { | ||
_i = _iterator.next(); | ||
if (_i.done) break; | ||
_ref = _i.value; | ||
} | ||
var _k2 = _ref; | ||
var _k2$validate = _k2.validate(value), | ||
e = _k2$validate[0], | ||
r = _k2$validate[1]; | ||
if (!e) { | ||
@@ -1088,2 +1403,3 @@ return [undefined, r]; | ||
} | ||
errors[0].type = type; | ||
@@ -1095,3 +1411,2 @@ return errors; | ||
} | ||
/** | ||
@@ -1105,20 +1420,45 @@ * Intersection. | ||
function intersection(schema, defaults$$1, options) { | ||
function intersection(schema, defaults, options) { | ||
if (kindOf(schema) !== 'array') { | ||
if (process.env.NODE_ENV !== 'production') { | ||
throw new Error(`Intersection structs must be defined as an array, but you passed: ${schema}`); | ||
throw new Error("Intersection structs must be defined as an array, but you passed: " + schema); | ||
} else { | ||
throw new Error(`Invalid schema: ${schema}`); | ||
throw new Error("Invalid schema: " + schema); | ||
} | ||
} | ||
const types = schema.map(s => any(s, undefined, options)); | ||
const name = 'intersection'; | ||
const type = types.map(t => t.type).join(' & '); | ||
const validate = (value = resolveDefaults(defaults$$1)) => { | ||
let v = value; | ||
var types = schema.map(function (s) { | ||
return any(s, undefined, options); | ||
}); | ||
var name = 'intersection'; | ||
var type = types.map(function (t) { | ||
return t.type; | ||
}).join(' & '); | ||
for (const t of types) { | ||
const [e, r] = t.validate(v); | ||
var validate = function validate(value) { | ||
if (value === void 0) { | ||
value = resolveDefaults(defaults); | ||
} | ||
var v = value; | ||
for (var _iterator2 = types, _isArray2 = Array.isArray(_iterator2), _i2 = 0, _iterator2 = _isArray2 ? _iterator2 : _iterator2[Symbol.iterator]();;) { | ||
var _ref2; | ||
if (_isArray2) { | ||
if (_i2 >= _iterator2.length) break; | ||
_ref2 = _iterator2[_i2++]; | ||
} else { | ||
_i2 = _iterator2.next(); | ||
if (_i2.done) break; | ||
_ref2 = _i2.value; | ||
} | ||
var t = _ref2; | ||
var _t$validate = t.validate(v), | ||
e = _t$validate[0], | ||
r = _t$validate[1]; | ||
if (e) { | ||
@@ -1137,3 +1477,2 @@ e.type = type; | ||
} | ||
/** | ||
@@ -1145,22 +1484,22 @@ * Kinds. | ||
const Kinds = { | ||
any, | ||
dict, | ||
var Kinds = { | ||
any: any, | ||
dict: dict, | ||
enum: en, | ||
enums, | ||
enums: enums, | ||
function: func, | ||
instance, | ||
instance: instance, | ||
interface: inter, | ||
lazy, | ||
list, | ||
literal, | ||
object, | ||
optional, | ||
partial, | ||
scalar, | ||
tuple, | ||
union, | ||
intersection, | ||
dynamic | ||
lazy: lazy, | ||
list: list, | ||
literal: literal, | ||
object: object, | ||
optional: optional, | ||
partial: partial, | ||
scalar: scalar, | ||
tuple: tuple, | ||
union: union, | ||
intersection: intersection, | ||
dynamic: dynamic | ||
/** | ||
@@ -1180,4 +1519,3 @@ * Export. | ||
const TYPES = ['arguments', 'array', 'boolean', 'buffer', 'error', 'float32array', 'float64array', 'function', 'generatorfunction', 'int16array', 'int32array', 'int8array', 'map', 'null', 'number', 'object', 'promise', 'regexp', 'set', 'string', 'symbol', 'uint16array', 'uint32array', 'uint8array', 'uint8clampedarray', 'undefined', 'weakmap', 'weakset']; | ||
var TYPES = ['arguments', 'array', 'boolean', 'buffer', 'error', 'float32array', 'float64array', 'function', 'generatorfunction', 'int16array', 'int32array', 'int8array', 'map', 'null', 'number', 'object', 'promise', 'regexp', 'set', 'string', 'symbol', 'uint16array', 'uint32array', 'uint8array', 'uint8clampedarray', 'undefined', 'weakmap', 'weakset']; | ||
/** | ||
@@ -1189,10 +1527,12 @@ * The default types that Superstruct ships with. | ||
const Types = { | ||
any: value => value !== undefined | ||
var Types = { | ||
any: function any(value) { | ||
return value !== undefined; | ||
} | ||
}; | ||
TYPES.forEach(type => { | ||
Types[type] = value => kindOf(value) === type; | ||
TYPES.forEach(function (type) { | ||
Types[type] = function (value) { | ||
return kindOf(value) === type; | ||
}; | ||
}); | ||
/** | ||
@@ -1205,3 +1545,5 @@ * Handle the 'date' case specially, to throw out invalid `Date` objects. | ||
Types.date = value => kindOf(value) === 'date' && !isNaN(value); | ||
Types.date = function (value) { | ||
return kindOf(value) === 'date' && !isNaN(value); | ||
}; | ||
@@ -1215,5 +1557,8 @@ /** | ||
function superstruct(config = {}) { | ||
const types = _extends({}, Types, config.types || {}); | ||
function superstruct(config) { | ||
if (config === void 0) { | ||
config = {}; | ||
} | ||
var types = _objectSpread({}, Types, config.types || {}); | ||
/** | ||
@@ -1228,3 +1573,8 @@ * Create a `kind` struct with `schema`, `defaults` and `options`. | ||
function struct(schema, defaults$$1, options = {}) { | ||
function struct(schema, defaults, options) { | ||
if (options === void 0) { | ||
options = {}; | ||
} | ||
if (isStruct(schema)) { | ||
@@ -1234,3 +1584,5 @@ schema = schema.schema; | ||
const kind = Kinds.any(schema, defaults$$1, _extends({}, options, { types })); | ||
var kind = Kinds.any(schema, defaults, _objectSpread({}, options, { | ||
types: types | ||
})); | ||
@@ -1249,13 +1601,18 @@ function Struct(data) { | ||
Object.defineProperty(Struct, IS_STRUCT, { value: true }); | ||
Object.defineProperty(Struct, KIND, { value: kind }); | ||
Object.defineProperty(Struct, IS_STRUCT, { | ||
value: true | ||
}); | ||
Object.defineProperty(Struct, KIND, { | ||
value: kind | ||
}); | ||
Struct.kind = kind.name; | ||
Struct.type = kind.type; | ||
Struct.schema = schema; | ||
Struct.defaults = defaults$$1; | ||
Struct.defaults = defaults; | ||
Struct.options = options; | ||
Struct.assert = value => { | ||
const [error, result] = kind.validate(value); | ||
Struct.assert = function (value) { | ||
var _kind$validate = kind.validate(value), | ||
error = _kind$validate[0], | ||
result = _kind$validate[1]; | ||
@@ -1269,9 +1626,13 @@ if (error) { | ||
Struct.test = value => { | ||
const [error] = kind.validate(value); | ||
Struct.test = function (value) { | ||
var _kind$validate2 = kind.validate(value), | ||
error = _kind$validate2[0]; | ||
return !error; | ||
}; | ||
Struct.validate = value => { | ||
const [error, result] = kind.validate(value); | ||
Struct.validate = function (value) { | ||
var _kind$validate3 = kind.validate(value), | ||
error = _kind$validate3[0], | ||
result = _kind$validate3[1]; | ||
@@ -1287,3 +1648,2 @@ if (error) { | ||
} | ||
/** | ||
@@ -1293,12 +1653,14 @@ * Mix in a factory for each specific kind of struct. | ||
Object.keys(Kinds).forEach(name => { | ||
const kind = Kinds[name]; | ||
struct[name] = (schema, defaults$$1, options) => { | ||
const type = kind(schema, defaults$$1, _extends({}, options, { types })); | ||
const s = struct(type, defaults$$1, options); | ||
Object.keys(Kinds).forEach(function (name) { | ||
var kind = Kinds[name]; | ||
struct[name] = function (schema, defaults, options) { | ||
var type = kind(schema, defaults, _objectSpread({}, options, { | ||
types: types | ||
})); | ||
var s = struct(type, defaults, options); | ||
return s; | ||
}; | ||
}); | ||
/** | ||
@@ -1317,3 +1679,3 @@ * Return the struct factory. | ||
const struct = superstruct(); | ||
var struct = superstruct(); | ||
@@ -1320,0 +1682,0 @@ exports.struct = struct; |
{ | ||
"name": "superstruct", | ||
"description": "A simple, expressive way to validate data in JavaScript.", | ||
"version": "0.6.2", | ||
"version": "0.7.0", | ||
"license": "MIT", | ||
@@ -18,11 +18,11 @@ "repository": "git://github.com/ianstormtaylor/superstruct.git", | ||
"devDependencies": { | ||
"babel-cli": "^6.10.1", | ||
"babel-core": "^6.26.0", | ||
"babel-eslint": "^8.0.2", | ||
"@babel/cli": "^7.0.0", | ||
"@babel/core": "^7.0.0", | ||
"@babel/plugin-proposal-object-rest-spread": "^7.0.0", | ||
"@babel/plugin-transform-modules-commonjs": "^7.0.0", | ||
"@babel/preset-env": "^7.4.5", | ||
"@babel/register": "^7.4.4", | ||
"babel-eslint": "^9.0.0", | ||
"babel-plugin-dev-expression": "^0.2.1", | ||
"babel-plugin-external-helpers": "^6.22.0", | ||
"babel-plugin-transform-async-to-generator": "^6.24.1", | ||
"babel-plugin-transform-es2015-modules-commonjs": "^6.26.0", | ||
"babel-plugin-transform-inline-environment-variables": "^0.2.0", | ||
"babel-plugin-transform-object-rest-spread": "^6.26.0", | ||
"babelify": "^8.0.0", | ||
@@ -42,3 +42,3 @@ "browserify": "^14.5.0", | ||
"rollup": "^0.52.0", | ||
"rollup-plugin-babel": "^3.0.2", | ||
"rollup-plugin-babel": "^4.0.1", | ||
"rollup-plugin-commonjs": "^8.2.6", | ||
@@ -61,3 +61,3 @@ "rollup-plugin-node-resolve": "^3.0.0", | ||
"release": "np", | ||
"test": "yarn build:cjs && yarn lint && mocha --require babel-core/register ./test/index.js", | ||
"test": "yarn build:cjs && yarn lint && mocha --require @babel/register ./test/index.js", | ||
"watch": "rollup --config ./config/rollup-cjs.js --watch" | ||
@@ -64,0 +64,0 @@ }, |
@@ -7,2 +7,134 @@ (function (global, factory) { | ||
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; | ||
} | ||
function _objectSpread(target) { | ||
for (var i = 1; i < arguments.length; i++) { | ||
var source = arguments[i] != null ? arguments[i] : {}; | ||
var ownKeys = Object.keys(source); | ||
if (typeof Object.getOwnPropertySymbols === 'function') { | ||
ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) { | ||
return Object.getOwnPropertyDescriptor(source, sym).enumerable; | ||
})); | ||
} | ||
ownKeys.forEach(function (key) { | ||
_defineProperty(target, key, source[key]); | ||
}); | ||
} | ||
return target; | ||
} | ||
function _inheritsLoose(subClass, superClass) { | ||
subClass.prototype = Object.create(superClass.prototype); | ||
subClass.prototype.constructor = subClass; | ||
subClass.__proto__ = superClass; | ||
} | ||
function _getPrototypeOf(o) { | ||
_getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { | ||
return o.__proto__ || Object.getPrototypeOf(o); | ||
}; | ||
return _getPrototypeOf(o); | ||
} | ||
function _setPrototypeOf(o, p) { | ||
_setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { | ||
o.__proto__ = p; | ||
return o; | ||
}; | ||
return _setPrototypeOf(o, p); | ||
} | ||
function isNativeReflectConstruct() { | ||
if (typeof Reflect === "undefined" || !Reflect.construct) return false; | ||
if (Reflect.construct.sham) return false; | ||
if (typeof Proxy === "function") return true; | ||
try { | ||
Date.prototype.toString.call(Reflect.construct(Date, [], function () {})); | ||
return true; | ||
} catch (e) { | ||
return false; | ||
} | ||
} | ||
function _construct(Parent, args, Class) { | ||
if (isNativeReflectConstruct()) { | ||
_construct = Reflect.construct; | ||
} else { | ||
_construct = function _construct(Parent, args, Class) { | ||
var a = [null]; | ||
a.push.apply(a, args); | ||
var Constructor = Function.bind.apply(Parent, a); | ||
var instance = new Constructor(); | ||
if (Class) _setPrototypeOf(instance, Class.prototype); | ||
return instance; | ||
}; | ||
} | ||
return _construct.apply(null, arguments); | ||
} | ||
function _isNativeFunction(fn) { | ||
return Function.toString.call(fn).indexOf("[native code]") !== -1; | ||
} | ||
function _wrapNativeSuper(Class) { | ||
var _cache = typeof Map === "function" ? new Map() : undefined; | ||
_wrapNativeSuper = function _wrapNativeSuper(Class) { | ||
if (Class === null || !_isNativeFunction(Class)) return Class; | ||
if (typeof Class !== "function") { | ||
throw new TypeError("Super expression must either be null or a function"); | ||
} | ||
if (typeof _cache !== "undefined") { | ||
if (_cache.has(Class)) return _cache.get(Class); | ||
_cache.set(Class, Wrapper); | ||
} | ||
function Wrapper() { | ||
return _construct(Class, arguments, _getPrototypeOf(this).constructor); | ||
} | ||
Wrapper.prototype = Object.create(Class.prototype, { | ||
constructor: { | ||
value: Wrapper, | ||
enumerable: false, | ||
writable: true, | ||
configurable: true | ||
} | ||
}); | ||
return _setPrototypeOf(Wrapper, Class); | ||
}; | ||
return _wrapNativeSuper(Class); | ||
} | ||
function _assertThisInitialized(self) { | ||
if (self === void 0) { | ||
throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); | ||
} | ||
return self; | ||
} | ||
/** | ||
@@ -13,34 +145,50 @@ * Define a struct error. | ||
*/ | ||
var StructError = | ||
/*#__PURE__*/ | ||
function (_TypeError) { | ||
_inheritsLoose(StructError, _TypeError); | ||
class StructError extends TypeError { | ||
static format(attrs) { | ||
const { type, path, value } = attrs; | ||
const message = `Expected a value of type \`${type}\`${path.length ? ` for \`${path.join('.')}\`` : ''} but received \`${JSON.stringify(value)}\`.`; | ||
StructError.format = function format(attrs) { | ||
var type = attrs.type, | ||
path = attrs.path, | ||
value = attrs.value; | ||
var message = "Expected a value of type `" + type + "`" + (path.length ? " for `" + path.join('.') + "`" : '') + " but received `" + JSON.stringify(value) + "`."; | ||
return message; | ||
} | ||
}; | ||
constructor(attrs) { | ||
const message = StructError.format(attrs); | ||
super(message); | ||
function StructError(attrs) { | ||
var _this; | ||
const { data, path, value, reason, type, errors = [] } = attrs; | ||
this.data = data; | ||
this.path = path; | ||
this.value = value; | ||
this.reason = reason; | ||
this.type = type; | ||
this.errors = errors; | ||
var message = StructError.format(attrs); | ||
_this = _TypeError.call(this, message) || this; | ||
var data = attrs.data, | ||
path = attrs.path, | ||
value = attrs.value, | ||
reason = attrs.reason, | ||
type = attrs.type, | ||
_attrs$errors = attrs.errors, | ||
errors = _attrs$errors === void 0 ? [] : _attrs$errors; | ||
_this.data = data; | ||
_this.path = path; | ||
_this.value = value; | ||
_this.reason = reason; | ||
_this.type = type; | ||
_this.errors = errors; | ||
if (!errors.length) { | ||
errors.push(this); | ||
errors.push(_assertThisInitialized(_this)); | ||
} | ||
if (Error.captureStackTrace) { | ||
Error.captureStackTrace(this, this.constructor); | ||
Error.captureStackTrace(_assertThisInitialized(_this), _this.constructor); | ||
} else { | ||
this.stack = new Error().stack; | ||
_this.stack = new Error().stack; | ||
} | ||
return _this; | ||
} | ||
} | ||
return StructError; | ||
}(_wrapNativeSuper(TypeError)); | ||
var toString = Object.prototype.toString; | ||
@@ -181,5 +329,3 @@ | ||
*/ | ||
const IS_STRUCT = '@@__STRUCT__@@'; | ||
var IS_STRUCT = '@@__STRUCT__@@'; | ||
/** | ||
@@ -191,3 +337,3 @@ * A private string to refer to a struct's kind. | ||
const KIND = '@@__KIND__@@'; | ||
var KIND = '@@__KIND__@@'; | ||
@@ -204,3 +350,2 @@ /** | ||
} | ||
/** | ||
@@ -218,16 +363,2 @@ * Resolve `defaults`, for an optional `value`. | ||
var _extends = Object.assign || function (target) { | ||
for (var i = 1; i < arguments.length; i++) { | ||
var source = arguments[i]; | ||
for (var key in source) { | ||
if (Object.prototype.hasOwnProperty.call(source, key)) { | ||
target[key] = source[key]; | ||
} | ||
} | ||
} | ||
return target; | ||
}; | ||
/** | ||
@@ -239,10 +370,7 @@ * Kind. | ||
class Kind { | ||
constructor(name, type, validate) { | ||
this.name = name; | ||
this.type = type; | ||
this.validate = validate; | ||
} | ||
} | ||
var Kind = function Kind(name, type, validate) { | ||
this.name = name; | ||
this.type = type; | ||
this.validate = validate; | ||
}; | ||
/** | ||
@@ -256,3 +384,4 @@ * Any. | ||
function any(schema, defaults$$1, options) { | ||
function any(schema, defaults, options) { | ||
if (isStruct(schema)) { | ||
@@ -269,3 +398,3 @@ return schema[KIND]; | ||
{ | ||
return schema.length > 1 ? tuple(schema, defaults$$1, options) : list(schema, defaults$$1, options); | ||
return schema.length > 1 ? tuple(schema, defaults, options) : list(schema, defaults, options); | ||
} | ||
@@ -275,3 +404,3 @@ | ||
{ | ||
return func(schema, defaults$$1, options); | ||
return func(schema, defaults, options); | ||
} | ||
@@ -281,3 +410,3 @@ | ||
{ | ||
return object(schema, defaults$$1, options); | ||
return object(schema, defaults, options); | ||
} | ||
@@ -287,4 +416,4 @@ | ||
{ | ||
let required = true; | ||
let type; | ||
var required = true; | ||
var type; | ||
@@ -297,9 +426,10 @@ if (schema.endsWith('?')) { | ||
if (schema.includes('|')) { | ||
const scalars = schema.split(/\s*\|\s*/g); | ||
type = union(scalars, defaults$$1, options); | ||
var scalars = schema.split(/\s*\|\s*/g); | ||
type = union(scalars, defaults, options); | ||
} else if (schema.includes('&')) { | ||
const scalars = schema.split(/\s*&\s*/g); | ||
type = intersection(scalars, defaults$$1, options); | ||
var _scalars = schema.split(/\s*&\s*/g); | ||
type = intersection(_scalars, defaults, options); | ||
} else { | ||
type = scalar(schema, defaults$$1, options); | ||
type = scalar(schema, defaults, options); | ||
} | ||
@@ -316,6 +446,5 @@ | ||
{ | ||
throw new Error(`A schema definition must be an object, array, string or function, but you passed: ${schema}`); | ||
throw new Error("A schema definition must be an object, array, string or function, but you passed: " + schema); | ||
} | ||
} | ||
/** | ||
@@ -329,19 +458,23 @@ * Dict. | ||
function dict(schema, defaults$$1, options) { | ||
function dict(schema, defaults, options) { | ||
if (kindOf(schema) !== 'array' || schema.length !== 2) { | ||
{ | ||
throw new Error(`Dict structs must be defined as an array with two elements, but you passed: ${schema}`); | ||
throw new Error("Dict structs must be defined as an array with two elements, but you passed: " + schema); | ||
} | ||
} | ||
const obj = scalar('object', undefined, options); | ||
const keys = any(schema[0], undefined, options); | ||
const values = any(schema[1], undefined, options); | ||
const name = 'dict'; | ||
const type = `dict<${keys.type},${values.type}>`; | ||
const validate = value => { | ||
const resolved = resolveDefaults(defaults$$1); | ||
value = resolved ? _extends({}, resolved, value) : value; | ||
const [error] = obj.validate(value); | ||
var obj = scalar('object', undefined, options); | ||
var keys = any(schema[0], undefined, options); | ||
var values = any(schema[1], undefined, options); | ||
var name = 'dict'; | ||
var type = "dict<" + keys.type + "," + values.type + ">"; | ||
var validate = function validate(value) { | ||
var resolved = resolveDefaults(defaults); | ||
value = resolved ? _objectSpread({}, resolved, value) : value; | ||
var _obj$validate = obj.validate(value), | ||
error = _obj$validate[0]; | ||
if (error) { | ||
@@ -352,36 +485,53 @@ error.type = type; | ||
const ret = {}; | ||
const errors = []; | ||
var ret = {}; | ||
var errors = []; | ||
for (let k in value) { | ||
const v = value[k]; | ||
const [e, r] = keys.validate(k); | ||
var _loop = function _loop(_k) { | ||
var v = value[_k]; | ||
var _keys$validate = keys.validate(_k), | ||
e = _keys$validate[0], | ||
r = _keys$validate[1]; | ||
if (e) { | ||
const allE = e.errors || [e]; | ||
allE.forEach(singleE => { | ||
singleE.path = [k].concat(singleE.path); | ||
var allE = e.errors || [e]; | ||
allE.forEach(function (singleE) { | ||
singleE.path = [_k].concat(singleE.path); | ||
singleE.data = value; | ||
errors.push(singleE); | ||
}); | ||
continue; | ||
k = _k; | ||
return "continue"; | ||
} | ||
k = r; | ||
const [e2, r2] = values.validate(v); | ||
_k = r; | ||
var _values$validate = values.validate(v), | ||
e2 = _values$validate[0], | ||
r2 = _values$validate[1]; | ||
if (e2) { | ||
const allE2 = e2.errors || [e2]; | ||
allE2.forEach(singleE => { | ||
singleE.path = [k].concat(singleE.path); | ||
var allE2 = e2.errors || [e2]; | ||
allE2.forEach(function (singleE) { | ||
singleE.path = [_k].concat(singleE.path); | ||
singleE.data = value; | ||
errors.push(singleE); | ||
}); | ||
continue; | ||
k = _k; | ||
return "continue"; | ||
} | ||
ret[k] = r2; | ||
ret[_k] = r2; | ||
k = _k; | ||
}; | ||
for (var k in value) { | ||
var _ret = _loop(k); | ||
if (_ret === "continue") continue; | ||
} | ||
return errors.length ? [_extends({}, errors[0], { errors })] : [undefined, ret]; | ||
return errors.length ? [_objectSpread({}, errors[0], { | ||
errors: errors | ||
})] : [undefined, ret]; | ||
}; | ||
@@ -391,3 +541,2 @@ | ||
} | ||
/** | ||
@@ -401,11 +550,12 @@ * Enum. | ||
function en(schema, defaults$$1, options) { | ||
function en(schema, defaults, options) { | ||
if (kindOf(schema) !== 'array') { | ||
{ | ||
throw new Error(`Enum structs must be defined as an array, but you passed: ${schema}`); | ||
throw new Error("Enum structs must be defined as an array, but you passed: " + schema); | ||
} | ||
} | ||
const name = 'enum'; | ||
const type = schema.map(s => { | ||
var name = 'enum'; | ||
var type = schema.map(function (s) { | ||
try { | ||
@@ -418,4 +568,13 @@ return JSON.stringify(s); | ||
const validate = (value = resolveDefaults(defaults$$1)) => { | ||
return schema.includes(value) ? [undefined, value] : [{ data: value, path: [], value, type }]; | ||
var validate = function validate(value) { | ||
if (value === void 0) { | ||
value = resolveDefaults(defaults); | ||
} | ||
return schema.includes(value) ? [undefined, value] : [{ | ||
data: value, | ||
path: [], | ||
value: value, | ||
type: type | ||
}]; | ||
}; | ||
@@ -425,3 +584,2 @@ | ||
} | ||
/** | ||
@@ -435,8 +593,8 @@ * Enums. | ||
function enums(schema, defaults$$1, options) { | ||
const e = en(schema, undefined, options); | ||
const l = list([e], defaults$$1, options); | ||
function enums(schema, defaults, options) { | ||
var e = en(schema, undefined, options); | ||
var l = list([e], defaults, options); | ||
return l; | ||
} | ||
/** | ||
@@ -450,16 +608,25 @@ * Function. | ||
function func(schema, defaults$$1, options) { | ||
function func(schema, defaults, options) { | ||
if (kindOf(schema) !== 'function') { | ||
{ | ||
throw new Error(`Function structs must be defined as a function, but you passed: ${schema}`); | ||
throw new Error("Function structs must be defined as a function, but you passed: " + schema); | ||
} | ||
} | ||
const name = 'function'; | ||
const type = '<function>'; | ||
const validate = (value = resolveDefaults(defaults$$1), data) => { | ||
const result = schema(value, data); | ||
let failure = { path: [], reason: null }; | ||
let isValid; | ||
var name = 'function'; | ||
var type = '<function>'; | ||
var validate = function validate(value, data) { | ||
if (value === void 0) { | ||
value = resolveDefaults(defaults); | ||
} | ||
var result = schema(value, data); | ||
var failure = { | ||
path: [], | ||
reason: null | ||
}; | ||
var isValid; | ||
switch (kindOf(result)) { | ||
@@ -471,2 +638,3 @@ case 'boolean': | ||
} | ||
case 'string': | ||
@@ -478,12 +646,14 @@ { | ||
} | ||
case 'object': | ||
{ | ||
isValid = false; | ||
failure = _extends({}, failure, result); | ||
failure = _objectSpread({}, failure, result); | ||
break; | ||
} | ||
default: | ||
{ | ||
{ | ||
throw new Error(`Validator functions must return a boolean, an error reason string or an error reason object, but you passed: ${schema}`); | ||
throw new Error("Validator functions must return a boolean, an error reason string or an error reason object, but you passed: " + schema); | ||
} | ||
@@ -493,3 +663,7 @@ } | ||
return isValid ? [undefined, value] : [_extends({ type, value, data: value }, failure)]; | ||
return isValid ? [undefined, value] : [_objectSpread({ | ||
type: type, | ||
value: value, | ||
data: value | ||
}, failure)]; | ||
}; | ||
@@ -499,3 +673,2 @@ | ||
} | ||
/** | ||
@@ -509,7 +682,18 @@ * Instance. | ||
function instance(schema, defaults$$1, options) { | ||
const name = 'instance'; | ||
const type = `instance<${schema.name}>`; | ||
const validate = (value = resolveDefaults(defaults$$1)) => { | ||
return value instanceof schema ? [undefined, value] : [{ data: value, path: [], value, type }]; | ||
function instance(schema, defaults, options) { | ||
var name = 'instance'; | ||
var type = "instance<" + schema.name + ">"; | ||
var validate = function validate(value) { | ||
if (value === void 0) { | ||
value = resolveDefaults(defaults); | ||
} | ||
return value instanceof schema ? [undefined, value] : [{ | ||
data: value, | ||
path: [], | ||
value: value, | ||
type: type | ||
}]; | ||
}; | ||
@@ -519,3 +703,2 @@ | ||
} | ||
/** | ||
@@ -529,25 +712,32 @@ * Interface. | ||
function inter(schema, defaults$$1, options) { | ||
function inter(schema, defaults, options) { | ||
if (kindOf(schema) !== 'object') { | ||
{ | ||
throw new Error(`Interface structs must be defined as an object, but you passed: ${schema}`); | ||
throw new Error("Interface structs must be defined as an object, but you passed: " + schema); | ||
} | ||
} | ||
const obj = scalar('object', undefined, options); | ||
const ks = []; | ||
const properties = {}; | ||
var obj = scalar('object', undefined, options); | ||
var ks = []; | ||
var properties = {}; | ||
for (const key in schema) { | ||
for (var key in schema) { | ||
ks.push(key); | ||
const s = schema[key]; | ||
const kind = any(s, undefined, options); | ||
var s = schema[key]; | ||
var kind = any(s, undefined, options); | ||
properties[key] = kind; | ||
} | ||
const name = 'interface'; | ||
const type = `{${ks.join()}}`; | ||
const validate = (value = resolveDefaults(defaults$$1)) => { | ||
const [error] = obj.validate(value); | ||
var name = 'interface'; | ||
var type = "{" + ks.join() + "}"; | ||
var validate = function validate(value) { | ||
if (value === void 0) { | ||
value = resolveDefaults(defaults); | ||
} | ||
var _obj$validate2 = obj.validate(value), | ||
error = _obj$validate2[0]; | ||
if (error) { | ||
@@ -558,32 +748,42 @@ error.type = type; | ||
const errors = []; | ||
const ret = value; | ||
var errors = []; | ||
var ret = value; | ||
for (const key in properties) { | ||
let v = value[key]; | ||
const kind = properties[key]; | ||
var _loop2 = function _loop2(_key) { | ||
var v = value[_key]; | ||
var kind = properties[_key]; | ||
if (v === undefined) { | ||
const d = defaults$$1 && defaults$$1[key]; | ||
var d = defaults && defaults[_key]; | ||
v = resolveDefaults(d, value); | ||
} | ||
const [e, r] = kind.validate(v, value); | ||
var _kind$validate = kind.validate(v, value), | ||
e = _kind$validate[0], | ||
r = _kind$validate[1]; | ||
if (e) { | ||
const allE = e.errors || [e]; | ||
allE.forEach(singleE => { | ||
singleE.path = [key].concat(singleE.path); | ||
var allE = e.errors || [e]; | ||
allE.forEach(function (singleE) { | ||
singleE.path = [_key].concat(singleE.path); | ||
singleE.data = value; | ||
errors.push(singleE); | ||
}); | ||
continue; | ||
return "continue"; | ||
} | ||
if (key in value || r !== undefined) { | ||
ret[key] = r; | ||
if (_key in value || r !== undefined) { | ||
ret[_key] = r; | ||
} | ||
}; | ||
for (var _key in properties) { | ||
var _ret2 = _loop2(_key); | ||
if (_ret2 === "continue") continue; | ||
} | ||
return errors.length ? [_extends({}, errors[0], { errors })] : [undefined, ret]; | ||
return errors.length ? [_objectSpread({}, errors[0], { | ||
errors: errors | ||
})] : [undefined, ret]; | ||
}; | ||
@@ -593,3 +793,2 @@ | ||
} | ||
/** | ||
@@ -603,14 +802,16 @@ * Lazy. | ||
function lazy(schema, defaults$$1, options) { | ||
function lazy(schema, defaults, options) { | ||
if (kindOf(schema) !== 'function') { | ||
{ | ||
throw new Error(`Lazy structs must be defined as an function that returns a schema, but you passed: ${schema}`); | ||
throw new Error("Lazy structs must be defined as an function that returns a schema, but you passed: " + schema); | ||
} | ||
} | ||
let kind; | ||
let struct; | ||
const name = 'lazy'; | ||
const type = `lazy...`; | ||
const compile = value => { | ||
var kind; | ||
var struct; | ||
var name = 'lazy'; | ||
var type = "lazy..."; | ||
var compile = function compile(value) { | ||
struct = schema(); | ||
@@ -626,3 +827,2 @@ kind.name = struct.kind; | ||
} | ||
/** | ||
@@ -636,21 +836,29 @@ * Dynamic. | ||
function dynamic(createSchema, defaults$$1, options) { | ||
function dynamic(createSchema, defaults, options) { | ||
if (kindOf(createSchema) !== 'function') { | ||
{ | ||
throw new Error(`Dynamic structs must be defined as a function, but you passed: ${createSchema}`); | ||
throw new Error("Dynamic structs must be defined as a function, but you passed: " + createSchema); | ||
} | ||
} | ||
const name = 'dynamic'; | ||
const type = 'dynamic...'; | ||
const validate = (value = resolveDefaults(defaults$$1), data) => { | ||
const schema = createSchema(value, data); | ||
var name = 'dynamic'; | ||
var type = 'dynamic...'; | ||
var validate = function validate(value, data) { | ||
if (value === void 0) { | ||
value = resolveDefaults(defaults); | ||
} | ||
var schema = createSchema(value, data); | ||
if (kindOf(schema) !== 'function') { | ||
{ | ||
throw new Error(`Dynamic structs must return a schema, but you passed: ${schema}`); | ||
throw new Error("Dynamic structs must return a schema, but you passed: " + schema); | ||
} | ||
} | ||
const [error, result] = schema.validate(value); | ||
var _schema$validate = schema.validate(value), | ||
error = _schema$validate[0], | ||
result = _schema$validate[1]; | ||
@@ -666,3 +874,2 @@ if (error) { | ||
} | ||
/** | ||
@@ -676,16 +883,24 @@ * List. | ||
function list(schema, defaults$$1, options) { | ||
function list(schema, defaults, options) { | ||
if (kindOf(schema) !== 'array' || schema.length !== 1) { | ||
{ | ||
throw new Error(`List structs must be defined as an array with a single element, but you passed: ${schema}`); | ||
throw new Error("List structs must be defined as an array with a single element, but you passed: " + schema); | ||
} | ||
} | ||
const array = scalar('array', undefined, options); | ||
const element = any(schema[0], undefined, options); | ||
const name = 'list'; | ||
const type = `[${element.type}]`; | ||
const validate = (value = resolveDefaults(defaults$$1)) => { | ||
const [error, result] = array.validate(value); | ||
var array = scalar('array', undefined, options); | ||
var element = any(schema[0], undefined, options); | ||
var name = 'list'; | ||
var type = "[" + element.type + "]"; | ||
var validate = function validate(value) { | ||
if (value === void 0) { | ||
value = resolveDefaults(defaults); | ||
} | ||
var _array$validate = array.validate(value), | ||
error = _array$validate[0], | ||
result = _array$validate[1]; | ||
if (error) { | ||
@@ -697,12 +912,15 @@ error.type = type; | ||
value = result; | ||
const errors = []; | ||
const ret = []; | ||
var errors = []; | ||
var ret = []; | ||
for (let i = 0; i < value.length; i++) { | ||
const v = value[i]; | ||
const [e, r] = element.validate(v); | ||
var _loop3 = function _loop3(i) { | ||
var v = value[i]; | ||
var _element$validate = element.validate(v), | ||
e = _element$validate[0], | ||
r = _element$validate[1]; | ||
if (e) { | ||
const allE = e.errors || [e]; | ||
allE.forEach(singleE => { | ||
var allE = e.errors || [e]; | ||
allE.forEach(function (singleE) { | ||
singleE.path = [i].concat(singleE.path); | ||
@@ -712,9 +930,17 @@ singleE.data = value; | ||
}); | ||
continue; | ||
return "continue"; | ||
} | ||
ret[i] = r; | ||
}; | ||
for (var i = 0; i < value.length; i++) { | ||
var _ret3 = _loop3(i); | ||
if (_ret3 === "continue") continue; | ||
} | ||
return errors.length ? [_extends({}, errors[0], { errors })] : [undefined, ret]; | ||
return errors.length ? [_objectSpread({}, errors[0], { | ||
errors: errors | ||
})] : [undefined, ret]; | ||
}; | ||
@@ -724,3 +950,2 @@ | ||
} | ||
/** | ||
@@ -734,7 +959,18 @@ * Literal. | ||
function literal(schema, defaults$$1, options) { | ||
const name = 'literal'; | ||
const type = `literal: ${JSON.stringify(schema)}`; | ||
const validate = (value = resolveDefaults(defaults$$1)) => { | ||
return value === schema ? [undefined, value] : [{ data: value, path: [], value, type }]; | ||
function literal(schema, defaults, options) { | ||
var name = 'literal'; | ||
var type = "literal: " + JSON.stringify(schema); | ||
var validate = function validate(value) { | ||
if (value === void 0) { | ||
value = resolveDefaults(defaults); | ||
} | ||
return value === schema ? [undefined, value] : [{ | ||
data: value, | ||
path: [], | ||
value: value, | ||
type: type | ||
}]; | ||
}; | ||
@@ -744,3 +980,2 @@ | ||
} | ||
/** | ||
@@ -754,25 +989,32 @@ * Object. | ||
function object(schema, defaults$$1, options) { | ||
function object(schema, defaults, options) { | ||
if (kindOf(schema) !== 'object') { | ||
{ | ||
throw new Error(`Object structs must be defined as an object, but you passed: ${schema}`); | ||
throw new Error("Object structs must be defined as an object, but you passed: " + schema); | ||
} | ||
} | ||
const obj = scalar('object', undefined, options); | ||
const ks = []; | ||
const properties = {}; | ||
var obj = scalar('object', undefined, options); | ||
var ks = []; | ||
var properties = {}; | ||
for (const key in schema) { | ||
for (var key in schema) { | ||
ks.push(key); | ||
const s = schema[key]; | ||
const kind = any(s, undefined, options); | ||
var s = schema[key]; | ||
var kind = any(s, undefined, options); | ||
properties[key] = kind; | ||
} | ||
const name = 'object'; | ||
const type = `{${ks.join()}}`; | ||
const validate = (value = resolveDefaults(defaults$$1)) => { | ||
const [error] = obj.validate(value); | ||
var name = 'object'; | ||
var type = "{" + ks.join() + "}"; | ||
var validate = function validate(value) { | ||
if (value === void 0) { | ||
value = resolveDefaults(defaults); | ||
} | ||
var _obj$validate3 = obj.validate(value), | ||
error = _obj$validate3[0]; | ||
if (error) { | ||
@@ -783,14 +1025,13 @@ error.type = type; | ||
const errors = []; | ||
const ret = {}; | ||
const valueKeys = Object.keys(value); | ||
const propertiesKeys = Object.keys(properties); | ||
const keys = new Set(valueKeys.concat(propertiesKeys)); | ||
var errors = []; | ||
var ret = {}; | ||
var valueKeys = Object.keys(value); | ||
var propertiesKeys = Object.keys(properties); | ||
var keys = new Set(valueKeys.concat(propertiesKeys)); | ||
keys.forEach(function (key) { | ||
var v = value[key]; | ||
var kind = properties[key]; | ||
keys.forEach(key => { | ||
let v = value[key]; | ||
const kind = properties[key]; | ||
if (v === undefined) { | ||
const d = defaults$$1 && defaults$$1[key]; | ||
var d = defaults && defaults[key]; | ||
v = resolveDefaults(d, value); | ||
@@ -800,12 +1041,18 @@ } | ||
if (!kind) { | ||
const e = { data: value, path: [key], value: v }; | ||
errors.push(e); | ||
var _e = { | ||
data: value, | ||
path: [key], | ||
value: v | ||
}; | ||
errors.push(_e); | ||
return; | ||
} | ||
const [e, r] = kind.validate(v, value); | ||
var _kind$validate2 = kind.validate(v, value), | ||
e = _kind$validate2[0], | ||
r = _kind$validate2[1]; | ||
if (e) { | ||
const allE = e.errors || [e]; | ||
allE.forEach(singleE => { | ||
var allE = e.errors || [e]; | ||
allE.forEach(function (singleE) { | ||
singleE.path = [key].concat(singleE.path); | ||
@@ -822,4 +1069,5 @@ singleE.data = value; | ||
}); | ||
return errors.length ? [_extends({}, errors[0], { errors })] : [undefined, ret]; | ||
return errors.length ? [_objectSpread({}, errors[0], { | ||
errors: errors | ||
})] : [undefined, ret]; | ||
}; | ||
@@ -829,3 +1077,2 @@ | ||
} | ||
/** | ||
@@ -839,6 +1086,6 @@ * Optional. | ||
function optional(schema, defaults$$1, options) { | ||
return union([schema, 'undefined'], defaults$$1, options); | ||
function optional(schema, defaults, options) { | ||
return union([schema, 'undefined'], defaults, options); | ||
} | ||
/** | ||
@@ -852,25 +1099,32 @@ * Partial. | ||
function partial(schema, defaults$$1, options) { | ||
function partial(schema, defaults, options) { | ||
if (kindOf(schema) !== 'object') { | ||
{ | ||
throw new Error(`Partial structs must be defined as an object, but you passed: ${schema}`); | ||
throw new Error("Partial structs must be defined as an object, but you passed: " + schema); | ||
} | ||
} | ||
const obj = scalar('object', undefined, options); | ||
const ks = []; | ||
const properties = {}; | ||
var obj = scalar('object', undefined, options); | ||
var ks = []; | ||
var properties = {}; | ||
for (const key in schema) { | ||
for (var key in schema) { | ||
ks.push(key); | ||
const s = schema[key]; | ||
const kind = any(s, undefined, options); | ||
var s = schema[key]; | ||
var kind = any(s, undefined, options); | ||
properties[key] = kind; | ||
} | ||
const name = 'partial'; | ||
const type = `{${ks.join()},...}`; | ||
const validate = (value = resolveDefaults(defaults$$1)) => { | ||
const [error] = obj.validate(value); | ||
var name = 'partial'; | ||
var type = "{" + ks.join() + ",...}"; | ||
var validate = function validate(value) { | ||
if (value === void 0) { | ||
value = resolveDefaults(defaults); | ||
} | ||
var _obj$validate4 = obj.validate(value), | ||
error = _obj$validate4[0]; | ||
if (error) { | ||
@@ -881,32 +1135,42 @@ error.type = type; | ||
const errors = []; | ||
const ret = {}; | ||
var errors = []; | ||
var ret = {}; | ||
for (const key in properties) { | ||
let v = value[key]; | ||
const kind = properties[key]; | ||
var _loop4 = function _loop4(_key2) { | ||
var v = value[_key2]; | ||
var kind = properties[_key2]; | ||
if (v === undefined) { | ||
const d = defaults$$1 && defaults$$1[key]; | ||
var d = defaults && defaults[_key2]; | ||
v = resolveDefaults(d, value); | ||
} | ||
const [e, r] = kind.validate(v, value); | ||
var _kind$validate3 = kind.validate(v, value), | ||
e = _kind$validate3[0], | ||
r = _kind$validate3[1]; | ||
if (e) { | ||
const allE = e.errors || [e]; | ||
allE.forEach(singleE => { | ||
singleE.path = [key].concat(singleE.path); | ||
var allE = e.errors || [e]; | ||
allE.forEach(function (singleE) { | ||
singleE.path = [_key2].concat(singleE.path); | ||
singleE.data = value; | ||
errors.push(singleE); | ||
}); | ||
continue; | ||
return "continue"; | ||
} | ||
if (key in value || r !== undefined) { | ||
ret[key] = r; | ||
if (_key2 in value || r !== undefined) { | ||
ret[_key2] = r; | ||
} | ||
}; | ||
for (var _key2 in properties) { | ||
var _ret4 = _loop4(_key2); | ||
if (_ret4 === "continue") continue; | ||
} | ||
return errors.length ? [_extends({}, errors[0], { errors })] : [undefined, ret]; | ||
return errors.length ? [_objectSpread({}, errors[0], { | ||
errors: errors | ||
})] : [undefined, ret]; | ||
}; | ||
@@ -916,3 +1180,2 @@ | ||
} | ||
/** | ||
@@ -926,24 +1189,28 @@ * Scalar. | ||
function scalar(schema, defaults$$1, options) { | ||
function scalar(schema, defaults, options) { | ||
if (kindOf(schema) !== 'string') { | ||
{ | ||
throw new Error(`Scalar structs must be defined as a string, but you passed: ${schema}`); | ||
throw new Error("Scalar structs must be defined as a string, but you passed: " + schema); | ||
} | ||
} | ||
const { types } = options; | ||
const fn = types[schema]; | ||
var types = options.types; | ||
var fn = types[schema]; | ||
if (kindOf(fn) !== 'function') { | ||
{ | ||
throw new Error(`No struct validator function found for type "${schema}".`); | ||
throw new Error("No struct validator function found for type \"" + schema + "\"."); | ||
} | ||
} | ||
const kind = func(fn, defaults$$1, options); | ||
const name = 'scalar'; | ||
const type = schema; | ||
const validate = value => { | ||
const [error, result] = kind.validate(value); | ||
var kind = func(fn, defaults, options); | ||
var name = 'scalar'; | ||
var type = schema; | ||
var validate = function validate(value) { | ||
var _kind$validate4 = kind.validate(value), | ||
error = _kind$validate4[0], | ||
result = _kind$validate4[1]; | ||
if (error) { | ||
@@ -959,3 +1226,2 @@ error.type = type; | ||
} | ||
/** | ||
@@ -969,16 +1235,27 @@ * Tuple. | ||
function tuple(schema, defaults$$1, options) { | ||
function tuple(schema, defaults, options) { | ||
if (kindOf(schema) !== 'array') { | ||
{ | ||
throw new Error(`Tuple structs must be defined as an array, but you passed: ${schema}`); | ||
throw new Error("Tuple structs must be defined as an array, but you passed: " + schema); | ||
} | ||
} | ||
const kinds = schema.map(s => any(s, undefined, options)); | ||
const array = scalar('array', undefined, options); | ||
const name = 'tuple'; | ||
const type = `[${kinds.map(k => k.type).join()}]`; | ||
const validate = (value = resolveDefaults(defaults$$1)) => { | ||
const [error] = array.validate(value); | ||
var kinds = schema.map(function (s) { | ||
return any(s, undefined, options); | ||
}); | ||
var array = scalar('array', undefined, options); | ||
var name = 'tuple'; | ||
var type = "[" + kinds.map(function (k) { | ||
return k.type; | ||
}).join() + "]"; | ||
var validate = function validate(value) { | ||
if (value === void 0) { | ||
value = resolveDefaults(defaults); | ||
} | ||
var _array$validate2 = array.validate(value), | ||
error = _array$validate2[0]; | ||
if (error) { | ||
@@ -989,21 +1266,27 @@ error.type = type; | ||
const ret = []; | ||
const errors = []; | ||
const length = Math.max(value.length, kinds.length); | ||
var ret = []; | ||
var errors = []; | ||
var length = Math.max(value.length, kinds.length); | ||
for (let i = 0; i < length; i++) { | ||
const kind = kinds[i]; | ||
const v = value[i]; | ||
var _loop5 = function _loop5(i) { | ||
var kind = kinds[i]; | ||
var v = value[i]; | ||
if (!kind) { | ||
const e = { data: value, path: [i], value: v }; | ||
errors.push(e); | ||
continue; | ||
var _e2 = { | ||
data: value, | ||
path: [i], | ||
value: v | ||
}; | ||
errors.push(_e2); | ||
return "continue"; | ||
} | ||
const [e, r] = kind.validate(v); | ||
var _kind$validate5 = kind.validate(v), | ||
e = _kind$validate5[0], | ||
r = _kind$validate5[1]; | ||
if (e) { | ||
const allE = e.errors || [e]; | ||
allE.forEach(singleE => { | ||
var allE = e.errors || [e]; | ||
allE.forEach(function (singleE) { | ||
singleE.path = [i].concat(singleE.path); | ||
@@ -1013,9 +1296,17 @@ singleE.data = value; | ||
}); | ||
continue; | ||
return "continue"; | ||
} | ||
ret[i] = r; | ||
}; | ||
for (var i = 0; i < length; i++) { | ||
var _ret5 = _loop5(i); | ||
if (_ret5 === "continue") continue; | ||
} | ||
return errors.length ? [_extends({}, errors[0], { errors })] : [undefined, ret]; | ||
return errors.length ? [_objectSpread({}, errors[0], { | ||
errors: errors | ||
})] : [undefined, ret]; | ||
}; | ||
@@ -1025,3 +1316,2 @@ | ||
} | ||
/** | ||
@@ -1035,18 +1325,43 @@ * Union. | ||
function union(schema, defaults$$1, options) { | ||
function union(schema, defaults, options) { | ||
if (kindOf(schema) !== 'array') { | ||
{ | ||
throw new Error(`Union structs must be defined as an array, but you passed: ${schema}`); | ||
throw new Error("Union structs must be defined as an array, but you passed: " + schema); | ||
} | ||
} | ||
const kinds = schema.map(s => any(s, undefined, options)); | ||
const name = 'union'; | ||
const type = kinds.map(k => k.type).join(' | '); | ||
const validate = (value = resolveDefaults(defaults$$1)) => { | ||
const errors = []; | ||
var kinds = schema.map(function (s) { | ||
return any(s, undefined, options); | ||
}); | ||
var name = 'union'; | ||
var type = kinds.map(function (k) { | ||
return k.type; | ||
}).join(' | '); | ||
for (const k of kinds) { | ||
const [e, r] = k.validate(value); | ||
var validate = function validate(value) { | ||
if (value === void 0) { | ||
value = resolveDefaults(defaults); | ||
} | ||
var errors = []; | ||
for (var _iterator = kinds, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) { | ||
var _ref; | ||
if (_isArray) { | ||
if (_i >= _iterator.length) break; | ||
_ref = _iterator[_i++]; | ||
} else { | ||
_i = _iterator.next(); | ||
if (_i.done) break; | ||
_ref = _i.value; | ||
} | ||
var _k2 = _ref; | ||
var _k2$validate = _k2.validate(value), | ||
e = _k2$validate[0], | ||
r = _k2$validate[1]; | ||
if (!e) { | ||
@@ -1058,2 +1373,3 @@ return [undefined, r]; | ||
} | ||
errors[0].type = type; | ||
@@ -1065,3 +1381,2 @@ return errors; | ||
} | ||
/** | ||
@@ -1075,18 +1390,43 @@ * Intersection. | ||
function intersection(schema, defaults$$1, options) { | ||
function intersection(schema, defaults, options) { | ||
if (kindOf(schema) !== 'array') { | ||
{ | ||
throw new Error(`Intersection structs must be defined as an array, but you passed: ${schema}`); | ||
throw new Error("Intersection structs must be defined as an array, but you passed: " + schema); | ||
} | ||
} | ||
const types = schema.map(s => any(s, undefined, options)); | ||
const name = 'intersection'; | ||
const type = types.map(t => t.type).join(' & '); | ||
const validate = (value = resolveDefaults(defaults$$1)) => { | ||
let v = value; | ||
var types = schema.map(function (s) { | ||
return any(s, undefined, options); | ||
}); | ||
var name = 'intersection'; | ||
var type = types.map(function (t) { | ||
return t.type; | ||
}).join(' & '); | ||
for (const t of types) { | ||
const [e, r] = t.validate(v); | ||
var validate = function validate(value) { | ||
if (value === void 0) { | ||
value = resolveDefaults(defaults); | ||
} | ||
var v = value; | ||
for (var _iterator2 = types, _isArray2 = Array.isArray(_iterator2), _i2 = 0, _iterator2 = _isArray2 ? _iterator2 : _iterator2[Symbol.iterator]();;) { | ||
var _ref2; | ||
if (_isArray2) { | ||
if (_i2 >= _iterator2.length) break; | ||
_ref2 = _iterator2[_i2++]; | ||
} else { | ||
_i2 = _iterator2.next(); | ||
if (_i2.done) break; | ||
_ref2 = _i2.value; | ||
} | ||
var t = _ref2; | ||
var _t$validate = t.validate(v), | ||
e = _t$validate[0], | ||
r = _t$validate[1]; | ||
if (e) { | ||
@@ -1105,3 +1445,2 @@ e.type = type; | ||
} | ||
/** | ||
@@ -1113,22 +1452,22 @@ * Kinds. | ||
const Kinds = { | ||
any, | ||
dict, | ||
var Kinds = { | ||
any: any, | ||
dict: dict, | ||
enum: en, | ||
enums, | ||
enums: enums, | ||
function: func, | ||
instance, | ||
instance: instance, | ||
interface: inter, | ||
lazy, | ||
list, | ||
literal, | ||
object, | ||
optional, | ||
partial, | ||
scalar, | ||
tuple, | ||
union, | ||
intersection, | ||
dynamic | ||
lazy: lazy, | ||
list: list, | ||
literal: literal, | ||
object: object, | ||
optional: optional, | ||
partial: partial, | ||
scalar: scalar, | ||
tuple: tuple, | ||
union: union, | ||
intersection: intersection, | ||
dynamic: dynamic | ||
/** | ||
@@ -1148,4 +1487,3 @@ * Export. | ||
const TYPES = ['arguments', 'array', 'boolean', 'buffer', 'error', 'float32array', 'float64array', 'function', 'generatorfunction', 'int16array', 'int32array', 'int8array', 'map', 'null', 'number', 'object', 'promise', 'regexp', 'set', 'string', 'symbol', 'uint16array', 'uint32array', 'uint8array', 'uint8clampedarray', 'undefined', 'weakmap', 'weakset']; | ||
var TYPES = ['arguments', 'array', 'boolean', 'buffer', 'error', 'float32array', 'float64array', 'function', 'generatorfunction', 'int16array', 'int32array', 'int8array', 'map', 'null', 'number', 'object', 'promise', 'regexp', 'set', 'string', 'symbol', 'uint16array', 'uint32array', 'uint8array', 'uint8clampedarray', 'undefined', 'weakmap', 'weakset']; | ||
/** | ||
@@ -1157,10 +1495,12 @@ * The default types that Superstruct ships with. | ||
const Types = { | ||
any: value => value !== undefined | ||
var Types = { | ||
any: function any(value) { | ||
return value !== undefined; | ||
} | ||
}; | ||
TYPES.forEach(type => { | ||
Types[type] = value => kindOf(value) === type; | ||
TYPES.forEach(function (type) { | ||
Types[type] = function (value) { | ||
return kindOf(value) === type; | ||
}; | ||
}); | ||
/** | ||
@@ -1173,3 +1513,5 @@ * Handle the 'date' case specially, to throw out invalid `Date` objects. | ||
Types.date = value => kindOf(value) === 'date' && !isNaN(value); | ||
Types.date = function (value) { | ||
return kindOf(value) === 'date' && !isNaN(value); | ||
}; | ||
@@ -1183,5 +1525,8 @@ /** | ||
function superstruct(config = {}) { | ||
const types = _extends({}, Types, config.types || {}); | ||
function superstruct(config) { | ||
if (config === void 0) { | ||
config = {}; | ||
} | ||
var types = _objectSpread({}, Types, config.types || {}); | ||
/** | ||
@@ -1196,3 +1541,8 @@ * Create a `kind` struct with `schema`, `defaults` and `options`. | ||
function struct(schema, defaults$$1, options = {}) { | ||
function struct(schema, defaults, options) { | ||
if (options === void 0) { | ||
options = {}; | ||
} | ||
if (isStruct(schema)) { | ||
@@ -1202,3 +1552,5 @@ schema = schema.schema; | ||
const kind = Kinds.any(schema, defaults$$1, _extends({}, options, { types })); | ||
var kind = Kinds.any(schema, defaults, _objectSpread({}, options, { | ||
types: types | ||
})); | ||
@@ -1215,13 +1567,18 @@ function Struct(data) { | ||
Object.defineProperty(Struct, IS_STRUCT, { value: true }); | ||
Object.defineProperty(Struct, KIND, { value: kind }); | ||
Object.defineProperty(Struct, IS_STRUCT, { | ||
value: true | ||
}); | ||
Object.defineProperty(Struct, KIND, { | ||
value: kind | ||
}); | ||
Struct.kind = kind.name; | ||
Struct.type = kind.type; | ||
Struct.schema = schema; | ||
Struct.defaults = defaults$$1; | ||
Struct.defaults = defaults; | ||
Struct.options = options; | ||
Struct.assert = value => { | ||
const [error, result] = kind.validate(value); | ||
Struct.assert = function (value) { | ||
var _kind$validate = kind.validate(value), | ||
error = _kind$validate[0], | ||
result = _kind$validate[1]; | ||
@@ -1235,9 +1592,13 @@ if (error) { | ||
Struct.test = value => { | ||
const [error] = kind.validate(value); | ||
Struct.test = function (value) { | ||
var _kind$validate2 = kind.validate(value), | ||
error = _kind$validate2[0]; | ||
return !error; | ||
}; | ||
Struct.validate = value => { | ||
const [error, result] = kind.validate(value); | ||
Struct.validate = function (value) { | ||
var _kind$validate3 = kind.validate(value), | ||
error = _kind$validate3[0], | ||
result = _kind$validate3[1]; | ||
@@ -1253,3 +1614,2 @@ if (error) { | ||
} | ||
/** | ||
@@ -1259,12 +1619,14 @@ * Mix in a factory for each specific kind of struct. | ||
Object.keys(Kinds).forEach(name => { | ||
const kind = Kinds[name]; | ||
struct[name] = (schema, defaults$$1, options) => { | ||
const type = kind(schema, defaults$$1, _extends({}, options, { types })); | ||
const s = struct(type, defaults$$1, options); | ||
Object.keys(Kinds).forEach(function (name) { | ||
var kind = Kinds[name]; | ||
struct[name] = function (schema, defaults, options) { | ||
var type = kind(schema, defaults, _objectSpread({}, options, { | ||
types: types | ||
})); | ||
var s = struct(type, defaults, options); | ||
return s; | ||
}; | ||
}); | ||
/** | ||
@@ -1283,3 +1645,3 @@ * Return the struct factory. | ||
const struct = superstruct(); | ||
var struct = superstruct(); | ||
@@ -1286,0 +1648,0 @@ exports.struct = struct; |
@@ -1,1 +0,1 @@ | ||
!function(t,r){"object"==typeof exports&&"undefined"!=typeof module?r(exports):"function"==typeof define&&define.amd?define(["exports"],r):r(t.Superstruct={})}(this,function(t){"use strict";function r(t){return t.constructor?t.constructor.name:null}function e(t){return!(!t||!t[m])}function n(t,r){return"function"==typeof t?t(r):t}function a(t,r,n){if(e(t))return t[w];if(t instanceof j)return t;switch(v(t)){case"array":return t.length>1?d(t,r,n):i(t,r,n);case"function":return s(t,r,n);case"object":return c(t,r,n);case"string":{let e,a=!0;if(t.endsWith("?")&&(a=!1,t=t.slice(0,-1)),t.includes("|")){e=p(t.split(/\s*\|\s*/g),r,n)}else if(t.includes("&")){e=y(t.split(/\s*&\s*/g),r,n)}else e=f(t,r,n);return a||(e=u(e,void 0,n)),e}}throw new Error(`A schema definition must be an object, array, string or function, but you passed: ${t}`)}function o(t,r,e){if("array"!==v(t))throw new Error(`Enum structs must be defined as an array, but you passed: ${t}`);const a=t.map(t=>{try{return JSON.stringify(t)}catch(r){return String(t)}}).join(" | ");return new j("enum",a,(e=n(r))=>t.includes(e)?[void 0,e]:[{data:e,path:[],value:e,type:a}])}function s(t,r,e){if("function"!==v(t))throw new Error(`Function structs must be defined as a function, but you passed: ${t}`);return new j("function","<function>",(e=n(r),a)=>{const o=t(e,a);let s,i={path:[],reason:null};switch(v(o)){case"boolean":s=o;break;case"string":s=!1,i.reason=o;break;case"object":s=!1,i=g({},i,o);break;default:throw new Error(`Validator functions must return a boolean, an error reason string or an error reason object, but you passed: ${t}`)}return s?[void 0,e]:[g({type:"<function>",value:e,data:e},i)]})}function i(t,r,e){if("array"!==v(t)||1!==t.length)throw new Error(`List structs must be defined as an array with a single element, but you passed: ${t}`);const o=f("array",void 0,e),s=a(t[0],void 0,e),i=`[${s.type}]`;return new j("list",i,(t=n(r))=>{const[e,a]=o.validate(t);if(e)return e.type=i,[e];t=a;const c=[],u=[];for(let r=0;r<t.length;r++){const e=t[r],[n,a]=s.validate(e);n?(n.errors||[n]).forEach(e=>{e.path=[r].concat(e.path),e.data=t,c.push(e)}):u[r]=a}return c.length?[g({},c[0],{errors:c})]:[void 0,u]})}function c(t,r,e){if("object"!==v(t))throw new Error(`Object structs must be defined as an object, but you passed: ${t}`);const o=f("object",void 0,e),s=[],i={};for(const r in t){s.push(r);const n=a(t[r],void 0,e);i[r]=n}const c=`{${s.join()}}`;return new j("object",c,(t=n(r))=>{const[e]=o.validate(t);if(e)return e.type=c,[e];const a=[],s={},u=Object.keys(t),f=Object.keys(i);return new Set(u.concat(f)).forEach(e=>{let o=t[e];const c=i[e];if(void 0===o&&(o=n(r&&r[e],t)),!c){const r={data:t,path:[e],value:o};return void a.push(r)}const[u,f]=c.validate(o,t);u?(u.errors||[u]).forEach(r=>{r.path=[e].concat(r.path),r.data=t,a.push(r)}):(e in t||void 0!==f)&&(s[e]=f)}),a.length?[g({},a[0],{errors:a})]:[void 0,s]})}function u(t,r,e){return p([t,"undefined"],r,e)}function f(t,r,e){if("string"!==v(t))throw new Error(`Scalar structs must be defined as a string, but you passed: ${t}`);const{types:n}=e,a=n[t];if("function"!==v(a))throw new Error(`No struct validator function found for type "${t}".`);const o=s(a,r),i=t;return new j("scalar",i,t=>{const[r,e]=o.validate(t);return r?(r.type=i,[r]):[void 0,e]})}function d(t,r,e){if("array"!==v(t))throw new Error(`Tuple structs must be defined as an array, but you passed: ${t}`);const o=t.map(t=>a(t,void 0,e)),s=f("array",void 0,e),i=`[${o.map(t=>t.type).join()}]`;return new j("tuple",i,(t=n(r))=>{const[e]=s.validate(t);if(e)return e.type=i,[e];const a=[],c=[],u=Math.max(t.length,o.length);for(let r=0;r<u;r++){const e=o[r],n=t[r];if(!e){const e={data:t,path:[r],value:n};c.push(e);continue}const[s,i]=e.validate(n);s?(s.errors||[s]).forEach(e=>{e.path=[r].concat(e.path),e.data=t,c.push(e)}):a[r]=i}return c.length?[g({},c[0],{errors:c})]:[void 0,a]})}function p(t,r,e){if("array"!==v(t))throw new Error(`Union structs must be defined as an array, but you passed: ${t}`);const o=t.map(t=>a(t,void 0,e)),s=o.map(t=>t.type).join(" | ");return new j("union",s,(t=n(r))=>{const e=[];for(const r of o){const[n,a]=r.validate(t);if(!n)return[void 0,a];e.push(n)}return e[0].type=s,e})}function y(t,r,e){if("array"!==v(t))throw new Error(`Intersection structs must be defined as an array, but you passed: ${t}`);const o=t.map(t=>a(t,void 0,e)),s=o.map(t=>t.type).join(" & ");return new j("intersection",s,(t=n(r))=>{let e=t;for(const t of o){const[r,n]=t.validate(e);if(r)return r.type=s,[r];e=n}return[void 0,e]})}function l(t={}){function r(t,r,a={}){function o(t){if(this instanceof o)throw new Error("The `Struct` creation function should not be used with the `new` keyword.");return o.assert(t)}e(t)&&(t=t.schema);const s=E.any(t,r,g({},a,{types:n}));return Object.defineProperty(o,m,{value:!0}),Object.defineProperty(o,w,{value:s}),o.kind=s.name,o.type=s.type,o.schema=t,o.defaults=r,o.options=a,o.assert=(t=>{const[r,e]=s.validate(t);if(r)throw new h(r);return e}),o.test=(t=>{const[r]=s.validate(t);return!r}),o.validate=(t=>{const[r,e]=s.validate(t);return r?[new h(r)]:[void 0,e]}),o}const n=g({},$,t.types||{});return Object.keys(E).forEach(t=>{const e=E[t];r[t]=((t,a,o)=>{return r(e(t,a,g({},o,{types:n})),a,o)})}),r}class h extends TypeError{static format(t){const{type:r,path:e,value:n}=t;return`Expected a value of type \`${r}\`${e.length?` for \`${e.join(".")}\``:""} but received \`${JSON.stringify(n)}\`.`}constructor(t){super(h.format(t));const{data:r,path:e,value:n,reason:a,type:o,errors:s=[]}=t;this.data=r,this.path=e,this.value=n,this.reason=a,this.type=o,this.errors=s,s.length||s.push(this),Error.captureStackTrace?Error.captureStackTrace(this,this.constructor):this.stack=(new Error).stack}}var b=Object.prototype.toString,v=function(t){if(void 0===t)return"undefined";if(null===t)return"null";var e=typeof t;if("boolean"===e)return"boolean";if("string"===e)return"string";if("number"===e)return"number";if("symbol"===e)return"symbol";if("function"===e)return function(t,e){return"GeneratorFunction"===r(t)}(t)?"generatorfunction":"function";if(function(t){return Array.isArray?Array.isArray(t):t instanceof Array}(t))return"array";if(function(t){return!(!t.constructor||"function"!=typeof t.constructor.isBuffer)&&t.constructor.isBuffer(t)}(t))return"buffer";if(function(t){try{if("number"==typeof t.length&&"function"==typeof t.callee)return!0}catch(t){if(-1!==t.message.indexOf("callee"))return!0}return!1}(t))return"arguments";if(function(t){return t instanceof Date||"function"==typeof t.toDateString&&"function"==typeof t.getDate&&"function"==typeof t.setDate}(t))return"date";if(function(t){return t instanceof Error||"string"==typeof t.message&&t.constructor&&"number"==typeof t.constructor.stackTraceLimit}(t))return"error";if(function(t){return t instanceof RegExp||"string"==typeof t.flags&&"boolean"==typeof t.ignoreCase&&"boolean"==typeof t.multiline&&"boolean"==typeof t.global}(t))return"regexp";switch(r(t)){case"Symbol":return"symbol";case"Promise":return"promise";case"WeakMap":return"weakmap";case"WeakSet":return"weakset";case"Map":return"map";case"Set":return"set";case"Int8Array":return"int8array";case"Uint8Array":return"uint8array";case"Uint8ClampedArray":return"uint8clampedarray";case"Int16Array":return"int16array";case"Uint16Array":return"uint16array";case"Int32Array":return"int32array";case"Uint32Array":return"uint32array";case"Float32Array":return"float32array";case"Float64Array":return"float64array"}if(function(t){return"function"==typeof t.throw&&"function"==typeof t.return&&"function"==typeof t.next}(t))return"generator";switch(e=b.call(t)){case"[object Object]":return"object";case"[object Map Iterator]":return"mapiterator";case"[object Set Iterator]":return"setiterator";case"[object String Iterator]":return"stringiterator";case"[object Array Iterator]":return"arrayiterator"}return e.slice(8,-1).toLowerCase().replace(/\s/g,"")};const m="@@__STRUCT__@@",w="@@__KIND__@@";var g=Object.assign||function(t){for(var r=1;r<arguments.length;r++){var e=arguments[r];for(var n in e)Object.prototype.hasOwnProperty.call(e,n)&&(t[n]=e[n])}return t};class j{constructor(t,r,e){this.name=t,this.type=r,this.validate=e}}const E={any:a,dict:function(t,r,e){if("array"!==v(t)||2!==t.length)throw new Error(`Dict structs must be defined as an array with two elements, but you passed: ${t}`);const o=f("object",void 0,e),s=a(t[0],void 0,e),i=a(t[1],void 0,e),c=`dict<${s.type},${i.type}>`;return new j("dict",c,t=>{const e=n(r);t=e?g({},e,t):t;const[a]=o.validate(t);if(a)return a.type=c,[a];const u={},f=[];for(let r in t){const e=t[r],[n,a]=s.validate(r);if(n){(n.errors||[n]).forEach(e=>{e.path=[r].concat(e.path),e.data=t,f.push(e)});continue}r=a;const[o,c]=i.validate(e);o?(o.errors||[o]).forEach(e=>{e.path=[r].concat(e.path),e.data=t,f.push(e)}):u[r]=c}return f.length?[g({},f[0],{errors:f})]:[void 0,u]})},enum:o,enums:function(t,r,e){return i([o(t,void 0)],r,e)},function:s,instance:function(t,r,e){const a=`instance<${t.name}>`;return new j("instance",a,(e=n(r))=>e instanceof t?[void 0,e]:[{data:e,path:[],value:e,type:a}])},interface:function(t,r,e){if("object"!==v(t))throw new Error(`Interface structs must be defined as an object, but you passed: ${t}`);const o=f("object",void 0,e),s=[],i={};for(const r in t){s.push(r);const n=a(t[r],void 0,e);i[r]=n}const c=`{${s.join()}}`;return new j("interface",c,(t=n(r))=>{const[e]=o.validate(t);if(e)return e.type=c,[e];const a=[],s=t;for(const e in i){let o=t[e];const c=i[e];void 0===o&&(o=n(r&&r[e],t));const[u,f]=c.validate(o,t);u?(u.errors||[u]).forEach(r=>{r.path=[e].concat(r.path),r.data=t,a.push(r)}):(e in t||void 0!==f)&&(s[e]=f)}return a.length?[g({},a[0],{errors:a})]:[void 0,s]})},lazy:function(t,r,e){if("function"!==v(t))throw new Error(`Lazy structs must be defined as an function that returns a schema, but you passed: ${t}`);let n,a;return n=new j("lazy","lazy...",r=>(a=t(),n.name=a.kind,n.type=a.type,n.validate=a.validate,n.validate(r)))},list:i,literal:function(t,r,e){const a=`literal: ${JSON.stringify(t)}`;return new j("literal",a,(e=n(r))=>e===t?[void 0,e]:[{data:e,path:[],value:e,type:a}])},object:c,optional:u,partial:function(t,r,e){if("object"!==v(t))throw new Error(`Partial structs must be defined as an object, but you passed: ${t}`);const o=f("object",void 0,e),s=[],i={};for(const r in t){s.push(r);const n=a(t[r],void 0,e);i[r]=n}const c=`{${s.join()},...}`;return new j("partial",c,(t=n(r))=>{const[e]=o.validate(t);if(e)return e.type=c,[e];const a=[],s={};for(const e in i){let o=t[e];const c=i[e];void 0===o&&(o=n(r&&r[e],t));const[u,f]=c.validate(o,t);u?(u.errors||[u]).forEach(r=>{r.path=[e].concat(r.path),r.data=t,a.push(r)}):(e in t||void 0!==f)&&(s[e]=f)}return a.length?[g({},a[0],{errors:a})]:[void 0,s]})},scalar:f,tuple:d,union:p,intersection:y,dynamic:function(t,r,e){if("function"!==v(t))throw new Error(`Dynamic structs must be defined as a function, but you passed: ${t}`);return new j("dynamic","dynamic...",(e=n(r),a)=>{const o=t(e,a);if("function"!==v(o))throw new Error(`Dynamic structs must return a schema, but you passed: ${o}`);const[s,i]=o.validate(e);return s?[s]:[void 0,i]})}},$={any:t=>void 0!==t};["arguments","array","boolean","buffer","error","float32array","float64array","function","generatorfunction","int16array","int32array","int8array","map","null","number","object","promise","regexp","set","string","symbol","uint16array","uint32array","uint8array","uint8clampedarray","undefined","weakmap","weakset"].forEach(t=>{$[t]=(r=>v(r)===t)}),$.date=(t=>"date"===v(t)&&!isNaN(t));const k=l();t.struct=k,t.superstruct=l,t.isStruct=e,t.StructError=h,Object.defineProperty(t,"__esModule",{value:!0})}); | ||
!function(r,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t(r.Superstruct={})}(this,function(r){"use strict";function t(r,t,e){return t in r?Object.defineProperty(r,t,{value:e,enumerable:!0,configurable:!0,writable:!0}):r[t]=e,r}function e(r){for(var e=1;e<arguments.length;e++){var n=null!=arguments[e]?arguments[e]:{},a=Object.keys(n);"function"==typeof Object.getOwnPropertySymbols&&(a=a.concat(Object.getOwnPropertySymbols(n).filter(function(r){return Object.getOwnPropertyDescriptor(n,r).enumerable}))),a.forEach(function(e){t(r,e,n[e])})}return r}function n(r){return(n=Object.setPrototypeOf?Object.getPrototypeOf:function(r){return r.__proto__||Object.getPrototypeOf(r)})(r)}function a(r,t){return(a=Object.setPrototypeOf||function(r,t){return r.__proto__=t,r})(r,t)}function o(r,t,e){return(o=function(){if("undefined"==typeof Reflect||!Reflect.construct)return!1;if(Reflect.construct.sham)return!1;if("function"==typeof Proxy)return!0;try{return Date.prototype.toString.call(Reflect.construct(Date,[],function(){})),!0}catch(r){return!1}}()?Reflect.construct:function(r,t,e){var n=[null];n.push.apply(n,t);var o=new(Function.bind.apply(r,n));return e&&a(o,e.prototype),o}).apply(null,arguments)}function i(r){var t="function"==typeof Map?new Map:void 0;return(i=function(r){function e(){return o(r,arguments,n(this).constructor)}if(null===r||!function(r){return-1!==Function.toString.call(r).indexOf("[native code]")}(r))return r;if("function"!=typeof r)throw new TypeError("Super expression must either be null or a function");if(void 0!==t){if(t.has(r))return t.get(r);t.set(r,e)}return e.prototype=Object.create(r.prototype,{constructor:{value:e,enumerable:!1,writable:!0,configurable:!0}}),a(e,r)})(r)}function u(r){if(void 0===r)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return r}function c(r){return r.constructor?r.constructor.name:null}function f(r){return!(!r||!r[k])}function s(r,t){return"function"==typeof r?r(t):r}function d(r,t,e){if(f(r))return r[_];if(r instanceof A)return r;switch(S(r)){case"array":return r.length>1?m(r,t,e):l(r,t,e);case"function":return y(r,t,e);case"object":return v(r,t,e);case"string":var n,a=!0;if(r.endsWith("?")&&(a=!1,r=r.slice(0,-1)),r.includes("|")){n=w(r.split(/\s*\|\s*/g),t,e)}else if(r.includes("&")){n=g(r.split(/\s*&\s*/g),t,e)}else n=b(r,t,e);return a||(n=h(n,void 0,e)),n}throw new Error("A schema definition must be an object, array, string or function, but you passed: "+r)}function p(r,t,e){if("array"!==S(r))throw new Error("Enum structs must be defined as an array, but you passed: "+r);var n=r.map(function(r){try{return JSON.stringify(r)}catch(t){return String(r)}}).join(" | ");return new A("enum",n,function(e){return void 0===e&&(e=s(t)),r.includes(e)?[void 0,e]:[{data:e,path:[],value:e,type:n}]})}function y(r,t,n){if("function"!==S(r))throw new Error("Function structs must be defined as a function, but you passed: "+r);return new A("function","<function>",function(n,a){void 0===n&&(n=s(t));var o,i=r(n,a),u={path:[],reason:null};switch(S(i)){case"boolean":o=i;break;case"string":o=!1,u.reason=i;break;case"object":o=!1,u=e({},u,i);break;default:throw new Error("Validator functions must return a boolean, an error reason string or an error reason object, but you passed: "+r)}return o?[void 0,n]:[e({type:"<function>",value:n,data:n},u)]})}function l(r,t,n){if("array"!==S(r)||1!==r.length)throw new Error("List structs must be defined as an array with a single element, but you passed: "+r);var a=b("array",void 0,n),o=d(r[0],void 0,n),i="["+o.type+"]";return new A("list",i,function(r){void 0===r&&(r=s(t));var n=a.validate(r),u=n[0],c=n[1];if(u)return u.type=i,[u];r=c;for(var f=[],d=[],p=function(t){var e=r[t],n=o.validate(e),a=n[0],i=n[1];if(a)return(a.errors||[a]).forEach(function(e){e.path=[t].concat(e.path),e.data=r,f.push(e)}),"continue";d[t]=i},y=0;y<r.length;y++)p(y);return f.length?[e({},f[0],{errors:f})]:[void 0,d]})}function v(r,t,n){if("object"!==S(r))throw new Error("Object structs must be defined as an object, but you passed: "+r);var a=b("object",void 0,n),o=[],i={};for(var u in r){o.push(u);var c=d(r[u],void 0,n);i[u]=c}var f="{"+o.join()+"}";return new A("object",f,function(r){void 0===r&&(r=s(t));var n=a.validate(r)[0];if(n)return n.type=f,[n];var o=[],u={},c=Object.keys(r),d=Object.keys(i);return new Set(c.concat(d)).forEach(function(e){var n=r[e],a=i[e];if(void 0===n&&(n=s(t&&t[e],r)),a){var c=a.validate(n,r),f=c[0],d=c[1];f?(f.errors||[f]).forEach(function(t){t.path=[e].concat(t.path),t.data=r,o.push(t)}):(e in r||void 0!==d)&&(u[e]=d)}else{var p={data:r,path:[e],value:n};o.push(p)}}),o.length?[e({},o[0],{errors:o})]:[void 0,u]})}function h(r,t,e){return w([r,"undefined"],t,e)}function b(r,t,e){if("string"!==S(r))throw new Error("Scalar structs must be defined as a string, but you passed: "+r);var n=e.types[r];if("function"!==S(n))throw new Error('No struct validator function found for type "'+r+'".');var a=y(n,t),o=r;return new A("scalar",o,function(r){var t=a.validate(r),e=t[0],n=t[1];return e?(e.type=o,[e]):[void 0,n]})}function m(r,t,n){if("array"!==S(r))throw new Error("Tuple structs must be defined as an array, but you passed: "+r);var a=r.map(function(r){return d(r,void 0,n)}),o=b("array",void 0,n),i="["+a.map(function(r){return r.type}).join()+"]";return new A("tuple",i,function(r){void 0===r&&(r=s(t));var n=o.validate(r)[0];if(n)return n.type=i,[n];for(var u=[],c=[],f=Math.max(r.length,a.length),d=function(t){var e=a[t],n=r[t];if(!e){var o={data:r,path:[t],value:n};return c.push(o),"continue"}var i=e.validate(n),f=i[0],s=i[1];if(f)return(f.errors||[f]).forEach(function(e){e.path=[t].concat(e.path),e.data=r,c.push(e)}),"continue";u[t]=s},p=0;p<f;p++)d(p);return c.length?[e({},c[0],{errors:c})]:[void 0,u]})}function w(r,t,e){if("array"!==S(r))throw new Error("Union structs must be defined as an array, but you passed: "+r);var n=r.map(function(r){return d(r,void 0,e)}),a=n.map(function(r){return r.type}).join(" | ");return new A("union",a,function(r){void 0===r&&(r=s(t));var e=[],o=n,i=Array.isArray(o),u=0;for(o=i?o:o[Symbol.iterator]();;){var c;if(i){if(u>=o.length)break;c=o[u++]}else{if((u=o.next()).done)break;c=u.value}var f=c.validate(r),d=f[0],p=f[1];if(!d)return[void 0,p];e.push(d)}return e[0].type=a,e})}function g(r,t,e){if("array"!==S(r))throw new Error("Intersection structs must be defined as an array, but you passed: "+r);var n=r.map(function(r){return d(r,void 0,e)}),a=n.map(function(r){return r.type}).join(" & ");return new A("intersection",a,function(r){void 0===r&&(r=s(t));var e=r,o=n,i=Array.isArray(o),u=0;for(o=i?o:o[Symbol.iterator]();;){var c;if(i){if(u>=o.length)break;c=o[u++]}else{if((u=o.next()).done)break;c=u.value}var f=c.validate(e),d=f[0],p=f[1];if(d)return d.type=a,[d];e=p}return[void 0,e]})}function j(r){function t(r,t,a){function o(r){if(this instanceof o)throw new Error("The `Struct` creation function should not be used with the `new` keyword.");return o.assert(r)}void 0===a&&(a={}),f(r)&&(r=r.schema);var i=x.any(r,t,e({},a,{types:n}));return Object.defineProperty(o,k,{value:!0}),Object.defineProperty(o,_,{value:i}),o.kind=i.name,o.type=i.type,o.schema=r,o.defaults=t,o.options=a,o.assert=function(r){var t=i.validate(r),e=t[0],n=t[1];if(e)throw new E(e);return n},o.test=function(r){return!i.validate(r)[0]},o.validate=function(r){var t=i.validate(r),e=t[0],n=t[1];return e?[new E(e)]:[void 0,n]},o}void 0===r&&(r={});var n=e({},P,r.types||{});return Object.keys(x).forEach(function(r){var a=x[r];t[r]=function(r,o,i){return t(a(r,o,e({},i,{types:n})),o,i)}}),t}var E=function(r){function t(e){var n,a=t.format(e);n=r.call(this,a)||this;var o=e.data,i=e.path,c=e.value,f=e.reason,s=e.type,d=e.errors,p=void 0===d?[]:d;return n.data=o,n.path=i,n.value=c,n.reason=f,n.type=s,n.errors=p,p.length||p.push(u(n)),Error.captureStackTrace?Error.captureStackTrace(u(n),n.constructor):n.stack=(new Error).stack,n}return function(r,t){r.prototype=Object.create(t.prototype),r.prototype.constructor=r,r.__proto__=t}(t,r),t.format=function(r){var t=r.type,e=r.path,n=r.value;return"Expected a value of type `"+t+"`"+(e.length?" for `"+e.join(".")+"`":"")+" but received `"+JSON.stringify(n)+"`."},t}(i(TypeError)),O=Object.prototype.toString,S=function(r){if(void 0===r)return"undefined";if(null===r)return"null";var t=typeof r;if("boolean"===t)return"boolean";if("string"===t)return"string";if("number"===t)return"number";if("symbol"===t)return"symbol";if("function"===t)return function(r,t){return"GeneratorFunction"===c(r)}(r)?"generatorfunction":"function";if(function(r){return Array.isArray?Array.isArray(r):r instanceof Array}(r))return"array";if(function(r){return!(!r.constructor||"function"!=typeof r.constructor.isBuffer)&&r.constructor.isBuffer(r)}(r))return"buffer";if(function(r){try{if("number"==typeof r.length&&"function"==typeof r.callee)return!0}catch(r){if(-1!==r.message.indexOf("callee"))return!0}return!1}(r))return"arguments";if(function(r){return r instanceof Date||"function"==typeof r.toDateString&&"function"==typeof r.getDate&&"function"==typeof r.setDate}(r))return"date";if(function(r){return r instanceof Error||"string"==typeof r.message&&r.constructor&&"number"==typeof r.constructor.stackTraceLimit}(r))return"error";if(function(r){return r instanceof RegExp||"string"==typeof r.flags&&"boolean"==typeof r.ignoreCase&&"boolean"==typeof r.multiline&&"boolean"==typeof r.global}(r))return"regexp";switch(c(r)){case"Symbol":return"symbol";case"Promise":return"promise";case"WeakMap":return"weakmap";case"WeakSet":return"weakset";case"Map":return"map";case"Set":return"set";case"Int8Array":return"int8array";case"Uint8Array":return"uint8array";case"Uint8ClampedArray":return"uint8clampedarray";case"Int16Array":return"int16array";case"Uint16Array":return"uint16array";case"Int32Array":return"int32array";case"Uint32Array":return"uint32array";case"Float32Array":return"float32array";case"Float64Array":return"float64array"}if(function(r){return"function"==typeof r.throw&&"function"==typeof r.return&&"function"==typeof r.next}(r))return"generator";switch(t=O.call(r)){case"[object Object]":return"object";case"[object Map Iterator]":return"mapiterator";case"[object Set Iterator]":return"setiterator";case"[object String Iterator]":return"stringiterator";case"[object Array Iterator]":return"arrayiterator"}return t.slice(8,-1).toLowerCase().replace(/\s/g,"")},k="@@__STRUCT__@@",_="@@__KIND__@@",A=function(r,t,e){this.name=r,this.type=t,this.validate=e},x={any:d,dict:function(r,t,n){if("array"!==S(r)||2!==r.length)throw new Error("Dict structs must be defined as an array with two elements, but you passed: "+r);var a=b("object",void 0,n),o=d(r[0],void 0,n),i=d(r[1],void 0,n),u="dict<"+o.type+","+i.type+">";return new A("dict",u,function(r){var n=s(t);r=n?e({},n,r):r;var c=a.validate(r)[0];if(c)return c.type=u,[c];var f={},d=[],p=function(t){var e=r[t],n=o.validate(t),a=n[0],u=n[1];if(a)return(a.errors||[a]).forEach(function(e){e.path=[t].concat(e.path),e.data=r,d.push(e)}),y=t,"continue";t=u;var c=i.validate(e),s=c[0],p=c[1];if(s)return(s.errors||[s]).forEach(function(e){e.path=[t].concat(e.path),e.data=r,d.push(e)}),y=t,"continue";f[t]=p,y=t};for(var y in r)p(y);return d.length?[e({},d[0],{errors:d})]:[void 0,f]})},enum:p,enums:function(r,t,e){return l([p(r,void 0)],t,e)},function:y,instance:function(r,t,e){var n="instance<"+r.name+">";return new A("instance",n,function(e){return void 0===e&&(e=s(t)),e instanceof r?[void 0,e]:[{data:e,path:[],value:e,type:n}]})},interface:function(r,t,n){if("object"!==S(r))throw new Error("Interface structs must be defined as an object, but you passed: "+r);var a=b("object",void 0,n),o=[],i={};for(var u in r){o.push(u);var c=d(r[u],void 0,n);i[u]=c}var f="{"+o.join()+"}";return new A("interface",f,function(r){void 0===r&&(r=s(t));var n=a.validate(r)[0];if(n)return n.type=f,[n];var o=[],u=r,c=function(e){var n=r[e],a=i[e];void 0===n&&(n=s(t&&t[e],r));var c=a.validate(n,r),f=c[0],d=c[1];if(f)return(f.errors||[f]).forEach(function(t){t.path=[e].concat(t.path),t.data=r,o.push(t)}),"continue";(e in r||void 0!==d)&&(u[e]=d)};for(var d in i)c(d);return o.length?[e({},o[0],{errors:o})]:[void 0,u]})},lazy:function(r,t,e){if("function"!==S(r))throw new Error("Lazy structs must be defined as an function that returns a schema, but you passed: "+r);var n,a;return n=new A("lazy","lazy...",function(t){return a=r(),n.name=a.kind,n.type=a.type,n.validate=a.validate,n.validate(t)})},list:l,literal:function(r,t,e){var n="literal: "+JSON.stringify(r);return new A("literal",n,function(e){return void 0===e&&(e=s(t)),e===r?[void 0,e]:[{data:e,path:[],value:e,type:n}]})},object:v,optional:h,partial:function(r,t,n){if("object"!==S(r))throw new Error("Partial structs must be defined as an object, but you passed: "+r);var a=b("object",void 0,n),o=[],i={};for(var u in r){o.push(u);var c=d(r[u],void 0,n);i[u]=c}var f="{"+o.join()+",...}";return new A("partial",f,function(r){void 0===r&&(r=s(t));var n=a.validate(r)[0];if(n)return n.type=f,[n];var o=[],u={},c=function(e){var n=r[e],a=i[e];void 0===n&&(n=s(t&&t[e],r));var c=a.validate(n,r),f=c[0],d=c[1];if(f)return(f.errors||[f]).forEach(function(t){t.path=[e].concat(t.path),t.data=r,o.push(t)}),"continue";(e in r||void 0!==d)&&(u[e]=d)};for(var d in i)c(d);return o.length?[e({},o[0],{errors:o})]:[void 0,u]})},scalar:b,tuple:m,union:w,intersection:g,dynamic:function(r,t,e){if("function"!==S(r))throw new Error("Dynamic structs must be defined as a function, but you passed: "+r);return new A("dynamic","dynamic...",function(e,n){void 0===e&&(e=s(t));var a=r(e,n);if("function"!==S(a))throw new Error("Dynamic structs must return a schema, but you passed: "+a);var o=a.validate(e),i=o[0],u=o[1];return i?[i]:[void 0,u]})}},P={any:function(r){return void 0!==r}};["arguments","array","boolean","buffer","error","float32array","float64array","function","generatorfunction","int16array","int32array","int8array","map","null","number","object","promise","regexp","set","string","symbol","uint16array","uint32array","uint8array","uint8clampedarray","undefined","weakmap","weakset"].forEach(function(r){P[r]=function(t){return S(t)===r}}),P.date=function(r){return"date"===S(r)&&!isNaN(r)};var D=j();r.struct=D,r.superstruct=j,r.isStruct=f,r.StructError=E,Object.defineProperty(r,"__esModule",{value:!0})}); |
Sorry, the diff of this file is not supported yet
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
259560
3990