@pitininja/envious-zod 6.0.0-beta10
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 +56 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.js +25 -0
- package/dist/index.js.map +1 -0
- package/package.json +26 -0
package/README.md
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
# Envious Zod resolver
|
|
2
|
+
|
|
3
|
+
[](https://badge.fury.io/js/@pitininja%2Fenvious-zod)
|
|
4
|
+
|
|
5
|
+
> Only Zod v4 or superior is supported
|
|
6
|
+
|
|
7
|
+
- [Install](#install)
|
|
8
|
+
- [Usage](#usage)
|
|
9
|
+
- [About Zod schemas](#about-zod-schemas)
|
|
10
|
+
|
|
11
|
+
## Install
|
|
12
|
+
|
|
13
|
+
```shell
|
|
14
|
+
npm i @pitininja/envious-zod
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## Usage
|
|
18
|
+
|
|
19
|
+
```typescript
|
|
20
|
+
import { envious } from '@pitininja/envious';
|
|
21
|
+
import { zodResolver } from '@pitininja/envious-zod';
|
|
22
|
+
import * as z from 'zod';
|
|
23
|
+
|
|
24
|
+
const env = envious({
|
|
25
|
+
resolver: zodResolver({
|
|
26
|
+
schema: z.object({
|
|
27
|
+
STRING_VAR: z.string(),
|
|
28
|
+
NUMBER_VAR: z.coerce.number().int(),
|
|
29
|
+
BOOLEAN_VAR_WITH_DEFAULT: z.stringbool().default(true),
|
|
30
|
+
OPTIONAL_VAR: z.optional(z.string())
|
|
31
|
+
})
|
|
32
|
+
})
|
|
33
|
+
});
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
## About Zod schemas
|
|
37
|
+
|
|
38
|
+
Zod cannot programmatically convert object values from a schema. Therefore the conversion must happen at the schema level using :
|
|
39
|
+
|
|
40
|
+
- [coerce](https://zod.dev/api?id=coercion) for numbers
|
|
41
|
+
- [stringbool](https://zod.dev/api?id=stringbool) for booleans
|
|
42
|
+
|
|
43
|
+
```typescript
|
|
44
|
+
import { envious } from '@pitininja/envious';
|
|
45
|
+
import { zodResolver } from '@pitininja/envious-zod';
|
|
46
|
+
import * as z from 'zod';
|
|
47
|
+
|
|
48
|
+
const env = envious({
|
|
49
|
+
resolver: zodResolver({
|
|
50
|
+
schema: z.object({
|
|
51
|
+
NUMBER_VAR: z.coerce.number().int(),
|
|
52
|
+
BOOLEAN_VAR: z.stringbool()
|
|
53
|
+
})
|
|
54
|
+
})
|
|
55
|
+
});
|
|
56
|
+
```
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { type core as ZodCore, type infer as ZodInfer, type ZodObject } from 'zod';
|
|
2
|
+
type EnviousZodResolver<T extends ZodObject> = (env: NodeJS.ProcessEnv) => ZodInfer<T>;
|
|
3
|
+
export type EnviousZodResolverOptions<T extends ZodObject> = {
|
|
4
|
+
schema: T;
|
|
5
|
+
};
|
|
6
|
+
export declare const zodResolver: <T extends ZodObject<ZodCore.$ZodLooseShape, ZodCore.$strip>>({ schema }: EnviousZodResolverOptions<T>) => EnviousZodResolver<T>;
|
|
7
|
+
export {};
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.zodResolver = void 0;
|
|
4
|
+
const envious_1 = require("@pitininja/envious");
|
|
5
|
+
const zod_1 = require("zod");
|
|
6
|
+
const zodResolver = ({ schema }) => {
|
|
7
|
+
return (env) => {
|
|
8
|
+
const { success, data, error } = zod_1.z.safeParse(schema, env);
|
|
9
|
+
if (success && data) {
|
|
10
|
+
return data;
|
|
11
|
+
}
|
|
12
|
+
if (error) {
|
|
13
|
+
throw new envious_1.EnviousError({
|
|
14
|
+
resolverErrors: error.issues,
|
|
15
|
+
transform: (zodIssue) => {
|
|
16
|
+
const { path, message } = zodIssue;
|
|
17
|
+
return { name: path.join('/'), message };
|
|
18
|
+
}
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
throw new Error();
|
|
22
|
+
};
|
|
23
|
+
};
|
|
24
|
+
exports.zodResolver = zodResolver;
|
|
25
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAAA,gDAAkD;AAClD,6BAKa;AAUN,MAAM,WAAW,GAAG,CAAsB,EAC7C,MAAM,EACqB,EAAyB,EAAE,CAAC;IACvD,OAAO,CAAC,GAAG,EAAE,EAAE,CAAC;QACZ,MAAM,EAAE,OAAO,EAAE,IAAI,EAAE,KAAK,EAAE,GAAG,OAAC,CAAC,SAAS,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;QAC1D,IAAI,OAAO,IAAI,IAAI,EAAE,CAAC;YAClB,OAAO,IAAI,CAAC;QAChB,CAAC;QACD,IAAI,KAAK,EAAE,CAAC;YACR,MAAM,IAAI,sBAAY,CAAC;gBACnB,cAAc,EAAE,KAAK,CAAC,MAAM;gBAC5B,SAAS,EAAE,CAAC,QAA2B,EAAE,EAAE,CAAC;oBACxC,MAAM,EAAE,IAAI,EAAE,OAAO,EAAE,GAAG,QAAQ,CAAC;oBACnC,OAAO,EAAE,IAAI,EAAE,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE,OAAO,EAAE,CAAC;gBAAA,CAC5C;aACJ,CAAC,CAAC;QACP,CAAC;QACD,MAAM,IAAI,KAAK,EAAE,CAAC;IAAA,CACrB,CAAC;AAAA,CACL,CAAC;AAnBW,QAAA,WAAW,GAAX,WAAW,CAmBtB"}
|
package/package.json
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@pitininja/envious-zod",
|
|
3
|
+
"version": "6.0.0-beta10",
|
|
4
|
+
"license": "AGPL-3.0-or-later",
|
|
5
|
+
"homepage": "https://codeberg.org/pitininja/envious",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "https://codeberg.org/pitininja/envious"
|
|
9
|
+
},
|
|
10
|
+
"publishConfig": {
|
|
11
|
+
"access": "public"
|
|
12
|
+
},
|
|
13
|
+
"files": [
|
|
14
|
+
"./dist",
|
|
15
|
+
"./package.json"
|
|
16
|
+
],
|
|
17
|
+
"main": "./dist/resolvers/zod/src/index.js",
|
|
18
|
+
"types": "./dist/resolvers/zod/src/index.d.ts",
|
|
19
|
+
"scripts": {
|
|
20
|
+
"build": "npx tsgo --build --clean ./tsconfig.build.json && npx tsgo -b ./tsconfig.build.json"
|
|
21
|
+
},
|
|
22
|
+
"dependencies": {
|
|
23
|
+
"@pitininja/envious": "^6.0.0-0",
|
|
24
|
+
"zod": "^4.1.11"
|
|
25
|
+
}
|
|
26
|
+
}
|