(function(_,Y){typeof exports=="object"&&typeof module<"u"?Y(exports):typeof define=="function"&&define.amd?define(["exports"],Y):(_=typeof globalThis<"u"?globalThis:_||self,Y(_.yrel={}))})(this,function(_){"use strict";const Y=Symbol("YREL"),U=Symbol("YREL_OPTIONAL"),$=Symbol("YREL_NULLABLE"),L=Symbol("YREL_ANY"),b=Symbol("YREL_BOOLEAN"),E=Symbol("YREL_NUMBER"),S=Symbol("YREL_STRING"),v=Symbol("YREL_LITERAL"),O=Symbol("YREL_ARRAY"),A=Symbol("YREL_UNION"),N=Symbol("YREL_TUPLE"),V=Symbol("YREL_OBJECT"),T=Symbol("YREL_RECORD"),j=Symbol("YREL_REPORT"),k=(s,e,r)=>{const o=(r==null?void 0:r.key)??"",n=[];let i=e,t=!0,u=[];try{for(const a of s.__checkers)if(a(i,s.__cache))return{key:o,isValid:t,data:i,errors:n,children:u};for(const a of s.__cache.preprocessors??[])i=a(i,s.__cache);s.__cache.defaultData!==void 0&&i===void 0&&(i=s.__cache.defaultData);for(const a of s.__resolvers){const l=a(i,s.__cache,{key:o});i=l.data,l.errors&&n.push(...l.errors),l.children&&(u=l.children)}for(const a of s.__validators){const l=a(i,s.__cache);if(Array.isArray(l))n.push(...l);else if(l===!1)n.push(["err_unknown"]);else if(typeof l!="boolean"&&(l.errors&&n.push(...l.errors),l.children)){const f=l.children.map(c=>({key:o?`${o}.${c.key}`:c.key,isValid:!1,data:void 0,errors:c.errors,children:[]}));u.push(...f)}}if(!n.length)for(const a of s.__cache.transformers??[])i=a(i,s.__cache)}catch(a){console.error(a),n.push(["err_unknown"])}return t=n.length===0&&u.every(a=>a.isValid),{key:o,isValid:t,data:i,errors:n,children:u}},P=s=>s!==null&&typeof s=="object"&&typeof s!="function"&&!Array.isArray(s),p=s=>{const{name:e,resolver:r,properties:o,validators:n,narrowers:i}=s,t=s.schemaBase??{},{__cache:u,__checkers:a,__resolvers:l,__validators:f}=t;for(const c of Object.keys(t))delete t[c];if(t.__type=Y,t.__name=e,t.__cache=u??{},t.__checkers=Array.isArray(a)?[...a]:[(c,y)=>!!y.isOptional&&c===void 0,(c,y)=>!!y.isNullable&&c===null],t.__resolvers=l??[],t.__resolvers.push(r),t.__validators=Array.isArray(f)?[...f]:[],o&&Object.assign(t,o),t.preprocess=c=>(t.__cache.preprocessors=t.__cache.preprocessors?[...t.__cache.preprocessors,c]:[c],t),t.defaultsTo=c=>(t.__cache.defaultData=c,t),t.transform=c=>(t.__cache.transformers=t.__cache.transformers?[...t.__cache.transformers,c]:[c],t),t.validate=c=>(t.__validators.push(y=>c(y,t.__cache)),t),n)for(const c of Object.keys(n)){const y=c,d=n[y];Object.assign(t,{[y]:(...h)=>{const[m,...g]=[...h].reverse(),q=g.reverse(),w=m!==null&&typeof m=="object"&&"errors"in m&&Array.isArray(m.errors)&&m.errors.every(R=>Array.isArray(R)),F=w?q:h,Z=(R,W)=>{const B=d(...F)(R,W);return B===!0?!0:w&&m.errors||B};return t.__validators.push(Z),t}})}if(Object.assign(t,{coerce:()=>(t.__cache.coerce=!0,t),optional:()=>(t.__cache.isOptional=!0,t),nullable:()=>(t.__cache.isNullable=!0,t)}),i)for(const c of Object.keys(i)){const y=c,d=i[y];Object.assign(t,{[y]:(...h)=>d(t)(...h)})}return t},D={any:()=>p({schemaBase:null,name:L,resolver:(s,e,r)=>({key:r.key,isValid:!0,data:s,errors:[],children:[]})}),boolean:s=>p({schemaBase:s,name:b,resolver:(e,r,o)=>{const n=r.coerce?!!e:e;return typeof n=="boolean"?{key:o.key,isValid:!0,data:n,errors:[],children:[]}:{key:o.key,isValid:!1,data:n,errors:[["err_boolean"]],children:[]}},validators:{truthy:()=>e=>e===!0||[["err_boolean_truthy"]]}}),number:s=>p({schemaBase:s,name:E,resolver:(e,r,o)=>{const n=r.coerce?Number(e):e;return typeof n=="number"&&!isNaN(n)&&Number.isFinite(n)?{key:o.key,isValid:!0,data:n,errors:[],children:[]}:{key:o.key,isValid:!1,data:n,errors:[["err_number"]],children:[]}},validators:{gt:e=>r=>typeof r!="number"?[]:r>e||[["err_number_gt",{gt:e}]],gte:e=>r=>typeof r!="number"?[]:r>=e||[["err_number_gte",{gte:e}]],lt:e=>r=>typeof r!="number"?[]:rr=>typeof r!="number"?[]:r<=e||[["err_number_lte",{lte:e}]],integer:()=>e=>typeof e!="number"?[]:Number.isInteger(e)&&Number.isSafeInteger(e)||[["err_number_integer"]]}}),string:s=>p({schemaBase:s,name:S,resolver:(e,r,o)=>{const n=r.coerce?e instanceof Date?e.toISOString():String(e):e;return typeof n=="string"?{key:o.key,isValid:!0,data:n,errors:[],children:[]}:{key:o.key,isValid:!1,data:n,errors:[["err_string"]],children:[]}},validators:{nonempty:()=>e=>typeof e!="string"?[]:e.length>0||[["err_string_nonempty"]],trim:()=>e=>typeof e!="string"?[]:e===e.trim()||[["err_string_trim"]],length:e=>r=>typeof r!="string"?[]:r.length===e||[["err_string_length",{length:e}]],min:e=>r=>typeof r!="string"?[]:r.length>=e||[["err_string_min",{min:e}]],max:e=>r=>typeof r!="string"?[]:r.length<=e||[["err_string_max",{max:e}]],datetime:()=>e=>{if(typeof e!="string")return[];const r=Date.parse(e),o=isNaN(r)?null:new Date(r).toISOString().slice(0,19),n=e.slice(0,19),i=/^(\.\d{1,3})?Z$/.test(e.slice(19));return o===n&&i||[["err_string_date_time"]]},date:()=>e=>{if(typeof e!="string")return[];const r=Date.parse(e);return(isNaN(r)?null:new Date(r).toISOString().slice(0,10))===e||[["err_string_date"]]},time:()=>e=>{if(typeof e!="string")return[];const r=Date.parse(`2000-01-01T${e}Z`),o=isNaN(r)?null:new Date(r).toISOString().slice(11,19),n=e.slice(0,8),i=/^(\.\d{1,3})?$/.test(e.slice(8));return o===n&&i||[["err_string_time"]]},lowercase:()=>e=>typeof e!="string"?[]:e===e.toLowerCase()||[["err_string_lowercase"]],uppercase:()=>e=>typeof e!="string"?[]:e===e.toUpperCase()||[["err_string_uppercase"]],capitalcase:e=>r=>{if(typeof r!="string")return[];const o=e!=null&&e.lower?r.toLowerCase():r;return r===o.replace(/(?:^|\s|["'([{])+\S/g,n=>n.toUpperCase())||[["err_string_capitalcase",{lower:!!(e!=null&&e.lower)}]]}}}),literal:(s,e)=>p({schemaBase:e,name:v,resolver:(r,o,n)=>Object.is(r,s)?{key:n.key,isValid:!0,data:r,errors:[],children:[]}:{key:n.key,isValid:!1,data:r,errors:[["err_literal",{literal:s}]],children:[]}}),array:(s,e)=>p({schemaBase:e,name:O,resolver:(r,o,n)=>{if(!Array.isArray(r))return{key:n.key,isValid:!1,data:r,errors:[["err_array"]],children:[]};const i=[...r],t=i.map((a,l)=>{const f=n.key?`${n.key}.${l}`:String(l),c=k(s,a,{key:f});return i.splice(l,1,c.data),c}),u=t.every(a=>a.isValid);return{key:n.key,isValid:u,data:i,errors:[],children:t}},validators:{nonempty:()=>r=>Array.isArray(r)?r.length>0||[["err_array_nonempty"]]:[],length:r=>o=>Array.isArray(o)?o.length===r||[["err_array_length",{length:r}]]:[],min:r=>o=>Array.isArray(o)?o.length>=r||[["err_array_min",{min:r}]]:[],max:r=>o=>Array.isArray(o)?o.length<=r||[["err_array_max",{max:r}]]:[]}}),union:(s,e,r)=>{if(!s||!s.length)throw new Error("Data validator .union([...schemas]) requires schema definitions.");return p({schemaBase:r,name:A,resolver:(o,n,i)=>{for(const t of s){const u=k(t,o,i);if(u.isValid)return{key:i.key,isValid:!0,data:u.data,errors:[],children:[]}}return e!=null&&e.errors?{key:i.key,isValid:!1,data:o,errors:e.errors,children:[]}:{key:i.key,isValid:!1,data:o,errors:[["err_union"]],children:[]}}})},tuple:(s,e,r)=>{if(!s||!s.length)throw new Error("Data validator .tuple([...schemas]) requires at least one schema definition.");return p({schemaBase:r,name:N,resolver:(o,n,i)=>{if(!Array.isArray(o)||!e&&o.length!==s.length)return{key:i.key,isValid:!1,data:o,errors:[["err_tuple"]],children:[]};const t=[...o],u=s.map((c,y)=>{const d=i.key?`${i.key}.${y}`:String(y),h=k(c,t[y],{key:d});return t.splice(y,1,h.data),h});let a=[];e&&(a=t.slice(s.length).map((y,d)=>{const h=s.length+d,m=i.key?`${i.key}.${h}`:String(h),g=k(e,y,{key:m});return t.splice(h,1,g.data),g}));const l=[...u,...a],f=l.every(c=>c.isValid);return{key:i.key,isValid:f,data:t,errors:[],children:l}}})},object:(s,e)=>p({schemaBase:e,name:V,resolver:(r,o,n)=>{if(!P(r))return{key:n.key,isValid:!1,data:r,errors:[["err_object"]],children:[]};const i={...r},t=Object.keys(s),u=Object.keys(i);if(!o.passthroughObjectProps){const f=u.filter(c=>!Object.prototype.hasOwnProperty.call(s,c));if(f.length>0)return{key:n.key,isValid:!1,data:i,errors:[["err_object_unexpected_props",{props:f}]],children:[]}}const a=t.map(f=>{const c=s[f],y=i[f],d=n.key?`${n.key}.${String(f)}`:String(f),h=k(c,y,{key:d});return i[f]=h.data,h}),l=a.every(f=>f.isValid);return{key:n.key,isValid:l,data:i,errors:[],children:a}},properties:{shape:s},narrowers:{passthrough:r=>()=>(r.__cache.passthroughObjectProps=!0,r)}}),record:(s,e,r)=>p({schemaBase:r,name:T,resolver:(o,n,i)=>{if(!P(o))return{key:i.key,isValid:!1,data:o,errors:[["err_record"]],children:[]};const t={...o},u=Object.keys(t),a=u.filter(y=>!k(s,y).isValid),l=u.map(y=>{const d=t[y],h=i.key?`${i.key}.${y}`:y,m=k(e,d,{key:h});return t[y]=m.data,m}),f=!a.length&&l.every(y=>y.isValid),c=a.length?[["err_record_keys",{keys:a}]]:[];return{key:i.key,isValid:f,data:t,errors:c,children:l}}})},C=D,M=s=>s!==null&&typeof s=="object"&&"__type"in s&&s.__type===Y,G=s=>({...s,__type:j}),I=(s,e)=>{s.errors.length&&(e[s.key]=e[s.key]?[...e[s.key],...s.errors]:s.errors);for(const r of s.children)I(r,e);return e},J=(s,e,r)=>{const{rootKey:o=""}={...r},n=k(s,e,{key:o}),i=n.isValid;if(i)return{isValid:i,issues:[],data:n.data};const t=I(n,{}),u=Object.keys(t).map(a=>({key:a,errors:t[a]}));return{isValid:i,issues:u,data:void 0}};_.YREL=Y,_.YREL_ANY=L,_.YREL_ARRAY=O,_.YREL_BOOLEAN=b,_.YREL_LITERAL=v,_.YREL_NULLABLE=$,_.YREL_NUMBER=E,_.YREL_OBJECT=V,_.YREL_OPTIONAL=U,_.YREL_RECORD=T,_.YREL_REPORT=j,_.YREL_STRING=S,_.YREL_TUPLE=N,_.YREL_UNION=A,_.isYrel=M,_.processYrel=k,_.reportYrel=G,_.validateYrel=J,_.y=C,_.yrel=D,Object.defineProperty(_,Symbol.toStringTag,{value:"Module"})});