@raminy/ajv-ts-schema
Version:
Simplify AJV Schema Validation with TypeScript
15 lines (12 loc) • 15.5 kB
JavaScript
(function(m,O){typeof exports=="object"&&typeof module<"u"?O(exports):typeof define=="function"&&define.amd?define(["exports"],O):(m=typeof globalThis<"u"?globalThis:m||self,O(m.Index={}))})(this,function(m){"use strict";var Je=Object.defineProperty;var Le=(m,O,P)=>O in m?Je(m,O,{enumerable:!0,configurable:!0,writable:!0,value:P}):m[O]=P;var ve=(m,O,P)=>(Le(m,typeof O!="symbol"?O+"":O,P),P);const U=class U{static getSchema(){return Reflect.getMetadata("schema",this)}};ve(U,"fromJson",(v,d)=>{const g=new v;return Object.keys(d).forEach(_=>{var k;let j;d[_]&&typeof d[_]=="object"&&((k=v.prototype[_])!=null&&k.constructor)?j=U.fromJson(v.prototype[_].constructor,d[_]):j=d[_],g[_]=j}),g});let O=U;var P=typeof globalThis<"u"?globalThis:typeof window<"u"?window:typeof global<"u"?global:typeof self<"u"?self:{};/*! *****************************************************************************
Copyright (C) Microsoft. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License"); you may not use
this file except in compliance with the License. You may obtain a copy of the
License at http://www.apache.org/licenses/LICENSE-2.0
THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
MERCHANTABLITY OR NON-INFRINGEMENT.
See the Apache Version 2.0 License for specific language governing permissions
and limitations under the License.
***************************************************************************** */var Q;(function(c){(function(v){var d=typeof globalThis=="object"?globalThis:typeof P=="object"?P:typeof self=="object"?self:typeof this=="object"?this:W(),g=_(c);typeof d.Reflect<"u"&&(g=_(d.Reflect,g)),v(g,d),typeof d.Reflect>"u"&&(d.Reflect=c);function _(R,A){return function(S,G){Object.defineProperty(R,S,{configurable:!0,writable:!0,value:G}),A&&A(S,G)}}function j(){try{return Function("return this;")()}catch{}}function k(){try{return(0,eval)("(function() { return this; })()")}catch{}}function W(){return j()||k()}})(function(v,d){var g=Object.prototype.hasOwnProperty,_=typeof Symbol=="function",j=_&&typeof Symbol.toPrimitive<"u"?Symbol.toPrimitive:"@@toPrimitive",k=_&&typeof Symbol.iterator<"u"?Symbol.iterator:"@@iterator",W=typeof Object.create=="function",R={__proto__:[]}instanceof Array,A=!W&&!R,S={create:W?function(){return $(Object.create(null))}:R?function(){return $({__proto__:null})}:function(){return $({})},has:A?function(e,t){return g.call(e,t)}:function(e,t){return t in e},get:A?function(e,t){return g.call(e,t)?e[t]:void 0}:function(e,t){return e[t]}},G=Object.getPrototypeOf(Function),D=typeof Map=="function"&&typeof Map.prototype.entries=="function"?Map:We(),B=typeof Set=="function"&&typeof Set.prototype.entries=="function"?Set:He(),J=typeof WeakMap=="function"?WeakMap:Ve(),I=_?Symbol.for("@reflect-metadata:registry"):void 0,H=Fe(),X=qe(H);function _e(e,t,r,n){if(s(r)){if(!fe(e))throw new TypeError;if(!ue(t))throw new TypeError;return Se(e,t)}else{if(!fe(e))throw new TypeError;if(!p(t))throw new TypeError;if(!p(n)&&!s(n)&&!C(n))throw new TypeError;return C(n)&&(n=void 0),r=E(r),Re(e,t,r,n)}}v("decorate",_e);function be(e,t){function r(n,o){if(!p(n))throw new TypeError;if(!s(o)&&!Ge(o))throw new TypeError;te(e,t,n,o)}return r}v("metadata",be);function ge(e,t,r,n){if(!p(r))throw new TypeError;return s(n)||(n=E(n)),te(e,t,r,n)}v("defineMetadata",ge);function Oe(e,t,r){if(!p(t))throw new TypeError;return s(r)||(r=E(r)),Y(e,t,r)}v("hasMetadata",Oe);function me(e,t,r){if(!p(t))throw new TypeError;return s(r)||(r=E(r)),L(e,t,r)}v("hasOwnMetadata",me);function ke(e,t,r){if(!p(t))throw new TypeError;return s(r)||(r=E(r)),K(e,t,r)}v("getMetadata",ke);function Te(e,t,r){if(!p(t))throw new TypeError;return s(r)||(r=E(r)),ee(e,t,r)}v("getOwnMetadata",Te);function je(e,t){if(!p(e))throw new TypeError;return s(t)||(t=E(t)),re(e,t)}v("getMetadataKeys",je);function Ee(e,t){if(!p(e))throw new TypeError;return s(t)||(t=E(t)),ne(e,t)}v("getOwnMetadataKeys",Ee);function Pe(e,t,r){if(!p(t))throw new TypeError;if(s(r)||(r=E(r)),!p(t))throw new TypeError;s(r)||(r=E(r));var n=F(t,r,!1);return s(n)?!1:n.OrdinaryDeleteMetadata(e,t,r)}v("deleteMetadata",Pe);function Se(e,t){for(var r=e.length-1;r>=0;--r){var n=e[r],o=n(t);if(!s(o)&&!C(o)){if(!ue(o))throw new TypeError;t=o}}return t}function Re(e,t,r,n){for(var o=e.length-1;o>=0;--o){var w=e[o],b=w(t,r,n);if(!s(b)&&!C(b)){if(!p(b))throw new TypeError;n=b}}return n}function Y(e,t,r){var n=L(e,t,r);if(n)return!0;var o=Z(t);return C(o)?!1:Y(e,o,r)}function L(e,t,r){var n=F(t,r,!1);return s(n)?!1:ie(n.OrdinaryHasOwnMetadata(e,t,r))}function K(e,t,r){var n=L(e,t,r);if(n)return ee(e,t,r);var o=Z(t);if(!C(o))return K(e,o,r)}function ee(e,t,r){var n=F(t,r,!1);if(!s(n))return n.OrdinaryGetOwnMetadata(e,t,r)}function te(e,t,r,n){var o=F(r,n,!0);o.OrdinaryDefineOwnMetadata(e,t,r,n)}function re(e,t){var r=ne(e,t),n=Z(e);if(n===null)return r;var o=re(n,t);if(o.length<=0)return r;if(r.length<=0)return o;for(var w=new B,b=[],l=0,a=r;l<a.length;l++){var i=a[l],f=w.has(i);f||(w.add(i),b.push(i))}for(var u=0,h=o;u<h.length;u++){var i=h[u],f=w.has(i);f||(w.add(i),b.push(i))}return b}function ne(e,t){var r=F(e,t,!1);return r?r.OrdinaryOwnMetadataKeys(e,t):[]}function ae(e){if(e===null)return 1;switch(typeof e){case"undefined":return 0;case"boolean":return 2;case"string":return 3;case"symbol":return 4;case"number":return 5;case"object":return e===null?1:6;default:return 6}}function s(e){return e===void 0}function C(e){return e===null}function Ie(e){return typeof e=="symbol"}function p(e){return typeof e=="object"?e!==null:typeof e=="function"}function Ce(e,t){switch(ae(e)){case 0:return e;case 1:return e;case 2:return e;case 3:return e;case 4:return e;case 5:return e}var r=t===3?"string":t===5?"number":"default",n=oe(e,j);if(n!==void 0){var o=n.call(e,r);if(p(o))throw new TypeError;return o}return xe(e,r==="default"?"number":r)}function xe(e,t){if(t==="string"){var r=e.toString;if(x(r)){var n=r.call(e);if(!p(n))return n}var o=e.valueOf;if(x(o)){var n=o.call(e);if(!p(n))return n}}else{var o=e.valueOf;if(x(o)){var n=o.call(e);if(!p(n))return n}var w=e.toString;if(x(w)){var n=w.call(e);if(!p(n))return n}}throw new TypeError}function ie(e){return!!e}function Ae(e){return""+e}function E(e){var t=Ce(e,3);return Ie(t)?t:Ae(t)}function fe(e){return Array.isArray?Array.isArray(e):e instanceof Object?e instanceof Array:Object.prototype.toString.call(e)==="[object Array]"}function x(e){return typeof e=="function"}function ue(e){return typeof e=="function"}function Ge(e){switch(ae(e)){case 3:return!0;case 4:return!0;default:return!1}}function N(e,t){return e===t||e!==e&&t!==t}function oe(e,t){var r=e[t];if(r!=null){if(!x(r))throw new TypeError;return r}}function se(e){var t=oe(e,k);if(!x(t))throw new TypeError;var r=t.call(e);if(!p(r))throw new TypeError;return r}function ce(e){return e.value}function de(e){var t=e.next();return t.done?!1:t}function le(e){var t=e.return;t&&t.call(e)}function Z(e){var t=Object.getPrototypeOf(e);if(typeof e!="function"||e===G||t!==G)return t;var r=e.prototype,n=r&&Object.getPrototypeOf(r);if(n==null||n===Object.prototype)return t;var o=n.constructor;return typeof o!="function"||o===e?t:o}function De(){var e;!s(I)&&typeof d.Reflect<"u"&&!(I in d.Reflect)&&typeof d.Reflect.defineMetadata=="function"&&(e=Ue(d.Reflect));var t,r,n,o=new J,w={registerProvider:b,getProvider:a,setProvider:f};return w;function b(u){if(!Object.isExtensible(w))throw new Error("Cannot add provider to a frozen registry.");switch(!0){case e===u:break;case s(t):t=u;break;case t===u:break;case s(r):r=u;break;case r===u:break;default:n===void 0&&(n=new B),n.add(u);break}}function l(u,h){if(!s(t)){if(t.isProviderFor(u,h))return t;if(!s(r)){if(r.isProviderFor(u,h))return t;if(!s(n))for(var y=se(n);;){var M=de(y);if(!M)return;var T=ce(M);if(T.isProviderFor(u,h))return le(y),T}}}if(!s(e)&&e.isProviderFor(u,h))return e}function a(u,h){var y=o.get(u),M;return s(y)||(M=y.get(h)),s(M)&&(M=l(u,h),s(M)||(s(y)&&(y=new D,o.set(u,y)),y.set(h,M))),M}function i(u){if(s(u))throw new TypeError;return t===u||r===u||!s(n)&&n.has(u)}function f(u,h,y){if(!i(y))throw new Error("Metadata provider not registered.");var M=a(u,h);if(M!==y){if(!s(M))return!1;var T=o.get(u);s(T)&&(T=new D,o.set(u,T)),T.set(h,y)}return!0}}function Fe(){var e;return!s(I)&&p(d.Reflect)&&Object.isExtensible(d.Reflect)&&(e=d.Reflect[I]),s(e)&&(e=De()),!s(I)&&p(d.Reflect)&&Object.isExtensible(d.Reflect)&&Object.defineProperty(d.Reflect,I,{enumerable:!1,configurable:!1,writable:!1,value:e}),e}function qe(e){var t=new J,r={isProviderFor:function(i,f){var u=t.get(i);return s(u)?!1:u.has(f)},OrdinaryDefineOwnMetadata:b,OrdinaryHasOwnMetadata:o,OrdinaryGetOwnMetadata:w,OrdinaryOwnMetadataKeys:l,OrdinaryDeleteMetadata:a};return H.registerProvider(r),r;function n(i,f,u){var h=t.get(i),y=!1;if(s(h)){if(!u)return;h=new D,t.set(i,h),y=!0}var M=h.get(f);if(s(M)){if(!u)return;if(M=new D,h.set(f,M),!e.setProvider(i,f,r))throw h.delete(f),y&&t.delete(i),new Error("Wrong provider for target.")}return M}function o(i,f,u){var h=n(f,u,!1);return s(h)?!1:ie(h.has(i))}function w(i,f,u){var h=n(f,u,!1);if(!s(h))return h.get(i)}function b(i,f,u,h){var y=n(u,h,!0);y.set(i,f)}function l(i,f){var u=[],h=n(i,f,!1);if(s(h))return u;for(var y=h.keys(),M=se(y),T=0;;){var he=de(M);if(!he)return u.length=T,u;var ze=ce(he);try{u[T]=ze}catch(Be){try{le(M)}finally{throw Be}}T++}}function a(i,f,u){var h=n(f,u,!1);if(s(h)||!h.delete(i))return!1;if(h.size===0){var y=t.get(f);s(y)||(y.delete(u),y.size===0&&t.delete(y))}return!0}}function Ue(e){var t=e.defineMetadata,r=e.hasOwnMetadata,n=e.getOwnMetadata,o=e.getOwnMetadataKeys,w=e.deleteMetadata,b=new J,l={isProviderFor:function(a,i){var f=b.get(a);return!s(f)&&f.has(i)?!0:o(a,i).length?(s(f)&&(f=new B,b.set(a,f)),f.add(i),!0):!1},OrdinaryDefineOwnMetadata:t,OrdinaryHasOwnMetadata:r,OrdinaryGetOwnMetadata:n,OrdinaryOwnMetadataKeys:o,OrdinaryDeleteMetadata:w};return l}function F(e,t,r){var n=H.getProvider(e,t);if(!s(n))return n;if(r){if(H.setProvider(e,t,X))return X;throw new Error("Illegal state.")}}function We(){var e={},t=[],r=function(){function l(a,i,f){this._index=0,this._keys=a,this._values=i,this._selector=f}return l.prototype["@@iterator"]=function(){return this},l.prototype[k]=function(){return this},l.prototype.next=function(){var a=this._index;if(a>=0&&a<this._keys.length){var i=this._selector(this._keys[a],this._values[a]);return a+1>=this._keys.length?(this._index=-1,this._keys=t,this._values=t):this._index++,{value:i,done:!1}}return{value:void 0,done:!0}},l.prototype.throw=function(a){throw this._index>=0&&(this._index=-1,this._keys=t,this._values=t),a},l.prototype.return=function(a){return this._index>=0&&(this._index=-1,this._keys=t,this._values=t),{value:a,done:!0}},l}(),n=function(){function l(){this._keys=[],this._values=[],this._cacheKey=e,this._cacheIndex=-2}return Object.defineProperty(l.prototype,"size",{get:function(){return this._keys.length},enumerable:!0,configurable:!0}),l.prototype.has=function(a){return this._find(a,!1)>=0},l.prototype.get=function(a){var i=this._find(a,!1);return i>=0?this._values[i]:void 0},l.prototype.set=function(a,i){var f=this._find(a,!0);return this._values[f]=i,this},l.prototype.delete=function(a){var i=this._find(a,!1);if(i>=0){for(var f=this._keys.length,u=i+1;u<f;u++)this._keys[u-1]=this._keys[u],this._values[u-1]=this._values[u];return this._keys.length--,this._values.length--,N(a,this._cacheKey)&&(this._cacheKey=e,this._cacheIndex=-2),!0}return!1},l.prototype.clear=function(){this._keys.length=0,this._values.length=0,this._cacheKey=e,this._cacheIndex=-2},l.prototype.keys=function(){return new r(this._keys,this._values,o)},l.prototype.values=function(){return new r(this._keys,this._values,w)},l.prototype.entries=function(){return new r(this._keys,this._values,b)},l.prototype["@@iterator"]=function(){return this.entries()},l.prototype[k]=function(){return this.entries()},l.prototype._find=function(a,i){if(!N(this._cacheKey,a)){this._cacheIndex=-1;for(var f=0;f<this._keys.length;f++)if(N(this._keys[f],a)){this._cacheIndex=f;break}}return this._cacheIndex<0&&i&&(this._cacheIndex=this._keys.length,this._keys.push(a),this._values.push(void 0)),this._cacheIndex},l}();return n;function o(l,a){return l}function w(l,a){return a}function b(l,a){return[l,a]}}function He(){var e=function(){function t(){this._map=new D}return Object.defineProperty(t.prototype,"size",{get:function(){return this._map.size},enumerable:!0,configurable:!0}),t.prototype.has=function(r){return this._map.has(r)},t.prototype.add=function(r){return this._map.set(r,r),this},t.prototype.delete=function(r){return this._map.delete(r)},t.prototype.clear=function(){this._map.clear()},t.prototype.keys=function(){return this._map.keys()},t.prototype.values=function(){return this._map.keys()},t.prototype.entries=function(){return this._map.entries()},t.prototype["@@iterator"]=function(){return this.keys()},t.prototype[k]=function(){return this.keys()},t}();return e}function Ve(){var e=16,t=S.create(),r=n();return function(){function a(){this._key=n()}return a.prototype.has=function(i){var f=o(i,!1);return f!==void 0?S.has(f,this._key):!1},a.prototype.get=function(i){var f=o(i,!1);return f!==void 0?S.get(f,this._key):void 0},a.prototype.set=function(i,f){var u=o(i,!0);return u[this._key]=f,this},a.prototype.delete=function(i){var f=o(i,!1);return f!==void 0?delete f[this._key]:!1},a.prototype.clear=function(){this._key=n()},a}();function n(){var a;do a="@@WeakMap@@"+l();while(S.has(t,a));return t[a]=!0,a}function o(a,i){if(!g.call(a,r)){if(!i)return;Object.defineProperty(a,r,{value:S.create()})}return a[r]}function w(a,i){for(var f=0;f<i;++f)a[f]=Math.random()*255|0;return a}function b(a){if(typeof Uint8Array=="function"){var i=new Uint8Array(a);return typeof crypto<"u"?crypto.getRandomValues(i):typeof msCrypto<"u"?msCrypto.getRandomValues(i):w(i,a),i}return w(new Array(a),a)}function l(){var a=b(e);a[6]=a[6]&79|64,a[8]=a[8]&191|128;for(var i="",f=0;f<e;++f){var u=a[f];(f===4||f===6||f===8)&&(i+="-"),u<16&&(i+="0"),i+=u.toString(16).toLowerCase()}return i}}function $(e){return e.__=void 0,delete e.__,e}})})(Q||(Q={}));function V(c){for(;c;){if(c===O)return!0;c=Object.getPrototypeOf(c)}return!1}const q=c=>{if(!(typeof c>"u"))return Array.isArray(c)?c.map(v=>q(v)):V(c)?z(c):typeof c=="object"?Object.keys(c).reduce((v,d)=>{if(["enum","const","default","meta"].some(g=>g===d))v[d]=c[d];else if(d==="type"&&c[d]==="formatted-string")v.type="string";else{if(d==="required")return v;v[d]=q(c[d])}return v},{}):c},z=c=>{if(c)return V(c)?c.getSchema():q(c)},ye=c=>c?V(c)?!!Reflect.getMetadata("objectIsRequired",c):!!c.required:!1;function pe(c){return function(v,d){const g=Reflect.getMetadata("properties",v)||{};g[d]=c,Reflect.defineMetadata("properties",g,v)}}function we(c){return function(v){const d=Reflect.getMetadata("properties",v.prototype)||{},g={type:"object",...q(c)||{},properties:Object.keys(d).reduce((_,j)=>{const k=z(d[j]);return k&&(_[j]=k),_},{}),required:Object.keys(d).filter(_=>ye(d[_]))};Reflect.defineMetadata("schema",g,v),Reflect.defineMetadata("objectIsRequired",c==null?void 0:c.required,v)}}const Me=c=>z(c);m.AjvObject=we,m.AjvProperty=pe,m.AjvSchema=O,m.getSchema=Me,Object.defineProperty(m,Symbol.toStringTag,{value:"Module"})});