zod-codecs 0.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/LICENSE +21 -0
- package/README.md +34 -0
- package/dist/index.cjs +45 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.ts +33 -0
- package/dist/index.js +34 -0
- package/dist/index.js.map +1 -0
- package/package.json +55 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Brandon Burrus
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
# zod-codecs
|
|
2
|
+
|
|
3
|
+
A library of useful pre-built codecs for Zod validation.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install zod zod-codecs
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
Import codecs from the package and use them with Zod's `codec()` method:
|
|
14
|
+
|
|
15
|
+
```typescript
|
|
16
|
+
import { z } from 'zod';
|
|
17
|
+
import { stringToInt } from 'zod-codecs';
|
|
18
|
+
|
|
19
|
+
// Parse a string as an integer
|
|
20
|
+
const result = stringToInt.parse('42');
|
|
21
|
+
console.log(result); // 42 (number)
|
|
22
|
+
|
|
23
|
+
// Encode back to string
|
|
24
|
+
const encoded = stringToInt.encode(42);
|
|
25
|
+
console.log(encoded); // '42' (string)
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
## Available Codecs
|
|
29
|
+
|
|
30
|
+
- `stringToInt` - Converts string to integer number
|
|
31
|
+
- `stringToNumber` - Converts string to number
|
|
32
|
+
- `stringToBigint` - Converts string to bigint
|
|
33
|
+
- `isoDateTimeToDate` - Converts ISO date-time string to Date object
|
|
34
|
+
- `unixSecondsToDate` - Converts Unix timestamp (seconds) to Date object
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var zod = require('zod');
|
|
4
|
+
|
|
5
|
+
const stringToIntCoder = {
|
|
6
|
+
decode: (value) => parseInt(value, 10),
|
|
7
|
+
encode: (value) => value.toString(),
|
|
8
|
+
};
|
|
9
|
+
const stringToInt = zod.z.codec(zod.z.string().regex(/^-?\d+$/), zod.z.number().int(), stringToIntCoder);
|
|
10
|
+
|
|
11
|
+
const stringToNumberCoder = {
|
|
12
|
+
decode: (value) => parseFloat(value),
|
|
13
|
+
encode: (value) => value.toString(),
|
|
14
|
+
};
|
|
15
|
+
const stringToNumber = zod.z.codec(zod.z.string().regex(/^-?(\d+\.?\d*|\.\d+)$/), zod.z.number(), stringToNumberCoder);
|
|
16
|
+
|
|
17
|
+
const stringToBigIntCoder = {
|
|
18
|
+
decode: (value) => BigInt(value),
|
|
19
|
+
encode: (value) => value.toString(),
|
|
20
|
+
};
|
|
21
|
+
const stringToBigInt = zod.z.codec(zod.z.string().regex(/^-?\d+$/), zod.z.bigint(), stringToBigIntCoder);
|
|
22
|
+
|
|
23
|
+
const isoDateTimeToDateCoder = {
|
|
24
|
+
decode: (value) => new Date(value),
|
|
25
|
+
encode: (value) => value.toISOString(),
|
|
26
|
+
};
|
|
27
|
+
const isoDateTimeToDate = zod.z.codec(zod.z.string().datetime({ offset: true }), zod.z.date(), isoDateTimeToDateCoder);
|
|
28
|
+
|
|
29
|
+
const unixSecondsToDateCoder = {
|
|
30
|
+
decode: (value) => new Date(value * 1000),
|
|
31
|
+
encode: (value) => Math.floor(value.getTime() / 1000),
|
|
32
|
+
};
|
|
33
|
+
const unixSecondsToDate = zod.z.codec(zod.z.number(), zod.z.date(), unixSecondsToDateCoder);
|
|
34
|
+
|
|
35
|
+
exports.isoDateTimeToDate = isoDateTimeToDate;
|
|
36
|
+
exports.isoDateTimeToDateCoder = isoDateTimeToDateCoder;
|
|
37
|
+
exports.stringToBigInt = stringToBigInt;
|
|
38
|
+
exports.stringToBigIntCoder = stringToBigIntCoder;
|
|
39
|
+
exports.stringToInt = stringToInt;
|
|
40
|
+
exports.stringToIntCoder = stringToIntCoder;
|
|
41
|
+
exports.stringToNumber = stringToNumber;
|
|
42
|
+
exports.stringToNumberCoder = stringToNumberCoder;
|
|
43
|
+
exports.unixSecondsToDate = unixSecondsToDate;
|
|
44
|
+
exports.unixSecondsToDateCoder = unixSecondsToDateCoder;
|
|
45
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.cjs","sources":["../src/string-to-int.ts","../src/string-to-number.ts","../src/string-to-bigint.ts","../src/iso-date-time-to-date.ts","../src/unix-seconds-to-date.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,CAAC,EAAE,MAAM,EAAE,IAAI,EAAE,CAAC,EACrCA,KAAC,CAAC,IAAI,EAAE,EACR,sBAAsB;;ACRjB,MAAM,sBAAsB,GAAG;AACpC,IAAA,MAAM,EAAE,CAAC,KAAa,KAAW,IAAI,IAAI,CAAC,KAAK,GAAG,IAAI,CAAC;AACvD,IAAA,MAAM,EAAE,CAAC,KAAW,KAAa,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,OAAO,EAAE,GAAG,IAAI,CAAC;;MAGxD,iBAAiB,GAAGA,KAAC,CAAC,KAAK,CACtCA,KAAC,CAAC,MAAM,EAAE,EACVA,KAAC,CAAC,IAAI,EAAE,EACR,sBAAsB;;;;;;;;;;;;;"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
|
|
3
|
+
declare const stringToIntCoder: {
|
|
4
|
+
decode: (value: string) => number;
|
|
5
|
+
encode: (value: number) => string;
|
|
6
|
+
};
|
|
7
|
+
declare const stringToInt: z.ZodCodec<z.ZodString, z.ZodNumber>;
|
|
8
|
+
|
|
9
|
+
declare const stringToNumberCoder: {
|
|
10
|
+
decode: (value: string) => number;
|
|
11
|
+
encode: (value: number) => string;
|
|
12
|
+
};
|
|
13
|
+
declare const stringToNumber: z.ZodCodec<z.ZodString, z.ZodNumber>;
|
|
14
|
+
|
|
15
|
+
declare const stringToBigIntCoder: {
|
|
16
|
+
decode: (value: string) => bigint;
|
|
17
|
+
encode: (value: bigint) => string;
|
|
18
|
+
};
|
|
19
|
+
declare const stringToBigInt: z.ZodCodec<z.ZodString, z.ZodBigInt>;
|
|
20
|
+
|
|
21
|
+
declare const isoDateTimeToDateCoder: {
|
|
22
|
+
decode: (value: string) => Date;
|
|
23
|
+
encode: (value: Date) => string;
|
|
24
|
+
};
|
|
25
|
+
declare const isoDateTimeToDate: z.ZodCodec<z.ZodString, z.ZodDate>;
|
|
26
|
+
|
|
27
|
+
declare const unixSecondsToDateCoder: {
|
|
28
|
+
decode: (value: number) => Date;
|
|
29
|
+
encode: (value: Date) => number;
|
|
30
|
+
};
|
|
31
|
+
declare const unixSecondsToDate: z.ZodCodec<z.ZodNumber, z.ZodDate>;
|
|
32
|
+
|
|
33
|
+
export { isoDateTimeToDate, isoDateTimeToDateCoder, stringToBigInt, stringToBigIntCoder, stringToInt, stringToIntCoder, stringToNumber, stringToNumberCoder, unixSecondsToDate, unixSecondsToDateCoder };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
|
|
3
|
+
const stringToIntCoder = {
|
|
4
|
+
decode: (value) => parseInt(value, 10),
|
|
5
|
+
encode: (value) => value.toString(),
|
|
6
|
+
};
|
|
7
|
+
const stringToInt = z.codec(z.string().regex(/^-?\d+$/), z.number().int(), stringToIntCoder);
|
|
8
|
+
|
|
9
|
+
const stringToNumberCoder = {
|
|
10
|
+
decode: (value) => parseFloat(value),
|
|
11
|
+
encode: (value) => value.toString(),
|
|
12
|
+
};
|
|
13
|
+
const stringToNumber = z.codec(z.string().regex(/^-?(\d+\.?\d*|\.\d+)$/), z.number(), stringToNumberCoder);
|
|
14
|
+
|
|
15
|
+
const stringToBigIntCoder = {
|
|
16
|
+
decode: (value) => BigInt(value),
|
|
17
|
+
encode: (value) => value.toString(),
|
|
18
|
+
};
|
|
19
|
+
const stringToBigInt = z.codec(z.string().regex(/^-?\d+$/), z.bigint(), stringToBigIntCoder);
|
|
20
|
+
|
|
21
|
+
const isoDateTimeToDateCoder = {
|
|
22
|
+
decode: (value) => new Date(value),
|
|
23
|
+
encode: (value) => value.toISOString(),
|
|
24
|
+
};
|
|
25
|
+
const isoDateTimeToDate = z.codec(z.string().datetime({ offset: true }), z.date(), isoDateTimeToDateCoder);
|
|
26
|
+
|
|
27
|
+
const unixSecondsToDateCoder = {
|
|
28
|
+
decode: (value) => new Date(value * 1000),
|
|
29
|
+
encode: (value) => Math.floor(value.getTime() / 1000),
|
|
30
|
+
};
|
|
31
|
+
const unixSecondsToDate = z.codec(z.number(), z.date(), unixSecondsToDateCoder);
|
|
32
|
+
|
|
33
|
+
export { isoDateTimeToDate, isoDateTimeToDateCoder, stringToBigInt, stringToBigIntCoder, stringToInt, stringToIntCoder, stringToNumber, stringToNumberCoder, unixSecondsToDate, unixSecondsToDateCoder };
|
|
34
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sources":["../src/string-to-int.ts","../src/string-to-number.ts","../src/string-to-bigint.ts","../src/iso-date-time-to-date.ts","../src/unix-seconds-to-date.ts"],"sourcesContent":[null,null,null,null,null],"names":[],"mappings":";;AAEO,MAAM,gBAAgB,GAAG;IAC9B,MAAM,EAAE,CAAC,KAAa,KAAa,QAAQ,CAAC,KAAK,EAAE,EAAE,CAAC;IACtD,MAAM,EAAE,CAAC,KAAa,KAAa,KAAK,CAAC,QAAQ,EAAE;;AAG9C,MAAM,WAAW,GAAG,CAAC,CAAC,KAAK,CAChC,CAAC,CAAC,MAAM,EAAE,CAAC,KAAK,CAAC,SAAS,CAAC,EAC3B,CAAC,CAAC,MAAM,EAAE,CAAC,GAAG,EAAE,EAChB,gBAAgB;;ACRX,MAAM,mBAAmB,GAAG;IACjC,MAAM,EAAE,CAAC,KAAa,KAAa,UAAU,CAAC,KAAK,CAAC;IACpD,MAAM,EAAE,CAAC,KAAa,KAAa,KAAK,CAAC,QAAQ,EAAE;;AAG9C,MAAM,cAAc,GAAG,CAAC,CAAC,KAAK,CACnC,CAAC,CAAC,MAAM,EAAE,CAAC,KAAK,CAAC,uBAAuB,CAAC,EACzC,CAAC,CAAC,MAAM,EAAE,EACV,mBAAmB;;ACRd,MAAM,mBAAmB,GAAG;IACjC,MAAM,EAAE,CAAC,KAAa,KAAa,MAAM,CAAC,KAAK,CAAC;IAChD,MAAM,EAAE,CAAC,KAAa,KAAa,KAAK,CAAC,QAAQ,EAAE;;AAG9C,MAAM,cAAc,GAAG,CAAC,CAAC,KAAK,CACnC,CAAC,CAAC,MAAM,EAAE,CAAC,KAAK,CAAC,SAAS,CAAC,EAC3B,CAAC,CAAC,MAAM,EAAE,EACV,mBAAmB;;ACRd,MAAM,sBAAsB,GAAG;IACpC,MAAM,EAAE,CAAC,KAAa,KAAW,IAAI,IAAI,CAAC,KAAK,CAAC;IAChD,MAAM,EAAE,CAAC,KAAW,KAAa,KAAK,CAAC,WAAW,EAAE;;AAG/C,MAAM,iBAAiB,GAAG,CAAC,CAAC,KAAK,CACtC,CAAC,CAAC,MAAM,EAAE,CAAC,QAAQ,CAAC,EAAE,MAAM,EAAE,IAAI,EAAE,CAAC,EACrC,CAAC,CAAC,IAAI,EAAE,EACR,sBAAsB;;ACRjB,MAAM,sBAAsB,GAAG;AACpC,IAAA,MAAM,EAAE,CAAC,KAAa,KAAW,IAAI,IAAI,CAAC,KAAK,GAAG,IAAI,CAAC;AACvD,IAAA,MAAM,EAAE,CAAC,KAAW,KAAa,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,OAAO,EAAE,GAAG,IAAI,CAAC;;MAGxD,iBAAiB,GAAG,CAAC,CAAC,KAAK,CACtC,CAAC,CAAC,MAAM,EAAE,EACV,CAAC,CAAC,IAAI,EAAE,EACR,sBAAsB;;;;"}
|
package/package.json
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "zod-codecs",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Library of useful pre-build Codecs for Zod.",
|
|
5
|
+
"author": "Brandon Burrus",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"type": "module",
|
|
8
|
+
"main": "./dist/index.cjs",
|
|
9
|
+
"module": "./dist/index.js",
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"exports": {
|
|
12
|
+
".": {
|
|
13
|
+
"types": "./dist/index.d.ts",
|
|
14
|
+
"import": "./dist/index.js",
|
|
15
|
+
"require": "./dist/index.cjs"
|
|
16
|
+
}
|
|
17
|
+
},
|
|
18
|
+
"files": [
|
|
19
|
+
"dist",
|
|
20
|
+
"LICENSE",
|
|
21
|
+
"README.md"
|
|
22
|
+
],
|
|
23
|
+
"homepage": "https://github.com/brandonburrus/zod-codecs#readme",
|
|
24
|
+
"keywords": [
|
|
25
|
+
"zod",
|
|
26
|
+
"codecs",
|
|
27
|
+
"validation"
|
|
28
|
+
],
|
|
29
|
+
"repository": {
|
|
30
|
+
"type": "git",
|
|
31
|
+
"url": "git+https://github.com/brandonburrus/zod-codecs.git"
|
|
32
|
+
},
|
|
33
|
+
"bugs": {
|
|
34
|
+
"url": "https://github.com/brandonburrus/zod-codecs/issues"
|
|
35
|
+
},
|
|
36
|
+
"scripts": {
|
|
37
|
+
"build": "rollup -c",
|
|
38
|
+
"test": "vitest",
|
|
39
|
+
"test:run": "vitest run",
|
|
40
|
+
"typecheck": "tsc --noEmit"
|
|
41
|
+
},
|
|
42
|
+
"peerDependencies": {
|
|
43
|
+
"zod": ">=4.1.0"
|
|
44
|
+
},
|
|
45
|
+
"devDependencies": {
|
|
46
|
+
"@rollup/plugin-node-resolve": "^16.0.3",
|
|
47
|
+
"@rollup/plugin-typescript": "^12.3.0",
|
|
48
|
+
"rollup": "^4.56.0",
|
|
49
|
+
"rollup-plugin-dts": "^6.3.0",
|
|
50
|
+
"tslib": "^2.8.1",
|
|
51
|
+
"typescript": "^5.9.3",
|
|
52
|
+
"vitest": "^4.0.18",
|
|
53
|
+
"zod": "^4.3.6"
|
|
54
|
+
}
|
|
55
|
+
}
|