• This function calls every function from the configuration object and tries to stores the result in the new config object. Validation errors are returned in the validationErrors variable. The definition object should consist of only functions without parameters which get the value from the process.env variables and validates them. The suggested way is to use the 'env-var' library.

    Type Parameters

    Parameters

    • config: T

      the configuration object where each config value is a function without parameters

    Returns {
        validationErrors: string[];
        values: Partial<ValueObject<T>>;
    }

    • validationErrors: string[]
    • values: Partial<ValueObject<T>>

    Example

    import { from } from 'env-var';

    export const getConfigDefinitions = (
    variables: NodeJS.ProcessEnv = process.env,
    ) => {
    const env = from(variables);

    return {
    environment: () => env.get('NODE_ENV').required().asEnum(['development', 'production', 'test']),
    serviceId: () => env.get('SERVICE_ID').required().asString(),
    port: () => env.get('PORT').default(10100).asPortNumber(),
    // many more...
    }
    }

    // Use the following where the configuration is needed
    const configDefinitions = getConfigDefinitions();
    const config = getConfig(configDefinitions);
    if (config.validationErrors.length === 0) {...}

Generated using TypeDoc