@infra-blocks/zod-utils 0.5.0-alpha.3 → 0.6.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.
Files changed (35) hide show
  1. package/README.md +51 -0
  2. package/lib/cjs/geojson/bounding-box.js +1 -2
  3. package/lib/cjs/geojson/bounding-box.js.map +1 -1
  4. package/lib/cjs/geojson/coordinate.js +1 -2
  5. package/lib/cjs/geojson/coordinate.js.map +1 -1
  6. package/lib/cjs/geojson/feature-collection.js +1 -2
  7. package/lib/cjs/geojson/feature-collection.js.map +1 -1
  8. package/lib/cjs/geojson/feature.js +1 -2
  9. package/lib/cjs/geojson/feature.js.map +1 -1
  10. package/lib/cjs/geojson/geojson.js +1 -2
  11. package/lib/cjs/geojson/geojson.js.map +1 -1
  12. package/lib/cjs/geojson/geometry.js +2 -3
  13. package/lib/cjs/geojson/geometry.js.map +1 -1
  14. package/lib/cjs/geojson/line-string.js +1 -2
  15. package/lib/cjs/geojson/line-string.js.map +1 -1
  16. package/lib/cjs/geojson/multi-line-string.js +1 -2
  17. package/lib/cjs/geojson/multi-line-string.js.map +1 -1
  18. package/lib/cjs/geojson/multi-point.js +1 -2
  19. package/lib/cjs/geojson/multi-point.js.map +1 -1
  20. package/lib/cjs/geojson/multi-polygon.js +1 -2
  21. package/lib/cjs/geojson/multi-polygon.js.map +1 -1
  22. package/lib/cjs/geojson/point.js +1 -2
  23. package/lib/cjs/geojson/point.js.map +1 -1
  24. package/lib/cjs/geojson/polygon.js +1 -2
  25. package/lib/cjs/geojson/polygon.js.map +1 -1
  26. package/lib/cjs/index.d.ts +37 -0
  27. package/lib/cjs/index.js +43 -0
  28. package/lib/cjs/index.js.map +1 -1
  29. package/lib/cjs/json/json.js +6 -6
  30. package/lib/cjs/json/json.js.map +1 -1
  31. package/lib/esm/index.d.ts +37 -0
  32. package/lib/esm/index.js +43 -0
  33. package/lib/esm/index.js.map +1 -1
  34. package/lib/esm/json/json.js.map +1 -1
  35. package/package.json +4 -3
package/README.md CHANGED
@@ -10,6 +10,9 @@ This package exposes various utilities extending the [zod](https://www.npmjs.com
10
10
 
11
11
  - [geojson](#geojson)
12
12
  - [json](#json)
13
+ - [csv](#csv)
14
+ - [typeGuard](#type-guard)
15
+ - [validate](#validate)
13
16
 
14
17
  ### GeoJson
15
18
 
@@ -220,3 +223,51 @@ const jsonObject: JsonObject = zu.json.object().parse({ hello: "world" });
220
223
  zu.json.object().parse(5); // Boom.
221
224
  zu.json.object().parse([]); // Boom.
222
225
  ```
226
+
227
+ ### CSV
228
+
229
+ The `csv` utility is nothing but a shorthand for `z.string().transform(s => s.split(","))`, which
230
+ is commonly written, in my experience. One can stop retyping all those letters and replace them
231
+ with a simple `zu.csv()` call.
232
+
233
+ ```typescript
234
+ import { zu } from "@infra-blocks/zod-utils";
235
+
236
+ const items = zu.csv().parse("one,two,three"); // items is ["one", "two", "three"]
237
+ ```
238
+
239
+ ### Type Guard
240
+
241
+ The `typeGuard` utility allows to obtain a function that will act as a type guard for the type
242
+ that the wrapped schema outputs. It is most useful with branded types, where the information
243
+ about the rules of the type is contained within it. Example:
244
+
245
+ ```typescript
246
+ import { z } from "zod";
247
+ import { zu } from "@infra-blocks/zod-utils";
248
+ import { expectTypeOf } from "expect-type"
249
+
250
+ export type Min5String = z.infer<typeof schema>;
251
+
252
+ const schema = z.string().min(5).brand("Min5String");
253
+ const isMin5String = zu.typeGuard(schema);
254
+ const myString = "toto-stfu";
255
+ if (isMin5String(myString)) {
256
+ // Here, the type of myString extends Min5String (it's actually `"toto-stfu" & z.$brand<"Min5String">`
257
+ // instead of `string & z.$brand<"Min5String">`)
258
+ expectTypeOf(myString).toExtend<Min5String>();
259
+ } else {
260
+ expectTypeOf(myString).toEqual<"toto-stfu">();
261
+ }
262
+ ```
263
+
264
+ It can still be used with vanilla types, but then the guarantees returned by a type guard are
265
+ not as strong. In our specific case, the type guard would assert that `myString` is a `string`,
266
+ despite the fact that it also checked that its length has to be greater than 5. That information
267
+ has been lost.
268
+
269
+ ### Validate
270
+
271
+ The validate API is very similar to the [type guard](#type-guard) one, except it doesn't bind
272
+ to a schema. The schema is passed as argument. Where you would write `zu.typeGuard(schema)(value)`,
273
+ you instead write `zu.validate(schema, value)`. Both behave the same.
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.boundingBox = void 0;
3
+ exports.boundingBox = boundingBox;
4
4
  const zod_1 = require("zod");
5
5
  const twoDimensionalBoundingBox = zod_1.z.tuple([
6
6
  zod_1.z.number(),
@@ -23,5 +23,4 @@ const boundingBoxSchema = zod_1.z.union([
23
23
  function boundingBox() {
24
24
  return boundingBoxSchema;
25
25
  }
26
- exports.boundingBox = boundingBox;
27
26
  //# sourceMappingURL=bounding-box.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"bounding-box.js","sourceRoot":"","sources":["../../../src/geojson/bounding-box.ts"],"names":[],"mappings":";;;AAAA,6BAAwB;AAExB,MAAM,yBAAyB,GAAG,OAAC,CAAC,KAAK,CAAC;IACxC,OAAC,CAAC,MAAM,EAAE;IACV,OAAC,CAAC,MAAM,EAAE;IACV,OAAC,CAAC,MAAM,EAAE;IACV,OAAC,CAAC,MAAM,EAAE;CACX,CAAC,CAAC;AAEH,MAAM,2BAA2B,GAAG,OAAC,CAAC,KAAK,CAAC;IAC1C,OAAC,CAAC,MAAM,EAAE;IACV,OAAC,CAAC,MAAM,EAAE;IACV,OAAC,CAAC,MAAM,EAAE;IACV,OAAC,CAAC,MAAM,EAAE;IACV,OAAC,CAAC,MAAM,EAAE;IACV,OAAC,CAAC,MAAM,EAAE;CACX,CAAC,CAAC;AAEH,MAAM,iBAAiB,GAAG,OAAC,CAAC,KAAK,CAAC;IAChC,yBAAyB;IACzB,2BAA2B;CAC5B,CAAC,CAAC;AAIH,SAAgB,WAAW;IACzB,OAAO,iBAAiB,CAAC;AAC3B,CAAC;AAFD,kCAEC"}
1
+ {"version":3,"file":"bounding-box.js","sourceRoot":"","sources":["../../../src/geojson/bounding-box.ts"],"names":[],"mappings":";;AAyBA,kCAEC;AA3BD,6BAAwB;AAExB,MAAM,yBAAyB,GAAG,OAAC,CAAC,KAAK,CAAC;IACxC,OAAC,CAAC,MAAM,EAAE;IACV,OAAC,CAAC,MAAM,EAAE;IACV,OAAC,CAAC,MAAM,EAAE;IACV,OAAC,CAAC,MAAM,EAAE;CACX,CAAC,CAAC;AAEH,MAAM,2BAA2B,GAAG,OAAC,CAAC,KAAK,CAAC;IAC1C,OAAC,CAAC,MAAM,EAAE;IACV,OAAC,CAAC,MAAM,EAAE;IACV,OAAC,CAAC,MAAM,EAAE;IACV,OAAC,CAAC,MAAM,EAAE;IACV,OAAC,CAAC,MAAM,EAAE;IACV,OAAC,CAAC,MAAM,EAAE;CACX,CAAC,CAAC;AAEH,MAAM,iBAAiB,GAAG,OAAC,CAAC,KAAK,CAAC;IAChC,yBAAyB;IACzB,2BAA2B;CAC5B,CAAC,CAAC;AAIH,SAAgB,WAAW;IACzB,OAAO,iBAAiB,CAAC;AAC3B,CAAC"}
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.coordinate = void 0;
3
+ exports.coordinate = coordinate;
4
4
  const zod_1 = require("zod");
5
5
  const twoDimensionalCoordinate = zod_1.z.tuple([zod_1.z.number(), zod_1.z.number()]);
6
6
  const threeDimensionalCoordinate = zod_1.z.tuple([
@@ -12,5 +12,4 @@ const schema = zod_1.z.union([twoDimensionalCoordinate, threeDimensionalCoordina
12
12
  function coordinate() {
13
13
  return schema;
14
14
  }
15
- exports.coordinate = coordinate;
16
15
  //# sourceMappingURL=coordinate.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"coordinate.js","sourceRoot":"","sources":["../../../src/geojson/coordinate.ts"],"names":[],"mappings":";;;AAAA,6BAAwB;AAExB,MAAM,wBAAwB,GAAG,OAAC,CAAC,KAAK,CAAC,CAAC,OAAC,CAAC,MAAM,EAAE,EAAE,OAAC,CAAC,MAAM,EAAE,CAAC,CAAC,CAAC;AACnE,MAAM,0BAA0B,GAAG,OAAC,CAAC,KAAK,CAAC;IACzC,OAAC,CAAC,MAAM,EAAE;IACV,OAAC,CAAC,MAAM,EAAE;IACV,OAAC,CAAC,MAAM,EAAE;CACX,CAAC,CAAC;AAEH,MAAM,MAAM,GAAG,OAAC,CAAC,KAAK,CAAC,CAAC,wBAAwB,EAAE,0BAA0B,CAAC,CAAC,CAAC;AAI/E,SAAgB,UAAU;IACxB,OAAO,MAAM,CAAC;AAChB,CAAC;AAFD,gCAEC"}
1
+ {"version":3,"file":"coordinate.js","sourceRoot":"","sources":["../../../src/geojson/coordinate.ts"],"names":[],"mappings":";;AAaA,gCAEC;AAfD,6BAAwB;AAExB,MAAM,wBAAwB,GAAG,OAAC,CAAC,KAAK,CAAC,CAAC,OAAC,CAAC,MAAM,EAAE,EAAE,OAAC,CAAC,MAAM,EAAE,CAAC,CAAC,CAAC;AACnE,MAAM,0BAA0B,GAAG,OAAC,CAAC,KAAK,CAAC;IACzC,OAAC,CAAC,MAAM,EAAE;IACV,OAAC,CAAC,MAAM,EAAE;IACV,OAAC,CAAC,MAAM,EAAE;CACX,CAAC,CAAC;AAEH,MAAM,MAAM,GAAG,OAAC,CAAC,KAAK,CAAC,CAAC,wBAAwB,EAAE,0BAA0B,CAAC,CAAC,CAAC;AAI/E,SAAgB,UAAU;IACxB,OAAO,MAAM,CAAC;AAChB,CAAC"}
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.featureCollection = void 0;
3
+ exports.featureCollection = featureCollection;
4
4
  const zod_1 = require("zod");
5
5
  const base_js_1 = require("./base.js");
6
6
  const feature_js_1 = require("./feature.js");
@@ -11,5 +11,4 @@ const featureCollectionSchema = base_js_1.schemaWithBoundingBox.extend({
11
11
  function featureCollection() {
12
12
  return featureCollectionSchema;
13
13
  }
14
- exports.featureCollection = featureCollection;
15
14
  //# sourceMappingURL=feature-collection.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"feature-collection.js","sourceRoot":"","sources":["../../../src/geojson/feature-collection.ts"],"names":[],"mappings":";;;AAAA,6BAAwB;AACxB,uCAAkD;AAClD,6CAAuC;AAEvC,MAAM,uBAAuB,GAAG,+BAAqB,CAAC,MAAM,CAAC;IAC3D,IAAI,EAAE,OAAC,CAAC,OAAO,CAAC,mBAAmB,CAAC;IACpC,QAAQ,EAAE,OAAC,CAAC,KAAK,CAAC,IAAA,oBAAO,GAAE,CAAC;CAC7B,CAAC,CAAC;AAIH,SAAgB,iBAAiB;IAC/B,OAAO,uBAAuB,CAAC;AACjC,CAAC;AAFD,8CAEC"}
1
+ {"version":3,"file":"feature-collection.js","sourceRoot":"","sources":["../../../src/geojson/feature-collection.ts"],"names":[],"mappings":";;AAWA,8CAEC;AAbD,6BAAwB;AACxB,uCAAkD;AAClD,6CAAuC;AAEvC,MAAM,uBAAuB,GAAG,+BAAqB,CAAC,MAAM,CAAC;IAC3D,IAAI,EAAE,OAAC,CAAC,OAAO,CAAC,mBAAmB,CAAC;IACpC,QAAQ,EAAE,OAAC,CAAC,KAAK,CAAC,IAAA,oBAAO,GAAE,CAAC;CAC7B,CAAC,CAAC;AAIH,SAAgB,iBAAiB;IAC/B,OAAO,uBAAuB,CAAC;AACjC,CAAC"}
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.feature = void 0;
3
+ exports.feature = feature;
4
4
  const zod_1 = require("zod");
5
5
  const index_js_1 = require("../json/index.js");
6
6
  const base_js_1 = require("./base.js");
@@ -14,5 +14,4 @@ const featureSchema = base_js_1.schemaWithBoundingBox.extend({
14
14
  function feature() {
15
15
  return featureSchema;
16
16
  }
17
- exports.feature = feature;
18
17
  //# sourceMappingURL=feature.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"feature.js","sourceRoot":"","sources":["../../../src/geojson/feature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1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.geojson = void 0;
3
+ exports.geojson = geojson;
4
4
  const zod_1 = require("zod");
5
5
  const feature_collection_js_1 = require("./feature-collection.js");
6
6
  const feature_js_1 = require("./feature.js");
@@ -9,5 +9,4 @@ const geojsonSchema = zod_1.z.union([(0, geometry_js_1.geometry)(), (0, feature_
9
9
  function geojson() {
10
10
  return geojsonSchema;
11
11
  }
12
- exports.geojson = geojson;
13
12
  //# sourceMappingURL=geojson.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"geojson.js","sourceRoot":"","sources":["../../../src/geojson/geojson.ts"],"names":[],"mappings":";;;AAAA,6BAAwB;AACxB,mEAA4D;AAC5D,6CAAuC;AACvC,+CAAyC;AAEzC,MAAM,aAAa,GAAG,OAAC,CAAC,KAAK,CAAC,CAAC,IAAA,sBAAQ,GAAE,EAAE,IAAA,oBAAO,GAAE,EAAE,IAAA,yCAAiB,GAAE,CAAC,CAAC,CAAC;AAI5E,SAAgB,OAAO;IACrB,OAAO,aAAa,CAAC;AACvB,CAAC;AAFD,0BAEC"}
1
+ {"version":3,"file":"geojson.js","sourceRoot":"","sources":["../../../src/geojson/geojson.ts"],"names":[],"mappings":";;AASA,0BAEC;AAXD,6BAAwB;AACxB,mEAA4D;AAC5D,6CAAuC;AACvC,+CAAyC;AAEzC,MAAM,aAAa,GAAG,OAAC,CAAC,KAAK,CAAC,CAAC,IAAA,sBAAQ,GAAE,EAAE,IAAA,oBAAO,GAAE,EAAE,IAAA,yCAAiB,GAAE,CAAC,CAAC,CAAC;AAI5E,SAAgB,OAAO;IACrB,OAAO,aAAa,CAAC;AACvB,CAAC"}
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.geometry = exports.geometryCollection = void 0;
3
+ exports.geometryCollection = geometryCollection;
4
+ exports.geometry = geometry;
4
5
  const zod_1 = require("zod");
5
6
  const base_js_1 = require("./base.js");
6
7
  const line_string_js_1 = require("./line-string.js");
@@ -16,7 +17,6 @@ const geometryCollectionSchema = base_js_1.schemaWithBoundingBox.extend({
16
17
  function geometryCollection() {
17
18
  return geometryCollectionSchema;
18
19
  }
19
- exports.geometryCollection = geometryCollection;
20
20
  const geometrySchema = zod_1.z.union([
21
21
  (0, line_string_js_1.lineString)(),
22
22
  (0, multi_line_string_js_1.multiLineString)(),
@@ -29,5 +29,4 @@ const geometrySchema = zod_1.z.union([
29
29
  function geometry() {
30
30
  return geometrySchema;
31
31
  }
32
- exports.geometry = geometry;
33
32
  //# sourceMappingURL=geometry.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"geometry.js","sourceRoot":"","sources":["../../../src/geojson/geometry.ts"],"names":[],"mappings":";;;AAAA,6BAAwB;AACxB,uCAAkD;AAClD,qDAA8C;AAC9C,iEAAyD;AACzD,qDAA8C;AAC9C,yDAAkD;AAClD,yCAAmC;AACnC,6CAAuC;AAEvC,MAAM,wBAAwB,GAC5B,+BAAqB,CAAC,MAAM,CAAC;IAC3B,IAAI,EAAE,OAAC,CAAC,OAAO,CAAC,oBAAoB,CAAC;IACrC,UAAU,EAAE,OAAC,CAAC,IAAI,CAAC,GAAG,EAAE,CAAC,OAAC,CAAC,KAAK,CAAC,QAAQ,EAAE,CAAC,CAAC;CAC9C,CAAC,CAAC;AAcL,SAAgB,kBAAkB;IAChC,OAAO,wBAAwB,CAAC;AAClC,CAAC;AAFD,gDAEC;AAED,MAAM,cAAc,GAAG,OAAC,CAAC,KAAK,CAAC;IAC7B,IAAA,2BAAU,GAAE;IACZ,IAAA,sCAAe,GAAE;IACjB,IAAA,2BAAU,GAAE;IACZ,IAAA,+BAAY,GAAE;IACd,IAAA,gBAAK,GAAE;IACP,IAAA,oBAAO,GAAE;IACT,kBAAkB,EAAE;CACrB,CAAC,CAAC;AAIH,SAAgB,QAAQ;IACtB,OAAO,cAAc,CAAC;AACxB,CAAC;AAFD,4BAEC"}
1
+ {"version":3,"file":"geometry.js","sourceRoot":"","sources":["../../../src/geojson/geometry.ts"],"names":[],"mappings":";;AA2BA,gDAEC;AAcD,4BAEC;AA7CD,6BAAwB;AACxB,uCAAkD;AAClD,qDAA8C;AAC9C,iEAAyD;AACzD,qDAA8C;AAC9C,yDAAkD;AAClD,yCAAmC;AACnC,6CAAuC;AAEvC,MAAM,wBAAwB,GAC5B,+BAAqB,CAAC,MAAM,CAAC;IAC3B,IAAI,EAAE,OAAC,CAAC,OAAO,CAAC,oBAAoB,CAAC;IACrC,UAAU,EAAE,OAAC,CAAC,IAAI,CAAC,GAAG,EAAE,CAAC,OAAC,CAAC,KAAK,CAAC,QAAQ,EAAE,CAAC,CAAC;CAC9C,CAAC,CAAC;AAcL,SAAgB,kBAAkB;IAChC,OAAO,wBAAwB,CAAC;AAClC,CAAC;AAED,MAAM,cAAc,GAAG,OAAC,CAAC,KAAK,CAAC;IAC7B,IAAA,2BAAU,GAAE;IACZ,IAAA,sCAAe,GAAE;IACjB,IAAA,2BAAU,GAAE;IACZ,IAAA,+BAAY,GAAE;IACd,IAAA,gBAAK,GAAE;IACP,IAAA,oBAAO,GAAE;IACT,kBAAkB,EAAE;CACrB,CAAC,CAAC;AAIH,SAAgB,QAAQ;IACtB,OAAO,cAAc,CAAC;AACxB,CAAC"}
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.lineString = void 0;
3
+ exports.lineString = lineString;
4
4
  const zod_1 = require("zod");
5
5
  const base_js_1 = require("./base.js");
6
6
  const coordinate_js_1 = require("./coordinate.js");
@@ -11,5 +11,4 @@ const lineStringSchema = base_js_1.schemaWithBoundingBox.extend({
11
11
  function lineString() {
12
12
  return lineStringSchema;
13
13
  }
14
- exports.lineString = lineString;
15
14
  //# sourceMappingURL=line-string.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"line-string.js","sourceRoot":"","sources":["../../../src/geojson/line-string.ts"],"names":[],"mappings":";;;AAAA,6BAAwB;AACxB,uCAAkD;AAClD,mDAA6C;AAE7C,MAAM,gBAAgB,GAAG,+BAAqB,CAAC,MAAM,CAAC;IACpD,IAAI,EAAE,OAAC,CAAC,OAAO,CAAC,YAAY,CAAC;IAC7B,WAAW,EAAE,OAAC,CAAC,KAAK,CAAC,IAAA,0BAAU,GAAE,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC;CAC1C,CAAC,CAAC;AAIH,SAAgB,UAAU;IACxB,OAAO,gBAAgB,CAAC;AAC1B,CAAC;AAFD,gCAEC"}
1
+ {"version":3,"file":"line-string.js","sourceRoot":"","sources":["../../../src/geojson/line-string.ts"],"names":[],"mappings":";;AAWA,gCAEC;AAbD,6BAAwB;AACxB,uCAAkD;AAClD,mDAA6C;AAE7C,MAAM,gBAAgB,GAAG,+BAAqB,CAAC,MAAM,CAAC;IACpD,IAAI,EAAE,OAAC,CAAC,OAAO,CAAC,YAAY,CAAC;IAC7B,WAAW,EAAE,OAAC,CAAC,KAAK,CAAC,IAAA,0BAAU,GAAE,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC;CAC1C,CAAC,CAAC;AAIH,SAAgB,UAAU;IACxB,OAAO,gBAAgB,CAAC;AAC1B,CAAC"}
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.multiLineString = void 0;
3
+ exports.multiLineString = multiLineString;
4
4
  const zod_1 = require("zod");
5
5
  const base_js_1 = require("./base.js");
6
6
  const line_string_js_1 = require("./line-string.js");
@@ -11,5 +11,4 @@ const multiLineStringSchema = base_js_1.schemaWithBoundingBox.extend({
11
11
  function multiLineString() {
12
12
  return multiLineStringSchema;
13
13
  }
14
- exports.multiLineString = multiLineString;
15
14
  //# sourceMappingURL=multi-line-string.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"multi-line-string.js","sourceRoot":"","sources":["../../../src/geojson/multi-line-string.ts"],"names":[],"mappings":";;;AAAA,6BAAwB;AACxB,uCAAkD;AAClD,qDAA8C;AAE9C,MAAM,qBAAqB,GAAG,+BAAqB,CAAC,MAAM,CAAC;IACzD,IAAI,EAAE,OAAC,CAAC,OAAO,CAAC,iBAAiB,CAAC;IAClC,WAAW,EAAE,OAAC,CAAC,KAAK,CAAC,IAAA,2BAAU,GAAE,CAAC,KAAK,CAAC,WAAW,CAAC;CACrD,CAAC,CAAC;AAIH,SAAgB,eAAe;IAC7B,OAAO,qBAAqB,CAAC;AAC/B,CAAC;AAFD,0CAEC"}
1
+ {"version":3,"file":"multi-line-string.js","sourceRoot":"","sources":["../../../src/geojson/multi-line-string.ts"],"names":[],"mappings":";;AAWA,0CAEC;AAbD,6BAAwB;AACxB,uCAAkD;AAClD,qDAA8C;AAE9C,MAAM,qBAAqB,GAAG,+BAAqB,CAAC,MAAM,CAAC;IACzD,IAAI,EAAE,OAAC,CAAC,OAAO,CAAC,iBAAiB,CAAC;IAClC,WAAW,EAAE,OAAC,CAAC,KAAK,CAAC,IAAA,2BAAU,GAAE,CAAC,KAAK,CAAC,WAAW,CAAC;CACrD,CAAC,CAAC;AAIH,SAAgB,eAAe;IAC7B,OAAO,qBAAqB,CAAC;AAC/B,CAAC"}
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.multiPoint = void 0;
3
+ exports.multiPoint = multiPoint;
4
4
  const zod_1 = require("zod");
5
5
  const base_js_1 = require("./base.js");
6
6
  const point_js_1 = require("./point.js");
@@ -11,5 +11,4 @@ const multiPointSchema = base_js_1.schemaWithBoundingBox.extend({
11
11
  function multiPoint() {
12
12
  return multiPointSchema;
13
13
  }
14
- exports.multiPoint = multiPoint;
15
14
  //# sourceMappingURL=multi-point.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"multi-point.js","sourceRoot":"","sources":["../../../src/geojson/multi-point.ts"],"names":[],"mappings":";;;AAAA,6BAAwB;AACxB,uCAAkD;AAClD,yCAAmC;AAEnC,MAAM,gBAAgB,GAAG,+BAAqB,CAAC,MAAM,CAAC;IACpD,IAAI,EAAE,OAAC,CAAC,OAAO,CAAC,YAAY,CAAC;IAC7B,WAAW,EAAE,OAAC,CAAC,KAAK,CAAC,IAAA,gBAAK,GAAE,CAAC,KAAK,CAAC,WAAW,CAAC;CAChD,CAAC,CAAC;AAIH,SAAgB,UAAU;IACxB,OAAO,gBAAgB,CAAC;AAC1B,CAAC;AAFD,gCAEC"}
1
+ {"version":3,"file":"multi-point.js","sourceRoot":"","sources":["../../../src/geojson/multi-point.ts"],"names":[],"mappings":";;AAWA,gCAEC;AAbD,6BAAwB;AACxB,uCAAkD;AAClD,yCAAmC;AAEnC,MAAM,gBAAgB,GAAG,+BAAqB,CAAC,MAAM,CAAC;IACpD,IAAI,EAAE,OAAC,CAAC,OAAO,CAAC,YAAY,CAAC;IAC7B,WAAW,EAAE,OAAC,CAAC,KAAK,CAAC,IAAA,gBAAK,GAAE,CAAC,KAAK,CAAC,WAAW,CAAC;CAChD,CAAC,CAAC;AAIH,SAAgB,UAAU;IACxB,OAAO,gBAAgB,CAAC;AAC1B,CAAC"}
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.multiPolygon = void 0;
3
+ exports.multiPolygon = multiPolygon;
4
4
  const zod_1 = require("zod");
5
5
  const base_js_1 = require("./base.js");
6
6
  const polygon_js_1 = require("./polygon.js");
@@ -11,5 +11,4 @@ const multiPolygonSchema = base_js_1.schemaWithBoundingBox.extend({
11
11
  function multiPolygon() {
12
12
  return multiPolygonSchema;
13
13
  }
14
- exports.multiPolygon = multiPolygon;
15
14
  //# sourceMappingURL=multi-polygon.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"multi-polygon.js","sourceRoot":"","sources":["../../../src/geojson/multi-polygon.ts"],"names":[],"mappings":";;;AAAA,6BAAwB;AACxB,uCAAkD;AAClD,6CAAuC;AAEvC,MAAM,kBAAkB,GAAG,+BAAqB,CAAC,MAAM,CAAC;IACtD,IAAI,EAAE,OAAC,CAAC,OAAO,CAAC,cAAc,CAAC;IAC/B,WAAW,EAAE,OAAC,CAAC,KAAK,CAAC,IAAA,oBAAO,GAAE,CAAC,KAAK,CAAC,WAAW,CAAC;CAClD,CAAC,CAAC;AAIH,SAAgB,YAAY;IAC1B,OAAO,kBAAkB,CAAC;AAC5B,CAAC;AAFD,oCAEC"}
1
+ {"version":3,"file":"multi-polygon.js","sourceRoot":"","sources":["../../../src/geojson/multi-polygon.ts"],"names":[],"mappings":";;AAWA,oCAEC;AAbD,6BAAwB;AACxB,uCAAkD;AAClD,6CAAuC;AAEvC,MAAM,kBAAkB,GAAG,+BAAqB,CAAC,MAAM,CAAC;IACtD,IAAI,EAAE,OAAC,CAAC,OAAO,CAAC,cAAc,CAAC;IAC/B,WAAW,EAAE,OAAC,CAAC,KAAK,CAAC,IAAA,oBAAO,GAAE,CAAC,KAAK,CAAC,WAAW,CAAC;CAClD,CAAC,CAAC;AAIH,SAAgB,YAAY;IAC1B,OAAO,kBAAkB,CAAC;AAC5B,CAAC"}
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.point = void 0;
3
+ exports.point = point;
4
4
  const zod_1 = require("zod");
5
5
  const base_js_1 = require("./base.js");
6
6
  const coordinate_js_1 = require("./coordinate.js");
@@ -11,5 +11,4 @@ const pointSchema = base_js_1.schemaWithBoundingBox.extend({
11
11
  function point() {
12
12
  return pointSchema;
13
13
  }
14
- exports.point = point;
15
14
  //# sourceMappingURL=point.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"point.js","sourceRoot":"","sources":["../../../src/geojson/point.ts"],"names":[],"mappings":";;;AAAA,6BAAwB;AACxB,uCAAkD;AAClD,mDAA6C;AAE7C,MAAM,WAAW,GAAG,+BAAqB,CAAC,MAAM,CAAC;IAC/C,IAAI,EAAE,OAAC,CAAC,OAAO,CAAC,OAAO,CAAC;IACxB,WAAW,EAAE,IAAA,0BAAU,GAAE;CAC1B,CAAC,CAAC;AAIH,SAAgB,KAAK;IACnB,OAAO,WAAW,CAAC;AACrB,CAAC;AAFD,sBAEC"}
1
+ {"version":3,"file":"point.js","sourceRoot":"","sources":["../../../src/geojson/point.ts"],"names":[],"mappings":";;AAWA,sBAEC;AAbD,6BAAwB;AACxB,uCAAkD;AAClD,mDAA6C;AAE7C,MAAM,WAAW,GAAG,+BAAqB,CAAC,MAAM,CAAC;IAC/C,IAAI,EAAE,OAAC,CAAC,OAAO,CAAC,OAAO,CAAC;IACxB,WAAW,EAAE,IAAA,0BAAU,GAAE;CAC1B,CAAC,CAAC;AAIH,SAAgB,KAAK;IACnB,OAAO,WAAW,CAAC;AACrB,CAAC"}
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.polygon = void 0;
3
+ exports.polygon = polygon;
4
4
  const zod_1 = require("zod");
5
5
  const base_js_1 = require("./base.js");
6
6
  const coordinate_js_1 = require("./coordinate.js");
@@ -11,5 +11,4 @@ const polygonSchema = base_js_1.schemaWithBoundingBox.extend({
11
11
  function polygon() {
12
12
  return polygonSchema;
13
13
  }
14
- exports.polygon = polygon;
15
14
  //# sourceMappingURL=polygon.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"polygon.js","sourceRoot":"","sources":["../../../src/geojson/polygon.ts"],"names":[],"mappings":";;;AAAA,6BAAwB;AACxB,uCAAkD;AAClD,mDAA6C;AAE7C,MAAM,aAAa,GAAG,+BAAqB,CAAC,MAAM,CAAC;IACjD,IAAI,EAAE,OAAC,CAAC,OAAO,CAAC,SAAS,CAAC;IAC1B,WAAW,EAAE,OAAC,CAAC,KAAK,CAAC,OAAC,CAAC,KAAK,CAAC,IAAA,0BAAU,GAAE,CAAC,CAAC;CAC5C,CAAC,CAAC;AAIH,SAAgB,OAAO;IACrB,OAAO,aAAa,CAAC;AACvB,CAAC;AAFD,0BAEC"}
1
+ {"version":3,"file":"polygon.js","sourceRoot":"","sources":["../../../src/geojson/polygon.ts"],"names":[],"mappings":";;AAWA,0BAEC;AAbD,6BAAwB;AACxB,uCAAkD;AAClD,mDAA6C;AAE7C,MAAM,aAAa,GAAG,+BAAqB,CAAC,MAAM,CAAC;IACjD,IAAI,EAAE,OAAC,CAAC,OAAO,CAAC,SAAS,CAAC;IAC1B,WAAW,EAAE,OAAC,CAAC,KAAK,CAAC,OAAC,CAAC,KAAK,CAAC,IAAA,0BAAU,GAAE,CAAC,CAAC;CAC5C,CAAC,CAAC;AAIH,SAAgB,OAAO;IACrB,OAAO,aAAa,CAAC;AACvB,CAAC"}
@@ -1,5 +1,40 @@
1
1
  import { z } from "zod";
2
+ import { TypeGuard } from "@infra-blocks/types";
3
+ /**
4
+ * Returns a string schema that transforms its input using a string split on commas.
5
+ *
6
+ * @returns A CSV schema.
7
+ */
2
8
  declare function csv(): z.ZodPipe<z.ZodString, z.ZodTransform<string[], string>>;
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
+ declare function typeGuard<S extends z.ZodType>(schema: S): TypeGuard<z.infer<S>>;
25
+ /**
26
+ * Validates that the value satisfies the provided schema.
27
+ *
28
+ * Upon success, the value's type is narrowed to the schema's output type.
29
+ *
30
+ * It uses `safeParse` internally.
31
+ *
32
+ * @param schema - The schema to validate against.
33
+ * @param value - The value to validate.
34
+ *
35
+ * @returns Whether the value satisfies the schema.
36
+ */
37
+ declare function validate<S extends z.ZodType>(schema: S, value: unknown): value is z.infer<S>;
3
38
  declare const zu: {
4
39
  geojson: {
5
40
  (): z.ZodUnion<readonly [z.ZodUnion<readonly [z.ZodObject<{
@@ -111,5 +146,7 @@ declare const zu: {
111
146
  stringified: typeof import("./json/json.js").stringifiedJson;
112
147
  };
113
148
  csv: typeof csv;
149
+ typeGuard: typeof typeGuard;
150
+ validate: typeof validate;
114
151
  };
115
152
  export { zu };
package/lib/cjs/index.js CHANGED
@@ -5,13 +5,56 @@ const zod_1 = require("zod");
5
5
  const index_js_1 = require("./json/index.js");
6
6
  const index_js_2 = require("./geojson/index.js");
7
7
  const csvSchema = zod_1.z.string().transform((str) => str.split(","));
8
+ csvSchema.brand("Toto");
9
+ /**
10
+ * Returns a string schema that transforms its input using a string split on commas.
11
+ *
12
+ * @returns A CSV schema.
13
+ */
8
14
  function csv() {
9
15
  return csvSchema;
10
16
  }
17
+ /**
18
+ * Returns a type guard function using the provided schema as the source of truth.
19
+ *
20
+ * It uses the `validate` function internally.
21
+ *
22
+ * @param schema - The schema used to verify the inputs will be of the correct type.
23
+ *
24
+ * @returns A type guard function that uses the provided schema to asserts whether the
25
+ * input is of the proper type or not.
26
+ *
27
+ * @note Type guards work best with branded types. Given two schemas, z.string().min(5),
28
+ * and z.string().min(5).brand("Min5String"), the former type guard would only assert
29
+ * that the input is string (even if the length check is also done), whereas in the
30
+ * former case, that information is also carried with the type.
31
+ */
32
+ function typeGuard(schema) {
33
+ return (value) => {
34
+ return validate(schema, value);
35
+ };
36
+ }
37
+ /**
38
+ * Validates that the value satisfies the provided schema.
39
+ *
40
+ * Upon success, the value's type is narrowed to the schema's output type.
41
+ *
42
+ * It uses `safeParse` internally.
43
+ *
44
+ * @param schema - The schema to validate against.
45
+ * @param value - The value to validate.
46
+ *
47
+ * @returns Whether the value satisfies the schema.
48
+ */
49
+ function validate(schema, value) {
50
+ return schema.safeParse(value).success;
51
+ }
11
52
  const zu = {
12
53
  geojson: index_js_2.geojson,
13
54
  json: index_js_1.json,
14
55
  csv,
56
+ typeGuard,
57
+ validate,
15
58
  };
16
59
  exports.zu = zu;
17
60
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;AAAA,6BAAwB;AACxB,8CAAuC;AACvC,iDAA6C;AAE7C,MAAM,SAAS,GAAG,OAAC,CAAC,MAAM,EAAE,CAAC,SAAS,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,GAAG,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC;AAEhE,SAAS,GAAG;IACV,OAAO,SAAS,CAAC;AACnB,CAAC;AAED,MAAM,EAAE,GAAG;IACT,OAAO,EAAP,kBAAO;IACP,IAAI,EAAJ,eAAI;IACJ,GAAG;CACJ,CAAC;AAEO,gBAAE"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;AAAA,6BAAwB;AACxB,8CAAuC;AACvC,iDAA6C;AAG7C,MAAM,SAAS,GAAG,OAAC,CAAC,MAAM,EAAE,CAAC,SAAS,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,GAAG,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC;AAEhE,SAAS,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC;AAExB;;;;GAIG;AACH,SAAS,GAAG;IACV,OAAO,SAAS,CAAC;AACnB,CAAC;AAED;;;;;;;;;;;;;;GAcG;AACH,SAAS,SAAS,CAAsB,MAAS;IAC/C,OAAO,CAAC,KAAc,EAAuB,EAAE;QAC7C,OAAO,QAAQ,CAAC,MAAM,EAAE,KAAK,CAAC,CAAC;IACjC,CAAC,CAAC;AACJ,CAAC;AAED;;;;;;;;;;;GAWG;AACH,SAAS,QAAQ,CACf,MAAS,EACT,KAAc;IAEd,OAAO,MAAM,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC,OAAO,CAAC;AACzC,CAAC;AAED,MAAM,EAAE,GAAG;IACT,OAAO,EAAP,kBAAO;IACP,IAAI,EAAJ,eAAI;IACJ,GAAG;IACH,SAAS;IACT,QAAQ;CACT,CAAC;AAEO,gBAAE"}
@@ -1,6 +1,11 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.stringifiedJson = exports.stringifiedJsonSchema = exports.array = exports.object = exports.json = exports.primitive = void 0;
3
+ exports.stringifiedJsonSchema = void 0;
4
+ exports.primitive = primitive;
5
+ exports.json = json;
6
+ exports.object = object;
7
+ exports.array = array;
8
+ exports.stringifiedJson = stringifiedJson;
4
9
  const zod_1 = require("zod");
5
10
  const primitiveSchema = zod_1.z.union([
6
11
  zod_1.z.string(),
@@ -11,22 +16,18 @@ const primitiveSchema = zod_1.z.union([
11
16
  function primitive() {
12
17
  return primitiveSchema;
13
18
  }
14
- exports.primitive = primitive;
15
19
  const jsonSchema = zod_1.z.lazy(() => zod_1.z.union([primitive(), zod_1.z.array(jsonSchema), zod_1.z.record(zod_1.z.string(), jsonSchema)]));
16
20
  function json() {
17
21
  return jsonSchema;
18
22
  }
19
- exports.json = json;
20
23
  const objectSchema = zod_1.z.record(zod_1.z.string(), jsonSchema);
21
24
  function object() {
22
25
  return objectSchema;
23
26
  }
24
- exports.object = object;
25
27
  const arraySchema = zod_1.z.array(jsonSchema);
26
28
  function array() {
27
29
  return arraySchema;
28
30
  }
29
- exports.array = array;
30
31
  exports.stringifiedJsonSchema = zod_1.z
31
32
  .string()
32
33
  .transform((str, ctx) => {
@@ -41,5 +42,4 @@ exports.stringifiedJsonSchema = zod_1.z
41
42
  function stringifiedJson() {
42
43
  return exports.stringifiedJsonSchema;
43
44
  }
44
- exports.stringifiedJson = stringifiedJson;
45
45
  //# sourceMappingURL=json.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"json.js","sourceRoot":"","sources":["../../../src/json/json.ts"],"names":[],"mappings":";;;AAAA,6BAAwB;AAExB,MAAM,eAAe,GAAG,OAAC,CAAC,KAAK,CAAC;IAC9B,OAAC,CAAC,MAAM,EAAE;IACV,OAAC,CAAC,MAAM,EAAE;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CAAC,CAAC;QAC1D,OAAO,OAAC,CAAC,KAAK,CAAC;IACjB,CAAC;AACH,CAAC,CAAC,CAAC;AAEL,SAAgB,eAAe;IAC7B,OAAO,6BAAqB,CAAC;AAC/B,CAAC"}
@@ -1,5 +1,40 @@
1
1
  import { z } from "zod";
2
+ import { TypeGuard } from "@infra-blocks/types";
3
+ /**
4
+ * Returns a string schema that transforms its input using a string split on commas.
5
+ *
6
+ * @returns A CSV schema.
7
+ */
2
8
  declare function csv(): z.ZodPipe<z.ZodString, z.ZodTransform<string[], string>>;
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
+ declare function typeGuard<S extends z.ZodType>(schema: S): TypeGuard<z.infer<S>>;
25
+ /**
26
+ * Validates that the value satisfies the provided schema.
27
+ *
28
+ * Upon success, the value's type is narrowed to the schema's output type.
29
+ *
30
+ * It uses `safeParse` internally.
31
+ *
32
+ * @param schema - The schema to validate against.
33
+ * @param value - The value to validate.
34
+ *
35
+ * @returns Whether the value satisfies the schema.
36
+ */
37
+ declare function validate<S extends z.ZodType>(schema: S, value: unknown): value is z.infer<S>;
3
38
  declare const zu: {
4
39
  geojson: {
5
40
  (): z.ZodUnion<readonly [z.ZodUnion<readonly [z.ZodObject<{
@@ -111,5 +146,7 @@ declare const zu: {
111
146
  stringified: typeof import("./json/json.js").stringifiedJson;
112
147
  };
113
148
  csv: typeof csv;
149
+ typeGuard: typeof typeGuard;
150
+ validate: typeof validate;
114
151
  };
115
152
  export { zu };
package/lib/esm/index.js CHANGED
@@ -2,13 +2,56 @@ import { z } from "zod";
2
2
  import { json } from "./json/index.js";
3
3
  import { geojson } from "./geojson/index.js";
4
4
  const csvSchema = z.string().transform((str) => str.split(","));
5
+ csvSchema.brand("Toto");
6
+ /**
7
+ * Returns a string schema that transforms its input using a string split on commas.
8
+ *
9
+ * @returns A CSV schema.
10
+ */
5
11
  function csv() {
6
12
  return csvSchema;
7
13
  }
14
+ /**
15
+ * Returns a type guard function using the provided schema as the source of truth.
16
+ *
17
+ * It uses the `validate` function internally.
18
+ *
19
+ * @param schema - The schema used to verify the inputs will be of the correct type.
20
+ *
21
+ * @returns A type guard function that uses the provided schema to asserts whether the
22
+ * input is of the proper type or not.
23
+ *
24
+ * @note Type guards work best with branded types. Given two schemas, z.string().min(5),
25
+ * and z.string().min(5).brand("Min5String"), the former type guard would only assert
26
+ * that the input is string (even if the length check is also done), whereas in the
27
+ * former case, that information is also carried with the type.
28
+ */
29
+ function typeGuard(schema) {
30
+ return (value) => {
31
+ return validate(schema, value);
32
+ };
33
+ }
34
+ /**
35
+ * Validates that the value satisfies the provided schema.
36
+ *
37
+ * Upon success, the value's type is narrowed to the schema's output type.
38
+ *
39
+ * It uses `safeParse` internally.
40
+ *
41
+ * @param schema - The schema to validate against.
42
+ * @param value - The value to validate.
43
+ *
44
+ * @returns Whether the value satisfies the schema.
45
+ */
46
+ function validate(schema, value) {
47
+ return schema.safeParse(value).success;
48
+ }
8
49
  const zu = {
9
50
  geojson,
10
51
  json,
11
52
  csv,
53
+ typeGuard,
54
+ validate,
12
55
  };
13
56
  export { zu };
14
57
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AACxB,OAAO,EAAE,IAAI,EAAE,MAAM,iBAAiB,CAAC;AACvC,OAAO,EAAE,OAAO,EAAE,MAAM,oBAAoB,CAAC;AAE7C,MAAM,SAAS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C;QACH,OAAO,IAAI,CAAC,KAAK,CAAC,GAAG,CAAS,CAAC;IACjC,CAAC;IAAC,OAAO,CAAC,EAAE,CAAC;QACX,GAAG,CAAC,QAAQ,CAAC,EAAE,IAAI,EAAE,QAAQ,EAAE,OAAO,EAAE,cAAc,EAAE,CAAC,CAAC;QAC1D,OAAO,CAAC,CAAC,KAAK,CAAC;IACjB,CAAC;AACH,CAAC,CAAC,CAAC;AAEL,MAAM,UAAU,eAAe;IAC7B,OAAO,qBAAqB,CAAC;AAC/B,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@infra-blocks/zod-utils",
3
- "version": "0.5.0-alpha.3",
3
+ "version": "0.6.0-alpha.0",
4
4
  "description": "Extensions to the zod package.",
5
5
  "keywords": [
6
6
  "zod",
@@ -50,10 +50,11 @@
50
50
  "test:unit": "mocha --config test/unit/.mocharc.cjs 'test/unit/**/*.spec.ts'"
51
51
  },
52
52
  "dependencies": {
53
+ "@infra-blocks/types": "^0.24.0",
53
54
  "zod": "^4.2.1"
54
55
  },
55
56
  "devDependencies": {
56
- "@infra-blocks/test": "^0.3.1",
57
+ "@infra-blocks/test": "^0.6.0",
57
58
  "@types/lodash": "^4.17.0",
58
59
  "@types/mocha": "^10.0.1",
59
60
  "@types/node": "^20.10.3",
@@ -68,7 +69,7 @@
68
69
  "mocha": "^10.2.0",
69
70
  "prettier": "^2.8.8",
70
71
  "ts-node": "^10.9.1",
71
- "typescript": "^5.0.4"
72
+ "typescript": "^5.9.3"
72
73
  },
73
74
  "engines": {
74
75
  "node": ">=18.0.0"