-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathenvalid.ts
40 lines (38 loc) · 1.56 KB
/
envalid.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import { CleanOptions, ValidatorSpec } from "./types.ts";
import { getSanitizedEnv } from "./core.ts";
import { applyDefaultMiddleware } from "./middleware.ts";
/**
* Returns a sanitized, immutable environment object. _Only_ the variables
* specified in the `specs` parameter will be accessible on the returned
* object.
*
* @param environment An object containing the variables, e.g. `Deno.env.toObject()`.
* @param specs The specification to enforce on the environment.
* @param options
*/
export function cleanEnv<T extends Record<never, never>>(
environment: unknown,
specs: { [K in keyof T]: ValidatorSpec<T[K]> },
options: CleanOptions<T> = {},
): Readonly<T> {
const cleaned = getSanitizedEnv(environment, specs, options);
return Object.freeze(applyDefaultMiddleware(cleaned, environment));
}
/**
* Returns a sanitized, immutable environment object, and passes it through a custom
* `applyMiddleware` function. This won't be required in most use cases.
*
* @param environment An object containing the variables, e.g. `Deno.env.toObject()`.
* @param specs The specification to enforce on the environment.
* @param applyMiddleware A function that applies transformations to the cleaned environment.
* @param options
*/
export function customCleanEnv<T, MW>(
environment: unknown,
specs: { [K in keyof T]: ValidatorSpec<T[K]> },
applyMiddleware: (cleaned: T, rawEnv: unknown) => MW,
options: CleanOptions<T> = {},
): Readonly<MW> {
const cleaned = getSanitizedEnv(environment, specs, options);
return Object.freeze(applyMiddleware(cleaned, environment));
}