@infra-blocks/zod-utils 0.23.1 → 0.24.0-alpha.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
@@ -25,11 +25,12 @@ compiles and runs successfully.
25
25
  - [aws](#aws)
26
26
  - [codec](#codec)
27
27
  - [geojson](#geojson)
28
+ - [inferBrand](#inferbrand)
28
29
  - [iso](#iso)
29
30
  - [json](#json)
30
31
  - [number](#number)
31
32
  - [string](#string)
32
- - [typeGuard](#type-guard)
33
+ - [typeGuard](#typeguard)
33
34
  - [isValid](#is-valid)
34
35
 
35
36
  ### aws
@@ -274,6 +275,20 @@ zu.geojson().parse({
274
275
  });
275
276
  ```
276
277
 
278
+ ### inferBrand
279
+
280
+ The `zu.inferBrand<T>` type utility extracts the brand(s) from a given type. It resolves to `never` if the
281
+ type is not branded. It unionizes the brands if more than one exists.
282
+
283
+ ```typescript
284
+ import { z } from "zod";
285
+ import { zu } from "@infra-blocks/zod-utils";
286
+
287
+ type Never = zu.inferBrand<string>; // Resolves to never.
288
+ type StringBrand = zu.inferBrand<number & z.$brand<"Toto">>; // Resolves to "Toto".
289
+ type UnionBrands = zu.inferBrand<number & z.$brand<"Toto"> & z.$brand<5>>; // Resolves to "Toto" | 5.
290
+ ```
291
+
277
292
  ### iso
278
293
 
279
294
  The `iso` module is an extension of `zod`'s own `iso` module. All schemas return [branded types](#branded-types).
@@ -408,7 +423,7 @@ expectTypeOf(result).toEqualTypeOf<NumberString>();
408
423
  expect(result).to.equal("1234.5678");
409
424
  ```
410
425
 
411
- ### Type Guard
426
+ ### typeGuard
412
427
 
413
428
  The `typeGuard` utility allows to obtain a function that will act as a type guard for the type
414
429
  that the wrapped schema outputs. It is most useful with branded types, where the information
@@ -1,77 +1,2 @@
1
- import type { TypeGuard } from "@infra-blocks/types";
2
- import type { z } from "zod";
3
- import { isValid } from "./is-valid.js";
4
- /**
5
- * Returns a type guard function using the provided schema as the source of truth.
6
- *
7
- * It uses the `validate` function internally.
8
- *
9
- * @param schema - The schema used to verify the inputs will be of the correct type.
10
- *
11
- * @returns A type guard function that uses the provided schema to asserts whether the
12
- * input is of the proper type or not.
13
- *
14
- * @note Type guards work best with branded types. Given two schemas, z.string().min(5),
15
- * and z.string().min(5).brand("Min5String"), the former type guard would only assert
16
- * that the input is string (even if the length check is also done), whereas in the
17
- * former case, that information is also carried with the type.
18
- */
19
- declare function typeGuard<S extends z.ZodType>(schema: S): TypeGuard<z.infer<S>>;
20
- declare const zu: {
21
- aws: {
22
- accountId: typeof import("./aws/account-id.js").accountId;
23
- arn: typeof import("./aws/arn.js").arn;
24
- region: typeof import("./aws/region.js").region;
25
- partition: typeof import("./aws/partition.js").partition;
26
- };
27
- codec: {
28
- csv: () => z.ZodCodec<z.ZodString, z.ZodArray<z.ZodString>>;
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>;
33
- stringtoInteger: () => z.ZodCodec<z.core.$ZodBranded<z.ZodString, "IntegerString">, z.core.$ZodBranded<z.ZodInt, "Integer">>;
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>>>;
35
- stringToUrl: () => z.ZodCodec<z.core.$ZodBranded<z.ZodURL, "UrlString">, z.ZodCustom<import("url").URL, import("url").URL>>;
36
- };
37
- geojson: typeof import("./geojson/geojson.js").geojson & {
38
- boundingBox: typeof import("./geojson/bounding-box.js").boundingBox;
39
- feature: typeof import("./geojson/feature.js").feature;
40
- featureCollection: typeof import("./geojson/feature-collection.js").featureCollection;
41
- geometryCollection: typeof import("./geojson/geometry.js").geometryCollection;
42
- lineString: typeof import("./geojson/line-string.js").lineString;
43
- multiLineString: typeof import("./geojson/multi-line-string.js").multiLineString;
44
- multiPoint: typeof import("./geojson/multi-point.js").multiPoint;
45
- multiPolygon: typeof import("./geojson/multi-polygon.js").multiPolygon;
46
- point: typeof import("./geojson/point.js").point;
47
- polygon: typeof import("./geojson/polygon.js").polygon;
48
- coordinate: typeof import("./geojson/coordinate.js").coordinate;
49
- };
50
- iso: {
51
- currencyCode: () => z.core.$ZodBranded<z.ZodEnum<{
52
- [x: string]: string;
53
- }>, "IsoCurrencyCode">;
54
- countryCode: {
55
- alpha3: () => z.core.$ZodBranded<z.ZodEnum<{
56
- [x: string]: string;
57
- }>, "IsoAlpha3CountryCode">;
58
- };
59
- };
60
- json: (() => z.ZodType<import("./json/json.js").Json, unknown, z.core.$ZodTypeInternals<import("./json/json.js").Json, unknown>>) & {
61
- array: () => z.ZodArray<z.ZodType<import("./json/json.js").Json, unknown, z.core.$ZodTypeInternals<import("./json/json.js").Json, unknown>>>;
62
- object: () => z.ZodRecord<z.ZodString, z.ZodType<import("./json/json.js").Json, unknown, z.core.$ZodTypeInternals<import("./json/json.js").Json, unknown>>>;
63
- primitive: () => z.ZodUnion<readonly [z.ZodString, z.ZodNumber, z.ZodBoolean, z.ZodNull]>;
64
- };
65
- number: {
66
- integer: () => z.core.$ZodBranded<z.ZodInt, "Integer">;
67
- };
68
- string: {
69
- integer: () => z.core.$ZodBranded<z.ZodString, "IntegerString">;
70
- json: () => z.core.$ZodBranded<z.ZodString, "JsonString">;
71
- number: () => z.core.$ZodBranded<z.ZodString, "NumberString">;
72
- url: () => z.core.$ZodBranded<z.ZodURL, "UrlString">;
73
- };
74
- isValid: typeof isValid;
75
- typeGuard: typeof typeGuard;
76
- };
1
+ import * as zu from "./zu.js";
77
2
  export { zu };
package/lib/cjs/index.js CHANGED
@@ -1,44 +1,39 @@
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.zu = void 0;
4
- const index_js_1 = require("./aws/index.js");
5
- const index_js_2 = require("./codec/index.js");
6
- const index_js_3 = require("./geojson/index.js");
7
- const is_valid_js_1 = require("./is-valid.js");
8
- const index_js_4 = require("./iso/index.js");
9
- const index_js_5 = require("./json/index.js");
10
- const index_js_6 = require("./number/index.js");
11
- const index_js_7 = require("./string/index.js");
12
- /**
13
- * Returns a type guard function using the provided schema as the source of truth.
14
- *
15
- * It uses the `validate` function internally.
16
- *
17
- * @param schema - The schema used to verify the inputs will be of the correct type.
18
- *
19
- * @returns A type guard function that uses the provided schema to asserts whether the
20
- * input is of the proper type or not.
21
- *
22
- * @note Type guards work best with branded types. Given two schemas, z.string().min(5),
23
- * and z.string().min(5).brand("Min5String"), the former type guard would only assert
24
- * that the input is string (even if the length check is also done), whereas in the
25
- * former case, that information is also carried with the type.
26
- */
27
- function typeGuard(schema) {
28
- return (value) => {
29
- return (0, is_valid_js_1.isValid)(schema, value);
30
- };
31
- }
32
- const zu = {
33
- aws: index_js_1.aws,
34
- codec: index_js_2.codec,
35
- geojson: index_js_3.geojson,
36
- iso: index_js_4.iso,
37
- json: index_js_5.json,
38
- number: index_js_6.number,
39
- string: index_js_7.string,
40
- isValid: is_valid_js_1.isValid,
41
- typeGuard,
42
- };
37
+ const zu = __importStar(require("./zu.js"));
43
38
  exports.zu = zu;
44
39
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
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"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,4CAA8B;AAErB,gBAAE"}
package/lib/cjs/lib.d.ts CHANGED
@@ -1,4 +1,8 @@
1
1
  import type { z } from "zod";
2
- type Brand<T> = T extends z.$brand<infer B> ? B : never;
3
- export type Unbranded<T> = T extends infer U & z.$brand<Brand<T>> ? Unbranded<U> : T;
4
- export {};
2
+ /**
3
+ * A type utility that removes the brand from T.
4
+ *
5
+ * If T extends string & z.$brand<"BigToto">, then zu.unbranded<T> = T.
6
+ * Otherwise, zu.unbranded<T> = T.
7
+ */
8
+ export type Unbranded<T> = T extends z.$brand<infer B> ? Omit<T, keyof z.$brand<B>> : T;
@@ -0,0 +1,18 @@
1
+ import type { TypeGuard } from "@infra-blocks/types";
2
+ import type { z } from "zod";
3
+ /**
4
+ * Returns a type guard function using the provided schema as the source of truth.
5
+ *
6
+ * It uses the `validate` function internally.
7
+ *
8
+ * @param schema - The schema used to verify the inputs will be of the correct type.
9
+ *
10
+ * @returns A type guard function that uses the provided schema to asserts whether the
11
+ * input is of the proper type or not.
12
+ *
13
+ * @note Type guards work best with branded types. Given two schemas, z.string().min(5),
14
+ * and z.string().min(5).brand("Min5String"), the former type guard would only assert
15
+ * that the input is string (even if the length check is also done), whereas in the
16
+ * former case, that information is also carried with the type.
17
+ */
18
+ export declare function typeGuard<S extends z.ZodType>(schema: S): TypeGuard<z.infer<S>>;
@@ -0,0 +1,25 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.typeGuard = typeGuard;
4
+ const is_valid_js_1 = require("./is-valid.js");
5
+ /**
6
+ * Returns a type guard function using the provided schema as the source of truth.
7
+ *
8
+ * It uses the `validate` function internally.
9
+ *
10
+ * @param schema - The schema used to verify the inputs will be of the correct type.
11
+ *
12
+ * @returns A type guard function that uses the provided schema to asserts whether the
13
+ * input is of the proper type or not.
14
+ *
15
+ * @note Type guards work best with branded types. Given two schemas, z.string().min(5),
16
+ * and z.string().min(5).brand("Min5String"), the former type guard would only assert
17
+ * that the input is string (even if the length check is also done), whereas in the
18
+ * former case, that information is also carried with the type.
19
+ */
20
+ function typeGuard(schema) {
21
+ return (value) => {
22
+ return (0, is_valid_js_1.isValid)(schema, value);
23
+ };
24
+ }
25
+ //# sourceMappingURL=type-guard.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"type-guard.js","sourceRoot":"","sources":["../../src/type-guard.ts"],"names":[],"mappings":";;AAoBA,8BAMC;AAxBD,+CAAwC;AAExC;;;;;;;;;;;;;;GAcG;AAEH,SAAgB,SAAS,CACvB,MAAS;IAET,OAAO,CAAC,KAAc,EAAuB,EAAE;QAC7C,OAAO,IAAA,qBAAO,EAAC,MAAM,EAAE,KAAK,CAAC,CAAC;IAChC,CAAC,CAAC;AACJ,CAAC"}
@@ -0,0 +1,19 @@
1
+ import type { z } from "zod";
2
+ export { aws } from "./aws/index.js";
3
+ export { codec } from "./codec/index.js";
4
+ export { geojson } from "./geojson/index.js";
5
+ export { isValid } from "./is-valid.js";
6
+ export { iso } from "./iso/index.js";
7
+ export { json } from "./json/index.js";
8
+ export { number } from "./number/index.js";
9
+ export { string } from "./string/index.js";
10
+ export { typeGuard } from "./type-guard.js";
11
+ /**
12
+ * A type utility that infers the brand of a branded type.
13
+ *
14
+ * If T extends string & z.$brand<"BigToto">, then
15
+ * zu.interBrand<T> = "BigToto";
16
+ *
17
+ * If T is not branded, then this type utility resolves to `never`.
18
+ */
19
+ export type inferBrand<T> = T extends z.$brand<infer B> ? B : never;
package/lib/cjs/zu.js ADDED
@@ -0,0 +1,22 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.typeGuard = exports.string = exports.number = exports.json = exports.iso = exports.isValid = exports.geojson = exports.codec = exports.aws = void 0;
4
+ var index_js_1 = require("./aws/index.js");
5
+ Object.defineProperty(exports, "aws", { enumerable: true, get: function () { return index_js_1.aws; } });
6
+ var index_js_2 = require("./codec/index.js");
7
+ Object.defineProperty(exports, "codec", { enumerable: true, get: function () { return index_js_2.codec; } });
8
+ var index_js_3 = require("./geojson/index.js");
9
+ Object.defineProperty(exports, "geojson", { enumerable: true, get: function () { return index_js_3.geojson; } });
10
+ var is_valid_js_1 = require("./is-valid.js");
11
+ Object.defineProperty(exports, "isValid", { enumerable: true, get: function () { return is_valid_js_1.isValid; } });
12
+ var index_js_4 = require("./iso/index.js");
13
+ Object.defineProperty(exports, "iso", { enumerable: true, get: function () { return index_js_4.iso; } });
14
+ var index_js_5 = require("./json/index.js");
15
+ Object.defineProperty(exports, "json", { enumerable: true, get: function () { return index_js_5.json; } });
16
+ var index_js_6 = require("./number/index.js");
17
+ Object.defineProperty(exports, "number", { enumerable: true, get: function () { return index_js_6.number; } });
18
+ var index_js_7 = require("./string/index.js");
19
+ Object.defineProperty(exports, "string", { enumerable: true, get: function () { return index_js_7.string; } });
20
+ var type_guard_js_1 = require("./type-guard.js");
21
+ Object.defineProperty(exports, "typeGuard", { enumerable: true, get: function () { return type_guard_js_1.typeGuard; } });
22
+ //# sourceMappingURL=zu.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"zu.js","sourceRoot":"","sources":["../../src/zu.ts"],"names":[],"mappings":";;;AAEA,2CAAqC;AAA5B,+FAAA,GAAG,OAAA;AACZ,6CAAyC;AAAhC,iGAAA,KAAK,OAAA;AACd,+CAA6C;AAApC,mGAAA,OAAO,OAAA;AAChB,6CAAwC;AAA/B,sGAAA,OAAO,OAAA;AAChB,2CAAqC;AAA5B,+FAAA,GAAG,OAAA;AACZ,4CAAuC;AAA9B,gGAAA,IAAI,OAAA;AACb,8CAA2C;AAAlC,kGAAA,MAAM,OAAA;AACf,8CAA2C;AAAlC,kGAAA,MAAM,OAAA;AACf,iDAA4C;AAAnC,0GAAA,SAAS,OAAA"}
@@ -1,77 +1,2 @@
1
- import type { TypeGuard } from "@infra-blocks/types";
2
- import type { z } from "zod";
3
- import { isValid } from "./is-valid.js";
4
- /**
5
- * Returns a type guard function using the provided schema as the source of truth.
6
- *
7
- * It uses the `validate` function internally.
8
- *
9
- * @param schema - The schema used to verify the inputs will be of the correct type.
10
- *
11
- * @returns A type guard function that uses the provided schema to asserts whether the
12
- * input is of the proper type or not.
13
- *
14
- * @note Type guards work best with branded types. Given two schemas, z.string().min(5),
15
- * and z.string().min(5).brand("Min5String"), the former type guard would only assert
16
- * that the input is string (even if the length check is also done), whereas in the
17
- * former case, that information is also carried with the type.
18
- */
19
- declare function typeGuard<S extends z.ZodType>(schema: S): TypeGuard<z.infer<S>>;
20
- declare const zu: {
21
- aws: {
22
- accountId: typeof import("./aws/account-id.js").accountId;
23
- arn: typeof import("./aws/arn.js").arn;
24
- region: typeof import("./aws/region.js").region;
25
- partition: typeof import("./aws/partition.js").partition;
26
- };
27
- codec: {
28
- csv: () => z.ZodCodec<z.ZodString, z.ZodArray<z.ZodString>>;
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>;
33
- stringtoInteger: () => z.ZodCodec<z.core.$ZodBranded<z.ZodString, "IntegerString">, z.core.$ZodBranded<z.ZodInt, "Integer">>;
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>>>;
35
- stringToUrl: () => z.ZodCodec<z.core.$ZodBranded<z.ZodURL, "UrlString">, z.ZodCustom<import("url").URL, import("url").URL>>;
36
- };
37
- geojson: typeof import("./geojson/geojson.js").geojson & {
38
- boundingBox: typeof import("./geojson/bounding-box.js").boundingBox;
39
- feature: typeof import("./geojson/feature.js").feature;
40
- featureCollection: typeof import("./geojson/feature-collection.js").featureCollection;
41
- geometryCollection: typeof import("./geojson/geometry.js").geometryCollection;
42
- lineString: typeof import("./geojson/line-string.js").lineString;
43
- multiLineString: typeof import("./geojson/multi-line-string.js").multiLineString;
44
- multiPoint: typeof import("./geojson/multi-point.js").multiPoint;
45
- multiPolygon: typeof import("./geojson/multi-polygon.js").multiPolygon;
46
- point: typeof import("./geojson/point.js").point;
47
- polygon: typeof import("./geojson/polygon.js").polygon;
48
- coordinate: typeof import("./geojson/coordinate.js").coordinate;
49
- };
50
- iso: {
51
- currencyCode: () => z.core.$ZodBranded<z.ZodEnum<{
52
- [x: string]: string;
53
- }>, "IsoCurrencyCode">;
54
- countryCode: {
55
- alpha3: () => z.core.$ZodBranded<z.ZodEnum<{
56
- [x: string]: string;
57
- }>, "IsoAlpha3CountryCode">;
58
- };
59
- };
60
- json: (() => z.ZodType<import("./json/json.js").Json, unknown, z.core.$ZodTypeInternals<import("./json/json.js").Json, unknown>>) & {
61
- array: () => z.ZodArray<z.ZodType<import("./json/json.js").Json, unknown, z.core.$ZodTypeInternals<import("./json/json.js").Json, unknown>>>;
62
- object: () => z.ZodRecord<z.ZodString, z.ZodType<import("./json/json.js").Json, unknown, z.core.$ZodTypeInternals<import("./json/json.js").Json, unknown>>>;
63
- primitive: () => z.ZodUnion<readonly [z.ZodString, z.ZodNumber, z.ZodBoolean, z.ZodNull]>;
64
- };
65
- number: {
66
- integer: () => z.core.$ZodBranded<z.ZodInt, "Integer">;
67
- };
68
- string: {
69
- integer: () => z.core.$ZodBranded<z.ZodString, "IntegerString">;
70
- json: () => z.core.$ZodBranded<z.ZodString, "JsonString">;
71
- number: () => z.core.$ZodBranded<z.ZodString, "NumberString">;
72
- url: () => z.core.$ZodBranded<z.ZodURL, "UrlString">;
73
- };
74
- isValid: typeof isValid;
75
- typeGuard: typeof typeGuard;
76
- };
1
+ import * as zu from "./zu.js";
77
2
  export { zu };
package/lib/esm/index.js CHANGED
@@ -1,41 +1,3 @@
1
- import { aws } from "./aws/index.js";
2
- import { codec } from "./codec/index.js";
3
- import { geojson } from "./geojson/index.js";
4
- import { isValid } from "./is-valid.js";
5
- import { iso } from "./iso/index.js";
6
- import { json } from "./json/index.js";
7
- import { number } from "./number/index.js";
8
- import { string } from "./string/index.js";
9
- /**
10
- * Returns a type guard function using the provided schema as the source of truth.
11
- *
12
- * It uses the `validate` function internally.
13
- *
14
- * @param schema - The schema used to verify the inputs will be of the correct type.
15
- *
16
- * @returns A type guard function that uses the provided schema to asserts whether the
17
- * input is of the proper type or not.
18
- *
19
- * @note Type guards work best with branded types. Given two schemas, z.string().min(5),
20
- * and z.string().min(5).brand("Min5String"), the former type guard would only assert
21
- * that the input is string (even if the length check is also done), whereas in the
22
- * former case, that information is also carried with the type.
23
- */
24
- function typeGuard(schema) {
25
- return (value) => {
26
- return isValid(schema, value);
27
- };
28
- }
29
- const zu = {
30
- aws,
31
- codec,
32
- geojson,
33
- iso,
34
- json,
35
- number,
36
- string,
37
- isValid,
38
- typeGuard,
39
- };
1
+ import * as zu from "./zu.js";
40
2
  export { zu };
41
3
  //# sourceMappingURL=index.js.map
@@ -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,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"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,SAAS,CAAC;AAE9B,OAAO,EAAE,EAAE,EAAE,CAAC"}
package/lib/esm/lib.d.ts CHANGED
@@ -1,4 +1,8 @@
1
1
  import type { z } from "zod";
2
- type Brand<T> = T extends z.$brand<infer B> ? B : never;
3
- export type Unbranded<T> = T extends infer U & z.$brand<Brand<T>> ? Unbranded<U> : T;
4
- export {};
2
+ /**
3
+ * A type utility that removes the brand from T.
4
+ *
5
+ * If T extends string & z.$brand<"BigToto">, then zu.unbranded<T> = T.
6
+ * Otherwise, zu.unbranded<T> = T.
7
+ */
8
+ export type Unbranded<T> = T extends z.$brand<infer B> ? Omit<T, keyof z.$brand<B>> : T;
@@ -0,0 +1,18 @@
1
+ import type { TypeGuard } from "@infra-blocks/types";
2
+ import type { z } from "zod";
3
+ /**
4
+ * Returns a type guard function using the provided schema as the source of truth.
5
+ *
6
+ * It uses the `validate` function internally.
7
+ *
8
+ * @param schema - The schema used to verify the inputs will be of the correct type.
9
+ *
10
+ * @returns A type guard function that uses the provided schema to asserts whether the
11
+ * input is of the proper type or not.
12
+ *
13
+ * @note Type guards work best with branded types. Given two schemas, z.string().min(5),
14
+ * and z.string().min(5).brand("Min5String"), the former type guard would only assert
15
+ * that the input is string (even if the length check is also done), whereas in the
16
+ * former case, that information is also carried with the type.
17
+ */
18
+ export declare function typeGuard<S extends z.ZodType>(schema: S): TypeGuard<z.infer<S>>;
@@ -0,0 +1,22 @@
1
+ import { isValid } from "./is-valid.js";
2
+ /**
3
+ * Returns a type guard function using the provided schema as the source of truth.
4
+ *
5
+ * It uses the `validate` function internally.
6
+ *
7
+ * @param schema - The schema used to verify the inputs will be of the correct type.
8
+ *
9
+ * @returns A type guard function that uses the provided schema to asserts whether the
10
+ * input is of the proper type or not.
11
+ *
12
+ * @note Type guards work best with branded types. Given two schemas, z.string().min(5),
13
+ * and z.string().min(5).brand("Min5String"), the former type guard would only assert
14
+ * that the input is string (even if the length check is also done), whereas in the
15
+ * former case, that information is also carried with the type.
16
+ */
17
+ export function typeGuard(schema) {
18
+ return (value) => {
19
+ return isValid(schema, value);
20
+ };
21
+ }
22
+ //# sourceMappingURL=type-guard.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"type-guard.js","sourceRoot":"","sources":["../../src/type-guard.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,OAAO,EAAE,MAAM,eAAe,CAAC;AAExC;;;;;;;;;;;;;;GAcG;AAEH,MAAM,UAAU,SAAS,CACvB,MAAS;IAET,OAAO,CAAC,KAAc,EAAuB,EAAE;QAC7C,OAAO,OAAO,CAAC,MAAM,EAAE,KAAK,CAAC,CAAC;IAChC,CAAC,CAAC;AACJ,CAAC"}
@@ -0,0 +1,19 @@
1
+ import type { z } from "zod";
2
+ export { aws } from "./aws/index.js";
3
+ export { codec } from "./codec/index.js";
4
+ export { geojson } from "./geojson/index.js";
5
+ export { isValid } from "./is-valid.js";
6
+ export { iso } from "./iso/index.js";
7
+ export { json } from "./json/index.js";
8
+ export { number } from "./number/index.js";
9
+ export { string } from "./string/index.js";
10
+ export { typeGuard } from "./type-guard.js";
11
+ /**
12
+ * A type utility that infers the brand of a branded type.
13
+ *
14
+ * If T extends string & z.$brand<"BigToto">, then
15
+ * zu.interBrand<T> = "BigToto";
16
+ *
17
+ * If T is not branded, then this type utility resolves to `never`.
18
+ */
19
+ export type inferBrand<T> = T extends z.$brand<infer B> ? B : never;
package/lib/esm/zu.js ADDED
@@ -0,0 +1,10 @@
1
+ export { aws } from "./aws/index.js";
2
+ export { codec } from "./codec/index.js";
3
+ export { geojson } from "./geojson/index.js";
4
+ export { isValid } from "./is-valid.js";
5
+ export { iso } from "./iso/index.js";
6
+ export { json } from "./json/index.js";
7
+ export { number } from "./number/index.js";
8
+ export { string } from "./string/index.js";
9
+ export { typeGuard } from "./type-guard.js";
10
+ //# sourceMappingURL=zu.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"zu.js","sourceRoot":"","sources":["../../src/zu.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;AAC3C,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@infra-blocks/zod-utils",
3
- "version": "0.23.1",
3
+ "version": "0.24.0-alpha.0",
4
4
  "description": "Extensions to the zod package.",
5
5
  "keywords": [
6
6
  "zod",