zodified-config-esm 2.1.0
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/dist/index.d.ts +17 -0
- package/dist/index.js +41 -0
- package/dist/index.js.map +1 -0
- package/package.json +41 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type z from 'zod';
|
|
2
|
+
import { ZodIssue } from 'zod';
|
|
3
|
+
export interface ValidatedConfig {
|
|
4
|
+
}
|
|
5
|
+
export declare class ZodValidationError extends Error {
|
|
6
|
+
failures: ZodIssue[];
|
|
7
|
+
constructor(message: string, failures: ZodIssue[]);
|
|
8
|
+
}
|
|
9
|
+
type DotNotation<T, Prefix extends string = ''> = T extends object ? {
|
|
10
|
+
[K in keyof T & string]: `${Prefix}${K}` | DotNotation<T[K], `${Prefix}${K}.`>;
|
|
11
|
+
}[keyof T & string] : '';
|
|
12
|
+
type TypeFromPath<T, Path extends string> = Path extends `${infer Key}.${infer Rest}` ? Key extends keyof T ? TypeFromPath<T[Key], Rest> : never : Path extends keyof T ? T[Path] : never;
|
|
13
|
+
declare const _default: {
|
|
14
|
+
validate: <T extends z.ZodType>(schema: T) => boolean;
|
|
15
|
+
get: <T extends ValidatedConfig, Path extends DotNotation<T>>(path?: Path) => TypeFromPath<T, Path>;
|
|
16
|
+
};
|
|
17
|
+
export default _default;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import config from 'config';
|
|
2
|
+
const fullConfig = config.util.toObject();
|
|
3
|
+
export class ZodValidationError extends Error {
|
|
4
|
+
constructor(message, failures) {
|
|
5
|
+
super(message);
|
|
6
|
+
this.name = 'ZodValidationError';
|
|
7
|
+
this.failures = failures;
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
const validate = (schema) => {
|
|
11
|
+
try {
|
|
12
|
+
const result = schema.safeParse(config);
|
|
13
|
+
if (result.success === false) {
|
|
14
|
+
throw new ZodValidationError('INVALID_CONFIG', result.error.issues);
|
|
15
|
+
}
|
|
16
|
+
return true;
|
|
17
|
+
}
|
|
18
|
+
catch (error) {
|
|
19
|
+
if (error instanceof Error && error.message === 'INVALID_CONFIG') {
|
|
20
|
+
throw error;
|
|
21
|
+
}
|
|
22
|
+
throw new Error('UNEXPECTED_ERROR');
|
|
23
|
+
}
|
|
24
|
+
};
|
|
25
|
+
const get = (path) => {
|
|
26
|
+
let result = fullConfig;
|
|
27
|
+
if (!path) {
|
|
28
|
+
return result;
|
|
29
|
+
}
|
|
30
|
+
path.split('.').forEach((key) => {
|
|
31
|
+
if (result[key] !== undefined) {
|
|
32
|
+
result = result[key];
|
|
33
|
+
}
|
|
34
|
+
});
|
|
35
|
+
return result;
|
|
36
|
+
};
|
|
37
|
+
export default {
|
|
38
|
+
validate,
|
|
39
|
+
get,
|
|
40
|
+
};
|
|
41
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,QAAQ,CAAC;AAM5B,MAAM,UAAU,GAAG,MAAM,CAAC,IAAI,CAAC,QAAQ,EAAE,CAAC;AAE1C,MAAM,OAAO,kBAAmB,SAAQ,KAAK;IAE3C,YAAY,OAAe,EAAE,QAAoB;QAC/C,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,IAAI,GAAG,oBAAoB,CAAC;QACjC,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAC;IAC3B,CAAC;CACF;AAED,MAAM,QAAQ,GAAG,CAAsB,MAAS,EAAE,EAAE;IAClD,IAAI,CAAC;QACH,MAAM,MAAM,GAAG,MAAM,CAAC,SAAS,CAAC,MAAM,CAAC,CAAC;QACxC,IAAI,MAAM,CAAC,OAAO,KAAK,KAAK,EAAE,CAAC;YAC7B,MAAM,IAAI,kBAAkB,CAAC,gBAAgB,EAAE,MAAM,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC;QACtE,CAAC;QACD,OAAO,IAAI,CAAC;IACd,CAAC;IAAC,OAAO,KAAc,EAAE,CAAC;QACxB,IAAI,KAAK,YAAY,KAAK,IAAI,KAAK,CAAC,OAAO,KAAK,gBAAgB,EAAE,CAAC;YACjE,MAAM,KAAK,CAAC;QACd,CAAC;QACD,MAAM,IAAI,KAAK,CAAC,kBAAkB,CAAC,CAAC;IACtC,CAAC;AACH,CAAC,CAAC;AAeF,MAAM,GAAG,GAAG,CACV,IAAW,EACY,EAAE;IACzB,IAAI,MAAM,GAAG,UAAU,CAAC;IAExB,IAAI,CAAC,IAAI,EAAE,CAAC;QACV,OAAO,MAA+B,CAAC;IACzC,CAAC;IAEA,IAAe,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,OAAO,CAAC,CAAC,GAAG,EAAE,EAAE;QAC1C,IAAI,MAAM,CAAC,GAAG,CAAC,KAAK,SAAS,EAAE,CAAC;YAC9B,MAAM,GAAG,MAAM,CAAC,GAAG,CAAC,CAAC;QACvB,CAAC;IACH,CAAC,CAAC,CAAC;IAEH,OAAO,MAAM,CAAC;AAChB,CAAC,CAAC;AAEF,eAAe;IACb,QAAQ;IACR,GAAG;CACJ,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "zodified-config-esm",
|
|
3
|
+
"type": "module",
|
|
4
|
+
"version": "2.1.0",
|
|
5
|
+
"description": "Typesafe config validated by Zod",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/jonathan-fielding/zodified-config.git"
|
|
9
|
+
},
|
|
10
|
+
"license": "MIT",
|
|
11
|
+
"author": "Jonathan Fielding <jonathan-fielding@users.noreply.github.com>",
|
|
12
|
+
"engines": {
|
|
13
|
+
"node": ">=14"
|
|
14
|
+
},
|
|
15
|
+
"main": "./dist/index.js",
|
|
16
|
+
"files": [
|
|
17
|
+
"dist"
|
|
18
|
+
],
|
|
19
|
+
"scripts": {
|
|
20
|
+
"build": "tsc",
|
|
21
|
+
"check-format": "prettier '**/*.{ts,json}' --ignore-path ./.gitignore --check",
|
|
22
|
+
"check-types": "tsc --noEmit",
|
|
23
|
+
"test": "jest"
|
|
24
|
+
},
|
|
25
|
+
"dependencies": {
|
|
26
|
+
"config": "^3.3.12",
|
|
27
|
+
"zod": "^4.1.5"
|
|
28
|
+
},
|
|
29
|
+
"devDependencies": {
|
|
30
|
+
"@eslint/js": "^9.22.0",
|
|
31
|
+
"@types/config": "3.3.4",
|
|
32
|
+
"@types/jest": "^29.5.14",
|
|
33
|
+
"@types/node": "20.14.10",
|
|
34
|
+
"eslint": "^9.22.0",
|
|
35
|
+
"globals": "^16.0.0",
|
|
36
|
+
"jest": "^29.7.0",
|
|
37
|
+
"ts-jest": "^29.2.6",
|
|
38
|
+
"typescript": "^5.8.2",
|
|
39
|
+
"typescript-eslint": "^8.26.1"
|
|
40
|
+
}
|
|
41
|
+
}
|