@typeofweb/schema
Version:
`@typeofweb/schema` is a lightweight and extensible library for data validation with full TypeScript support!
11 lines (9 loc) • 4.73 kB
JavaScript
/**
* @typeofweb/schema@0.7.3
* Copyright (c) 2021 Type of Web - Michał Miszczyszyn
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self)["@typeofweb/schema"]={})}(this,(function(t){"use strict";const e=t=>"object"==typeof t&&null!==t&&"__validate"in t&&"toString"in t,n=t=>t.toString(),i=t=>"{"+t+"}";class r extends Error{constructor(t,e){const i=n(t),l={kind:"TYPE_MISMATCH",got:"function"==typeof e?String(e):JSON.stringify(e),expected:i};super(`Invalid type! Expected ${l.expected} but got ${l.got}!`),this.details=l,this.name="ValidationError",Error.captureStackTrace(this),Object.setPrototypeOf(this,r.prototype)}}function l(t,...e){return e.reduce(((t,e)=>e(t)),"function"==typeof t?t():t)}const o=l,a={right:t=>({_t:"right",value:t}),left:t=>({_t:"left",value:t}),nextValid:t=>({_t:"nextValid",value:t}),nextNotValid:t=>({_t:"nextNotValid",value:t})},u=(t,e)=>n=>({...n,toString:()=>(t=>{const e=t.join(" | ");return t.length>1?`(${e})`:e})([null==n?void 0:n.toString(),null==e?void 0:e()].filter(Boolean)),__validate(e){const i=t.call(this,e,a);return"left"===(null==i?void 0:i._t)||"right"===(null==i?void 0:i._t)?i:n?n.__validate(i.value):i}}),f=u(((t,e)=>"boolean"!=typeof t?e.left(t):e.nextValid(t)),(()=>"boolean")),s=t=>"[object Date]"===Object.prototype.toString.call(t),c=/^[+-]?\d{4}/,d=u(((t,e)=>{const n=function(t){if("string"==typeof t&&(t=>c.test(t))(t))return new Date(t);return t}(t);return!s(n)||Number.isNaN(Number(n))?e.left(n):e.nextValid(n)}),(()=>"Date"));const g=u(((t,e)=>{const n=function(t){if("string"==typeof t)return""===t.trim()?t:Number(t);return t}(t);return"number"!=typeof n||Number.isNaN(n)?e.left(n):e.nextValid(n)}),(()=>"number"));const p=u(((t,e)=>{const n=function(t){if(s(t))return t.toISOString();return t}(t);return"string"!=typeof n?e.left(n):e.nextValid(n)}),(()=>"string"));const h=u(((t,e)=>e.nextValid(t)),(()=>"unknown")),_=u(((t,e)=>null==t?e.right(t):e.nextNotValid(t)),(()=>"undefined | null")),y=u(((t,e)=>null===t?e.right(null):e.nextNotValid(t)),(()=>"null")),v=u(((t,e)=>void 0===t?e.right(void 0):e.nextNotValid(t)),(()=>"undefined"));t.ValidationError=r,t.array=(...t)=>u((function(e,n){if(!Array.isArray(e))return n.left(e);let i=!1;const l=new Array(e.length);t:for(let n=0;n<e.length;++n){const o=e[n];for(let e=0;e<t.length;++e){const i=t[e].__validate(o);if("right"===i._t||"nextValid"===i._t){l[n]=i.value;continue t}}l[n]=new r(this,e),i=!0}return i?n.left(l):n.nextValid(l)}),(()=>{const e=t.map((t=>n(t))).join(" | ");return t.length>1?`(${e})[]`:`${e}[]`})),t.boolean=f,t.date=d,t.isSchema=e,t.minArrayLength=t=>u(((e,n)=>e.length>=t?n.nextValid(e):n.left(e))),t.minStringLength=t=>u(((e,n)=>e.length>=t?n.nextValid(e):n.left(e))),t.nil=_,t.nullable=y,t.number=g,t.object=(t,e)=>u((function(n,i){if("object"!=typeof n||null===n)return i.left(n);const l=n,o=t,a=!!(null==e?void 0:e.allowUnknownKeys);let u=!1;const f={};for(const t in l){if(!Object.prototype.hasOwnProperty.call(l,t))continue;const e=l[t],n=o[t];if(n){const i=n.__validate(e);f[t]=i.value,u||(u="left"===i._t)}else a?f[t]=e:(u=!0,f[t]=new r(this,l))}for(const t in o){if(!Object.prototype.hasOwnProperty.call(o,t))continue;if(t in f)continue;const e=o[t],n=l[t],i=e.__validate(n);u||(u="left"===i._t)}return u?i.left(f):i.nextValid(f)}),(()=>{const e=Object.entries(t).map((([t,e])=>[t,n(e)]));return 0===e.length?i(""):i(" "+e.map((([t,e])=>{return(/\s/.test(n=t)?`"${n}"`:n)+": "+e;var n})).join(", ")+" ")})),t.oneOf=t=>u((function(n,i){for(let r=0;r<t.length;++r){const l=t[r];if(e(l)){const t=l.__validate(n);if("right"===t._t)return i.right(t.value);if("nextValid"===t._t)return i.nextValid(t.value)}else if(n===l)return i.right(l)}return i.left(n)}),(()=>{const i=t.map((t=>e(t)?n(t):JSON.stringify(t))).join(" | ");return t.length>1?`(${i})`:i})),t.optional=v,t.pipe=l,t.refine=u,t.string=p,t.tuple=t=>u((function(n,i){if(!Array.isArray(n)||n.length!==t.length)return i.left(n);let l=!1;const o=new Array(n.length);for(let i=0;i<n.length;++i){const a=t[i],u=n[i];if(e(a)){const t=a.__validate(u);o[i]=t.value,l||(l="left"===t._t)}else a!==u?(o[i]=new r(this,t),l=!0):o[i]=u}return l?i.left(o):i.nextValid(o)}),(()=>"["+t.map((t=>e(t)?n(t):JSON.stringify(t))).join(", ")+"]")),t.unknown=h,t.validate=t=>e=>{const n=t.__validate(e);if("right"===n._t||"nextValid"===n._t)return n.value;throw new r(t,e)},t.λ=o,Object.defineProperty(t,"__esModule",{value:!0})}));
//# sourceMappingURL=index.umd.js.map