@infra-blocks/zod-utils 0.22.0 → 0.23.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/README.md CHANGED
@@ -71,6 +71,20 @@ The `zu.codec.jsonParse(schema)` utility is a factory returning a codec where th
71
71
  `zu.string.json()` and the second one is the one provided as input. This is almost verbatim
72
72
  what is describe in [Zod's documentation](https://zod.dev/codecs#jsonschema).
73
73
 
74
+ #### ms
75
+
76
+ The `zu.codec.ms(options)` utility is a factory returning a codec where the schemas are
77
+ `z.string()` and `z.number()`. The transformations in both directions are handled by the
78
+ [ms](https://www.npmjs.com/package/ms) package. When encoding, the codec passes the options
79
+ provided to `ms(number, options)`.
80
+
81
+ ```typescript
82
+ import { zu } from "@infra-blocks/zod-utils";
83
+
84
+ const item: number = zu.codec.ms().parse("1d"); // item is 86400000.
85
+ zu.codec.ms({ long: true }).encode(item); // result is "1 day".
86
+ ```
87
+
74
88
  #### stringSplit
75
89
 
76
90
  The `zu.codec.stringSplit(separator)` utility is a factory returning a codec where the first schema is
@@ -1,6 +1,9 @@
1
1
  declare const codec: {
2
2
  csv: () => import("zod").ZodCodec<import("zod").ZodString, import("zod").ZodArray<import("zod").ZodString>>;
3
3
  jsonParse: <S extends import("zod").ZodType>(schema: S) => import("zod").ZodCodec<import("zod/v4/core").$ZodBranded<import("zod").ZodString, "JsonString">, S>;
4
+ ms: (options?: {
5
+ long: boolean;
6
+ }) => import("zod").ZodCodec<import("zod").ZodString, import("zod").ZodNumber>;
4
7
  stringtoInteger: () => import("zod").ZodCodec<import("zod/v4/core").$ZodBranded<import("zod").ZodString, "IntegerString">, import("zod/v4/core").$ZodBranded<import("zod").ZodInt, "Integer">>;
5
8
  stringToJson: () => import("zod").ZodCodec<import("zod/v4/core").$ZodBranded<import("zod").ZodString, "JsonString">, import("zod").ZodType<import("../json/json.js").Json, unknown, import("zod/v4/core").$ZodTypeInternals<import("../json/json.js").Json, unknown>>>;
6
9
  stringToUrl: () => import("zod").ZodCodec<import("zod/v4/core").$ZodBranded<import("zod").ZodURL, "UrlString">, import("zod").ZodCustom<import("url").URL, import("url").URL>>;
@@ -3,12 +3,14 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.codec = void 0;
4
4
  const csv_js_1 = require("./csv.js");
5
5
  const json_parse_js_1 = require("./json-parse.js");
6
+ const ms_js_1 = require("./ms.js");
6
7
  const string_to_integer_js_1 = require("./string-to-integer.js");
7
8
  const string_to_json_js_1 = require("./string-to-json.js");
8
9
  const string_to_url_js_1 = require("./string-to-url.js");
9
10
  const codec = {
10
11
  csv: csv_js_1.csv,
11
12
  jsonParse: json_parse_js_1.jsonParse,
13
+ ms: ms_js_1.ms,
12
14
  stringtoInteger: string_to_integer_js_1.stringToInteger,
13
15
  stringToJson: string_to_json_js_1.stringToJson,
14
16
  stringToUrl: string_to_url_js_1.stringToUrl,
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/codec/index.ts"],"names":[],"mappings":";;;AAAA,qCAA+B;AAC/B,mDAA4C;AAC5C,iEAAyD;AACzD,2DAAmD;AACnD,yDAAiD;AAEjD,MAAM,KAAK,GAAG;IACZ,GAAG,EAAH,YAAG;IACH,SAAS,EAAT,yBAAS;IACT,eAAe,EAAE,sCAAe;IAChC,YAAY,EAAZ,gCAAY;IACZ,WAAW,EAAX,8BAAW;CACZ,CAAC;AAEO,sBAAK"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/codec/index.ts"],"names":[],"mappings":";;;AAAA,qCAA+B;AAC/B,mDAA4C;AAC5C,mCAA6B;AAC7B,iEAAyD;AACzD,2DAAmD;AACnD,yDAAiD;AAEjD,MAAM,KAAK,GAAG;IACZ,GAAG,EAAH,YAAG;IACH,SAAS,EAAT,yBAAS;IACT,EAAE,EAAF,UAAE;IACF,eAAe,EAAE,sCAAe;IAChC,YAAY,EAAZ,gCAAY;IACZ,WAAW,EAAX,8BAAW;CACZ,CAAC;AAEO,sBAAK"}
@@ -0,0 +1,15 @@
1
+ import { z } from "zod";
2
+ /**
3
+ * A codec factory that returns codec parsing strings formatted
4
+ * as expected by the [ms](https://www.npmjs.com/package/ms) and turning them
5
+ * into their equivalent number in milliseconds.
6
+ *
7
+ * @param options - Those options are passed to `ms` when turning the milliseconds
8
+ * number into a formatted string.
9
+ *
10
+ * @returns A codec where the schemas are {@link z.string} and {@link z.number},
11
+ * and where the transforms between each are handled by the `ms` package.
12
+ */
13
+ export declare const ms: (options?: {
14
+ long: boolean;
15
+ }) => z.ZodCodec<z.ZodString, z.ZodNumber>;
@@ -0,0 +1,25 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.ms = void 0;
7
+ const ms_1 = __importDefault(require("ms"));
8
+ const zod_1 = require("zod");
9
+ /**
10
+ * A codec factory that returns codec parsing strings formatted
11
+ * as expected by the [ms](https://www.npmjs.com/package/ms) and turning them
12
+ * into their equivalent number in milliseconds.
13
+ *
14
+ * @param options - Those options are passed to `ms` when turning the milliseconds
15
+ * number into a formatted string.
16
+ *
17
+ * @returns A codec where the schemas are {@link z.string} and {@link z.number},
18
+ * and where the transforms between each are handled by the `ms` package.
19
+ */
20
+ const ms = (options) => zod_1.z.codec(zod_1.z.string(), zod_1.z.number(), {
21
+ decode: (str) => (0, ms_1.default)(str) ?? zod_1.z.NEVER,
22
+ encode: (millis) => (0, ms_1.default)(millis, options),
23
+ });
24
+ exports.ms = ms;
25
+ //# sourceMappingURL=ms.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ms.js","sourceRoot":"","sources":["../../../src/codec/ms.ts"],"names":[],"mappings":";;;;;;AAAA,4CAAsB;AACtB,6BAAwB;AAExB;;;;;;;;;;GAUG;AACI,MAAM,EAAE,GAAG,CAAC,OAA2B,EAAE,EAAE,CAChD,OAAC,CAAC,KAAK,CAAC,OAAC,CAAC,MAAM,EAAE,EAAE,OAAC,CAAC,MAAM,EAAE,EAAE;IAC9B,MAAM,EAAE,CAAC,GAAG,EAAE,EAAE,CAAC,IAAA,YAAI,EAAC,GAAuB,CAAC,IAAI,OAAC,CAAC,KAAK;IACzD,MAAM,EAAE,CAAC,MAAM,EAAE,EAAE,CAAC,IAAA,YAAI,EAAC,MAAM,EAAE,OAAO,CAAC;CAC1C,CAAC,CAAC;AAJQ,QAAA,EAAE,MAIV"}
@@ -1,5 +1,6 @@
1
1
  import type { TypeGuard } from "@infra-blocks/types";
2
2
  import type { z } from "zod";
3
+ import { isValid } from "./is-valid.js";
3
4
  /**
4
5
  * Returns a type guard function using the provided schema as the source of truth.
5
6
  *
@@ -16,19 +17,6 @@ import type { z } from "zod";
16
17
  * former case, that information is also carried with the type.
17
18
  */
18
19
  declare function typeGuard<S extends z.ZodType>(schema: S): TypeGuard<z.infer<S>>;
19
- /**
20
- * Validates that the value satisfies the provided schema.
21
- *
22
- * Upon success, the value's type is narrowed to the schema's output type.
23
- *
24
- * It uses `safeParse` internally.
25
- *
26
- * @param schema - The schema to validate against.
27
- * @param value - The value to validate.
28
- *
29
- * @returns Whether the value satisfies the schema.
30
- */
31
- declare function isValid<S extends z.ZodType>(schema: S, value: unknown): value is z.infer<S>;
32
20
  declare const zu: {
33
21
  aws: {
34
22
  accountId: typeof import("./aws/account-id.js").accountId;
@@ -39,6 +27,9 @@ declare const zu: {
39
27
  codec: {
40
28
  csv: () => z.ZodCodec<z.ZodString, z.ZodArray<z.ZodString>>;
41
29
  jsonParse: <S extends z.ZodType>(schema: S) => z.ZodCodec<z.core.$ZodBranded<z.ZodString, "JsonString">, S>;
30
+ ms: (options?: {
31
+ long: boolean;
32
+ }) => z.ZodCodec<z.ZodString, z.ZodNumber>;
42
33
  stringtoInteger: () => z.ZodCodec<z.core.$ZodBranded<z.ZodString, "IntegerString">, z.core.$ZodBranded<z.ZodInt, "Integer">>;
43
34
  stringToJson: () => z.ZodCodec<z.core.$ZodBranded<z.ZodString, "JsonString">, z.ZodType<import("./json/json.js").Json, unknown, z.core.$ZodTypeInternals<import("./json/json.js").Json, unknown>>>;
44
35
  stringToUrl: () => z.ZodCodec<z.core.$ZodBranded<z.ZodURL, "UrlString">, z.ZodCustom<import("url").URL, import("url").URL>>;
package/lib/cjs/index.js CHANGED
@@ -4,6 +4,7 @@ exports.zu = void 0;
4
4
  const index_js_1 = require("./aws/index.js");
5
5
  const index_js_2 = require("./codec/index.js");
6
6
  const index_js_3 = require("./geojson/index.js");
7
+ const is_valid_js_1 = require("./is-valid.js");
7
8
  const index_js_4 = require("./iso/index.js");
8
9
  const index_js_5 = require("./json/index.js");
9
10
  const index_js_6 = require("./number/index.js");
@@ -25,24 +26,9 @@ const index_js_7 = require("./string/index.js");
25
26
  */
26
27
  function typeGuard(schema) {
27
28
  return (value) => {
28
- return isValid(schema, value);
29
+ return (0, is_valid_js_1.isValid)(schema, value);
29
30
  };
30
31
  }
31
- /**
32
- * Validates that the value satisfies the provided schema.
33
- *
34
- * Upon success, the value's type is narrowed to the schema's output type.
35
- *
36
- * It uses `safeParse` internally.
37
- *
38
- * @param schema - The schema to validate against.
39
- * @param value - The value to validate.
40
- *
41
- * @returns Whether the value satisfies the schema.
42
- */
43
- function isValid(schema, value) {
44
- return schema.safeParse(value).success;
45
- }
46
32
  const zu = {
47
33
  aws: index_js_1.aws,
48
34
  codec: index_js_2.codec,
@@ -51,7 +37,7 @@ const zu = {
51
37
  json: index_js_5.json,
52
38
  number: index_js_6.number,
53
39
  string: index_js_7.string,
54
- isValid,
40
+ isValid: is_valid_js_1.isValid,
55
41
  typeGuard,
56
42
  };
57
43
  exports.zu = zu;
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;AAEA,6CAAqC;AACrC,+CAAyC;AACzC,iDAA6C;AAC7C,6CAAqC;AACrC,8CAAuC;AACvC,gDAA2C;AAC3C,gDAA2C;AAE3C;;;;;;;;;;;;;;GAcG;AACH,SAAS,SAAS,CAAsB,MAAS;IAC/C,OAAO,CAAC,KAAc,EAAuB,EAAE;QAC7C,OAAO,OAAO,CAAC,MAAM,EAAE,KAAK,CAAC,CAAC;IAChC,CAAC,CAAC;AACJ,CAAC;AAED;;;;;;;;;;;GAWG;AACH,SAAS,OAAO,CACd,MAAS,EACT,KAAc;IAEd,OAAO,MAAM,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC,OAAO,CAAC;AACzC,CAAC;AAED,MAAM,EAAE,GAAG;IACT,GAAG,EAAH,cAAG;IACH,KAAK,EAAL,gBAAK;IACL,OAAO,EAAP,kBAAO;IACP,GAAG,EAAH,cAAG;IACH,IAAI,EAAJ,eAAI;IACJ,MAAM,EAAN,iBAAM;IACN,MAAM,EAAN,iBAAM;IACN,OAAO;IACP,SAAS;CACV,CAAC;AAEO,gBAAE"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;AAEA,6CAAqC;AACrC,+CAAyC;AACzC,iDAA6C;AAC7C,+CAAwC;AACxC,6CAAqC;AACrC,8CAAuC;AACvC,gDAA2C;AAC3C,gDAA2C;AAE3C;;;;;;;;;;;;;;GAcG;AACH,SAAS,SAAS,CAAsB,MAAS;IAC/C,OAAO,CAAC,KAAc,EAAuB,EAAE;QAC7C,OAAO,IAAA,qBAAO,EAAC,MAAM,EAAE,KAAK,CAAC,CAAC;IAChC,CAAC,CAAC;AACJ,CAAC;AAED,MAAM,EAAE,GAAG;IACT,GAAG,EAAH,cAAG;IACH,KAAK,EAAL,gBAAK;IACL,OAAO,EAAP,kBAAO;IACP,GAAG,EAAH,cAAG;IACH,IAAI,EAAJ,eAAI;IACJ,MAAM,EAAN,iBAAM;IACN,MAAM,EAAN,iBAAM;IACN,OAAO,EAAP,qBAAO;IACP,SAAS;CACV,CAAC;AAEO,gBAAE"}
@@ -0,0 +1,14 @@
1
+ import type { z } from "zod";
2
+ /**
3
+ * Validates that the value satisfies the provided schema.
4
+ *
5
+ * Upon success, the value's type is narrowed to the schema's output type.
6
+ *
7
+ * It uses `safeParse` internally.
8
+ *
9
+ * @param schema - The schema to validate against.
10
+ * @param value - The value to validate.
11
+ *
12
+ * @returns Whether the value satisfies the schema.
13
+ */
14
+ export declare function isValid<S extends z.ZodType>(schema: S, value: unknown): value is z.infer<S>;
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.isValid = isValid;
4
+ /**
5
+ * Validates that the value satisfies the provided schema.
6
+ *
7
+ * Upon success, the value's type is narrowed to the schema's output type.
8
+ *
9
+ * It uses `safeParse` internally.
10
+ *
11
+ * @param schema - The schema to validate against.
12
+ * @param value - The value to validate.
13
+ *
14
+ * @returns Whether the value satisfies the schema.
15
+ */
16
+ function isValid(schema, value) {
17
+ return schema.safeParse(value).success;
18
+ }
19
+ //# sourceMappingURL=is-valid.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"is-valid.js","sourceRoot":"","sources":["../../src/is-valid.ts"],"names":[],"mappings":";;AAcA,0BAKC;AAjBD;;;;;;;;;;;GAWG;AACH,SAAgB,OAAO,CACrB,MAAS,EACT,KAAc;IAEd,OAAO,MAAM,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC,OAAO,CAAC;AACzC,CAAC"}
@@ -1,7 +1,40 @@
1
1
  "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
2
35
  Object.defineProperty(exports, "__esModule", { value: true });
3
36
  exports.currencyCode = void 0;
4
- const currencyCodes = require("currency-codes");
37
+ const currencyCodes = __importStar(require("currency-codes"));
5
38
  const zod_1 = require("zod");
6
39
  const ISO_CODES = currencyCodes.codes();
7
40
  const currencyCodeSchema = zod_1.z.enum(ISO_CODES).brand("IsoCurrencyCode");
@@ -1 +1 @@
1
- {"version":3,"file":"currency-code.js","sourceRoot":"","sources":["../../../src/iso/currency-code.ts"],"names":[],"mappings":";;;AAAA,gDAAgD;AAChD,6BAAwB;AAExB,MAAM,SAAS,GAAG,aAAa,CAAC,KAAK,EAAE,CAAC;AAExC,MAAM,kBAAkB,GAAG,OAAC,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC,KAAK,CAAC,iBAAiB,CAAC,CAAC;AAItE;;;;;;;;;;;GAWG;AACI,MAAM,YAAY,GAAG,GAAG,EAAE,CAAC,kBAAkB,CAAC;AAAxC,QAAA,YAAY,gBAA4B"}
1
+ {"version":3,"file":"currency-code.js","sourceRoot":"","sources":["../../../src/iso/currency-code.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,8DAAgD;AAChD,6BAAwB;AAExB,MAAM,SAAS,GAAG,aAAa,CAAC,KAAK,EAAE,CAAC;AAExC,MAAM,kBAAkB,GAAG,OAAC,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC,KAAK,CAAC,iBAAiB,CAAC,CAAC;AAItE;;;;;;;;;;;GAWG;AACI,MAAM,YAAY,GAAG,GAAG,EAAE,CAAC,kBAAkB,CAAC;AAAxC,QAAA,YAAY,gBAA4B"}
@@ -1,5 +1,5 @@
1
1
  import { z } from "zod";
2
- export type Integer = z.infer<typeof integerSchema>;
3
- declare const integerSchema: z.core.$ZodBranded<z.ZodInt, "Integer">;
2
+ declare const schema: z.core.$ZodBranded<z.ZodInt, "Integer">;
3
+ export type Integer = z.infer<typeof schema>;
4
4
  export declare const integer: () => z.core.$ZodBranded<z.ZodInt, "Integer">;
5
5
  export {};
@@ -2,7 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.integer = void 0;
4
4
  const zod_1 = require("zod");
5
- const integerSchema = zod_1.z.int().brand("Integer");
6
- const integer = () => integerSchema;
5
+ const schema = zod_1.z.int().brand("Integer");
6
+ const integer = () => schema;
7
7
  exports.integer = integer;
8
8
  //# sourceMappingURL=integer.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"integer.js","sourceRoot":"","sources":["../../../src/number/integer.ts"],"names":[],"mappings":";;;AAAA,6BAAwB;AAIxB,MAAM,aAAa,GAAG,OAAC,CAAC,GAAG,EAAE,CAAC,KAAK,CAAC,SAAS,CAAC,CAAC;AAExC,MAAM,OAAO,GAAG,GAAG,EAAE,CAAC,aAAa,CAAC;AAA9B,QAAA,OAAO,WAAuB"}
1
+ {"version":3,"file":"integer.js","sourceRoot":"","sources":["../../../src/number/integer.ts"],"names":[],"mappings":";;;AAAA,6BAAwB;AAExB,MAAM,MAAM,GAAG,OAAC,CAAC,GAAG,EAAE,CAAC,KAAK,CAAC,SAAS,CAAC,CAAC;AAIjC,MAAM,OAAO,GAAG,GAAG,EAAE,CAAC,MAAM,CAAC;AAAvB,QAAA,OAAO,WAAgB"}
@@ -1,6 +1,9 @@
1
1
  declare const codec: {
2
2
  csv: () => import("zod").ZodCodec<import("zod").ZodString, import("zod").ZodArray<import("zod").ZodString>>;
3
3
  jsonParse: <S extends import("zod").ZodType>(schema: S) => import("zod").ZodCodec<import("zod/v4/core").$ZodBranded<import("zod").ZodString, "JsonString">, S>;
4
+ ms: (options?: {
5
+ long: boolean;
6
+ }) => import("zod").ZodCodec<import("zod").ZodString, import("zod").ZodNumber>;
4
7
  stringtoInteger: () => import("zod").ZodCodec<import("zod/v4/core").$ZodBranded<import("zod").ZodString, "IntegerString">, import("zod/v4/core").$ZodBranded<import("zod").ZodInt, "Integer">>;
5
8
  stringToJson: () => import("zod").ZodCodec<import("zod/v4/core").$ZodBranded<import("zod").ZodString, "JsonString">, import("zod").ZodType<import("../json/json.js").Json, unknown, import("zod/v4/core").$ZodTypeInternals<import("../json/json.js").Json, unknown>>>;
6
9
  stringToUrl: () => import("zod").ZodCodec<import("zod/v4/core").$ZodBranded<import("zod").ZodURL, "UrlString">, import("zod").ZodCustom<import("url").URL, import("url").URL>>;
@@ -1,11 +1,13 @@
1
1
  import { csv } from "./csv.js";
2
2
  import { jsonParse } from "./json-parse.js";
3
+ import { ms } from "./ms.js";
3
4
  import { stringToInteger } from "./string-to-integer.js";
4
5
  import { stringToJson } from "./string-to-json.js";
5
6
  import { stringToUrl } from "./string-to-url.js";
6
7
  const codec = {
7
8
  csv,
8
9
  jsonParse,
10
+ ms,
9
11
  stringtoInteger: stringToInteger,
10
12
  stringToJson,
11
13
  stringToUrl,
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/codec/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,UAAU,CAAC;AAC/B,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAC5C,OAAO,EAAE,eAAe,EAAE,MAAM,wBAAwB,CAAC;AACzD,OAAO,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAC;AACnD,OAAO,EAAE,WAAW,EAAE,MAAM,oBAAoB,CAAC;AAEjD,MAAM,KAAK,GAAG;IACZ,GAAG;IACH,SAAS;IACT,eAAe,EAAE,eAAe;IAChC,YAAY;IACZ,WAAW;CACZ,CAAC;AAEF,OAAO,EAAE,KAAK,EAAE,CAAC"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/codec/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,UAAU,CAAC;AAC/B,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAC5C,OAAO,EAAE,EAAE,EAAE,MAAM,SAAS,CAAC;AAC7B,OAAO,EAAE,eAAe,EAAE,MAAM,wBAAwB,CAAC;AACzD,OAAO,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAC;AACnD,OAAO,EAAE,WAAW,EAAE,MAAM,oBAAoB,CAAC;AAEjD,MAAM,KAAK,GAAG;IACZ,GAAG;IACH,SAAS;IACT,EAAE;IACF,eAAe,EAAE,eAAe;IAChC,YAAY;IACZ,WAAW;CACZ,CAAC;AAEF,OAAO,EAAE,KAAK,EAAE,CAAC"}
@@ -0,0 +1,15 @@
1
+ import { z } from "zod";
2
+ /**
3
+ * A codec factory that returns codec parsing strings formatted
4
+ * as expected by the [ms](https://www.npmjs.com/package/ms) and turning them
5
+ * into their equivalent number in milliseconds.
6
+ *
7
+ * @param options - Those options are passed to `ms` when turning the milliseconds
8
+ * number into a formatted string.
9
+ *
10
+ * @returns A codec where the schemas are {@link z.string} and {@link z.number},
11
+ * and where the transforms between each are handled by the `ms` package.
12
+ */
13
+ export declare const ms: (options?: {
14
+ long: boolean;
15
+ }) => z.ZodCodec<z.ZodString, z.ZodNumber>;
@@ -0,0 +1,18 @@
1
+ import impl from "ms";
2
+ import { z } from "zod";
3
+ /**
4
+ * A codec factory that returns codec parsing strings formatted
5
+ * as expected by the [ms](https://www.npmjs.com/package/ms) and turning them
6
+ * into their equivalent number in milliseconds.
7
+ *
8
+ * @param options - Those options are passed to `ms` when turning the milliseconds
9
+ * number into a formatted string.
10
+ *
11
+ * @returns A codec where the schemas are {@link z.string} and {@link z.number},
12
+ * and where the transforms between each are handled by the `ms` package.
13
+ */
14
+ export const ms = (options) => z.codec(z.string(), z.number(), {
15
+ decode: (str) => impl(str) ?? z.NEVER,
16
+ encode: (millis) => impl(millis, options),
17
+ });
18
+ //# sourceMappingURL=ms.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ms.js","sourceRoot":"","sources":["../../../src/codec/ms.ts"],"names":[],"mappings":"AAAA,OAAO,IAAI,MAAM,IAAI,CAAC;AACtB,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB;;;;;;;;;;GAUG;AACH,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,OAA2B,EAAE,EAAE,CAChD,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC,MAAM,EAAE,EAAE;IAC9B,MAAM,EAAE,CAAC,GAAG,EAAE,EAAE,CAAC,IAAI,CAAC,GAAuB,CAAC,IAAI,CAAC,CAAC,KAAK;IACzD,MAAM,EAAE,CAAC,MAAM,EAAE,EAAE,CAAC,IAAI,CAAC,MAAM,EAAE,OAAO,CAAC;CAC1C,CAAC,CAAC"}
@@ -1,5 +1,6 @@
1
1
  import type { TypeGuard } from "@infra-blocks/types";
2
2
  import type { z } from "zod";
3
+ import { isValid } from "./is-valid.js";
3
4
  /**
4
5
  * Returns a type guard function using the provided schema as the source of truth.
5
6
  *
@@ -16,19 +17,6 @@ import type { z } from "zod";
16
17
  * former case, that information is also carried with the type.
17
18
  */
18
19
  declare function typeGuard<S extends z.ZodType>(schema: S): TypeGuard<z.infer<S>>;
19
- /**
20
- * Validates that the value satisfies the provided schema.
21
- *
22
- * Upon success, the value's type is narrowed to the schema's output type.
23
- *
24
- * It uses `safeParse` internally.
25
- *
26
- * @param schema - The schema to validate against.
27
- * @param value - The value to validate.
28
- *
29
- * @returns Whether the value satisfies the schema.
30
- */
31
- declare function isValid<S extends z.ZodType>(schema: S, value: unknown): value is z.infer<S>;
32
20
  declare const zu: {
33
21
  aws: {
34
22
  accountId: typeof import("./aws/account-id.js").accountId;
@@ -39,6 +27,9 @@ declare const zu: {
39
27
  codec: {
40
28
  csv: () => z.ZodCodec<z.ZodString, z.ZodArray<z.ZodString>>;
41
29
  jsonParse: <S extends z.ZodType>(schema: S) => z.ZodCodec<z.core.$ZodBranded<z.ZodString, "JsonString">, S>;
30
+ ms: (options?: {
31
+ long: boolean;
32
+ }) => z.ZodCodec<z.ZodString, z.ZodNumber>;
42
33
  stringtoInteger: () => z.ZodCodec<z.core.$ZodBranded<z.ZodString, "IntegerString">, z.core.$ZodBranded<z.ZodInt, "Integer">>;
43
34
  stringToJson: () => z.ZodCodec<z.core.$ZodBranded<z.ZodString, "JsonString">, z.ZodType<import("./json/json.js").Json, unknown, z.core.$ZodTypeInternals<import("./json/json.js").Json, unknown>>>;
44
35
  stringToUrl: () => z.ZodCodec<z.core.$ZodBranded<z.ZodURL, "UrlString">, z.ZodCustom<import("url").URL, import("url").URL>>;
package/lib/esm/index.js CHANGED
@@ -1,6 +1,7 @@
1
1
  import { aws } from "./aws/index.js";
2
2
  import { codec } from "./codec/index.js";
3
3
  import { geojson } from "./geojson/index.js";
4
+ import { isValid } from "./is-valid.js";
4
5
  import { iso } from "./iso/index.js";
5
6
  import { json } from "./json/index.js";
6
7
  import { number } from "./number/index.js";
@@ -25,21 +26,6 @@ function typeGuard(schema) {
25
26
  return isValid(schema, value);
26
27
  };
27
28
  }
28
- /**
29
- * Validates that the value satisfies the provided schema.
30
- *
31
- * Upon success, the value's type is narrowed to the schema's output type.
32
- *
33
- * It uses `safeParse` internally.
34
- *
35
- * @param schema - The schema to validate against.
36
- * @param value - The value to validate.
37
- *
38
- * @returns Whether the value satisfies the schema.
39
- */
40
- function isValid(schema, value) {
41
- return schema.safeParse(value).success;
42
- }
43
29
  const zu = {
44
30
  aws,
45
31
  codec,
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,GAAG,EAAE,MAAM,gBAAgB,CAAC;AACrC,OAAO,EAAE,KAAK,EAAE,MAAM,kBAAkB,CAAC;AACzC,OAAO,EAAE,OAAO,EAAE,MAAM,oBAAoB,CAAC;AAC7C,OAAO,EAAE,GAAG,EAAE,MAAM,gBAAgB,CAAC;AACrC,OAAO,EAAE,IAAI,EAAE,MAAM,iBAAiB,CAAC;AACvC,OAAO,EAAE,MAAM,EAAE,MAAM,mBAAmB,CAAC;AAC3C,OAAO,EAAE,MAAM,EAAE,MAAM,mBAAmB,CAAC;AAE3C;;;;;;;;;;;;;;GAcG;AACH,SAAS,SAAS,CAAsB,MAAS;IAC/C,OAAO,CAAC,KAAc,EAAuB,EAAE;QAC7C,OAAO,OAAO,CAAC,MAAM,EAAE,KAAK,CAAC,CAAC;IAChC,CAAC,CAAC;AACJ,CAAC;AAED;;;;;;;;;;;GAWG;AACH,SAAS,OAAO,CACd,MAAS,EACT,KAAc;IAEd,OAAO,MAAM,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC,OAAO,CAAC;AACzC,CAAC;AAED,MAAM,EAAE,GAAG;IACT,GAAG;IACH,KAAK;IACL,OAAO;IACP,GAAG;IACH,IAAI;IACJ,MAAM;IACN,MAAM;IACN,OAAO;IACP,SAAS;CACV,CAAC;AAEF,OAAO,EAAE,EAAE,EAAE,CAAC"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,GAAG,EAAE,MAAM,gBAAgB,CAAC;AACrC,OAAO,EAAE,KAAK,EAAE,MAAM,kBAAkB,CAAC;AACzC,OAAO,EAAE,OAAO,EAAE,MAAM,oBAAoB,CAAC;AAC7C,OAAO,EAAE,OAAO,EAAE,MAAM,eAAe,CAAC;AACxC,OAAO,EAAE,GAAG,EAAE,MAAM,gBAAgB,CAAC;AACrC,OAAO,EAAE,IAAI,EAAE,MAAM,iBAAiB,CAAC;AACvC,OAAO,EAAE,MAAM,EAAE,MAAM,mBAAmB,CAAC;AAC3C,OAAO,EAAE,MAAM,EAAE,MAAM,mBAAmB,CAAC;AAE3C;;;;;;;;;;;;;;GAcG;AACH,SAAS,SAAS,CAAsB,MAAS;IAC/C,OAAO,CAAC,KAAc,EAAuB,EAAE;QAC7C,OAAO,OAAO,CAAC,MAAM,EAAE,KAAK,CAAC,CAAC;IAChC,CAAC,CAAC;AACJ,CAAC;AAED,MAAM,EAAE,GAAG;IACT,GAAG;IACH,KAAK;IACL,OAAO;IACP,GAAG;IACH,IAAI;IACJ,MAAM;IACN,MAAM;IACN,OAAO;IACP,SAAS;CACV,CAAC;AAEF,OAAO,EAAE,EAAE,EAAE,CAAC"}
@@ -0,0 +1,14 @@
1
+ import type { z } from "zod";
2
+ /**
3
+ * Validates that the value satisfies the provided schema.
4
+ *
5
+ * Upon success, the value's type is narrowed to the schema's output type.
6
+ *
7
+ * It uses `safeParse` internally.
8
+ *
9
+ * @param schema - The schema to validate against.
10
+ * @param value - The value to validate.
11
+ *
12
+ * @returns Whether the value satisfies the schema.
13
+ */
14
+ export declare function isValid<S extends z.ZodType>(schema: S, value: unknown): value is z.infer<S>;
@@ -0,0 +1,16 @@
1
+ /**
2
+ * Validates that the value satisfies the provided schema.
3
+ *
4
+ * Upon success, the value's type is narrowed to the schema's output type.
5
+ *
6
+ * It uses `safeParse` internally.
7
+ *
8
+ * @param schema - The schema to validate against.
9
+ * @param value - The value to validate.
10
+ *
11
+ * @returns Whether the value satisfies the schema.
12
+ */
13
+ export function isValid(schema, value) {
14
+ return schema.safeParse(value).success;
15
+ }
16
+ //# sourceMappingURL=is-valid.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"is-valid.js","sourceRoot":"","sources":["../../src/is-valid.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;;GAWG;AACH,MAAM,UAAU,OAAO,CACrB,MAAS,EACT,KAAc;IAEd,OAAO,MAAM,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC,OAAO,CAAC;AACzC,CAAC"}
@@ -1,5 +1,5 @@
1
1
  import { z } from "zod";
2
- export type Integer = z.infer<typeof integerSchema>;
3
- declare const integerSchema: z.core.$ZodBranded<z.ZodInt, "Integer">;
2
+ declare const schema: z.core.$ZodBranded<z.ZodInt, "Integer">;
3
+ export type Integer = z.infer<typeof schema>;
4
4
  export declare const integer: () => z.core.$ZodBranded<z.ZodInt, "Integer">;
5
5
  export {};
@@ -1,4 +1,4 @@
1
1
  import { z } from "zod";
2
- const integerSchema = z.int().brand("Integer");
3
- export const integer = () => integerSchema;
2
+ const schema = z.int().brand("Integer");
3
+ export const integer = () => schema;
4
4
  //# sourceMappingURL=integer.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"integer.js","sourceRoot":"","sources":["../../../src/number/integer.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAIxB,MAAM,aAAa,GAAG,CAAC,CAAC,GAAG,EAAE,CAAC,KAAK,CAAC,SAAS,CAAC,CAAC;AAE/C,MAAM,CAAC,MAAM,OAAO,GAAG,GAAG,EAAE,CAAC,aAAa,CAAC"}
1
+ {"version":3,"file":"integer.js","sourceRoot":"","sources":["../../../src/number/integer.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB,MAAM,MAAM,GAAG,CAAC,CAAC,GAAG,EAAE,CAAC,KAAK,CAAC,SAAS,CAAC,CAAC;AAIxC,MAAM,CAAC,MAAM,OAAO,GAAG,GAAG,EAAE,CAAC,MAAM,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@infra-blocks/zod-utils",
3
- "version": "0.22.0",
3
+ "version": "0.23.0",
4
4
  "description": "Extensions to the zod package.",
5
5
  "keywords": [
6
6
  "zod",
@@ -69,12 +69,14 @@
69
69
  "@infra-blocks/types": "^0.24.0",
70
70
  "currency-codes": "^2.2.0",
71
71
  "iso-3166": "^4.4.0",
72
+ "ms": "^2.1.3",
72
73
  "zod": "^4.2.1"
73
74
  },
74
75
  "devDependencies": {
75
76
  "@biomejs/biome": "^2.3.10",
76
77
  "@infra-blocks/test": "^0.6.0",
77
78
  "@types/mocha": "^10.0.1",
79
+ "@types/ms": "^2.1.0",
78
80
  "@types/node": "^24.10.4",
79
81
  "c8": "^8.0.0",
80
82
  "lefthook": "^2.0.13",