Skip to main content
Module

x/enzastdlib/environment/dotenv.ts>validateDotenv

enzastdlib is a set of TypeScript modules that follow a common design API philosophy aiming at sane defaults and ease-of-use targeting the Deno TypeScript runtime.
Latest
function validateDotenv
import { validateDotenv } from "https://deno.land/x/enzastdlib@v0.0.4/environment/dotenv.ts";

Throws an exception if any validation errors occured regarding the specified environment variables in a dotenv file, otherwise returns the values of those environment variables.

NOTE: To specify environment variables to validate you MUST supply a JSON Schema that defines a top-level object.

NOTE: Only second-level keys are used for validating environment variables.

Examples

.env

MY_STRING='Hello World!'

schema.ts

import type { JSONSchema, typeofschema } from 'https://deno.land/x/enzastdlib/schema/mod.ts';

export const MY_STRING_SCHEMA = {
    type: 'object',

    properties: {
        MY_STRING: {
            type: 'string',

            minLength: 1,
        },
    },
} as const satisfies JSONSchema;

export type MyStringType = typeofschema<typeof MY_STRING_SCHEMA>;

mod.ts

import { assertEquals } from 'https://deno.land/std/testing/asserts.ts';
import { validateDotenv } from 'https://deno.land/x/enzastdlib/environment/mod.ts';

import type { MyStringType } from "./schema.ts";
import { MY_STRING_SCHEMA } from './schema.ts';

assertEquals(
    validateDotenv<MyStringType>(MY_STRING_SCHEMA, {
        envPath: './.env',
    }),
    { MY_STRING: 'Hello World!' },
);

Parameters

optional
options: Options

Returns

Promise<Type>