envase
Version:
Type-safe environment variable validation with Standard Schema compliance
34 lines • 1.23 kB
JavaScript
const isStandardJsonSchema = (value) => {
return (value !== null &&
typeof value === 'object' &&
'~standard' in value &&
value['~standard'] !== null &&
typeof value['~standard'] === 'object' &&
'jsonSchema' in value['~standard']);
};
/**
* Extracts all envvar entries from a nested envase schema.
* Recursively traverses the schema tree to find all [name, schema] tuples.
*/
export const extractEnvvars = (schema, path = []) => {
const extractedEnvvars = [];
for (const [key, value] of Object.entries(schema)) {
if (Array.isArray(value)) {
const [envName, standardSchema] = value;
if (!isStandardJsonSchema(standardSchema)) {
const combinedPath = [...path, key].join('.');
throw new Error(`Path "${combinedPath}" does not contain a valid standard json schema`);
}
extractedEnvvars.push({
envName,
path,
schema: standardSchema,
});
}
else {
extractedEnvvars.push(...extractEnvvars(value, [...path, key]));
}
}
return extractedEnvvars;
};
//# sourceMappingURL=extract-envvars.js.map