UNPKG

@alecvision/borg

Version:

Model your data with Borg and get run-time type validation, TypeScript types, (de)serialization, and MongoDB niceties such as BSON type conversion and auto-generated validation schemas - for free.

23 lines (17 loc) 23.6 kB
import { Double, ObjectId } from 'bson'; var te=(l,s,e)=>{if(!s.has(l))throw TypeError("Cannot "+e)};var r=(l,s,e)=>(te(l,s,"read from private field"),e?e.call(l):s.get(l)),i=(l,s,e)=>{if(s.has(l))throw TypeError("Cannot add the same private member more than once");s instanceof WeakSet?s.add(l):s.set(l,e);},a=(l,s,e,t)=>(te(l,s,"write to private field"),t?t.call(l,e):s.set(l,e),e);var _=(l,s,e)=>(te(l,s,"access private method"),e);var p=class{constructor(){if(new.target===p)throw new TypeError("Borg is an abstract class and cannot be instantiated directly")}};var k,n=class extends Error{constructor(e,t,o){super(`${e}`);i(this,k,[]);a(this,k,[...o||[],...t?.path||[]]),this.message=`${e} at "${r(this,k).join(".")||"{ROOT}"}"${t?.message?`: ${t.message}`:""}`,t&&(this.stack=`${this.stack} ### [CAUSED BY]:### ${t.stack}`);}get path(){return r(this,k)}};k=new WeakMap;var P=(l,s)=>s in l;function S(l){switch(l.kind){case"union":{let{nullable:s,borgMembers:e}=l;return Object.freeze({oneOf:[...e.map(t=>t.bsonSchema),...s?[{bsonType:"null"}]:[]]})}case"string":{let{minLength:s,maxLength:e,nullable:t,regex:o}=l;return Object.freeze({bsonType:t?Object.freeze(["string","null"]):"string",...s!==null?{minLength:s}:{},...e!==null?{maxLength:e}:{},...o?{pattern:o.source}:{}})}case"number":{let{min:s,max:e,nullable:t}=l;return {bsonType:t?Object.freeze(["double","null"]):"double",...s!==null?{minimum:s}:{},...e!==null?{maximum:e}:{}}}case"array":{let{minItems:s,maxItems:e,nullable:t,borgItems:o}=l;return {bsonType:t?Object.freeze(["array","null"]):"array",items:o.bsonSchema,...s!==null?{minItems:s}:{},...e!==null?{maxItems:e}:{}}}case"object":{let{nullable:s,borgShape:e,requiredKeys:t,additionalProperties:o}=l,c=Object.fromEntries(Object.entries(e).map(([ge,pe])=>[ge,pe.bsonSchema]));return {bsonType:s?Object.freeze(["object","null"]):"object",...Object.keys(c).length>0?{properties:Object.freeze(c)}:{},...t.length>0?{required:Object.freeze([...t])}:{},...o==="strict"||o==="strip"?{additionalProperties:!1}:{},...o instanceof p?{additionalProperties:o.bsonSchema}:{}}}case"boolean":{let{nullable:s}=l;return {bsonType:s?Object.freeze(["bool","null"]):"bool"}}case"id":{let{nullable:s}=l;return {bsonType:s?Object.freeze(["objectId","null"]):"objectId"}}}}var y,v,b,J,oe,X=class extends p{constructor(e){super();i(this,y,void 0);i(this,v,"strip");i(this,b,{optional:!1,nullable:!1,private:!1});a(this,y,Object.freeze(Object.fromEntries(Object.entries(e).map(([t,o])=>[t,o.copy()]))));}get meta(){return Object.freeze({kind:"object",borgShape:r(this,y),keys:Object.freeze(Object.keys(r(this,y))),requiredKeys:Object.freeze(Object.keys(r(this,y)).filter(e=>r(this,y)[e].meta.optional===!1)),additionalProperties:r(this,v)instanceof p?r(this,v).copy():r(this,v),...r(this,b)})}get bsonSchema(){return S(this.meta)}is(e){return this.try(e).ok}copy(){var e;return _(e=X,J,oe).call(e,this)}parse(e){if(e===void 0){if(r(this,b).optional)return;throw new n(`OBJECT_ERROR: Expected object${r(this,b).nullable?" or null":""}, got undefined`)}if(e===null){if(r(this,b).nullable)return null;throw new n(`OBJECT_ERROR: Expected object${r(this,b).optional?" or undefined":""}, got null`)}if(typeof e!="object"||Array.isArray(e))throw new n(`OBJECT_ERROR: Expected object,${r(this,b).optional?" or undefined,":""}${r(this,b).nullable?" or null,":""} got ${Array.isArray(e)?"array":typeof e}`);let t={};if(r(this,v)==="strict"){for(let o in e)if(!P(r(this,y),o))throw new n(`OBJECT_ERROR: Unexpected property "${o}"`,void 0,[o])}for(let o in r(this,y)){if(!P(e,o)){if(r(this,y)[o].meta.optional===!1)throw new n(`OBJECT_ERROR: Missing property "${o}"`,void 0,[o]);continue}try{let c=r(this,y)[o].parse(e[o]);t[o]=c;continue}catch(c){throw c instanceof n?new n(`OBJECT_ERROR: Invalid value for property "${o}"`,c,[o]):new n(`OBJECT_ERROR: Unknown error parsing "${o}": ${JSON.stringify(c)}`,void 0,[o])}}if(r(this,v)==="passthrough")for(let o in e)P(r(this,y),o)||(t[o]=e[o]);if(r(this,v)instanceof p){for(let o in e)if(!P(r(this,y),o)){let c=r(this,v).try(e[o]);if(c.ok)t[o]=c.value;else throw new n(`OBJECT_ERROR: Invalid value for extra property "${o}"`,c.error,[o])}}return t}try(e){try{return {value:this.parse(e),ok:!0,meta:this.meta}}catch(t){return t instanceof n?{ok:!1,error:t}:{ok:!1,error:new n(`OBJECT_ERROR(try): Unknown error parsing: ${JSON.stringify(t)}`)}}}toBson(e){if(e==null)return e;let t={};for(let o in r(this,y))P(e,o)&&(t[o]=r(this,y)[o].toBson(e[o]));return t}fromBson(e){if(e==null)return e;let t={};for(let o in r(this,y))P(e,o)&&(t[o]=r(this,y)[o].fromBson(e[o]));return t}optional(){let e=this.copy();return r(e,b).optional=!0,e}nullable(){let e=this.copy();return r(e,b).nullable=!0,e}nullish(){let e=this.copy();return r(e,b).optional=!0,r(e,b).nullable=!0,e}required(){let e=this.copy();return r(e,b).optional=!1,e}notNull(){let e=this.copy();return r(e,b).nullable=!1,e}notNullish(){let e=this.copy();return r(e,b).optional=!1,r(e,b).nullable=!1,e}private(){let e=this.copy();return r(e,b).private=!0,e}public(){let e=this.copy();return r(e,b).private=!1,e}additionalProperties(e){let t=this.copy();return a(t,v,e),t}},q=X;y=new WeakMap,v=new WeakMap,b=new WeakMap,J=new WeakSet,oe=function(e){let t={};for(let c in r(e,y))t[c]=r(e,y)[c].copy();let o=new X(t);return a(o,b,{...r(e,b)}),a(o,v,r(e,v)instanceof p?r(e,v).copy():r(e,v)),o},i(q,J);var F,h,x,E,H,ne,V=class extends p{constructor(e){super();i(this,F,void 0);i(this,h,{optional:!1,nullable:!1,private:!1});i(this,x,null);i(this,E,null);a(this,F,e);}get meta(){return Object.freeze({kind:"array",maxItems:r(this,x),minItems:r(this,E),borgItems:r(this,F).copy(),...r(this,h)})}get bsonSchema(){return S(this.meta)}is(e){return this.try(e).ok}copy(){var e;return _(e=V,H,ne).call(e,this)}parse(e){if(e===void 0){if(r(this,h).optional)return;throw new n(`ARRAY_ERROR: Expected array${r(this,h).nullable?" or null":""}, got undefined`)}if(e===null){if(r(this,h).nullable)return null;throw new n(`ARRAY_ERROR: Expected array${r(this,h).optional?" or undefined":""}, got null`)}if(!Array.isArray(e))throw new n(`ARRAY_ERROR: Expected array,${r(this,h).optional?" or undefined,":""}${r(this,h).nullable?" or null,":""} got ${typeof e}`);if(r(this,E)!==null&&e.length<r(this,E))throw new n(`ARRAY_ERROR: Expected array length to be greater than or equal to ${r(this,E)}, got ${e.length}`);if(r(this,x)!==null&&e.length>r(this,x))throw new n(`ARRAY_ERROR: Expected array length to be less than or equal to ${r(this,x)}, got ${e.length}`);let t=new Array(e.length);for(let o=0;o<e.length;o++)try{let c=r(this,F).parse(e[o]);t[o]=c;continue}catch(c){throw c instanceof n?new n(`ARRAY_ERROR: ${c.message} at index ${o}`,c,[o]):new n(`ARRAY_ERROR: Unknown error parsing index "${o}": ${JSON.stringify(c)}`,void 0,[o])}return t}try(e){try{return {value:this.parse(e),ok:!0,meta:this.meta}}catch(t){return t instanceof n?{ok:!1,error:t}:{ok:!1,error:new n(`ARRAY_ERROR(try): Unknown error parsing: ${JSON.stringify(t)}`)}}}toBson(e){if(e==null)return e;let t=new Array(e.length);for(let o=0;o<e.length;o++)t[o]=r(this,F).toBson(e[o]);return t}fromBson(e){if(e==null)return e;let t=new Array(e.length);for(let o=0;o<e.length;o++)t[o]=r(this,F).fromBson(e[o]);return t}optional(){let e=this.copy();return r(e,h).optional=!0,e}nullable(){let e=this.copy();return r(e,h).nullable=!0,e}nullish(){let e=this.copy();return r(e,h).nullable=!0,r(e,h).optional=!0,e}required(){let e=this.copy();return r(e,h).optional=!1,e}notNull(){let e=this.copy();return r(e,h).nullable=!1,e}notNullish(){let e=this.copy();return r(e,h).nullable=!1,r(e,h).optional=!1,e}private(){let e=this.copy();return r(e,h).private=!0,e}public(){let e=this.copy();return r(e,h).private=!1,e}minItems(e){if(e&&e<0)throw new RangeError("Min length cannot be negative");if(e&&e>Number.MAX_SAFE_INTEGER)throw new RangeError(`Min length cannot be greater than ${Number.MAX_SAFE_INTEGER}`);if(e!==null&&!Number.isInteger(e))throw new TypeError("Min length must be an integer or null");let t=this.copy();if(a(t,E,e),r(t,E)!==null&&r(t,x)!==null&&r(t,E)>r(t,x))throw new RangeError("Min length cannot be greater than max length");return t}maxItems(e){if(e&&e<0)throw new RangeError("Max length cannot be negative");if(e&&e>Number.MAX_SAFE_INTEGER)throw new RangeError(`Max length cannot be greater than ${Number.MAX_SAFE_INTEGER}`);if(e!==null&&!Number.isInteger(e))throw new TypeError("Max length must be an integer or null");let t=this.copy();if(a(t,x,e),r(t,E)!==null&&r(t,x)!==null&&r(t,E)>r(t,x))throw new RangeError("Max length cannot be greater than max length");return t}length(e,t){if(e&&e<0||t&&t<0)throw new RangeError("Length cannot be negative");if(e&&e>Number.MAX_SAFE_INTEGER||t&&t>Number.MAX_SAFE_INTEGER)throw new RangeError(`Length cannot be greater than ${Number.MAX_SAFE_INTEGER}`);if(e!==null&&!Number.isInteger(e))throw new TypeError(`${t===void 0?"L":"Min l"}ength must be an integer or null`);if(t!=null&&!Number.isInteger(t))throw new TypeError("Max length must be an integer or null");let o=this.copy();if(a(o,E,e),a(o,x,t===void 0?e:t),r(o,E)!==null&&r(o,x)!==null&&r(o,E)>r(o,x))throw new RangeError("Min length cannot be greater than max length");return o}},A=V;F=new WeakMap,h=new WeakMap,x=new WeakMap,E=new WeakMap,H=new WeakSet,ne=function(e){let t=new V(r(e,F));return a(t,h,{...r(e,h)}),a(t,x,r(e,x)),a(t,E,r(e,E)),t},i(A,H);var f,T,B,I,z,ae,D=class extends p{constructor(){super();i(this,f,{optional:!1,nullable:!1,private:!1});i(this,T,null);i(this,B,null);i(this,I,null);}get meta(){return Object.freeze({...r(this,f),kind:"string",maxLength:r(this,B),minLength:r(this,T),pattern:r(this,I),regex:r(this,I)?Object.freeze(new RegExp(r(this,I))):void 0})}get bsonSchema(){return S(this.meta)}is(e){return this.try(e).ok}copy(){var e;return _(e=D,z,ae).call(e,this)}parse(e){if(e===void 0){if(r(this,f).optional)return;throw new n(`STRING_ERROR: Expected string${r(this,f).nullable?" or null":""}, got undefined`)}if(e===null){if(r(this,f).nullable)return null;throw new n(`STRING_ERROR: Expected string${r(this,f).optional?" or undefined":""}, got null`)}if(typeof e!="string")throw new n(`STRING_ERROR: Expected string,${r(this,f).optional?" or undefined,":""}${r(this,f).nullable?" or null,":""} got ${typeof e}`);if(r(this,T)!==null&&e.length<r(this,T))throw new n(`STRING_ERROR: Expected string length to be greater than or equal to ${r(this,T)}, got ${e.length}`);if(r(this,B)!==null&&e.length>r(this,B))throw new n(`STRING_ERROR: Expected string length to be less than or equal to ${r(this,B)}, got ${e.length}`);if(r(this,I)!==null&&!new RegExp(r(this,I),"u").test(e))throw new n(`STRING_ERROR: Expected string to match pattern ${r(this,I)}, got ${e}`);return e}try(e){try{return {value:this.parse(e),ok:!0,meta:this.meta}}catch(t){return t instanceof n?{ok:!1,error:t}:{ok:!1,error:new n(`STRING_ERROR(try): Unknown error parsing: ${JSON.stringify(t)}`)}}}toBson(e){return e}fromBson(e){return e}optional(){let e=this.copy();return r(e,f).optional=!0,e}nullable(){let e=this.copy();return r(e,f).nullable=!0,e}nullish(){let e=this.copy();return r(e,f).optional=!0,r(e,f).nullable=!0,e}required(){let e=this.copy();return r(e,f).optional=!1,e}notNull(){let e=this.copy();return r(e,f).nullable=!1,e}notNullish(){let e=this.copy();return r(e,f).optional=!1,r(e,f).nullable=!1,e}private(){let e=this.copy();return r(e,f).private=!0,e}public(){let e=this.copy();return r(e,f).private=!1,e}minLength(e){if(e&&e<0)throw new RangeError("Min length cannot be negative");if(e&&e>Number.MAX_SAFE_INTEGER)throw new RangeError(`Min length cannot be greater than ${Number.MAX_SAFE_INTEGER}`);if(e!==null&&!Number.isInteger(e))throw new TypeError("Min length must be an integer or null");let t=this.copy();if(a(t,T,e),r(t,T)!==null&&r(t,B)!==null&&r(t,T)>r(t,B))throw new RangeError("Min length cannot be greater than max length");return t}maxLength(e){if(e&&e<0)throw new RangeError("Max length cannot be negative");if(e&&e>Number.MAX_SAFE_INTEGER)throw new RangeError(`Max length cannot be greater than ${Number.MAX_SAFE_INTEGER}`);if(e!==null&&!Number.isInteger(e))throw new TypeError("Max length must be an integer or null");let t=this.copy();if(a(t,B,e),r(t,T)!==null&&r(t,B)!==null&&r(t,T)>r(t,B))throw new RangeError("Max length cannot be greater than max length");return t}length(e,t){if(e&&e<0||t&&t<0)throw new RangeError("Length cannot be negative");if(e&&e>Number.MAX_SAFE_INTEGER||t&&t>Number.MAX_SAFE_INTEGER)throw new RangeError(`Length cannot be greater than ${Number.MAX_SAFE_INTEGER}`);if(e!==null&&!Number.isInteger(e))throw new TypeError(`${t===void 0?"L":"Min l"}ength must be an integer or null`);if(t!=null&&!Number.isInteger(t))throw new TypeError("Max length must be an integer or null");let o=this.copy();if(a(o,T,e),a(o,B,t===void 0?e:t),r(o,T)!==null&&r(o,B)!==null&&r(o,T)>r(o,B))throw new RangeError("Min length cannot be greater than max length");return o}pattern(e){let t=this.copy();return a(t,I,e),t}},j=D;f=new WeakMap,T=new WeakMap,B=new WeakMap,I=new WeakMap,z=new WeakSet,ae=function(e){let t=new D;return a(t,f,{...r(e,f)}),a(t,T,r(e,T)),a(t,B,r(e,B)),a(t,I,r(e,I)),t},i(j,z);var d,Y,se,K=class extends p{constructor(){super();i(this,d,{optional:!1,nullable:!1,private:!1});}get meta(){return Object.freeze({kind:"boolean",...r(this,d)})}get bsonSchema(){return S(this.meta)}is(e){return this.try(e).ok}copy(){var e;return _(e=K,Y,se).call(e,this)}parse(e){if(e===void 0){if(r(this,d).optional)return;throw new n(`BOOLEAN_ERROR: Expected boolean${r(this,d).nullable?" or null":""}, got undefined`)}if(e===null){if(r(this,d).nullable)return null;throw new n(`BOOLEAN_ERROR: Expected boolean${r(this,d).optional?" or undefined":""}, got null`)}if(typeof e!="boolean")throw new n(`BOOLEAN_ERROR: Expected boolean,${r(this,d).optional?" or undefined,":""}${r(this,d).nullable?" or null,":""} got ${typeof e}`);return e}try(e){try{return {value:this.parse(e),ok:!0,meta:this.meta}}catch(t){return t instanceof n?{ok:!1,error:t}:{ok:!1,error:new n(`BOOLEAN_ERROR(try): Unknown error parsing: ${JSON.stringify(t)}`)}}}toBson(e){return e}fromBson(e){return e}optional(){let e=this.copy();return r(e,d).optional=!0,e}nullable(){let e=this.copy();return r(e,d).nullable=!0,e}nullish(){let e=this.copy();return r(e,d).optional=!0,r(e,d).nullable=!0,e}required(){let e=this.copy();return r(e,d).optional=!1,e}notNull(){let e=this.copy();return r(e,d).nullable=!1,e}notNullish(){let e=this.copy();return r(e,d).optional=!1,r(e,d).nullable=!1,e}private(){let e=this.copy();return r(e,d).private=!0,e}public(){let e=this.copy();return r(e,d).private=!1,e}},$=K;d=new WeakMap,Y=new WeakSet,se=function(e){let t=new K;return a(t,d,{...r(e,d)}),t},i($,Y);var u,w,N,W,le,Q=class extends p{constructor(){super();i(this,u,{optional:!1,nullable:!1,private:!1});i(this,w,null);i(this,N,null);}get meta(){return Object.freeze({kind:"number",max:r(this,N),min:r(this,w),...r(this,u)})}get bsonSchema(){return S(this.meta)}is(e){return this.try(e).ok}copy(){var e;return _(e=Q,W,le).call(e,this)}parse(e){if(e===void 0){if(r(this,u).optional)return;throw new n(`NUMBER_ERROR: Expected number${r(this,u).nullable?" or null":""}, got undefined`)}if(e===null){if(r(this,u).nullable)return null;throw new n(`NUMBER_ERROR: Expected number${r(this,u).optional?" or undefined":""}, got null`)}if(typeof e!="number")throw new n(`NUMBER_ERROR: Expected number,${r(this,u).optional?" or undefined,":""}${r(this,u).nullable?" or null,":""} got ${typeof e}`);if(Number.isNaN(e))throw new n(`NUMBER_ERROR: Expected number,${r(this,u).optional?" or undefined,":""}${r(this,u).nullable?" or null,":""} got NaN`);if(r(this,w)!==null&&e<r(this,w))throw new n(`NUMBER_ERROR: Expected number to be greater than or equal to ${r(this,w)}`);if(r(this,N)!==null&&e>r(this,N))throw new n(`NUMBER_ERROR: Expected number to be less than or equal to ${r(this,N)}`);return e}try(e){try{return {value:this.parse(e),ok:!0,meta:this.meta}}catch(t){return t instanceof n?{ok:!1,error:t}:{ok:!1,error:new n(`NUMBER_ERROR(try): Unknown error parsing: ${JSON.stringify(t)}`)}}}toBson(e){return typeof e=="number"?new Double(e):e}fromBson(e){return e instanceof Double?e.valueOf():e}optional(){let e=this.copy();return r(e,u).optional=!0,e}nullable(){let e=this.copy();return r(e,u).nullable=!0,e}nullish(){let e=this.copy();return r(e,u).optional=!0,r(e,u).nullable=!0,e}required(){let e=this.copy();return r(e,u).optional=!1,e}notNull(){let e=this.copy();return r(e,u).nullable=!1,e}notNullish(){let e=this.copy();return r(e,u).optional=!1,r(e,u).nullable=!1,e}private(){let e=this.copy();return r(e,u).private=!0,e}public(){let e=this.copy();return r(e,u).private=!1,e}min(e){if(e&&e>Number.MAX_SAFE_INTEGER||e&&e<Number.MIN_SAFE_INTEGER)throw new RangeError(`Numbers cannot be less than ${Number.MIN_SAFE_INTEGER} or greater than ${Number.MAX_SAFE_INTEGER}`);if(Number.isNaN(e))throw new TypeError("Min cannot be NaN");let t=this.copy();if(a(t,w,e),r(t,w)!==null&&r(t,N)!==null&&r(t,w)>r(t,N))throw new RangeError("Minimum cannot be greater than maximum");return t}max(e){if(e&&e>Number.MAX_SAFE_INTEGER||e&&e<Number.MIN_SAFE_INTEGER)throw new RangeError(`Numbers cannot be less than ${Number.MIN_SAFE_INTEGER} or greater than ${Number.MAX_SAFE_INTEGER}`);if(Number.isNaN(e))throw new TypeError("Max cannot be NaN");let t=this.copy();if(a(t,N,e),r(t,N)!==null&&r(t,w)!==null&&r(t,w)>r(t,N))throw new RangeError("Minimum cannot be greater than maximum");return t}range(e,t){if(e&&e>Number.MAX_SAFE_INTEGER||e&&e<Number.MIN_SAFE_INTEGER||t&&t>Number.MAX_SAFE_INTEGER||t&&t<Number.MIN_SAFE_INTEGER)throw new RangeError(`Numbers cannot be less than ${Number.MIN_SAFE_INTEGER} or greater than ${Number.MAX_SAFE_INTEGER}`);if(Number.isNaN(e))throw new TypeError("Min cannot be NaN");if(Number.isNaN(t))throw new TypeError("Max cannot be NaN");let o=this.copy();if(a(o,w,e),a(o,N,t),r(o,w)!==null&&r(o,N)!==null&&r(o,w)>r(o,N))throw new RangeError("Minimum cannot be greater than maximum");return o}},L=Q;u=new WeakMap,w=new WeakMap,N=new WeakMap,W=new WeakSet,le=function(e){let t=new Q;return a(t,u,{...r(e,u)}),a(t,w,r(e,w)),a(t,N,r(e,N)),t},i(L,W);var m,R,Z,ce,G=class extends p{constructor(){super();i(this,m,{optional:!1,nullable:!1,private:!1});i(this,R,!0);}static isObjectIdLike(e){return typeof e!="object"||e===null?!1:"toHexString"in e&&"id"in e&&typeof e.toHexString=="function"&&(typeof e.id=="string"||e.id instanceof Uint8Array)}get meta(){return Object.freeze({kind:"id",format:r(this,R)?"string":"oid",...r(this,m)})}get bsonSchema(){return S(this.meta)}is(e){return this.try(e).ok}copy(){var e;return _(e=G,Z,ce).call(e,this)}parse(e){if(e===void 0){if(r(this,m).optional)return;throw new n(`ID_ERROR: Expected valid ObjectId${r(this,m).nullable?" or null":""}, got undefined`)}if(e===null){if(r(this,m).nullable)return null;throw new n(`ID_ERROR: Expected valid ObjectId${r(this,m).optional?" or undefined":""}, got null`)}if(typeof e=="string"&&ObjectId.isValid(e))return r(this,R)?e:ObjectId.createFromHexString(e);if(typeof e=="number")throw new n(`ID_ERROR: Numeric IDs are not supported, got ${e}`);if(e instanceof Uint8Array){let t=Buffer.from(e).toString("hex");if(ObjectId.isValid(e))return r(this,R)?t:ObjectId.createFromHexString(t)}if(e instanceof ObjectId)return r(this,R)?e.toHexString():e;if(G.isObjectIdLike(e)){let t=e.toHexString();if(ObjectId.isValid(e))return r(this,R)?t:ObjectId.createFromHexString(t)}throw new n(`ID_ERROR: Expected valid ObjectId,${r(this,m).optional?" or undefined,":""}${r(this,m).nullable?" or null,":""} got ${typeof e}`)}try(e){try{return {value:this.parse(e),ok:!0,meta:this.meta}}catch(t){return t instanceof n?{ok:!1,error:t}:{ok:!1,error:new n(`ID_ERROR(try): Unknown error parsing: ${JSON.stringify(t)}`)}}}toBson(e){return e instanceof ObjectId||e===void 0||e===null?e:ObjectId.createFromHexString(e.toString())}fromBson(e){if(e==null||!r(this,R))return e;try{return e.toHexString()}catch{throw new n(`ID_ERROR(fromBson): Expected valid ObjectId, got ${typeof e}`)}}optional(){let e=this.copy();return r(e,m).optional=!0,e}nullable(){let e=this.copy();return r(e,m).nullable=!0,e}nullish(){let e=this.copy();return r(e,m).optional=!0,r(e,m).nullable=!0,e}required(){let e=this.copy();return r(e,m).optional=!1,e}notNull(){let e=this.copy();return r(e,m).nullable=!1,e}notNullish(){let e=this.copy();return r(e,m).optional=!1,r(e,m).nullable=!1,e}private(){let e=this.copy();return r(e,m).private=!0,e}public(){let e=this.copy();return r(e,m).private=!1,e}asString(){let e=this.copy();return a(e,R,!0),e}asObjectId(){let e=this.copy();return a(e,R,!1),e}},C=G;m=new WeakMap,R=new WeakMap,Z=new WeakSet,ce=function(e){let t=new G;return a(t,m,{...r(e,m)}),a(t,R,r(e,R)),t},i(C,Z);var O,g,ee,ue,re=class extends p{constructor(e){super();i(this,O,void 0);i(this,g,{optional:!1,nullable:!1,private:!1});a(this,O,Object.freeze(e.map(t=>t.copy())));}get meta(){return Object.freeze({kind:"union",borgMembers:r(this,O).map(e=>e.copy()),...r(this,g)})}get bsonSchema(){return S(this.meta)}is(e){return this.try(e).ok}copy(){var e;return _(e=re,ee,ue).call(e,this)}parse(e){if(e===void 0){if(r(this,O).some(o=>o.meta.optional)||r(this,g).optional)return;throw new n(`UNION_ERROR: Expected valid type${r(this,g).nullable?" or null":""}, got undefined`)}if(e===null){if(r(this,g).nullable)return null;throw new n(`UNION_ERROR: Expected valid type${r(this,g).optional?" or undefined":""}, got null`)}if(typeof e=="symbol")throw new n(`UNION_ERROR: Expected valid type${r(this,g).optional?" or undefined":""}${r(this,g).nullable?" or null":""}, got symbol: ${e.toString()}`);let t=[];for(let o of r(this,O)){let c=o.try(e);if(c.ok)return c.value;t.push(c.error);}throw new n(`UNION_ERROR: Expected valid type${r(this,g).optional?" or undefined":""}${r(this,g).nullable?" or null":""}, got ${e}. Errors: ${t.map(o=>typeof o=="string"?o:typeof o=="object"&&o&&"message"in o&&typeof o.message=="string"?o.message:"UNKNOWN ERROR").join(` `)}`)}try(e){try{return {value:this.parse(e),ok:!0,meta:this.meta}}catch(t){return t instanceof n?{ok:!1,error:t}:{ok:!1,error:new n(`UNION_ERROR(try): Unknown error parsing: ${JSON.stringify(t)}`)}}}toBson(e){if(e==null)return e;for(let t of r(this,O))if(t.is(e))return t.toBson(e);throw new n("TO_BSON_ERROR: Invalid input")}fromBson(e){if(e==null)return e;for(let t of r(this,O))try{try{let o=t.fromBson(e),c=t.try(o);if(c.ok)return o;throw c.error}catch(o){if(!(o instanceof n))throw o}}catch(o){throw o instanceof n?o:new n(`UNION_ERROR(fromBson): Unknown error parsing ${JSON.stringify(e)}: ${JSON.stringify(o&&typeof o=="object"&&"message"in o&&typeof o.message=="string"?o.message:o)}`)}throw new Error("UNION_ERROR(fromBson): Invalid input")}optional(){let e=this.copy();return r(e,g).optional=!0,e}nullable(){let e=this.copy();return r(e,g).nullable=!0,e}nullish(){let e=this.copy();return r(e,g).optional=!0,r(e,g).nullable=!0,e}required(){let e=this.copy();return r(e,g).optional=!1,e}notNull(){let e=this.copy();return r(e,g).nullable=!1,e}notNullish(){let e=this.copy();return r(e,g).optional=!1,r(e,g).nullable=!1,e}private(){let e=this.copy();return r(e,g).private=!0,e}public(){let e=this.copy();return r(e,g).private=!1,e}},U=re;O=new WeakMap,g=new WeakMap,ee=new WeakSet,ue=function(e){let t=r(e,O).map(c=>c.copy()),o=new re(t);return a(o,g,{...r(e,g)}),o},i(U,ee);var be={id:()=>new C,string:()=>new j,number:()=>new L,boolean:()=>new $,array:l=>new A(l),object:l=>new q(l),union:l=>new U(l)},ur=be; export { ur as default };