@arkenv/standard 1.0.0-alpha.5 → 1.0.0-alpha.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -1,4 +1 @@
1
- Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:`Module`}});const e=`ArkEnvError`,t=e=>{if(typeof e==`number`||typeof e!=`string`||!e.trim())return e;if(e.trim()===`NaN`)return NaN;let t=Number(e);return Number.isNaN(t)?e:t},n=e=>e===`true`?!0:e===`false`?!1:e,r=e=>{if(typeof e!=`string`)return e;let t=e.trim();if(t[0]!==`{`&&t[0]!==`[`)return e;try{return JSON.parse(t)}catch{return e}},i=e=>{if(e instanceof Date||typeof e!=`string`||!e.trim())return e;let t=new Date(e);return Number.isNaN(t.getTime())?e:t},a=e=>{let t={};for(let n in e){let r=e[n];r!==``&&(t[n]=r)}return t},o=(e,t=[])=>{let n=[];if(!e||typeof e!=`object`||Array.isArray(e))return n;let r=e;if(`const`in r){let e=typeof r.const;(e===`number`||e===`boolean`)&&n.push({path:[...t],type:`primitive`})}`enum`in r&&Array.isArray(r.enum)&&r.enum.some(e=>typeof e==`number`||typeof e==`boolean`)&&n.push({path:[...t],type:`primitive`});let i=r.type;if(i===`number`||i===`integer`||i===`boolean`)n.push({path:[...t],type:`primitive`});else if(i===`string`&&`format`in r&&(r.format===`date-time`||r.format===`date`))n.push({path:[...t],type:`date`});else if(i===`object`){if(r.properties&&Object.keys(r.properties).length>0){n.push({path:[...t],type:`object`});for(let e in r.properties)n.push(...o(r.properties[e],[...t,e]))}}else i===`array`&&(n.push({path:[...t],type:`array`}),r.items&&(Array.isArray(r.items)?r.items.forEach((e,r)=>{n.push(...o(e,[...t,String(r)]))}):n.push(...o(r.items,[...t,`*`]))));for(let e of[`anyOf`,`allOf`,`oneOf`])if(r[e]&&Array.isArray(r[e]))for(let i of r[e])n.push(...o(i,t));let a=new Set;return n.filter(e=>{let t=e.path.join(`/`)+`:`+e.type;return a.has(t)?!1:a.add(t)})},s=(e,a,o={})=>{let{arrayFormat:s=`comma`}=o,c=e=>{if(s===`json`)try{return JSON.parse(e)}catch{return e}return e.trim()?e.split(`,`).map(e=>e.trim()):[]},l=(e,a)=>{if(a===`array`&&typeof e==`string`)return c(e);if(a===`object`&&typeof e==`string`)return r(e);if(a===`date`&&typeof e==`string`)return i(e);if(a===`primitive`){if(Array.isArray(e))return e.map(e=>{if(typeof e!=`string`)return e;let r=t(e);return typeof r==`number`?r:n(e)});if(typeof e!=`string`)return e;let r=t(e);return typeof r==`number`?r:n(e)}return e};if(typeof e!=`object`||!e){let t=a.find(e=>e.path.length===0);return t?l(e,t.type):e}let u=[...a].sort((e,t)=>e.path.length-t.path.length),d=(e,t,n)=>{if(t.length===0)return n(e);let[r,...i]=t;if(r===`*`){if(Array.isArray(e)){let t=!1,r=e.map(e=>{let r=d(e,i,n);return r!==e&&(t=!0),r});return t?r:e}return e}if(!e||typeof e!=`object`)return e;if(Array.isArray(e)){let t=Number(r);if(!Number.isNaN(t)&&t>=0&&t<e.length){let r=d(e[t],i,n);if(r!==e[t]){let n=[...e];return n[t]=r,n}}return e}if(Object.hasOwn(e,r)){let t=d(e[r],i,n);if(t!==e[r])return{...e,[r]:t}}return e},f=e;for(let e of u)e.path.length>0&&(f=d(f,e.path,t=>l(t,e.type)));return f};function c(e,t,n,r){let i=t?a(e):e,c={...i},l=[];if(r){let e=r();l.push(...e.missingKeys||[]),e.hasSchema&&(c=s(c,o(e.schema),{arrayFormat:n}))}return{processedEnv:i,coercedEnv:c,missingKeys:l}}const l=(e,t=2,{dontDetectNewlines:n=!1}={})=>n?`${` `.repeat(t)}${e}`:e.split(`
2
- `).map(e=>`${` `.repeat(t)}${e}`).join(`
3
- `),u={red:`\x1B[31m`,yellow:`\x1B[33m`,cyan:`\x1B[36m`,reset:`\x1B[0m`},d=()=>typeof process<`u`&&process.versions!=null&&process.versions.node!=null,f=()=>!!(!d()||process.env.NO_COLOR!==void 0||process.env.CI!==void 0||process.stdout&&!process.stdout.isTTY),p=(e,t)=>d()&&!f()?`${u[e]}${t}${u.reset}`:t;function m(e){return e.map(e=>`${p(`yellow`,e.path)} ${e.message.trimStart()}`).join(`
4
- `)}var h=class extends Error{constructor(t,n=`Errors found while validating environment variables`){let r=m(t);super(`${p(`red`,n)}\n${l(r)}\n`),this.name=e,this.issues=t}};Object.defineProperty(h,`name`,{value:e});function g(e,t){if(typeof t==`string`)throw new h([{path:e,message:`ArkType DSL strings are not supported in "standard" mode. Use a Standard Schema validator (e.g., Zod, Valibot) or import from "arkenv" for ArkType schemas.`,code:`INVALID_SCHEMA`}])}function _(e,t){let n=t&&typeof t==`object`&&`~standard`in t&&t[`~standard`];if(!n||typeof n!=`object`||!(`validate`in n)||typeof n.validate!=`function`)throw new h([{path:e,message:`Invalid validator: expected a Standard Schema 1.0 validator (must have "~standard" property). Import from "arkenv" to use ArkType schemas.`,code:`INVALID_SCHEMA`}])}function v(e){if(!e||typeof e!=`object`||Array.isArray(e))throw new h([{path:``,message:`Invalid schema: expected an object mapping in "standard" mode.`,code:`INVALID_SCHEMA`}])}const y=/secret|(_|^)key(_|$)|token|(_|^)password(_|$)|(_|^)pass(_|$)|(_|^)auth(_|$)|jwt|cert|credential|database_url|db_url/i;function b(e){if(e!==void 0)return e;if(typeof process>`u`)return!1;let t=process.env.ARKENV_DEBUG_SECRETS;return t===`true`||t===`1`}function x(e){return y.test(e)&&!/public/i.test(e)}function S(e,t,n){let r=b(n?.debugSecrets);if(e===void 0)return`missing`;if(e===null)return`null`;if(!r&&x(t))return`[REDACTED]`;if(typeof e==`string`)return JSON.stringify(e);if(typeof e==`number`||typeof e==`boolean`||typeof e==`bigint`)return String(e);if(typeof e==`symbol`)return e.toString();if(typeof e==`function`)return`[Function]`;if(e&&typeof e==`object`)try{if(Array.isArray(e)){let r=e.slice(0,3).map(e=>S(e,t,n));return e.length>3&&r.push(`...(+${e.length-3} more)`),`[${r.join(`, `)}]`}let r=Object.keys(e),i=r.slice(0,3).map(r=>`${r}: ${S(e[r],t,n)}`);return r.length>3&&i.push(`...(+${r.length-3} more)`),`{ ${i.join(`, `)} }`}catch{return Object.prototype.toString.call(e)}return String(e)}const C={too_small:`VALUE_TOO_SMALL`,too_big:`VALUE_TOO_LARGE`,invalid_string:`INVALID_FORMAT`,invalid_date:`INVALID_FORMAT`,custom:`INVALID_FORMAT`};function w(e,t,n){let r=t.toLowerCase();return e===`invalid_type`&&(n===void 0||n===`undefined`)||r===`required`?`MISSING_VARIABLE`:e in C?C[e]:/regex|pattern|match/.test(r)?`PATTERN_MISMATCH`:`INVALID_TYPE`}function T(e){let t=e.minimum??e.min,n=e.maximum??e.max;return{...typeof t==`number`?{min:t}:{},...typeof n==`number`?{max:n}:{}}}function E(e){try{return{success:!0,data:e()}}catch(e){if(e instanceof h)return{success:!1,issues:e.issues};throw e}}function D(e,t,n,r,i,a){let o={path:e,message:t,code:n,meta:r??{}};return i&&(o.expected=i),a!==void 0&&(o.received=a),o}function O(e,t,n,r,i,a){if(t===`MISSING_VARIABLE`)return n?`must be ${n} (was missing)`:`is required`;if(e.includes(`(was `))return e;let o=`(was ${p(`cyan`,!b(a?.debugSecrets)&&x(i)?`[REDACTED]`:S(r,i,a))})`;return n&&!e.includes(`Expected`)?`must be ${n} ${o}`:`${e} ${o}`}function k(e){return Object.prototype.toString.call(e)===`[object Object]`}function A(e,t){let n={type:`object`,properties:{}},r=!1,i=[];for(let a in e){let o=e[a];if(!o){i.push(a);continue}let s=o[`~standard`];if(typeof s?.jsonSchema?.input==`function`)try{let e=s.jsonSchema.input({target:`draft-07`});if(e){n.properties[a]=e,r=!0;continue}}catch{}if(typeof o.jsonSchema?.input==`function`)try{let e=o.jsonSchema.input({target:`draft-07`});if(e){n.properties[a]=e,r=!0;continue}}catch{}if(typeof o.toJSONSchema==`function`)try{let e=o.toJSONSchema();if(e){n.properties[a]=e,r=!0;continue}}catch{}if(typeof o.toStandardJSONSchema?.v1==`function`)try{let e=o.toStandardJSONSchema.v1();if(e){n.properties[a]=e,r=!0;continue}}catch{}if(t){let e;try{e=t(o)}catch(e){throw new h([D(a,`toJsonSchema failed for '${a}': ${e instanceof Error?e.message:String(e)}`,`INVALID_SCHEMA`)])}if(!e){i.push(a);continue}if(!k(e))throw new h([D(a,`toJsonSchema must return a plain object or undefined for '${a}'.`,`INVALID_SCHEMA`)]);n.properties[a]=e,r=!0;continue}i.push(a)}return{jsonSchema:n,hasJsonSchema:r,missingKeys:i}}function j(e){return typeof e==`object`&&e&&`key`in e?String(e.key):String(e)}function M(e,t){return!t||t.length===0?e:[e,...t.map(j)].join(`.`)}function N(e,t){let n=e,r;try{let i=e,a=e.trim();if(a[0]===`{`||a[0]===`[`)try{i=JSON.parse(e)}catch(e){r=`[Unparseable JSON: ${e.message}]`}if(!r){for(let e of t)i=i?.[j(e)];n=i}}catch(e){r=`[Traversal error: ${e.message}]`}return{receivedVal:n,traversalError:r}}function P(e,t){let{env:n=process.env,onUndeclaredKey:r=`delete`,coerce:i=!0,arrayFormat:a=`comma`,emptyAsUndefined:o=!1,toJsonSchema:s}=t,l={},u=[],{processedEnv:d,coercedEnv:f,missingKeys:p}=c(n,o,a,i?()=>{let{jsonSchema:t,hasJsonSchema:n,missingKeys:r}=A(e,s);return{schema:t,hasSchema:n,missingKeys:r}}:void 0),m=new Set(Object.keys(d));for(let n in e){let r=e[n],a=f[n];if(!r||typeof r!=`object`||!(`~standard`in r))throw new h([D(n,`Invalid schema: expected a Standard Schema 1.0 validator (e.g. Zod, Valibot) in 'standard' mode.`,`INVALID_SCHEMA`)]);let o=r[`~standard`].validate(a);if(o instanceof Promise)throw new h([D(n,`Async validation is not supported. ArkEnv is synchronous.`,`INVALID_SCHEMA`)]);if(o.issues)for(let e of o.issues){let r=M(n,e.path),a,o;if(n in d){let t=d[n];if(typeof t==`string`&&e.path?.length){let n=N(t,e.path);a=n.receivedVal,o=n.traversalError}else a=t}else a=e.received;let s=w(e.code||`invalid_type`,e.message||``,a),c=e.expected||void 0,l={...T(e)},f=e;f.validation!==void 0&&(l.validation=f.validation),o!==void 0&&(l.traversalError=o);let m=O(e.message||``,s,c,a,r,t);i&&p.includes(n)&&(m+=` (Hint: coercion is enabled by default, but the validator for '${n}' lacks Standard JSON Schema support.)`),u.push(D(r,m,s,l,c,a))}else l[n]=o.value;m.delete(n)}if(r!==`delete`)for(let e of m)r===`reject`?u.push(D(e,`Undeclared key`,`UNDECLARED_KEY`)):r===`ignore`&&(l[e]=f[e]);if(u.length>0)throw new h(u);return l}function F(e){if(!e||typeof e!=`object`&&typeof e!=`function`)return[];if(e.json&&typeof e.json==`object`&&e.json.domain===`object`){let t=[];if(Array.isArray(e.json.required))for(let n of e.json.required)n&&typeof n==`object`&&`key`in n&&t.push(n.key);if(Array.isArray(e.json.optional))for(let n of e.json.optional)n&&typeof n==`object`&&`key`in n&&t.push(n.key);return t}let t=e[`~standard`],n=typeof t?.jsonSchema?.input==`function`&&t.jsonSchema.input||typeof e.jsonSchema?.input==`function`&&e.jsonSchema.input;if(n)try{let e=n({target:`draft-07`});if(e&&typeof e==`object`&&e.properties)return Object.keys(e.properties)}catch{}if(typeof e.toJSONSchema==`function`)try{let t=e.toJSONSchema();if(t&&typeof t==`object`&&t.properties)return Object.keys(t.properties)}catch{}if(typeof e.toStandardJSONSchema?.v1==`function`)try{let t=e.toStandardJSONSchema.v1();if(t&&typeof t==`object`&&t.properties)return Object.keys(t.properties)}catch{}return Object.keys(e)}function I(e,t){let n=t??{};v(e);for(let t in e){let n=e[t];g(t,n),_(t,n)}return n.safe?E(()=>P(e,n)):P(e,n)}exports.ArkEnvError=h,exports.arkenv=I,exports.default=I,exports.formatIssues=m,exports.getSchemaKeys=F;
1
+ Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:`Module`}});const e=require(`./src-BZoXXFsx.cjs`);exports.ArkEnvError=e.n,exports.arkenv=e.t,exports.default=e.t,exports.formatIssues=e.r,exports.getSchemaKeys=e.i;