superstruct
Advanced tools
Comparing version 0.4.6 to 0.4.7
@@ -186,2 +186,13 @@ /** | ||
/** | ||
* Resolve a `defaults` value. | ||
* | ||
* @param {Function|Any} defaults | ||
* @return {Any} | ||
*/ | ||
function resolveDefaults(defaults) { | ||
return typeof defaults === 'function' ? defaults() : defaults; | ||
} | ||
/** | ||
* Kind. | ||
@@ -287,3 +298,3 @@ * | ||
const type = `dict<${keys.type},${values.type}>`; | ||
const validate = (value = defaults) => { | ||
const validate = (value = resolveDefaults(defaults)) => { | ||
const [error] = obj.validate(value); | ||
@@ -343,3 +354,3 @@ | ||
function enums(schema, defaults, options) { | ||
function en(schema, defaults, options) { | ||
if (kindOf(schema) !== 'array') { | ||
@@ -362,3 +373,3 @@ if (process.env.NODE_ENV !== 'production') { | ||
const validate = (value = defaults) => { | ||
const validate = (value = resolveDefaults(defaults)) => { | ||
return schema.includes(value) ? [undefined, value] : [{ data: value, path: [], value, type }]; | ||
@@ -371,2 +382,16 @@ }; | ||
/** | ||
* Enums. | ||
* | ||
* @param {Array} schema | ||
* @param {Any} defaults | ||
* @param {Object} options | ||
*/ | ||
function enums(schema, defaults, options) { | ||
const e = en(schema, undefined, options); | ||
const l = list([e], defaults, options); | ||
return l; | ||
} | ||
/** | ||
* Function. | ||
@@ -390,3 +415,3 @@ * | ||
const type = '<function>'; | ||
const validate = (value = defaults) => { | ||
const validate = (value = resolveDefaults(defaults)) => { | ||
return schema(value) ? [undefined, value] : [{ type, value, data: value, path: [] }]; | ||
@@ -409,3 +434,3 @@ }; | ||
const type = `instance<${schema.name}>`; | ||
const validate = (value = defaults) => { | ||
const validate = (value = resolveDefaults(defaults)) => { | ||
return value instanceof schema ? [undefined, value] : [{ data: value, path: [], value, type }]; | ||
@@ -446,3 +471,3 @@ }; | ||
const type = `{${ks.join()}}`; | ||
const validate = (value = defaults) => { | ||
const validate = (value = resolveDefaults(defaults)) => { | ||
const errors = []; | ||
@@ -496,3 +521,3 @@ | ||
const type = `[${element.type}]`; | ||
const validate = (value = defaults) => { | ||
const validate = (value = resolveDefaults(defaults)) => { | ||
const [error, result] = array.validate(value); | ||
@@ -546,3 +571,3 @@ | ||
const type = `literal: ${JSON.stringify(schema)}`; | ||
const validate = (value = defaults) => { | ||
const validate = (value = resolveDefaults(defaults)) => { | ||
return value === schema ? [undefined, value] : [{ data: value, path: [], value, type }]; | ||
@@ -585,3 +610,3 @@ }; | ||
const type = `{${ks.join()}}`; | ||
const validate = (value = defaults) => { | ||
const validate = (value = resolveDefaults(defaults)) => { | ||
const [error] = obj.validate(value); | ||
@@ -714,3 +739,3 @@ | ||
const type = `[${kinds.map(k => k.type).join()}]`; | ||
const validate = (value = defaults) => { | ||
const validate = (value = resolveDefaults(defaults)) => { | ||
const [error] = array.validate(value); | ||
@@ -781,3 +806,3 @@ | ||
const type = kinds.map(k => k.type).join(' | '); | ||
const validate = (value = defaults) => { | ||
const validate = (value = resolveDefaults(defaults)) => { | ||
let error; | ||
@@ -818,3 +843,3 @@ | ||
const type = types.map(t => t.type).join(' & '); | ||
const validate = (value = defaults) => { | ||
const validate = (value = resolveDefaults(defaults)) => { | ||
let v = value; | ||
@@ -848,3 +873,4 @@ | ||
dict, | ||
enum: enums, | ||
enum: en, | ||
enums, | ||
function: func, | ||
@@ -926,3 +952,5 @@ instance, | ||
function struct(schema, defaults$$1, options = {}) { | ||
if (isStruct(schema)) schema = schema.schema; | ||
if (isStruct(schema)) { | ||
schema = schema.schema; | ||
} | ||
@@ -994,3 +1022,3 @@ const kind = Kinds.any(schema, defaults$$1, _extends({}, options, { types })); | ||
/** | ||
* Create a simple `struct` method for the default types. | ||
* Create a convenience `struct` factory for the default types. | ||
* | ||
@@ -1002,3 +1030,3 @@ * @type {Function} | ||
export { struct, superstruct, StructError }; | ||
export { struct, superstruct, isStruct, StructError }; | ||
//# sourceMappingURL=index.es.js.map |
@@ -190,2 +190,13 @@ 'use strict'; | ||
/** | ||
* Resolve a `defaults` value. | ||
* | ||
* @param {Function|Any} defaults | ||
* @return {Any} | ||
*/ | ||
function resolveDefaults(defaults) { | ||
return typeof defaults === 'function' ? defaults() : defaults; | ||
} | ||
/** | ||
* Kind. | ||
@@ -291,3 +302,3 @@ * | ||
const type = `dict<${keys.type},${values.type}>`; | ||
const validate = (value = defaults) => { | ||
const validate = (value = resolveDefaults(defaults)) => { | ||
const [error] = obj.validate(value); | ||
@@ -347,3 +358,3 @@ | ||
function enums(schema, defaults, options) { | ||
function en(schema, defaults, options) { | ||
if (kindOf(schema) !== 'array') { | ||
@@ -366,3 +377,3 @@ if (process.env.NODE_ENV !== 'production') { | ||
const validate = (value = defaults) => { | ||
const validate = (value = resolveDefaults(defaults)) => { | ||
return schema.includes(value) ? [undefined, value] : [{ data: value, path: [], value, type }]; | ||
@@ -375,2 +386,16 @@ }; | ||
/** | ||
* Enums. | ||
* | ||
* @param {Array} schema | ||
* @param {Any} defaults | ||
* @param {Object} options | ||
*/ | ||
function enums(schema, defaults, options) { | ||
const e = en(schema, undefined, options); | ||
const l = list([e], defaults, options); | ||
return l; | ||
} | ||
/** | ||
* Function. | ||
@@ -394,3 +419,3 @@ * | ||
const type = '<function>'; | ||
const validate = (value = defaults) => { | ||
const validate = (value = resolveDefaults(defaults)) => { | ||
return schema(value) ? [undefined, value] : [{ type, value, data: value, path: [] }]; | ||
@@ -413,3 +438,3 @@ }; | ||
const type = `instance<${schema.name}>`; | ||
const validate = (value = defaults) => { | ||
const validate = (value = resolveDefaults(defaults)) => { | ||
return value instanceof schema ? [undefined, value] : [{ data: value, path: [], value, type }]; | ||
@@ -450,3 +475,3 @@ }; | ||
const type = `{${ks.join()}}`; | ||
const validate = (value = defaults) => { | ||
const validate = (value = resolveDefaults(defaults)) => { | ||
const errors = []; | ||
@@ -500,3 +525,3 @@ | ||
const type = `[${element.type}]`; | ||
const validate = (value = defaults) => { | ||
const validate = (value = resolveDefaults(defaults)) => { | ||
const [error, result] = array.validate(value); | ||
@@ -550,3 +575,3 @@ | ||
const type = `literal: ${JSON.stringify(schema)}`; | ||
const validate = (value = defaults) => { | ||
const validate = (value = resolveDefaults(defaults)) => { | ||
return value === schema ? [undefined, value] : [{ data: value, path: [], value, type }]; | ||
@@ -589,3 +614,3 @@ }; | ||
const type = `{${ks.join()}}`; | ||
const validate = (value = defaults) => { | ||
const validate = (value = resolveDefaults(defaults)) => { | ||
const [error] = obj.validate(value); | ||
@@ -718,3 +743,3 @@ | ||
const type = `[${kinds.map(k => k.type).join()}]`; | ||
const validate = (value = defaults) => { | ||
const validate = (value = resolveDefaults(defaults)) => { | ||
const [error] = array.validate(value); | ||
@@ -785,3 +810,3 @@ | ||
const type = kinds.map(k => k.type).join(' | '); | ||
const validate = (value = defaults) => { | ||
const validate = (value = resolveDefaults(defaults)) => { | ||
let error; | ||
@@ -822,3 +847,3 @@ | ||
const type = types.map(t => t.type).join(' & '); | ||
const validate = (value = defaults) => { | ||
const validate = (value = resolveDefaults(defaults)) => { | ||
let v = value; | ||
@@ -852,3 +877,4 @@ | ||
dict, | ||
enum: enums, | ||
enum: en, | ||
enums, | ||
function: func, | ||
@@ -930,3 +956,5 @@ instance, | ||
function struct(schema, defaults$$1, options = {}) { | ||
if (isStruct(schema)) schema = schema.schema; | ||
if (isStruct(schema)) { | ||
schema = schema.schema; | ||
} | ||
@@ -998,3 +1026,3 @@ const kind = Kinds.any(schema, defaults$$1, _extends({}, options, { types })); | ||
/** | ||
* Create a simple `struct` method for the default types. | ||
* Create a convenience `struct` factory for the default types. | ||
* | ||
@@ -1008,3 +1036,4 @@ * @type {Function} | ||
exports.superstruct = superstruct; | ||
exports.isStruct = isStruct; | ||
exports.StructError = StructError; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "superstruct", | ||
"description": "A simple, expressive way to validate data in JavaScript.", | ||
"version": "0.4.6", | ||
"version": "0.4.7", | ||
"license": "MIT", | ||
@@ -6,0 +6,0 @@ "repository": "git://github.com/ianstormtaylor/superstruct.git", |
@@ -192,2 +192,13 @@ (function (global, factory) { | ||
/** | ||
* Resolve a `defaults` value. | ||
* | ||
* @param {Function|Any} defaults | ||
* @return {Any} | ||
*/ | ||
function resolveDefaults(defaults) { | ||
return typeof defaults === 'function' ? defaults() : defaults; | ||
} | ||
/** | ||
* Kind. | ||
@@ -293,3 +304,3 @@ * | ||
const type = `dict<${keys.type},${values.type}>`; | ||
const validate = (value = defaults) => { | ||
const validate = (value = resolveDefaults(defaults)) => { | ||
const [error] = obj.validate(value); | ||
@@ -349,3 +360,3 @@ | ||
function enums(schema, defaults, options) { | ||
function en(schema, defaults, options) { | ||
if (kindOf(schema) !== 'array') { | ||
@@ -368,3 +379,3 @@ if (process.env.NODE_ENV !== 'production') { | ||
const validate = (value = defaults) => { | ||
const validate = (value = resolveDefaults(defaults)) => { | ||
return schema.includes(value) ? [undefined, value] : [{ data: value, path: [], value, type }]; | ||
@@ -377,2 +388,16 @@ }; | ||
/** | ||
* Enums. | ||
* | ||
* @param {Array} schema | ||
* @param {Any} defaults | ||
* @param {Object} options | ||
*/ | ||
function enums(schema, defaults, options) { | ||
const e = en(schema, undefined, options); | ||
const l = list([e], defaults, options); | ||
return l; | ||
} | ||
/** | ||
* Function. | ||
@@ -396,3 +421,3 @@ * | ||
const type = '<function>'; | ||
const validate = (value = defaults) => { | ||
const validate = (value = resolveDefaults(defaults)) => { | ||
return schema(value) ? [undefined, value] : [{ type, value, data: value, path: [] }]; | ||
@@ -415,3 +440,3 @@ }; | ||
const type = `instance<${schema.name}>`; | ||
const validate = (value = defaults) => { | ||
const validate = (value = resolveDefaults(defaults)) => { | ||
return value instanceof schema ? [undefined, value] : [{ data: value, path: [], value, type }]; | ||
@@ -452,3 +477,3 @@ }; | ||
const type = `{${ks.join()}}`; | ||
const validate = (value = defaults) => { | ||
const validate = (value = resolveDefaults(defaults)) => { | ||
const errors = []; | ||
@@ -502,3 +527,3 @@ | ||
const type = `[${element.type}]`; | ||
const validate = (value = defaults) => { | ||
const validate = (value = resolveDefaults(defaults)) => { | ||
const [error, result] = array.validate(value); | ||
@@ -552,3 +577,3 @@ | ||
const type = `literal: ${JSON.stringify(schema)}`; | ||
const validate = (value = defaults) => { | ||
const validate = (value = resolveDefaults(defaults)) => { | ||
return value === schema ? [undefined, value] : [{ data: value, path: [], value, type }]; | ||
@@ -591,3 +616,3 @@ }; | ||
const type = `{${ks.join()}}`; | ||
const validate = (value = defaults) => { | ||
const validate = (value = resolveDefaults(defaults)) => { | ||
const [error] = obj.validate(value); | ||
@@ -720,3 +745,3 @@ | ||
const type = `[${kinds.map(k => k.type).join()}]`; | ||
const validate = (value = defaults) => { | ||
const validate = (value = resolveDefaults(defaults)) => { | ||
const [error] = array.validate(value); | ||
@@ -787,3 +812,3 @@ | ||
const type = kinds.map(k => k.type).join(' | '); | ||
const validate = (value = defaults) => { | ||
const validate = (value = resolveDefaults(defaults)) => { | ||
let error; | ||
@@ -824,3 +849,3 @@ | ||
const type = types.map(t => t.type).join(' & '); | ||
const validate = (value = defaults) => { | ||
const validate = (value = resolveDefaults(defaults)) => { | ||
let v = value; | ||
@@ -854,3 +879,4 @@ | ||
dict, | ||
enum: enums, | ||
enum: en, | ||
enums, | ||
function: func, | ||
@@ -932,3 +958,5 @@ instance, | ||
function struct(schema, defaults$$1, options = {}) { | ||
if (isStruct(schema)) schema = schema.schema; | ||
if (isStruct(schema)) { | ||
schema = schema.schema; | ||
} | ||
@@ -1000,3 +1028,3 @@ const kind = Kinds.any(schema, defaults$$1, _extends({}, options, { types })); | ||
/** | ||
* Create a simple `struct` method for the default types. | ||
* Create a convenience `struct` factory for the default types. | ||
* | ||
@@ -1010,2 +1038,3 @@ * @type {Function} | ||
exports.superstruct = superstruct; | ||
exports.isStruct = isStruct; | ||
exports.StructError = StructError; | ||
@@ -1012,0 +1041,0 @@ |
@@ -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 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 instance(schema,defaults,options){const name="instance";const type=`instance<${schema.name}>`;const validate=(value=defaults)=>{return value instanceof schema?[undefined,value]:[{data:value,path:[],value:value,type:type}]};return new Kind(name,type,validate)}function inter(schema,defaults,options){if(kindOf(schema)!=="object"){{throw new Error(`Invalid schema: ${schema}`)}}const ks=[];const properties={};for(const key in schema){ks.push(key);const s=schema[key];const kind=any(s,undefined,options);properties[key]=kind}const name="interface";const type=`{${ks.join()}}`;const validate=(value=defaults)=>{const errors=[];for(const key in properties){const v=value[key];const kind=properties[key];const[e]=kind.validate(v);if(e){e.path=[key].concat(e.path);e.data=value;errors.push(e);continue}}if(errors.length){const first=errors[0];first.errors=errors;return[first]}return[undefined,value]};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 literal(schema,defaults,options){const name="literal";const type=`literal: ${JSON.stringify(schema)}`;const validate=(value=defaults)=>{return value===schema?[undefined,value]:[{data:value,path:[],value:value,type:type}]};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]=obj.validate(value);if(error){error.type=type;return[error]}const errors=[];const ret={};const valueKeys=Object.keys(value);const propertiesKeys=Object.keys(properties);const keys=new Set(valueKeys.concat(propertiesKeys));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||r!==undefined){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,instance:instance,interface:inter,list:list,literal:literal,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","promise","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})}); | ||
(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])}function resolveDefaults(defaults){return typeof defaults==="function"?defaults():defaults}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=resolveDefaults(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 en(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=resolveDefaults(defaults))=>{return schema.includes(value)?[undefined,value]:[{data:value,path:[],value:value,type:type}]};return new Kind(name,type,validate)}function enums(schema,defaults,options){const e=en(schema,undefined,options);const l=list([e],defaults,options);return l}function func(schema,defaults,options){if(kindOf(schema)!=="function"){{throw new Error(`Invalid schema: ${schema}`)}}const name="function";const type="<function>";const validate=(value=resolveDefaults(defaults))=>{return schema(value)?[undefined,value]:[{type:type,value:value,data:value,path:[]}]};return new Kind(name,type,validate)}function instance(schema,defaults,options){const name="instance";const type=`instance<${schema.name}>`;const validate=(value=resolveDefaults(defaults))=>{return value instanceof schema?[undefined,value]:[{data:value,path:[],value:value,type:type}]};return new Kind(name,type,validate)}function inter(schema,defaults,options){if(kindOf(schema)!=="object"){{throw new Error(`Invalid schema: ${schema}`)}}const ks=[];const properties={};for(const key in schema){ks.push(key);const s=schema[key];const kind=any(s,undefined,options);properties[key]=kind}const name="interface";const type=`{${ks.join()}}`;const validate=(value=resolveDefaults(defaults))=>{const errors=[];for(const key in properties){const v=value[key];const kind=properties[key];const[e]=kind.validate(v);if(e){e.path=[key].concat(e.path);e.data=value;errors.push(e);continue}}if(errors.length){const first=errors[0];first.errors=errors;return[first]}return[undefined,value]};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=resolveDefaults(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 literal(schema,defaults,options){const name="literal";const type=`literal: ${JSON.stringify(schema)}`;const validate=(value=resolveDefaults(defaults))=>{return value===schema?[undefined,value]:[{data:value,path:[],value:value,type:type}]};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=resolveDefaults(defaults))=>{const[error]=obj.validate(value);if(error){error.type=type;return[error]}const errors=[];const ret={};const valueKeys=Object.keys(value);const propertiesKeys=Object.keys(properties);const keys=new Set(valueKeys.concat(propertiesKeys));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||r!==undefined){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=resolveDefaults(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=resolveDefaults(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=resolveDefaults(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:en,enums:enums,function:func,instance:instance,interface:inter,list:list,literal:literal,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","promise","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.isStruct=isStruct;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
187684
2560