jet-schema
Version:
Simple, typescript-first schema validation tool
2 lines (1 loc) • 4.7 kB
JavaScript
function t(t){return void 0===t}function n(t){return"number"==typeof t}function r(t){return"string"==typeof t}function e(t){return!!t&&"object"==typeof t}function o(t){if(!s(t))throw Error('"getEnumKeys" must receive an enum object.');let r=Object.keys(t).reduce(((n,r)=>(n.includes(r)||n.push(t[r]),n)),[]);return n(t[r[0]])&&(r=r.map((n=>t[n]))),[r[0],t=>r.some((n=>n===t))]}function s(t){if(!e(t)||Array.isArray(t))return!1;const o=t,s=Object.keys(o),f=Math.floor(s.length/2);if(!n(o[s[f]]))return function(t,n){if(e(t))for(const r of Object.entries(t))if(!n(r[0],r[1]))return!1;return!0}(t,((t,n)=>r(t)&&r(n)));if(s.length%2!=0)return!1;for(let t=0;t<f;t++){const n=s[t],r=o[n],e=s[t+f],i=o[e];if(r!==e||n!==String(i))return!1}return!0}const f=t=>t instanceof Date&&!isNaN(new Date(t).getTime());function i(t){return t instanceof Date?new Date(t):e(t)?structuredClone(t):t}const l='"init:" option must be "true" if schema is not optional.',a="Default value did not pass validation.",u="Validator function failed.",c="Argument was undefined but not optional.",d="Argument was null but not nullable",m="Argument must be an object.",p="Extra properties not allowed in strict mode.";function h(t){if(t){if(Array.isArray(t)&&!(t.length>0))return;let n;throw n=r(t)?t:Array.isArray(t)&&1===t.length?JSON.stringify(t[0]):JSON.stringify(t),new Error(n)}}function y(t,n,r,e,o){const s={};return e&&(s.property=e),5===arguments.length&&(s.value=o),t&&(s.message=t),n&&(s.location=n),r&&(s.schemaId=r),s}function g(n){const r=function(t){const n=new Map;for(const r of t){const{vf:t,...e}=r;n.set(t,e)}return n}(n?.globals??[]),p=n?.cloneFn??i,g=n?.onError??h;return(n,...i)=>{const[h]=i,E=function(n){const r={optional:!1,nullable:!1,init:!0,safety:"filter"};if(t(n))return r;t(n.init)||(r.init=n.init);n.id&&(r.schemaId=n.id);n.safety&&(r.safety=n.safety);t(n.optional)||(r.optional=!!n.optional);t(n.nullable)||(r.nullable=!!n.nullable);"nullish"in n&&!t(n.nullish)&&(r.optional=n.nullish,r.nullable=n.nullish);return r}(h);if(!E.optional&&(!1===E.init||t(E.init))){const t=y(l,".schema",E.schemaId);g(t)}const A=function(n,r,e,i,u){const c={},d={},m=[];for(const i in n){const p={vf:t=>!!t,default:()=>{},formatError:t=>t},h=n[i];if(h===Date)p.vf=f,p.transform=t=>new Date(t),p.default=()=>new Date;else if("function"==typeof h||b(h)){let n,o,s=!1;if(b(h)){const t=h;n=t.vf,"default"in t&&(o=t.default,s=!0),t.transform&&(p.transform=t.transform),t.formatError&&(p.formatError=t.formatError)}else n=h;const f=r.get(n);if(f&&(!s&&"default"in f&&(o=f.default),!p.transform&&f.transform&&(p.transform=f.transform),!p.formatError&&f.formatError&&(p.formatError=f.formatError)),t(o))p.default=()=>{};else{const t=e(o);p.default=()=>t}p.vf=n}else if(v(h)){const t=h,n=t._schemaOptions.init;p.default=!0===n?()=>t.new():null===n?()=>null:()=>{},d[i]=()=>t.new(),p.vf=t.test}else if(s(h)){const[t,n]=o(h);p.default=()=>e(t),p.vf=n}else{const t=y(l,"_setupAllVldtrsHolder",u,i);m.push(t)}const g=p.default();if(!p.vf(g)){const t=y(a,"_setupAllVldtrsHolder",u,i,g),n=p.formatError(t);m.push(n)}c[i]=p}m.length>0&&i(m);return{childSchemaNewFns:d,allVldtrsHolder:c}}(n,r,p,g,E.schemaId),D=function(t,n,r,e){return(o={})=>{const s={},f=[];for(const n in t){const r=t[n].default();void 0!==r&&(s[n]=r)}for(const r in o){const i=t[r];if(!i)continue;let l=o[r];if(i.transform&&(l=i.transform(l)),s[r]=n(l),!i.vf(l)){const t=y(u,".new",e,r,l),n=i.formatError(t);f.push(n)}}return f.length>0&&r(f),s}}(A.allVldtrsHolder,p,g,E.schemaId),O=function(n,r,o){const{schemaId:s,safety:f}=r,i=y(c,".test",s),l=y(d,".test",s),a=w(n,f,".test",s);return n=>{if(t(n))return!!r.optional||(o(i),!1);if(null===n)return!!r.nullable||(o(l),!1);if(!e(n))return!1;const s=a(n);return!(s.length>0)||(o(s),!1)}}(A.allVldtrsHolder,E,g),V=function(t,n,r){const{schemaId:o,safety:s}=n,f=y(m,".parse",o),i=w(t,s,".parse",o);return t=>{if(!e(t))return r(f),t;const n=i(t);return n.length>0&&r(n),t}}(A.allVldtrsHolder,E,g);return{new:D,test:O,pick:t=>{const r=n[t],e=t,o=A.allVldtrsHolder[e],s=o.transform;let f=o.vf;if(s&&(f=t=>(t=s(t),o.vf(t))),r)return{default:o.default,test:f,...v(r)&&{pick:r.pick,new:A.childSchemaNewFns[e],schema:()=>({...r})}}},parse:V,_schemaOptions:E}}}function v(t){return e(t)&&"_schemaOptions"in t}function b(t){return e(t)&&"vf"in t&&"function"==typeof t.vf}function w(t,n,r,e){return o=>{const s=[];for(const n in t){const f=t[n];let i=o[n];if(f.transform&&(i=f.transform(i),o[n]=i),!f.vf(i)){const t=y(u,r,e,n,i),o=f.formatError(t);s.push(o)}}if("pass"!==n)for(const f in o)if(!(f in t)){if("strict"===n){const t=y(p,r,e,f);s.push(t)}Reflect.deleteProperty(o,f)}return s}}const E=g();export{g as default,E as schema};