UNPKG

digitaltwin-core

Version:

Minimalist framework to collect and handle data in a Digital Twin project

55 lines 1.94 kB
export class Env { static { this.schema = { string: (opts) => ({ type: 'string', ...opts }), number: (opts) => ({ type: 'number', ...opts }), enum: (values) => ({ type: 'enum', values }) }; } static { this.config = {}; } static validate(schema, rawEnv = process.env) { const config = {}; for (const [key, rules] of Object.entries(schema)) { const value = rawEnv[key]; if (value === undefined || value === '') { if (rules.optional) continue; throw new Error(`Missing environment variable: ${key}`); } switch (rules.type) { case 'string': if (rules.format === 'url' && !/^https?:\/\/.+$/.test(value)) { throw new Error(`Invalid URL format for ${key}`); } if (rules.format === 'email' && !/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(value)) { throw new Error(`Invalid email format for ${key}`); } config[key] = value; break; case 'number': const parsed = Number(value); if (isNaN(parsed)) { throw new Error(`Invalid number format for ${key}`); } config[key] = parsed; break; case 'enum': if (!rules.values.includes(value)) { throw new Error(`Invalid value for ${key}, expected one of ${rules.values.join(', ')}`); } config[key] = value; break; } } this.config = config; return config; } } //# sourceMappingURL=env.js.map