@guillaume-docquier/tools-ts 9.0.0 → 10.0.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
@@ -35,6 +35,17 @@ external APIs, CLI input, local storage, or other unknown data.
35
35
  | `getEnumKey` | Looks up the enum key associated with a value. | You have an enum value and need the more descriptive key for display, logs, or diagnostics. |
36
36
  | `asArray` | Normalizes a single value or array into an array. | An API accepts both `T` and `T[]`, but downstream logic should iterate uniformly. |
37
37
 
38
+ ### Zod Parsing
39
+
40
+ Import these tools from `@guillaume-docquier/tools-ts/schemas`. Install Zod in the consuming project to use this optional subpath; the root entry point does not load Zod.
41
+
42
+ | Export | What it is | Use it when |
43
+ | ----------------------------------------- | -------------------------------------------------------------------------------- | ------------------------------------------------------------------ |
44
+ | typedParse | Parses a typed, deeply unbranded value with a Zod schema and returns its output. | Invalid data is an internal invariant violation that should throw. |
45
+ | safeTypedParse | The non-throwing counterpart, returning a Zod safe parse result. | Validation may fail and the caller will handle the result. |
46
+ | IntegerSchema and `Integer` | Parses and brands whole numbers. | A value must be an integer, including zero or a negative integer. |
47
+ | PositiveNumberSchema and `PositiveNumber` | Parses and brands numbers greater than zero. | A value must be positive but may be fractional. |
48
+
38
49
  ### Numeric Utilities
39
50
 
40
51
  Use these tools when code needs reusable numeric operations or validated
@@ -0,0 +1,3 @@
1
+ export { IntegerSchema, type Integer } from "./schemas/Integer.js";
2
+ export { PositiveNumberSchema, type PositiveNumber } from "./schemas/PositiveNumber.js";
3
+ export { typedParse, safeTypedParse } from "./schemas/typedParse.js";
@@ -0,0 +1,4 @@
1
+ export { IntegerSchema } from "./schemas/Integer.js";
2
+ export { PositiveNumberSchema } from "./schemas/PositiveNumber.js";
3
+ export { typedParse, safeTypedParse } from "./schemas/typedParse.js";
4
+ //# sourceMappingURL=entry.schemas.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"entry.schemas.js","sourceRoot":"","sources":["../src/entry.schemas.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAgB,MAAM,sBAAsB,CAAA;AAClE,OAAO,EAAE,oBAAoB,EAAuB,MAAM,6BAA6B,CAAA;AACvF,OAAO,EAAE,UAAU,EAAE,cAAc,EAAE,MAAM,yBAAyB,CAAA"}
@@ -0,0 +1,10 @@
1
+ import { z } from "zod";
2
+ import { type Branded } from "../Brand.js";
3
+ /**
4
+ * A whole number, including zero and negative numbers.
5
+ */
6
+ export type Integer = Branded<"Integer", number>;
7
+ /**
8
+ * Parses a number as an integer.
9
+ */
10
+ export declare const IntegerSchema: z.ZodPipe<z.ZodInt, z.ZodTransform<Integer, number>>;
@@ -0,0 +1,7 @@
1
+ import { z } from "zod";
2
+ import { branded } from "../Brand.js";
3
+ /**
4
+ * Parses a number as an integer.
5
+ */
6
+ export const IntegerSchema = z.int().transform(branded);
7
+ //# sourceMappingURL=Integer.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Integer.js","sourceRoot":"","sources":["../../src/schemas/Integer.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAA;AACvB,OAAO,EAAE,OAAO,EAAgB,MAAM,aAAa,CAAA;AAOnD;;GAEG;AACH,MAAM,CAAC,MAAM,aAAa,GAAG,CAAC,CAAC,GAAG,EAAE,CAAC,SAAS,CAAC,OAAgB,CAAC,CAAA"}
@@ -0,0 +1,10 @@
1
+ import { z } from "zod";
2
+ import { type Branded } from "../Brand.js";
3
+ /**
4
+ * A number greater than zero; zero is neither positive nor negative.
5
+ */
6
+ export type PositiveNumber = Branded<"PositiveNumber", number>;
7
+ /**
8
+ * Parses a number greater than zero, including positive fractions.
9
+ */
10
+ export declare const PositiveNumberSchema: z.ZodPipe<z.ZodNumber, z.ZodTransform<PositiveNumber, number>>;
@@ -0,0 +1,10 @@
1
+ import { z } from "zod";
2
+ import { branded } from "../Brand.js";
3
+ /**
4
+ * Parses a number greater than zero, including positive fractions.
5
+ */
6
+ export const PositiveNumberSchema = z
7
+ .number()
8
+ .positive()
9
+ .transform(branded);
10
+ //# sourceMappingURL=PositiveNumber.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"PositiveNumber.js","sourceRoot":"","sources":["../../src/schemas/PositiveNumber.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAA;AACvB,OAAO,EAAE,OAAO,EAAgB,MAAM,aAAa,CAAA;AAOnD;;GAEG;AACH,MAAM,CAAC,MAAM,oBAAoB,GAAG,CAAC;KAClC,MAAM,EAAE;KACR,QAAQ,EAAE;KACV,SAAS,CAAC,OAAuB,CAAC,CAAA"}
@@ -0,0 +1,29 @@
1
+ import type { z } from "zod";
2
+ import type { DeepUnbranded } from "../Brand.js";
3
+ /**
4
+ * We use zod schemas on unknown inputs to parse & validate the data.
5
+ * However, when you have already parsed data (usually because it is trusted data) and want to validate, using schema.parse is bad because the parameter is `unknown`, so you don't get feedback from TypeScript.
6
+ *
7
+ * Use this `typedParse` method instead, which accepts as input a strongly typed but unbranded version of the data and outputs a branded version of it, after validation.
8
+ *
9
+ * This is really just a schema.parse() with type safe inputs when you only need to run the validation part of the schema.
10
+ *
11
+ * @example
12
+ * ```ts
13
+ * typedParse(IntegerSchema, 42) // ✓
14
+ * typedParse(IntegerSchema, 42.5) // throws
15
+ * typedParse(IntegerSchema, "42") // TS error
16
+ * ```
17
+ */
18
+ export declare function typedParse<TSchema extends z.ZodType>(schema: TSchema, value: DeepUnbranded<z.output<TSchema>>): z.output<TSchema>;
19
+ /**
20
+ * The safeParse counterpart to {@link typedParse}
21
+ *
22
+ * @example
23
+ * ```ts
24
+ * safeTypedParse(IntegerSchema, 42) // ✓
25
+ * safeTypedParse(IntegerSchema, 42.5) // returns success = false
26
+ * safeTypedParse(IntegerSchema, "42") // TS error
27
+ * ```
28
+ */
29
+ export declare function safeTypedParse<TSchema extends z.ZodType>(schema: TSchema, value: DeepUnbranded<z.output<TSchema>>): z.ZodSafeParseResult<z.output<TSchema>>;
@@ -0,0 +1,32 @@
1
+ /**
2
+ * We use zod schemas on unknown inputs to parse & validate the data.
3
+ * However, when you have already parsed data (usually because it is trusted data) and want to validate, using schema.parse is bad because the parameter is `unknown`, so you don't get feedback from TypeScript.
4
+ *
5
+ * Use this `typedParse` method instead, which accepts as input a strongly typed but unbranded version of the data and outputs a branded version of it, after validation.
6
+ *
7
+ * This is really just a schema.parse() with type safe inputs when you only need to run the validation part of the schema.
8
+ *
9
+ * @example
10
+ * ```ts
11
+ * typedParse(IntegerSchema, 42) // ✓
12
+ * typedParse(IntegerSchema, 42.5) // throws
13
+ * typedParse(IntegerSchema, "42") // TS error
14
+ * ```
15
+ */
16
+ export function typedParse(schema, value) {
17
+ return schema.parse(value);
18
+ }
19
+ /**
20
+ * The safeParse counterpart to {@link typedParse}
21
+ *
22
+ * @example
23
+ * ```ts
24
+ * safeTypedParse(IntegerSchema, 42) // ✓
25
+ * safeTypedParse(IntegerSchema, 42.5) // returns success = false
26
+ * safeTypedParse(IntegerSchema, "42") // TS error
27
+ * ```
28
+ */
29
+ export function safeTypedParse(schema, value) {
30
+ return schema.safeParse(value);
31
+ }
32
+ //# sourceMappingURL=typedParse.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"typedParse.js","sourceRoot":"","sources":["../../src/schemas/typedParse.ts"],"names":[],"mappings":"AAGA;;;;;;;;;;;;;;GAcG;AACH,MAAM,UAAU,UAAU,CAA4B,MAAe,EAAE,KAAuC;IAC5G,OAAO,MAAM,CAAC,KAAK,CAAC,KAAK,CAAC,CAAA;AAC5B,CAAC;AAED;;;;;;;;;GASG;AACH,MAAM,UAAU,cAAc,CAC5B,MAAe,EACf,KAAuC;IAEvC,OAAO,MAAM,CAAC,SAAS,CAAC,KAAK,CAAC,CAAA;AAChC,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@guillaume-docquier/tools-ts",
3
- "version": "9.0.0",
3
+ "version": "10.0.0",
4
4
  "description": "My personal collection of typescript tools",
5
5
  "homepage": "https://github.com/Guillaume-Docquier/tools-ts",
6
6
  "license": "MIT",
@@ -18,6 +18,11 @@
18
18
  "types": "./dist/entry.tools-ts.d.ts",
19
19
  "import": "./dist/entry.tools-ts.js",
20
20
  "require": "./dist/entry.tools-ts.js"
21
+ },
22
+ "./schemas": {
23
+ "types": "./dist/entry.schemas.d.ts",
24
+ "import": "./dist/entry.schemas.js",
25
+ "require": "./dist/entry.schemas.js"
21
26
  }
22
27
  },
23
28
  "publishConfig": {
@@ -47,7 +52,16 @@
47
52
  "semantic-release": "^25.0.9",
48
53
  "typescript": "~7.0.2",
49
54
  "vite": "^8.3.0",
50
- "vitest": "^5.0.0"
55
+ "vitest": "^5.0.0",
56
+ "zod": "^4.6.5"
57
+ },
58
+ "peerDependencies": {
59
+ "zod": "^4.6.5"
60
+ },
61
+ "peerDependenciesMeta": {
62
+ "zod": {
63
+ "optional": true
64
+ }
51
65
  },
52
66
  "lint-staged": {
53
67
  "*.{md,json,yml,yaml,css,scss}": "oxfmt --no-error-on-unmatched-pattern",