@arkenv/standard 1.0.0-alpha.3

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Yam Borodetsky <https://yam.codes/>
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/dist/index.cjs ADDED
@@ -0,0 +1,4 @@
1
+ Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:`Module`}});const e=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},t=e=>e===`true`?!0:e===`false`?!1:e,n=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}},r=e=>{if(e instanceof Date||typeof e!=`string`||!e.trim())return e;let t=new Date(e);return Number.isNaN(t.getTime())?e:t},i=e=>{let t={};for(let n in e){let r=e[n];r!==``&&(t[n]=r)}return t},a=(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(...a(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(...a(e,[...t,String(r)]))}):n.push(...a(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(...a(i,t));let o=new Set;return n.filter(e=>{let t=e.path.join(`/`)+`:`+e.type;return o.has(t)?!1:o.add(t)})},o=(i,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=(i,a)=>{if(a===`array`&&typeof i==`string`)return c(i);if(a===`object`&&typeof i==`string`)return n(i);if(a===`date`&&typeof i==`string`)return r(i);if(a===`primitive`){if(Array.isArray(i))return i.map(n=>{if(typeof n!=`string`)return n;let r=e(n);return typeof r==`number`?r:t(n)});if(typeof i!=`string`)return i;let n=e(i);return typeof n==`number`?n:t(i)}return i};if(typeof i!=`object`||!i){let e=a.find(e=>e.path.length===0);return e?l(i,e.type):i}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=i;for(let e of u)e.path.length>0&&(f=d(f,e.path,t=>l(t,e.type)));return f};function s(e,t,n,r){let s=t?i(e):e,c={...s},l=[];if(r){let e=r();l.push(...e.missingKeys||[]),e.hasSchema&&(c=o(c,a(e.schema),{arrayFormat:n}))}return{processedEnv:s,coercedEnv:c,missingKeys:l}}const c=(e,t=2,{dontDetectNewlines:n=!1}={})=>n?`${` `.repeat(t)}${e}`:e.split(`
2
+ `).map(e=>`${` `.repeat(t)}${e}`).join(`
3
+ `),l={red:`\x1B[31m`,yellow:`\x1B[33m`,cyan:`\x1B[36m`,reset:`\x1B[0m`},u=()=>typeof process<`u`&&process.versions!=null&&process.versions.node!=null,d=()=>!!(!u()||process.env.NO_COLOR!==void 0||process.env.CI!==void 0||process.stdout&&!process.stdout.isTTY),f=(e,t)=>u()&&!d()?`${l[e]}${t}${l.reset}`:t;function p(e){return e.map(e=>`${f(`yellow`,e.path)} ${e.message.trimStart()}`).join(`
4
+ `)}var m=class extends Error{constructor(e,t=`Errors found while validating environment variables`){let n=p(e);super(`${f(`red`,t)}\n${c(n)}\n`),this.name=`ArkEnvError`,this.issues=e}};Object.defineProperty(m,`name`,{value:`ArkEnvError`});function h(e,t){if(typeof t==`string`)throw new m([{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 g(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 m([{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 _(e){if(!e||typeof e!=`object`||Array.isArray(e))throw new m([{path:``,message:`Invalid schema: expected an object mapping in "standard" mode.`,code:`INVALID_SCHEMA`}])}const v=/secret|(_|^)key(_|$)|token|(_|^)password(_|$)|(_|^)pass(_|$)|(_|^)auth(_|$)|jwt|cert|credential|database_url|db_url/i;function y(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 b(e){return v.test(e)&&!/public/i.test(e)}function x(e,t,n){let r=y(n?.debugSecrets);if(e===void 0)return`missing`;if(e===null)return`null`;if(!r&&b(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=>x(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}: ${x(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 S={too_small:`VALUE_TOO_SMALL`,too_big:`VALUE_TOO_LARGE`,invalid_string:`INVALID_FORMAT`,invalid_date:`INVALID_FORMAT`,custom:`INVALID_FORMAT`};function C(e,t,n){let r=t.toLowerCase();return e===`invalid_type`&&(n===void 0||n===`undefined`)||r===`required`?`MISSING_VARIABLE`:e in S?S[e]:/regex|pattern|match/.test(r)?`PATTERN_MISMATCH`:`INVALID_TYPE`}function w(e){let t=e.minimum??e.min,n=e.maximum??e.max;return{...typeof t==`number`?{min:t}:{},...typeof n==`number`?{max:n}:{}}}function T(e){try{return{success:!0,data:e()}}catch(e){if(e instanceof m)return{success:!1,issues:e.issues};throw e}}function E(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 D(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 ${f(`cyan`,!y(a?.debugSecrets)&&b(i)?`[REDACTED]`:x(r,i,a))})`;return n&&!e.includes(`Expected`)?`must be ${n} ${o}`:`${e} ${o}`}function O(e){let t={type:`object`,properties:{}},n=!1,r=[];for(let i in e){let a=e[i];if(!a){r.push(i);continue}let o=a[`~standard`];if(typeof o?.jsonSchema?.input==`function`)try{let e=o.jsonSchema.input({target:`draft-07`});if(e){t.properties[i]=e,n=!0;continue}}catch{}if(typeof a.jsonSchema?.input==`function`)try{let e=a.jsonSchema.input({target:`draft-07`});if(e){t.properties[i]=e,n=!0;continue}}catch{}if(typeof a.toJSONSchema==`function`)try{let e=a.toJSONSchema();if(e){t.properties[i]=e,n=!0;continue}}catch{}if(typeof a.toStandardJSONSchema?.v1==`function`)try{let e=a.toStandardJSONSchema.v1();if(e){t.properties[i]=e,n=!0;continue}}catch{}r.push(i)}return{jsonSchema:t,hasJsonSchema:n,missingKeys:r}}function k(e){return typeof e==`object`&&e&&`key`in e?String(e.key):String(e)}function A(e,t){return!t||t.length===0?e:[e,...t.map(k)].join(`.`)}function j(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?.[k(e)];n=i}}catch(e){r=`[Traversal error: ${e.message}]`}return{receivedVal:n,traversalError:r}}function M(e,t){let{env:n=process.env,onUndeclaredKey:r=`delete`,coerce:i=!0,arrayFormat:a=`comma`,emptyAsUndefined:o=!1}=t,c={},l=[],{processedEnv:u,coercedEnv:d,missingKeys:f}=s(n,o,a,i?()=>{let{jsonSchema:t,hasJsonSchema:n,missingKeys:r}=O(e);return{schema:t,hasSchema:n,missingKeys:r}}:void 0),p=new Set(Object.keys(u));for(let n in e){let r=e[n],a=d[n];if(!r||typeof r!=`object`||!(`~standard`in r))throw new m([E(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 m([E(n,`Async validation is not supported. ArkEnv is synchronous.`,`INVALID_SCHEMA`)]);if(o.issues)for(let e of o.issues){let r=A(n,e.path),a,o;if(n in u){let t=u[n];if(typeof t==`string`&&e.path?.length){let n=j(t,e.path);a=n.receivedVal,o=n.traversalError}else a=t}else a=e.received;let s=C(e.code||`invalid_type`,e.message||``,a),c=e.expected||void 0,d={...w(e)},p=e;p.validation!==void 0&&(d.validation=p.validation),o!==void 0&&(d.traversalError=o);let m=D(e.message||``,s,c,a,r,t);i&&f.includes(n)&&(m+=` (Hint: coercion is enabled by default, but the validator for '${n}' lacks Standard JSON Schema support.)`),l.push(E(r,m,s,d,c,a))}else c[n]=o.value;p.delete(n)}if(r!==`delete`)for(let e of p)r===`reject`?l.push(E(e,`Undeclared key`,`UNDECLARED_KEY`)):r===`ignore`&&(c[e]=d[e]);if(l.length>0)throw new m(l);return c}function N(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 P(e,t={}){_(e);for(let t in e){let n=e[t];h(t,n),g(t,n)}return t.safe?T(()=>M(e,t)):M(e,t)}exports.ArkEnvError=m,exports.arkenv=P,exports.default=P,exports.formatIssues=p,exports.getSchemaKeys=N;