superstruct
Advanced tools
Comparing version 0.4.2 to 0.4.3
@@ -159,30 +159,16 @@ /** | ||
/** | ||
* The types that `kind-of` supports.. | ||
* A private string to identify structs by. | ||
* | ||
* @type {Array} | ||
* @type {String} | ||
*/ | ||
const KIND_OF_TYPES = ['arguments', 'array', 'boolean', 'buffer', 'date', 'error', 'float32array', 'float64array', 'function', 'generatorfunction', 'int16array', 'int32array', 'int8array', 'map', 'null', 'number', 'object', 'regexp', 'set', 'string', 'symbol', 'uint16array', 'uint32array', 'uint8array', 'uint8clampedarray', 'undefined', 'weakmap', 'weakset']; | ||
const IS_STRUCT = '@@__STRUCT__@@'; | ||
/** | ||
* The default types that Superstruct ships with. | ||
* A private string to refer to a struct's kind. | ||
* | ||
* @type {Object} | ||
*/ | ||
const TYPES = { | ||
any: value => value !== undefined | ||
}; | ||
KIND_OF_TYPES.forEach(type => { | ||
TYPES[type] = value => kindOf(value) === type; | ||
}); | ||
/** | ||
* A private string to identify structs by. | ||
* | ||
* @type {String} | ||
*/ | ||
const IS_STRUCT = '@@__STRUCT__@@'; | ||
const KIND = '@@__KIND__@@'; | ||
@@ -202,2 +188,4 @@ /** | ||
* Kind. | ||
* | ||
* @type {Kind} | ||
*/ | ||
@@ -224,3 +212,3 @@ | ||
function any(schema, defaults, options) { | ||
if (isStruct(schema)) return schema.__kind; | ||
if (isStruct(schema)) return schema[KIND]; | ||
if (schema instanceof Kind) return schema; | ||
@@ -749,8 +737,8 @@ | ||
/** | ||
* Export. | ||
* Kinds. | ||
* | ||
* @type {Function} | ||
* @type {Object} | ||
*/ | ||
var KINDS = { | ||
const Kinds = { | ||
any, | ||
@@ -767,4 +755,33 @@ dict, | ||
intersection | ||
/** | ||
* Export. | ||
* | ||
* @type {Object} | ||
*/ | ||
}; | ||
/** | ||
* The types that `kind-of` supports. | ||
* | ||
* @type {Array} | ||
*/ | ||
const TYPES = ['arguments', 'array', 'boolean', 'buffer', 'date', 'error', 'float32array', 'float64array', 'function', 'generatorfunction', 'int16array', 'int32array', 'int8array', 'map', 'null', 'number', 'object', 'regexp', 'set', 'string', 'symbol', 'uint16array', 'uint32array', 'uint8array', 'uint8clampedarray', 'undefined', 'weakmap', 'weakset']; | ||
/** | ||
* The default types that Superstruct ships with. | ||
* | ||
* @type {Object} | ||
*/ | ||
const Types = { | ||
any: value => value !== undefined | ||
}; | ||
TYPES.forEach(type => { | ||
Types[type] = value => kindOf(value) === type; | ||
}); | ||
var _extends = Object.assign || function (target) { | ||
@@ -792,3 +809,3 @@ for (var i = 1; i < arguments.length; i++) { | ||
function superstruct(config = {}) { | ||
const types = _extends({}, TYPES, config.types || {}); | ||
const types = _extends({}, Types, config.types || {}); | ||
@@ -807,3 +824,3 @@ /** | ||
const kind = KINDS.any(schema, defaults$$1, _extends({}, options, { types })); | ||
const kind = Kinds.any(schema, defaults$$1, _extends({}, options, { types })); | ||
@@ -823,3 +840,3 @@ function Struct(data) { | ||
Object.defineProperty(Struct, IS_STRUCT, { value: true }); | ||
Object.defineProperty(Struct, '__kind', { value: kind }); | ||
Object.defineProperty(Struct, KIND, { value: kind }); | ||
@@ -856,4 +873,4 @@ Struct.kind = kind.name; | ||
Object.keys(KINDS).forEach(name => { | ||
const kind = KINDS[name]; | ||
Object.keys(Kinds).forEach(name => { | ||
const kind = Kinds[name]; | ||
@@ -860,0 +877,0 @@ struct[name] = (schema, defaults$$1, options) => { |
@@ -163,30 +163,16 @@ 'use strict'; | ||
/** | ||
* The types that `kind-of` supports.. | ||
* A private string to identify structs by. | ||
* | ||
* @type {Array} | ||
* @type {String} | ||
*/ | ||
const KIND_OF_TYPES = ['arguments', 'array', 'boolean', 'buffer', 'date', 'error', 'float32array', 'float64array', 'function', 'generatorfunction', 'int16array', 'int32array', 'int8array', 'map', 'null', 'number', 'object', 'regexp', 'set', 'string', 'symbol', 'uint16array', 'uint32array', 'uint8array', 'uint8clampedarray', 'undefined', 'weakmap', 'weakset']; | ||
const IS_STRUCT = '@@__STRUCT__@@'; | ||
/** | ||
* The default types that Superstruct ships with. | ||
* A private string to refer to a struct's kind. | ||
* | ||
* @type {Object} | ||
*/ | ||
const TYPES = { | ||
any: value => value !== undefined | ||
}; | ||
KIND_OF_TYPES.forEach(type => { | ||
TYPES[type] = value => kindOf(value) === type; | ||
}); | ||
/** | ||
* A private string to identify structs by. | ||
* | ||
* @type {String} | ||
*/ | ||
const IS_STRUCT = '@@__STRUCT__@@'; | ||
const KIND = '@@__KIND__@@'; | ||
@@ -206,2 +192,4 @@ /** | ||
* Kind. | ||
* | ||
* @type {Kind} | ||
*/ | ||
@@ -228,3 +216,3 @@ | ||
function any(schema, defaults, options) { | ||
if (isStruct(schema)) return schema.__kind; | ||
if (isStruct(schema)) return schema[KIND]; | ||
if (schema instanceof Kind) return schema; | ||
@@ -753,8 +741,8 @@ | ||
/** | ||
* Export. | ||
* Kinds. | ||
* | ||
* @type {Function} | ||
* @type {Object} | ||
*/ | ||
var KINDS = { | ||
const Kinds = { | ||
any, | ||
@@ -771,4 +759,33 @@ dict, | ||
intersection | ||
/** | ||
* Export. | ||
* | ||
* @type {Object} | ||
*/ | ||
}; | ||
/** | ||
* The types that `kind-of` supports. | ||
* | ||
* @type {Array} | ||
*/ | ||
const TYPES = ['arguments', 'array', 'boolean', 'buffer', 'date', 'error', 'float32array', 'float64array', 'function', 'generatorfunction', 'int16array', 'int32array', 'int8array', 'map', 'null', 'number', 'object', 'regexp', 'set', 'string', 'symbol', 'uint16array', 'uint32array', 'uint8array', 'uint8clampedarray', 'undefined', 'weakmap', 'weakset']; | ||
/** | ||
* The default types that Superstruct ships with. | ||
* | ||
* @type {Object} | ||
*/ | ||
const Types = { | ||
any: value => value !== undefined | ||
}; | ||
TYPES.forEach(type => { | ||
Types[type] = value => kindOf(value) === type; | ||
}); | ||
var _extends = Object.assign || function (target) { | ||
@@ -796,3 +813,3 @@ for (var i = 1; i < arguments.length; i++) { | ||
function superstruct(config = {}) { | ||
const types = _extends({}, TYPES, config.types || {}); | ||
const types = _extends({}, Types, config.types || {}); | ||
@@ -811,3 +828,3 @@ /** | ||
const kind = KINDS.any(schema, defaults$$1, _extends({}, options, { types })); | ||
const kind = Kinds.any(schema, defaults$$1, _extends({}, options, { types })); | ||
@@ -827,3 +844,3 @@ function Struct(data) { | ||
Object.defineProperty(Struct, IS_STRUCT, { value: true }); | ||
Object.defineProperty(Struct, '__kind', { value: kind }); | ||
Object.defineProperty(Struct, KIND, { value: kind }); | ||
@@ -860,4 +877,4 @@ Struct.kind = kind.name; | ||
Object.keys(KINDS).forEach(name => { | ||
const kind = KINDS[name]; | ||
Object.keys(Kinds).forEach(name => { | ||
const kind = Kinds[name]; | ||
@@ -864,0 +881,0 @@ struct[name] = (schema, defaults$$1, options) => { |
{ | ||
"name": "superstruct", | ||
"description": "A simple, expressive way to validate data in JavaScript.", | ||
"version": "0.4.2", | ||
"version": "0.4.3", | ||
"license": "MIT", | ||
@@ -6,0 +6,0 @@ "repository": "git://github.com/ianstormtaylor/superstruct.git", |
@@ -165,30 +165,16 @@ (function (global, factory) { | ||
/** | ||
* The types that `kind-of` supports.. | ||
* A private string to identify structs by. | ||
* | ||
* @type {Array} | ||
* @type {String} | ||
*/ | ||
const KIND_OF_TYPES = ['arguments', 'array', 'boolean', 'buffer', 'date', 'error', 'float32array', 'float64array', 'function', 'generatorfunction', 'int16array', 'int32array', 'int8array', 'map', 'null', 'number', 'object', 'regexp', 'set', 'string', 'symbol', 'uint16array', 'uint32array', 'uint8array', 'uint8clampedarray', 'undefined', 'weakmap', 'weakset']; | ||
const IS_STRUCT = '@@__STRUCT__@@'; | ||
/** | ||
* The default types that Superstruct ships with. | ||
* A private string to refer to a struct's kind. | ||
* | ||
* @type {Object} | ||
*/ | ||
const TYPES = { | ||
any: value => value !== undefined | ||
}; | ||
KIND_OF_TYPES.forEach(type => { | ||
TYPES[type] = value => kindOf(value) === type; | ||
}); | ||
/** | ||
* A private string to identify structs by. | ||
* | ||
* @type {String} | ||
*/ | ||
const IS_STRUCT = '@@__STRUCT__@@'; | ||
const KIND = '@@__KIND__@@'; | ||
@@ -208,2 +194,4 @@ /** | ||
* Kind. | ||
* | ||
* @type {Kind} | ||
*/ | ||
@@ -230,3 +218,3 @@ | ||
function any(schema, defaults, options) { | ||
if (isStruct(schema)) return schema.__kind; | ||
if (isStruct(schema)) return schema[KIND]; | ||
if (schema instanceof Kind) return schema; | ||
@@ -755,8 +743,8 @@ | ||
/** | ||
* Export. | ||
* Kinds. | ||
* | ||
* @type {Function} | ||
* @type {Object} | ||
*/ | ||
var KINDS = { | ||
const Kinds = { | ||
any, | ||
@@ -773,4 +761,33 @@ dict, | ||
intersection | ||
/** | ||
* Export. | ||
* | ||
* @type {Object} | ||
*/ | ||
}; | ||
/** | ||
* The types that `kind-of` supports. | ||
* | ||
* @type {Array} | ||
*/ | ||
const TYPES = ['arguments', 'array', 'boolean', 'buffer', 'date', 'error', 'float32array', 'float64array', 'function', 'generatorfunction', 'int16array', 'int32array', 'int8array', 'map', 'null', 'number', 'object', 'regexp', 'set', 'string', 'symbol', 'uint16array', 'uint32array', 'uint8array', 'uint8clampedarray', 'undefined', 'weakmap', 'weakset']; | ||
/** | ||
* The default types that Superstruct ships with. | ||
* | ||
* @type {Object} | ||
*/ | ||
const Types = { | ||
any: value => value !== undefined | ||
}; | ||
TYPES.forEach(type => { | ||
Types[type] = value => kindOf(value) === type; | ||
}); | ||
var _extends = Object.assign || function (target) { | ||
@@ -798,3 +815,3 @@ for (var i = 1; i < arguments.length; i++) { | ||
function superstruct(config = {}) { | ||
const types = _extends({}, TYPES, config.types || {}); | ||
const types = _extends({}, Types, config.types || {}); | ||
@@ -813,3 +830,3 @@ /** | ||
const kind = KINDS.any(schema, defaults$$1, _extends({}, options, { types })); | ||
const kind = Kinds.any(schema, defaults$$1, _extends({}, options, { types })); | ||
@@ -829,3 +846,3 @@ function Struct(data) { | ||
Object.defineProperty(Struct, IS_STRUCT, { value: true }); | ||
Object.defineProperty(Struct, '__kind', { value: kind }); | ||
Object.defineProperty(Struct, KIND, { value: kind }); | ||
@@ -862,4 +879,4 @@ Struct.kind = kind.name; | ||
Object.keys(KINDS).forEach(name => { | ||
const kind = KINDS[name]; | ||
Object.keys(Kinds).forEach(name => { | ||
const kind = Kinds[name]; | ||
@@ -866,0 +883,0 @@ struct[name] = (schema, defaults$$1, options) => { |
@@ -1,1 +0,1 @@ | ||
(function(global,factory){typeof exports==="object"&&typeof module!=="undefined"?factory(exports):typeof define==="function"&&define.amd?define(["exports"],factory):factory(global.Superstruct={})})(this,function(exports){"use strict";class StructError extends TypeError{constructor(attrs){const{data:data,value:value,type:type,path:path,errors:errors=[]}=attrs;const message=`Expected a value of type \`${type}\`${path.length?` for \`${path.join(".")}\``:""} but received \`${value}\`.`;super(message);this.data=data;this.path=path;this.value=value;this.type=type;this.errors=errors;if(!errors.length)errors.push(this);if(Error.captureStackTrace){Error.captureStackTrace(this,this.constructor)}else{this.stack=(new Error).stack}}}var toString=Object.prototype.toString;var kindOf=function kindOf(val){if(val===void 0)return"undefined";if(val===null)return"null";var type=typeof val;if(type==="boolean")return"boolean";if(type==="string")return"string";if(type==="number")return"number";if(type==="symbol")return"symbol";if(type==="function"){return isGeneratorFn(val)?"generatorfunction":"function"}if(isArray(val))return"array";if(isBuffer(val))return"buffer";if(isArguments(val))return"arguments";if(isDate(val))return"date";if(isError(val))return"error";if(isRegexp(val))return"regexp";switch(ctorName(val)){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(isGeneratorObj(val)){return"generator"}type=toString.call(val);switch(type){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 type.slice(8,-1).toLowerCase().replace(/\s/g,"")};function ctorName(val){return val.constructor?val.constructor.name:null}function isArray(val){if(Array.isArray)return Array.isArray(val);return val instanceof Array}function isError(val){return val instanceof Error||typeof val.message==="string"&&val.constructor&&typeof val.constructor.stackTraceLimit==="number"}function isDate(val){if(val instanceof Date)return true;return typeof val.toDateString==="function"&&typeof val.getDate==="function"&&typeof val.setDate==="function"}function isRegexp(val){if(val instanceof RegExp)return true;return typeof val.flags==="string"&&typeof val.ignoreCase==="boolean"&&typeof val.multiline==="boolean"&&typeof val.global==="boolean"}function isGeneratorFn(name,val){return ctorName(name)==="GeneratorFunction"}function isGeneratorObj(val){return typeof val.throw==="function"&&typeof val.return==="function"&&typeof val.next==="function"}function isArguments(val){try{if(typeof val.length==="number"&&typeof val.callee==="function"){return true}}catch(err){if(err.message.indexOf("callee")!==-1){return true}}return false}function isBuffer(val){if(val.constructor&&typeof val.constructor.isBuffer==="function"){return val.constructor.isBuffer(val)}return false}const KIND_OF_TYPES=["arguments","array","boolean","buffer","date","error","float32array","float64array","function","generatorfunction","int16array","int32array","int8array","map","null","number","object","regexp","set","string","symbol","uint16array","uint32array","uint8array","uint8clampedarray","undefined","weakmap","weakset"];const TYPES={any:value=>value!==undefined};KIND_OF_TYPES.forEach(type=>{TYPES[type]=(value=>kindOf(value)===type)});const IS_STRUCT="@@__STRUCT__@@";function isStruct(value){return!!(value&&value[IS_STRUCT])}class Kind{constructor(name,type,validate){this.name=name;this.type=type;this.validate=validate}}function any(schema,defaults,options){if(isStruct(schema))return schema.__kind;if(schema instanceof Kind)return schema;switch(kindOf(schema)){case"array":{return schema.length>1?tuple(schema,defaults,options):list(schema,defaults,options)}case"function":{return func(schema,defaults,options)}case"object":{return object(schema,defaults,options)}case"string":{let required=true;let type;if(schema.endsWith("?")){required=false;schema=schema.slice(0,-1)}if(schema.includes("|")){const 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,options)}else{type=scalar(schema,defaults,options)}if(!required){type=optional(type,undefined,options)}return type}}{throw new Error(`Invalid schema: ${schema}`)}}function dict(schema,defaults,options){if(kindOf(schema)!=="array"||schema.length!==2){{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=defaults)=>{const[error]=obj.validate(value);if(error){error.type=type;return[error]}const ret={};const errors=[];for(let k in value){const v=value[k];const[e,r]=keys.validate(k);if(e){e.path=[k].concat(e.path);e.data=value;errors.push(e);continue}k=r;const[e2,r2]=values.validate(v);if(e2){e2.path=[k].concat(e2.path);e2.data=value;errors.push(e2);continue}ret[k]=r2}if(errors.length){const first=errors[0];first.errors=errors;return[first]}return[undefined,ret]};return new Kind(name,type,validate)}function enums(schema,defaults,options){if(kindOf(schema)!=="array"){{throw new Error(`Invalid schema: ${schema}`)}}const name="enum";const type=schema.map(s=>{try{return JSON.stringify(s)}catch(e){return String(s)}}).join(" | ");const validate=(value=defaults)=>{return schema.includes(value)?[undefined,value]:[{data:value,path:[],value:value,type:type}]};return new Kind(name,type,validate)}function func(schema,defaults,options){if(kindOf(schema)!=="function"){{throw new Error(`Invalid schema: ${schema}`)}}const name="function";const type="<function>";const validate=(value=defaults)=>{return schema(value)?[undefined,value]:[{type:type,value:value,data:value,path:[]}]};return new Kind(name,type,validate)}function list(schema,defaults,options){if(kindOf(schema)!=="array"||schema.length!==1){{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=defaults)=>{const[error,result]=array.validate(value);if(error){error.type=type;return[error]}value=result;const errors=[];const ret=[];for(let i=0;i<value.length;i++){const v=value[i];const[e,r]=element.validate(v);if(e){e.path=[i].concat(e.path);e.data=value;errors.push(e);continue}ret[i]=r}if(errors.length){const first=errors[0];first.errors=errors;return[first]}return[undefined,ret]};return new Kind(name,type,validate)}function object(schema,defaults,options){if(kindOf(schema)!=="object"){{throw new Error(`Invalid schema: ${schema}`)}}const obj=scalar("object",undefined,options);const ks=[];const properties={};for(const key in schema){ks.push(key);const s=schema[key];const d=defaults&&defaults[key];const kind=any(s,d,options);properties[key]=kind}const name="object";const type=`{${ks.join()}}`;const validate=(value=defaults)=>{const[error,result]=obj.validate(value);if(error){error.type=type;return[error]}value=result;const errors=[];const ret={};const valueKeys=Object.keys(value);const schemaKeys=Object.keys(properties);const keys=new Set(valueKeys.concat(schemaKeys));keys.forEach(key=>{const v=value[key];const kind=properties[key];if(!kind){const e={data:value,path:[key],value:v};errors.push(e);return}const[e,r]=kind.validate(v);if(e){e.path=[key].concat(e.path);e.data=value;errors.push(e);return}if(key in value){ret[key]=r}});if(errors.length){const first=errors[0];first.errors=errors;return[first]}return[undefined,ret]};return new Kind(name,type,validate)}function optional(schema,defaults,options){return union([schema,"undefined"],defaults,options)}function scalar(schema,defaults,options){if(kindOf(schema)!=="string"){{throw new Error(`Invalid schema: ${schema}`)}}const{types:types}=options;const fn=types[schema];if(kindOf(fn)!=="function"){{throw new Error(`Invalid type: ${schema}`)}}const kind=func(fn,defaults,options);const name="scalar";const type=schema;const validate=value=>{const[error,result]=kind.validate(value);if(error){error.type=type;return[error]}return[undefined,result]};return new Kind(name,type,validate)}function tuple(schema,defaults,options){if(kindOf(schema)!=="array"){{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=defaults)=>{const[error]=array.validate(value);if(error){error.type=type;return[error]}const ret=[];const errors=[];const length=Math.max(value.length,kinds.length);for(let i=0;i<length;i++){const kind=kinds[i];const v=value[i];if(!kind){const e={data:value,path:[i],value:v};errors.push(e);continue}const[e,r]=kind.validate(v);if(e){e.path=[i].concat(e.path);e.data=value;errors.push(e);continue}ret[i]=r}if(errors.length){const first=errors[0];first.errors=errors;return[first]}return[undefined,ret]};return new Kind(name,type,validate)}function union(schema,defaults,options){if(kindOf(schema)!=="array"){{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=defaults)=>{let error;for(const k of kinds){const[e,r]=k.validate(value);if(!e)return[undefined,r];error=e}error.type=type;return[error]};return new Kind(name,type,validate)}function intersection(schema,defaults,options){if(kindOf(schema)!=="array"){{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=defaults)=>{let v=value;for(const t of types){const[e,r]=t.validate(v);if(e){e.type=type;return[e]}v=r}return[undefined,v]};return new Kind(name,type,validate)}var KINDS={any:any,dict:dict,enum:enums,function:func,list:list,object:object,optional:optional,scalar:scalar,tuple:tuple,union:union,intersection:intersection};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};function superstruct(config={}){const types=_extends({},TYPES,config.types||{});function struct(schema,defaults$$1,options={}){if(isStruct(schema))schema=schema.schema;const kind=KINDS.any(schema,defaults$$1,_extends({},options,{types:types}));function Struct(data){if(this instanceof Struct){{throw new Error("Invalid `new` keyword!")}}return Struct.assert(data)}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.options=options;Struct.assert=(value=>{const[error,result]=kind.validate(value);if(error)throw new StructError(error);return result});Struct.test=(value=>{const[error]=kind.validate(value);return!error});Struct.validate=(value=>{const[error,result]=kind.validate(value);if(error)return[new StructError(error)];return[undefined,result]});return 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:types}));const s=struct(type,defaults$$1,options);return s})});return struct}const struct=superstruct();exports.struct=struct;exports.superstruct=superstruct;exports.StructError=StructError;Object.defineProperty(exports,"__esModule",{value:true})}); | ||
(function(global,factory){typeof exports==="object"&&typeof module!=="undefined"?factory(exports):typeof define==="function"&&define.amd?define(["exports"],factory):factory(global.Superstruct={})})(this,function(exports){"use strict";class StructError extends TypeError{constructor(attrs){const{data:data,value:value,type:type,path:path,errors:errors=[]}=attrs;const message=`Expected a value of type \`${type}\`${path.length?` for \`${path.join(".")}\``:""} but received \`${value}\`.`;super(message);this.data=data;this.path=path;this.value=value;this.type=type;this.errors=errors;if(!errors.length)errors.push(this);if(Error.captureStackTrace){Error.captureStackTrace(this,this.constructor)}else{this.stack=(new Error).stack}}}var toString=Object.prototype.toString;var kindOf=function kindOf(val){if(val===void 0)return"undefined";if(val===null)return"null";var type=typeof val;if(type==="boolean")return"boolean";if(type==="string")return"string";if(type==="number")return"number";if(type==="symbol")return"symbol";if(type==="function"){return isGeneratorFn(val)?"generatorfunction":"function"}if(isArray(val))return"array";if(isBuffer(val))return"buffer";if(isArguments(val))return"arguments";if(isDate(val))return"date";if(isError(val))return"error";if(isRegexp(val))return"regexp";switch(ctorName(val)){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(isGeneratorObj(val)){return"generator"}type=toString.call(val);switch(type){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 type.slice(8,-1).toLowerCase().replace(/\s/g,"")};function ctorName(val){return val.constructor?val.constructor.name:null}function isArray(val){if(Array.isArray)return Array.isArray(val);return val instanceof Array}function isError(val){return val instanceof Error||typeof val.message==="string"&&val.constructor&&typeof val.constructor.stackTraceLimit==="number"}function isDate(val){if(val instanceof Date)return true;return typeof val.toDateString==="function"&&typeof val.getDate==="function"&&typeof val.setDate==="function"}function isRegexp(val){if(val instanceof RegExp)return true;return typeof val.flags==="string"&&typeof val.ignoreCase==="boolean"&&typeof val.multiline==="boolean"&&typeof val.global==="boolean"}function isGeneratorFn(name,val){return ctorName(name)==="GeneratorFunction"}function isGeneratorObj(val){return typeof val.throw==="function"&&typeof val.return==="function"&&typeof val.next==="function"}function isArguments(val){try{if(typeof val.length==="number"&&typeof val.callee==="function"){return true}}catch(err){if(err.message.indexOf("callee")!==-1){return true}}return false}function isBuffer(val){if(val.constructor&&typeof val.constructor.isBuffer==="function"){return val.constructor.isBuffer(val)}return false}const IS_STRUCT="@@__STRUCT__@@";const KIND="@@__KIND__@@";function isStruct(value){return!!(value&&value[IS_STRUCT])}class Kind{constructor(name,type,validate){this.name=name;this.type=type;this.validate=validate}}function any(schema,defaults,options){if(isStruct(schema))return schema[KIND];if(schema instanceof Kind)return schema;switch(kindOf(schema)){case"array":{return schema.length>1?tuple(schema,defaults,options):list(schema,defaults,options)}case"function":{return func(schema,defaults,options)}case"object":{return object(schema,defaults,options)}case"string":{let required=true;let type;if(schema.endsWith("?")){required=false;schema=schema.slice(0,-1)}if(schema.includes("|")){const 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,options)}else{type=scalar(schema,defaults,options)}if(!required){type=optional(type,undefined,options)}return type}}{throw new Error(`Invalid schema: ${schema}`)}}function dict(schema,defaults,options){if(kindOf(schema)!=="array"||schema.length!==2){{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=defaults)=>{const[error]=obj.validate(value);if(error){error.type=type;return[error]}const ret={};const errors=[];for(let k in value){const v=value[k];const[e,r]=keys.validate(k);if(e){e.path=[k].concat(e.path);e.data=value;errors.push(e);continue}k=r;const[e2,r2]=values.validate(v);if(e2){e2.path=[k].concat(e2.path);e2.data=value;errors.push(e2);continue}ret[k]=r2}if(errors.length){const first=errors[0];first.errors=errors;return[first]}return[undefined,ret]};return new Kind(name,type,validate)}function enums(schema,defaults,options){if(kindOf(schema)!=="array"){{throw new Error(`Invalid schema: ${schema}`)}}const name="enum";const type=schema.map(s=>{try{return JSON.stringify(s)}catch(e){return String(s)}}).join(" | ");const validate=(value=defaults)=>{return schema.includes(value)?[undefined,value]:[{data:value,path:[],value:value,type:type}]};return new Kind(name,type,validate)}function func(schema,defaults,options){if(kindOf(schema)!=="function"){{throw new Error(`Invalid schema: ${schema}`)}}const name="function";const type="<function>";const validate=(value=defaults)=>{return schema(value)?[undefined,value]:[{type:type,value:value,data:value,path:[]}]};return new Kind(name,type,validate)}function list(schema,defaults,options){if(kindOf(schema)!=="array"||schema.length!==1){{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=defaults)=>{const[error,result]=array.validate(value);if(error){error.type=type;return[error]}value=result;const errors=[];const ret=[];for(let i=0;i<value.length;i++){const v=value[i];const[e,r]=element.validate(v);if(e){e.path=[i].concat(e.path);e.data=value;errors.push(e);continue}ret[i]=r}if(errors.length){const first=errors[0];first.errors=errors;return[first]}return[undefined,ret]};return new Kind(name,type,validate)}function object(schema,defaults,options){if(kindOf(schema)!=="object"){{throw new Error(`Invalid schema: ${schema}`)}}const obj=scalar("object",undefined,options);const ks=[];const properties={};for(const key in schema){ks.push(key);const s=schema[key];const d=defaults&&defaults[key];const kind=any(s,d,options);properties[key]=kind}const name="object";const type=`{${ks.join()}}`;const validate=(value=defaults)=>{const[error,result]=obj.validate(value);if(error){error.type=type;return[error]}value=result;const errors=[];const ret={};const valueKeys=Object.keys(value);const schemaKeys=Object.keys(properties);const keys=new Set(valueKeys.concat(schemaKeys));keys.forEach(key=>{const v=value[key];const kind=properties[key];if(!kind){const e={data:value,path:[key],value:v};errors.push(e);return}const[e,r]=kind.validate(v);if(e){e.path=[key].concat(e.path);e.data=value;errors.push(e);return}if(key in value){ret[key]=r}});if(errors.length){const first=errors[0];first.errors=errors;return[first]}return[undefined,ret]};return new Kind(name,type,validate)}function optional(schema,defaults,options){return union([schema,"undefined"],defaults,options)}function scalar(schema,defaults,options){if(kindOf(schema)!=="string"){{throw new Error(`Invalid schema: ${schema}`)}}const{types:types}=options;const fn=types[schema];if(kindOf(fn)!=="function"){{throw new Error(`Invalid type: ${schema}`)}}const kind=func(fn,defaults,options);const name="scalar";const type=schema;const validate=value=>{const[error,result]=kind.validate(value);if(error){error.type=type;return[error]}return[undefined,result]};return new Kind(name,type,validate)}function tuple(schema,defaults,options){if(kindOf(schema)!=="array"){{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=defaults)=>{const[error]=array.validate(value);if(error){error.type=type;return[error]}const ret=[];const errors=[];const length=Math.max(value.length,kinds.length);for(let i=0;i<length;i++){const kind=kinds[i];const v=value[i];if(!kind){const e={data:value,path:[i],value:v};errors.push(e);continue}const[e,r]=kind.validate(v);if(e){e.path=[i].concat(e.path);e.data=value;errors.push(e);continue}ret[i]=r}if(errors.length){const first=errors[0];first.errors=errors;return[first]}return[undefined,ret]};return new Kind(name,type,validate)}function union(schema,defaults,options){if(kindOf(schema)!=="array"){{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=defaults)=>{let error;for(const k of kinds){const[e,r]=k.validate(value);if(!e)return[undefined,r];error=e}error.type=type;return[error]};return new Kind(name,type,validate)}function intersection(schema,defaults,options){if(kindOf(schema)!=="array"){{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=defaults)=>{let v=value;for(const t of types){const[e,r]=t.validate(v);if(e){e.type=type;return[e]}v=r}return[undefined,v]};return new Kind(name,type,validate)}const Kinds={any:any,dict:dict,enum:enums,function:func,list:list,object:object,optional:optional,scalar:scalar,tuple:tuple,union:union,intersection:intersection};const TYPES=["arguments","array","boolean","buffer","date","error","float32array","float64array","function","generatorfunction","int16array","int32array","int8array","map","null","number","object","regexp","set","string","symbol","uint16array","uint32array","uint8array","uint8clampedarray","undefined","weakmap","weakset"];const Types={any:value=>value!==undefined};TYPES.forEach(type=>{Types[type]=(value=>kindOf(value)===type)});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};function superstruct(config={}){const types=_extends({},Types,config.types||{});function struct(schema,defaults$$1,options={}){if(isStruct(schema))schema=schema.schema;const kind=Kinds.any(schema,defaults$$1,_extends({},options,{types:types}));function Struct(data){if(this instanceof Struct){{throw new Error("Invalid `new` keyword!")}}return Struct.assert(data)}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.options=options;Struct.assert=(value=>{const[error,result]=kind.validate(value);if(error)throw new StructError(error);return result});Struct.test=(value=>{const[error]=kind.validate(value);return!error});Struct.validate=(value=>{const[error,result]=kind.validate(value);if(error)return[new StructError(error)];return[undefined,result]});return 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:types}));const s=struct(type,defaults$$1,options);return s})});return struct}const struct=superstruct();exports.struct=struct;exports.superstruct=superstruct;exports.StructError=StructError;Object.defineProperty(exports,"__esModule",{value:true})}); |
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
166492
2243