airi / assets /core-Ab2YLnjr.js
nekomeowww's picture
release: build d66f9d3e9030ef0a5a18dd206a1aafbde7b640fe
ae3055d
function e(e){let t=Object.values(e).filter(e=>typeof e==`number`),n=Object.entries(e).filter(([e,n])=>t.indexOf(+e)===-1).map(([e,t])=>t);return n}function t(e){let t=!1;return{get value(){{let t=e();return Object.defineProperty(this,`value`,{value:t}),t}}}}const n=Error.captureStackTrace?Error.captureStackTrace:(...e)=>{},r=t(()=>{if(typeof navigator<`u`&&navigator?.userAgent?.includes(`Cloudflare`))return!1;try{let e=Function;return new e(``),!0}catch{return!1}}),i={safeint:[-(2**53-1),2**53-1],int32:[-2147483648,2147483647],uint32:[0,4294967295],float32:[-34028234663852886e22,34028234663852886e22],float64:[-Number.MAX_VALUE,Number.MAX_VALUE]},a=Symbol(`ZodOutput`),o=Symbol(`ZodInput`);var s=class{constructor(){this._map=new WeakMap,this._idmap=new Map}add(e,...t){let n=t[0];if(this._map.set(e,n),n&&typeof n==`object`&&`id`in n){if(this._idmap.has(n.id))throw Error(`ID ${n.id} already exists in the registry`);this._idmap.set(n.id,e)}return this}remove(e){return this._map.delete(e),this}get(e){let t=e._zod.parent;if(t){let n={...this.get(t)??{}};return delete n.id,{...n,...this._map.get(e)}}return this._map.get(e)}has(e){return this._map.has(e)}};function c(){return new s}const l=c();var u=class{constructor(e){this.counter=0,this.metadataRegistry=e?.metadata??l,this.target=e?.target??`draft-2020-12`,this.unrepresentable=e?.unrepresentable??`throw`,this.override=e?.override??(()=>{}),this.io=e?.io??`output`,this.seen=new Map}process(t,n={path:[],schemaPath:[]}){var r;let i=t._zod.def,a={guid:`uuid`,url:`uri`,datetime:`date-time`,json_string:`json-string`,regex:``},o=this.seen.get(t);if(o){o.count++;let e=n.schemaPath.includes(t);return e&&(o.cycle=n.path),o.schema}let s={schema:{},count:1,cycle:void 0,path:n.path};this.seen.set(t,s);let c=t._zod.toJSONSchema?.();if(c)s.schema=c;else{let r={...n,schemaPath:[...n.schemaPath,t],path:n.path},o=t._zod.parent;if(o)s.ref=o,this.process(o,r),this.seen.get(o).isParent=!0;else{let n=s.schema;switch(i.type){case`string`:{let e=n;e.type=`string`;let{minimum:r,maximum:i,format:o,patterns:c,contentEncoding:l}=t._zod.bag;if(typeof r==`number`&&(e.minLength=r),typeof i==`number`&&(e.maxLength=i),o&&(e.format=a[o]??o,e.format===``&&delete e.format),l&&(e.contentEncoding=l),c&&c.size>0){let t=[...c];t.length===1?e.pattern=t[0].source:t.length>1&&(s.schema.allOf=[...t.map(e=>({...this.target===`draft-7`?{type:`string`}:{},pattern:e.source}))])}break}case`number`:{let e=n,{minimum:r,maximum:i,format:a,multipleOf:o,exclusiveMaximum:s,exclusiveMinimum:c}=t._zod.bag;typeof a==`string`&&a.includes(`int`)?e.type=`integer`:e.type=`number`,typeof c==`number`&&(e.exclusiveMinimum=c),typeof r==`number`&&(e.minimum=r,typeof c==`number`&&(c>=r?delete e.minimum:delete e.exclusiveMinimum)),typeof s==`number`&&(e.exclusiveMaximum=s),typeof i==`number`&&(e.maximum=i,typeof s==`number`&&(s<=i?delete e.maximum:delete e.exclusiveMaximum)),typeof o==`number`&&(e.multipleOf=o);break}case`boolean`:{let e=n;e.type=`boolean`;break}case`bigint`:if(this.unrepresentable===`throw`)throw Error(`BigInt cannot be represented in JSON Schema`);break;case`symbol`:if(this.unrepresentable===`throw`)throw Error(`Symbols cannot be represented in JSON Schema`);break;case`null`:n.type=`null`;break;case`any`:break;case`unknown`:break;case`undefined`:case`never`:n.not={};break;case`void`:if(this.unrepresentable===`throw`)throw Error(`Void cannot be represented in JSON Schema`);break;case`date`:if(this.unrepresentable===`throw`)throw Error(`Date cannot be represented in JSON Schema`);break;case`array`:{let e=n,{minimum:a,maximum:o}=t._zod.bag;typeof a==`number`&&(e.minItems=a),typeof o==`number`&&(e.maxItems=o),e.type=`array`,e.items=this.process(i.element,{...r,path:[...r.path,`items`]});break}case`object`:{let e=n;e.type=`object`,e.properties={};let t=i.shape;for(let n in t)e.properties[n]=this.process(t[n],{...r,path:[...r.path,`properties`,n]});let a=new Set(Object.keys(t)),o=new Set([...a].filter(e=>{let t=i.shape[e]._zod;return this.io===`input`?t.optin===void 0:t.optout===void 0}));o.size>0&&(e.required=Array.from(o)),i.catchall?._zod.def.type===`never`?e.additionalProperties=!1:i.catchall?i.catchall&&(e.additionalProperties=this.process(i.catchall,{...r,path:[...r.path,`additionalProperties`]})):this.io===`output`&&(e.additionalProperties=!1);break}case`union`:{let e=n;e.anyOf=i.options.map((e,t)=>this.process(e,{...r,path:[...r.path,`anyOf`,t]}));break}case`intersection`:{let e=n,t=this.process(i.left,{...r,path:[...r.path,`allOf`,0]}),a=this.process(i.right,{...r,path:[...r.path,`allOf`,1]}),o=e=>`allOf`in e&&Object.keys(e).length===1,s=[...o(t)?t.allOf:[t],...o(a)?a.allOf:[a]];e.allOf=s;break}case`tuple`:{let e=n;e.type=`array`;let a=i.items.map((e,t)=>this.process(e,{...r,path:[...r.path,`prefixItems`,t]}));if(this.target===`draft-2020-12`?e.prefixItems=a:e.items=a,i.rest){let t=this.process(i.rest,{...r,path:[...r.path,`items`]});this.target===`draft-2020-12`?e.items=t:e.additionalItems=t}i.rest&&(e.items=this.process(i.rest,{...r,path:[...r.path,`items`]}));let{minimum:o,maximum:s}=t._zod.bag;typeof o==`number`&&(e.minItems=o),typeof s==`number`&&(e.maxItems=s);break}case`record`:{let e=n;e.type=`object`,e.propertyNames=this.process(i.keyType,{...r,path:[...r.path,`propertyNames`]}),e.additionalProperties=this.process(i.valueType,{...r,path:[...r.path,`additionalProperties`]});break}case`map`:if(this.unrepresentable===`throw`)throw Error(`Map cannot be represented in JSON Schema`);break;case`set`:if(this.unrepresentable===`throw`)throw Error(`Set cannot be represented in JSON Schema`);break;case`enum`:{let t=n,r=e(i.entries);r.every(e=>typeof e==`number`)&&(t.type=`number`),r.every(e=>typeof e==`string`)&&(t.type=`string`),t.enum=r;break}case`literal`:{let e=n,t=[];for(let e of i.values)if(e===void 0){if(this.unrepresentable===`throw`)throw Error("Literal `undefined` cannot be represented in JSON Schema")}else if(typeof e==`bigint`){if(this.unrepresentable===`throw`)throw Error(`BigInt literals cannot be represented in JSON Schema`);t.push(Number(e))}else t.push(e);if(t.length!==0)if(t.length===1){let n=t[0];e.type=n===null?`null`:typeof n,e.const=n}else t.every(e=>typeof e==`number`)&&(e.type=`number`),t.every(e=>typeof e==`string`)&&(e.type=`string`),t.every(e=>typeof e==`boolean`)&&(e.type=`string`),t.every(e=>e===null)&&(e.type=`null`),e.enum=t;break}case`file`:{let e=n,r={type:`string`,format:`binary`,contentEncoding:`binary`},{minimum:i,maximum:a,mime:o}=t._zod.bag;i!==void 0&&(r.minLength=i),a!==void 0&&(r.maxLength=a),o?o.length===1?(r.contentMediaType=o[0],Object.assign(e,r)):e.anyOf=o.map(e=>{let t={...r,contentMediaType:e};return t}):Object.assign(e,r);break}case`transform`:if(this.unrepresentable===`throw`)throw Error(`Transforms cannot be represented in JSON Schema`);break;case`nullable`:{let e=this.process(i.innerType,r);n.anyOf=[e,{type:`null`}];break}case`nonoptional`:this.process(i.innerType,r),s.ref=i.innerType;break;case`success`:{let e=n;e.type=`boolean`;break}case`default`:this.process(i.innerType,r),s.ref=i.innerType,n.default=JSON.parse(JSON.stringify(i.defaultValue));break;case`prefault`:this.process(i.innerType,r),s.ref=i.innerType,this.io===`input`&&(n._prefault=JSON.parse(JSON.stringify(i.defaultValue)));break;case`catch`:{this.process(i.innerType,r),s.ref=i.innerType;let e;try{e=i.catchValue(void 0)}catch{throw Error(`Dynamic catch values are not supported in JSON Schema`)}n.default=e;break}case`nan`:if(this.unrepresentable===`throw`)throw Error(`NaN cannot be represented in JSON Schema`);break;case`template_literal`:{let e=n,r=t._zod.pattern;if(!r)throw Error(`Pattern not found in template literal`);e.type=`string`,e.pattern=r.source;break}case`pipe`:{let e=this.io===`input`?i.in._zod.def.type===`transform`?i.out:i.in:i.out;this.process(e,r),s.ref=e;break}case`readonly`:this.process(i.innerType,r),s.ref=i.innerType,n.readOnly=!0;break;case`promise`:this.process(i.innerType,r),s.ref=i.innerType;break;case`optional`:this.process(i.innerType,r),s.ref=i.innerType;break;case`lazy`:{let e=t._zod.innerType;this.process(e,r),s.ref=e;break}case`custom`:if(this.unrepresentable===`throw`)throw Error(`Custom types cannot be represented in JSON Schema`);break;default:}}}let l=this.metadataRegistry.get(t);l&&Object.assign(s.schema,l),this.io===`input`&&f(t)&&(delete s.schema.examples,delete s.schema.default),this.io===`input`&&s.schema._prefault&&((r=s.schema).default??(r.default=s.schema._prefault)),delete s.schema._prefault;let u=this.seen.get(t);return u.schema}emit(e,t){let n={cycles:t?.cycles??`ref`,reused:t?.reused??`inline`,external:t?.external??void 0},r=this.seen.get(e);if(!r)throw Error(`Unprocessed schema. This is a bug in Zod.`);let i=e=>{let t=this.target===`draft-2020-12`?`$defs`:`definitions`;if(n.external){let r=n.external.registry.get(e[0])?.id;if(r)return{ref:n.external.uri(r)};let i=e[1].defId??e[1].schema.id??`schema${this.counter++}`;return e[1].defId=i,{defId:i,ref:`${n.external.uri(`__shared`)}#/${t}/${i}`}}if(e[1]===r)return{ref:`#`};let i=`#`,a=`${i}/${t}/`,o=e[1].schema.id??`__schema${this.counter++}`;return{defId:o,ref:a+o}},a=e=>{if(e[1].schema.$ref)return;let t=e[1],{ref:n,defId:r}=i(e);t.def={...t.schema},r&&(t.defId=r);let a=t.schema;for(let e in a)delete a[e];a.$ref=n};for(let t of this.seen.entries()){let r=t[1];if(e===t[0]){a(t);continue}if(n.external){let r=n.external.registry.get(t[0])?.id;if(e!==t[0]&&r){a(t);continue}}let i=this.metadataRegistry.get(t[0])?.id;if(i){a(t);continue}if(r.cycle){if(n.cycles===`throw`)throw Error(`Cycle detected: #/${r.cycle?.join(`/`)}/<root>
Set the \`cycles\` parameter to \`"ref"\` to resolve cyclical schemas with defs.`);n.cycles===`ref`&&a(t);continue}if(r.count>1&&n.reused===`ref`){a(t);continue}}let o=(e,t)=>{let n=this.seen.get(e),r=n.def??n.schema,i={...r};if(n.ref===null)return;let a=n.ref;if(n.ref=null,a){o(a,t);let e=this.seen.get(a).schema;e.$ref&&t.target===`draft-7`?(r.allOf=r.allOf??[],r.allOf.push(e)):(Object.assign(r,e),Object.assign(r,i))}n.isParent||this.override({zodSchema:e,jsonSchema:r,path:n.path??[]})};for(let e of[...this.seen.entries()].reverse())o(e[0],{target:this.target});let s={};this.target===`draft-2020-12`?s.$schema=`https://json-schema.org/draft/2020-12/schema`:this.target===`draft-7`?s.$schema=`http://json-schema.org/draft-07/schema#`:console.warn(`Invalid target: ${this.target}`),Object.assign(s,r.def);let c=n.external?.defs??{};for(let e of this.seen.entries()){let t=e[1];t.def&&t.defId&&(c[t.defId]=t.def)}!n.external&&Object.keys(c).length>0&&(this.target===`draft-2020-12`?s.$defs=c:s.definitions=c);try{return JSON.parse(JSON.stringify(s))}catch{throw Error(`Error converting schema to JSON.`)}}};function d(e,t){if(e instanceof s){let n=new u(t),r={};for(let t of e._idmap.entries()){let[e,r]=t;n.process(r)}let i={},a={registry:e,uri:t?.uri||(e=>e),defs:r};for(let r of e._idmap.entries()){let[e,o]=r;i[e]=n.emit(o,{...t,external:a})}if(Object.keys(r).length>0){let e=n.target===`draft-2020-12`?`$defs`:`definitions`;i.__shared={[e]:r}}return{schemas:i}}let n=new u(t);return n.process(e),n.emit(e,t)}function f(e,t){let n=t??{seen:new Set};if(n.seen.has(e))return!1;n.seen.add(e);let r=e,i=r._zod.def;switch(i.type){case`string`:case`number`:case`bigint`:case`boolean`:case`date`:case`symbol`:case`undefined`:case`null`:case`any`:case`unknown`:case`never`:case`void`:case`literal`:case`enum`:case`nan`:case`file`:case`template_literal`:return!1;case`array`:return f(i.element,n);case`object`:for(let e in i.shape)if(f(i.shape[e],n))return!0;return!1;case`union`:for(let e of i.options)if(f(e,n))return!0;return!1;case`intersection`:return f(i.left,n)||f(i.right,n);case`tuple`:for(let e of i.items)if(f(e,n))return!0;return!!(i.rest&&f(i.rest,n));case`record`:return f(i.keyType,n)||f(i.valueType,n);case`map`:return f(i.keyType,n)||f(i.valueType,n);case`set`:return f(i.valueType,n);case`promise`:case`optional`:case`nonoptional`:case`nullable`:case`readonly`:return f(i.innerType,n);case`lazy`:return f(i.getter(),n);case`default`:return f(i.innerType,n);case`prefault`:return f(i.innerType,n);case`custom`:return!1;case`transform`:return!0;case`pipe`:return f(i.in,n)||f(i.out,n);case`success`:return!1;case`catch`:return!1;default:}throw Error(`Unknown schema type: ${i.type}`)}export{s as $ZodRegistry,u as JSONSchemaGenerator,l as globalRegistry,c as registry,d as toJSONSchema};