@adonisjs/env 3.0.9 → 4.0.0-1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +138 -38
- package/build/index.d.ts +3 -3
- package/build/index.js +3 -17
- package/build/src/env.d.ts +21 -0
- package/build/src/env.js +27 -0
- package/build/src/exceptions/invalid_env_variables.d.ts +6 -0
- package/build/src/exceptions/invalid_env_variables.js +6 -0
- package/build/src/exceptions/missing_env_path_file.d.ts +5 -0
- package/build/src/exceptions/missing_env_path_file.js +5 -0
- package/build/src/loader.d.ts +9 -7
- package/build/src/loader.js +28 -47
- package/build/src/parser.d.ts +6 -0
- package/build/src/parser.js +60 -0
- package/build/src/validator.d.ts +12 -0
- package/build/src/validator.js +26 -0
- package/package.json +116 -140
- package/build/adonis-typings/env.d.ts +0 -127
- package/build/adonis-typings/env.js +0 -8
- package/build/src/Env/index.d.ts +0 -59
- package/build/src/Env/index.js +0 -140
- package/build/src/Parser/index.d.ts +0 -54
- package/build/src/Parser/index.js +0 -172
- package/build/src/Schema/boolean.d.ts +0 -10
- package/build/src/Schema/boolean.js +0 -49
- package/build/src/Schema/helpers.d.ts +0 -12
- package/build/src/Schema/helpers.js +0 -29
- package/build/src/Schema/index.d.ts +0 -2
- package/build/src/Schema/index.js +0 -21
- package/build/src/Schema/number.d.ts +0 -13
- package/build/src/Schema/number.js +0 -48
- package/build/src/Schema/oneOf.d.ts +0 -8
- package/build/src/Schema/oneOf.js +0 -69
- package/build/src/Schema/string.d.ts +0 -8
- package/build/src/Schema/string.js +0 -66
package/README.md
CHANGED
|
@@ -1,41 +1,141 @@
|
|
|
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
|
-
|
|
1
|
+
# @adonisjs/env
|
|
2
|
+
> Environment variables parser and validator used by the AdonisJS.
|
|
3
|
+
|
|
4
|
+
[![gh-workflow-image]][gh-workflow-url] [![typescript-image]][typescript-url] [![npm-image]][npm-url] [![license-image]][license-url] [![synk-image]][synk-url]
|
|
5
|
+
|
|
6
|
+
> **Note:** This package is framework agnostic and can also be used outside of AdonisJS.
|
|
7
|
+
|
|
8
|
+
The `@adonisjs/env` package encapsulates the workflow around loading, parsing, and validating environment variables.
|
|
9
|
+
|
|
10
|
+
## Setup
|
|
11
|
+
Install the package from the npm packages registry as follows.
|
|
12
|
+
|
|
13
|
+
```sh
|
|
14
|
+
npm i @adonisjs/env
|
|
15
|
+
|
|
16
|
+
yarn add @adonisjs/env
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## EnvLoader
|
|
20
|
+
The `EnvLoader` class is responsible for loading the environment variable files from the disk and returning their contents as a string.
|
|
21
|
+
|
|
22
|
+
```ts
|
|
23
|
+
import { EnvLoader } from '@adonisjs/env'
|
|
24
|
+
|
|
25
|
+
const lookupPath = new URL('./', import.meta.url)
|
|
26
|
+
const loader = new EnvLoader(lookupPath)
|
|
27
|
+
|
|
28
|
+
const { envContents, currentEnvContents } = loader.load()
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
### `envContents`
|
|
32
|
+
|
|
33
|
+
- The `envContents` is read from the `.env` file from the root of the `lookupPath`.
|
|
34
|
+
- No exceptions are raised if the `.env` file is missing. In brief, it is optional to have a `.env` file.
|
|
35
|
+
- If the `ENV_PATH` environment variable is set, the loader will use that instead of the `.env` file. This allows overwriting the location of the dot-env file.
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
### `currentEnvContents`
|
|
39
|
+
|
|
40
|
+
- The `currentEnvContents` contents are read from the `.env.[NODE_ENV]` file.
|
|
41
|
+
- If the current `NODE_ENV = 'development'`, then the contents of this variable will be from the `.env.development` file and so on.
|
|
42
|
+
- The contents of this file should precede the `.env` file.
|
|
43
|
+
|
|
44
|
+
## EnvParser
|
|
45
|
+
The `EnvParser` class is responsible for parsing the contents of the `.env` file(s) and converting them into an object.
|
|
46
|
+
|
|
47
|
+
```ts
|
|
48
|
+
import { EnvLoader, EnvParser } from '@adonisjs/env'
|
|
49
|
+
|
|
50
|
+
const lookupPath = new URL('./', import.meta.url)
|
|
51
|
+
const loader = new EnvLoader(lookupPath)
|
|
52
|
+
const { envContents, currentEnvContents } = loader.load()
|
|
53
|
+
|
|
54
|
+
const envParser = new EnvParser(envContents)
|
|
55
|
+
const currentEnvParser = new EnvParser(currentEnvContents)
|
|
56
|
+
|
|
57
|
+
console.log(envParser.parse()) // { key: value }
|
|
58
|
+
console.log(currentEnvParser.parse()) // { key: value }
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
The return value of `parser.parse` is an object with key-value pair. The parser also has support for interpolation.
|
|
62
|
+
|
|
63
|
+
## Validating environment variables
|
|
64
|
+
Once you have the parsed objects, you can optionally validate them against a pre-defined schema. We recommend validation for the following reasons.
|
|
65
|
+
|
|
66
|
+
- Fail early if one or more environment variables are missing.
|
|
67
|
+
- Cast values to specific data types.
|
|
68
|
+
- Have type safety alongside runtime safety.
|
|
69
|
+
|
|
70
|
+
```ts
|
|
71
|
+
import { Env } from '@adonisjs/env'
|
|
72
|
+
|
|
73
|
+
const validate = Env.rules({
|
|
74
|
+
PORT: Env.schema.number(),
|
|
75
|
+
HOST: Env.schema.string({ format: 'host' })
|
|
76
|
+
})
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
The `Env.schema` is a reference to the [@poppinss/validator-lite](https://github.com/poppinss/validator-lite) `schema` object. Make sure to go through the package README to view all the available methods and options.
|
|
80
|
+
|
|
81
|
+
The `Env.rules` method returns a function to validate the environment variables. The return value is the validated object with type information inferred from the schema.
|
|
82
|
+
|
|
83
|
+
```ts
|
|
84
|
+
validate(process.env)
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
Following is a complete example of using the `EnvLoader`, `EnvParser`, and the validator to set up environment variables.
|
|
88
|
+
|
|
89
|
+
```ts
|
|
90
|
+
import { EnvLoader, EnvParser, Env } from '@adonisjs/env'
|
|
91
|
+
|
|
92
|
+
const lookupPath = new URL('./', import.meta.url)
|
|
93
|
+
const loader = new EnvLoader(lookupPath)
|
|
94
|
+
const { envContents, currentEnvContents } = loader.load()
|
|
95
|
+
|
|
96
|
+
const envValues = new EnvParser(envContents).parse()
|
|
97
|
+
const currentEnvValues = new EnvParser(currentEnvContents).parse()
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Loop over all the parsed env values and set
|
|
101
|
+
* them on "process.env"
|
|
102
|
+
*
|
|
103
|
+
* However, if the value already exists on "process.env", then
|
|
104
|
+
* do not overwrite it, instead update the envValues
|
|
105
|
+
* object.
|
|
106
|
+
*/
|
|
107
|
+
Object.keys(envValues).forEach((key) => {
|
|
108
|
+
if (process.env[key]) {
|
|
109
|
+
envValues[key] = process.env[key]
|
|
110
|
+
} else {
|
|
111
|
+
process.env[key] = envValues[key]
|
|
112
|
+
}
|
|
113
|
+
})
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Loop over all the current env parsed values and make
|
|
117
|
+
* them take precedence over the existing process.env
|
|
118
|
+
* values.
|
|
119
|
+
*/
|
|
120
|
+
Object.keys(currentEnvValues).forEach((key) => {
|
|
121
|
+
process.env[key] = envValues[key]
|
|
122
|
+
})
|
|
123
|
+
|
|
124
|
+
// Now perform the validation
|
|
125
|
+
const validate = Env.rules({
|
|
126
|
+
PORT: Env.schema.number(),
|
|
127
|
+
HOST: Env.schema.string({ format: 'host' })
|
|
128
|
+
})
|
|
129
|
+
|
|
130
|
+
const validated = validate({ ...envValues, ...currentEnvValues })
|
|
131
|
+
const env = new Env(validated)
|
|
132
|
+
|
|
133
|
+
env.get('PORT') // is a number
|
|
134
|
+
env.get('HOST') // is a string
|
|
135
|
+
env.get('NODE_ENV') // is unknown, hence a string or undefined
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
The above code may seem like a lot of work to set up environment variables. However, you have fine-grained control over each step. In the case of AdonisJS, all this boilerplate is hidden inside the framework's application bootstrapping logic.
|
|
39
139
|
|
|
40
140
|
[gh-workflow-image]: https://img.shields.io/github/workflow/status/adonisjs/env/test?style=for-the-badge
|
|
41
141
|
[gh-workflow-url]: https://github.com/adonisjs/env/actions/workflows/test.yml "Github action"
|
package/build/index.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export { Env } from './src/
|
|
2
|
-
export { EnvParser } from './src/
|
|
3
|
-
export {
|
|
1
|
+
export { Env } from './src/env.js';
|
|
2
|
+
export { EnvParser } from './src/parser.js';
|
|
3
|
+
export { EnvLoader } from './src/loader.js';
|
package/build/index.js
CHANGED
|
@@ -1,17 +1,3 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
*
|
|
5
|
-
* (c) Harminder Virk <virk@adonisjs.com>
|
|
6
|
-
*
|
|
7
|
-
* For the full copyright and license information, please view the LICENSE
|
|
8
|
-
* file that was distributed with this source code.
|
|
9
|
-
*/
|
|
10
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
11
|
-
exports.envLoader = exports.EnvParser = exports.Env = void 0;
|
|
12
|
-
var Env_1 = require("./src/Env");
|
|
13
|
-
Object.defineProperty(exports, "Env", { enumerable: true, get: function () { return Env_1.Env; } });
|
|
14
|
-
var Parser_1 = require("./src/Parser");
|
|
15
|
-
Object.defineProperty(exports, "EnvParser", { enumerable: true, get: function () { return Parser_1.EnvParser; } });
|
|
16
|
-
var loader_1 = require("./src/loader");
|
|
17
|
-
Object.defineProperty(exports, "envLoader", { enumerable: true, get: function () { return loader_1.envLoader; } });
|
|
1
|
+
export { Env } from './src/env.js';
|
|
2
|
+
export { EnvParser } from './src/parser.js';
|
|
3
|
+
export { EnvLoader } from './src/loader.js';
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { EnvValidator } from './validator.js';
|
|
2
|
+
import { type ValidateFn } from '@poppinss/validator-lite';
|
|
3
|
+
export declare class Env<EnvValues extends Record<string, any>> {
|
|
4
|
+
#private;
|
|
5
|
+
constructor(values: EnvValues);
|
|
6
|
+
static schema: {
|
|
7
|
+
number: typeof import("@poppinss/validator-lite/build/src/schema/number.js").number;
|
|
8
|
+
string: typeof import("@poppinss/validator-lite/build/src/schema/string.js").string;
|
|
9
|
+
boolean: typeof import("@poppinss/validator-lite/build/src/schema/boolean.js").boolean;
|
|
10
|
+
enum: typeof import("@poppinss/validator-lite/build/src/schema/oneOf.js").oneOf;
|
|
11
|
+
};
|
|
12
|
+
static rules<T extends {
|
|
13
|
+
[key: string]: ValidateFn<unknown>;
|
|
14
|
+
}>(schema: T): EnvValidator<T>['validate'];
|
|
15
|
+
get<K extends keyof EnvValues>(key: K): EnvValues[K];
|
|
16
|
+
get<K extends keyof EnvValues>(key: K, defaultValue: Exclude<EnvValues[K], undefined>): Exclude<EnvValues[K], undefined>;
|
|
17
|
+
get(key: string): string | undefined;
|
|
18
|
+
get(key: string, defaultValue: string): string;
|
|
19
|
+
set<K extends keyof EnvValues>(key: K, value: EnvValues[K]): void;
|
|
20
|
+
set(key: string, value: string): void;
|
|
21
|
+
}
|
package/build/src/env.js
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { EnvValidator } from './validator.js';
|
|
2
|
+
import { schema as envSchema } from '@poppinss/validator-lite';
|
|
3
|
+
export class Env {
|
|
4
|
+
#values;
|
|
5
|
+
constructor(values) {
|
|
6
|
+
this.#values = values;
|
|
7
|
+
}
|
|
8
|
+
static schema = envSchema;
|
|
9
|
+
static rules(schema) {
|
|
10
|
+
const validator = new EnvValidator(schema);
|
|
11
|
+
return validator.validate.bind(validator);
|
|
12
|
+
}
|
|
13
|
+
get(key, defaultValue) {
|
|
14
|
+
if (this.#values[key] !== undefined) {
|
|
15
|
+
return this.#values[key];
|
|
16
|
+
}
|
|
17
|
+
const envValue = process.env[key];
|
|
18
|
+
if (envValue) {
|
|
19
|
+
return envValue;
|
|
20
|
+
}
|
|
21
|
+
return defaultValue;
|
|
22
|
+
}
|
|
23
|
+
set(key, value) {
|
|
24
|
+
this.#values[key] = value;
|
|
25
|
+
process.env[key] = value;
|
|
26
|
+
}
|
|
27
|
+
}
|
package/build/src/loader.d.ts
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
1
|
+
/// <reference types="node" resolution-mode="require"/>
|
|
2
|
+
export declare class EnvLoader {
|
|
3
|
+
#private;
|
|
4
|
+
constructor(appRoot: string | URL);
|
|
5
|
+
load(): Promise<{
|
|
6
|
+
envContents: string;
|
|
7
|
+
currentEnvContents: string;
|
|
8
|
+
}>;
|
|
9
|
+
}
|
package/build/src/loader.js
CHANGED
|
@@ -1,53 +1,34 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
*/
|
|
10
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
11
|
-
exports.envLoader = void 0;
|
|
12
|
-
const fs_1 = require("fs");
|
|
13
|
-
const path_1 = require("path");
|
|
14
|
-
const utils_1 = require("@poppinss/utils");
|
|
15
|
-
/**
|
|
16
|
-
* Loads file from the disk and optionally ignores the missing
|
|
17
|
-
* file errors
|
|
18
|
-
*/
|
|
19
|
-
function loadFile(filePath, optional = false) {
|
|
20
|
-
try {
|
|
21
|
-
return (0, fs_1.readFileSync)(filePath, 'utf-8');
|
|
1
|
+
import { fileURLToPath } from 'node:url';
|
|
2
|
+
import { readFile } from 'node:fs/promises';
|
|
3
|
+
import { isAbsolute, join } from 'node:path';
|
|
4
|
+
import { MissingEnvPathFileException } from './exceptions/missing_env_path_file.js';
|
|
5
|
+
export class EnvLoader {
|
|
6
|
+
#appRoot;
|
|
7
|
+
constructor(appRoot) {
|
|
8
|
+
this.#appRoot = typeof appRoot === 'string' ? appRoot : fileURLToPath(appRoot);
|
|
22
9
|
}
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
10
|
+
async #loadFile(filePath, optional = false) {
|
|
11
|
+
try {
|
|
12
|
+
return await readFile(filePath, 'utf-8');
|
|
26
13
|
}
|
|
27
|
-
|
|
28
|
-
|
|
14
|
+
catch (error) {
|
|
15
|
+
if (error.code !== 'ENOENT') {
|
|
16
|
+
throw error;
|
|
17
|
+
}
|
|
18
|
+
if (optional) {
|
|
19
|
+
return '';
|
|
20
|
+
}
|
|
21
|
+
throw new MissingEnvPathFileException(`Cannot find env file from "ENV_PATH"`, {
|
|
22
|
+
cause: error,
|
|
23
|
+
});
|
|
29
24
|
}
|
|
30
25
|
}
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
const absPath = (0, path_1.isAbsolute)(envPath) ? envPath : (0, path_1.join)(appRoot, envPath);
|
|
39
|
-
const envContents = loadFile(absPath, true);
|
|
40
|
-
/**
|
|
41
|
-
* Optionally load the `.env.test` and `.env.testing` files
|
|
42
|
-
* in test environment
|
|
43
|
-
*/
|
|
44
|
-
let testEnvContent = '';
|
|
45
|
-
if (process.env.NODE_ENV === 'testing') {
|
|
46
|
-
testEnvContent = loadFile((0, path_1.join)(appRoot, '.env.testing'), true);
|
|
47
|
-
}
|
|
48
|
-
else if (process.env.NODE_ENV === 'test') {
|
|
49
|
-
testEnvContent = loadFile((0, path_1.join)(appRoot, '.env.test'), true);
|
|
26
|
+
async load() {
|
|
27
|
+
const envFile = process.env.ENV_PATH || '.env';
|
|
28
|
+
const envPath = isAbsolute(envFile) ? envFile : join(this.#appRoot, envFile);
|
|
29
|
+
const envContents = await this.#loadFile(envPath, !process.env.ENV_PATH);
|
|
30
|
+
const currentEnvPath = join(this.#appRoot, `.env.${process.env.NODE_ENV}`);
|
|
31
|
+
const currentEnvContents = await this.#loadFile(currentEnvPath, true);
|
|
32
|
+
return { envContents, currentEnvContents };
|
|
50
33
|
}
|
|
51
|
-
return { testEnvContent, envContents };
|
|
52
34
|
}
|
|
53
|
-
exports.envLoader = envLoader;
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import dotenv from 'dotenv';
|
|
2
|
+
export class EnvParser {
|
|
3
|
+
#envContents;
|
|
4
|
+
constructor(envContents) {
|
|
5
|
+
this.#envContents = envContents;
|
|
6
|
+
}
|
|
7
|
+
#getValue(key, parsed) {
|
|
8
|
+
if (parsed[key]) {
|
|
9
|
+
return this.#interpolate(parsed[key], parsed);
|
|
10
|
+
}
|
|
11
|
+
return '';
|
|
12
|
+
}
|
|
13
|
+
#interpolateMustache(token, parsed) {
|
|
14
|
+
const closingBrace = token.indexOf('}');
|
|
15
|
+
if (closingBrace === -1) {
|
|
16
|
+
return token;
|
|
17
|
+
}
|
|
18
|
+
const varReference = token.slice(1, closingBrace).trim();
|
|
19
|
+
return `${this.#getValue(varReference, parsed)}${token.slice(closingBrace + 1)}`;
|
|
20
|
+
}
|
|
21
|
+
#interpolateVariable(token, parsed) {
|
|
22
|
+
return token.replace(/[a-zA-Z0-9_]+/, (key) => {
|
|
23
|
+
return this.#getValue(key, parsed);
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
#interpolate(value, parsed) {
|
|
27
|
+
const tokens = value.split('$');
|
|
28
|
+
let newValue = '';
|
|
29
|
+
let skipNextToken = true;
|
|
30
|
+
tokens.forEach((token) => {
|
|
31
|
+
if (token === '\\') {
|
|
32
|
+
newValue += '$';
|
|
33
|
+
skipNextToken = true;
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
if (skipNextToken) {
|
|
37
|
+
newValue += token.replace(/\\$/, '$');
|
|
38
|
+
if (token.endsWith('\\')) {
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
else {
|
|
43
|
+
if (token.startsWith('{')) {
|
|
44
|
+
newValue += this.#interpolateMustache(token, parsed);
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
newValue += this.#interpolateVariable(token, parsed);
|
|
48
|
+
}
|
|
49
|
+
skipNextToken = false;
|
|
50
|
+
});
|
|
51
|
+
return newValue;
|
|
52
|
+
}
|
|
53
|
+
parse() {
|
|
54
|
+
const envCollection = dotenv.parse(this.#envContents.trim());
|
|
55
|
+
return Object.keys(envCollection).reduce((result, key) => {
|
|
56
|
+
result[key] = this.#interpolate(envCollection[key], envCollection);
|
|
57
|
+
return result;
|
|
58
|
+
}, {});
|
|
59
|
+
}
|
|
60
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { ValidateFn } from '@poppinss/validator-lite';
|
|
2
|
+
export declare class EnvValidator<Schema extends {
|
|
3
|
+
[key: string]: ValidateFn<unknown>;
|
|
4
|
+
}> {
|
|
5
|
+
#private;
|
|
6
|
+
constructor(schema: Schema);
|
|
7
|
+
validate(values: {
|
|
8
|
+
[K: string]: string | undefined;
|
|
9
|
+
}): {
|
|
10
|
+
[K in keyof Schema]: ReturnType<Schema[K]>;
|
|
11
|
+
};
|
|
12
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { InvalidEnvVariablesException } from './exceptions/invalid_env_variables.js';
|
|
2
|
+
export class EnvValidator {
|
|
3
|
+
#schema;
|
|
4
|
+
#error;
|
|
5
|
+
constructor(schema) {
|
|
6
|
+
this.#schema = schema;
|
|
7
|
+
this.#error = new InvalidEnvVariablesException();
|
|
8
|
+
}
|
|
9
|
+
validate(values) {
|
|
10
|
+
const help = [];
|
|
11
|
+
const validated = Object.keys(this.#schema).reduce((result, key) => {
|
|
12
|
+
try {
|
|
13
|
+
result[key] = this.#schema[key](key, values[key]);
|
|
14
|
+
}
|
|
15
|
+
catch (error) {
|
|
16
|
+
help.push(`- ${error.message}`);
|
|
17
|
+
}
|
|
18
|
+
return result;
|
|
19
|
+
}, { ...values });
|
|
20
|
+
if (help.length) {
|
|
21
|
+
this.#error.help = help.join('\n');
|
|
22
|
+
throw this.#error;
|
|
23
|
+
}
|
|
24
|
+
return validated;
|
|
25
|
+
}
|
|
26
|
+
}
|