@clipboard-health/contract-core 3.0.0 → 3.1.1
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 +15 -0
- package/package.json +2 -2
- package/src/lib/schemas/enum.d.ts +6 -5
- package/src/lib/schemas/enum.js.map +1 -1
package/README.md
CHANGED
|
@@ -37,6 +37,21 @@ This package provides four enum validation helpers to cover different use cases:
|
|
|
37
37
|
- `requiredEnum(values)` - Wraps `z.enum()` for required strict validation. Invalid values fail validation.
|
|
38
38
|
- `optionalEnum(values)` - Wraps `z.enum()` for optional strict validation. Invalid values fail validation, but `undefined` is allowed.
|
|
39
39
|
|
|
40
|
+
**Type narrowing:** All helpers reject widened `string[]` arrays at compile time. When passing a pre-declared variable, use `as const` to preserve literal types:
|
|
41
|
+
|
|
42
|
+
```ts
|
|
43
|
+
// Inline arrays work as-is
|
|
44
|
+
requiredEnum(["a", "b"]);
|
|
45
|
+
|
|
46
|
+
// Pre-declared variables require `as const`
|
|
47
|
+
const VALUES = ["a", "b"] as const;
|
|
48
|
+
requiredEnum(VALUES);
|
|
49
|
+
|
|
50
|
+
// Without `as const`, the type widens to string[] and is rejected
|
|
51
|
+
const widened = ["a", "b"];
|
|
52
|
+
requiredEnum(widened); // TS error
|
|
53
|
+
```
|
|
54
|
+
|
|
40
55
|
<embedex source="packages/contract-core/examples/schemas.ts">
|
|
41
56
|
|
|
42
57
|
```ts
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@clipboard-health/contract-core",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.1.1",
|
|
4
4
|
"description": "Shared Zod schemas for Clipboard's contracts.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"contract",
|
|
@@ -25,7 +25,7 @@
|
|
|
25
25
|
"tslib": "2.8.1"
|
|
26
26
|
},
|
|
27
27
|
"devDependencies": {
|
|
28
|
-
"@clipboard-health/testing-core": "2.2.
|
|
28
|
+
"@clipboard-health/testing-core": "2.2.89",
|
|
29
29
|
"zod": "3.25.76"
|
|
30
30
|
},
|
|
31
31
|
"peerDependencies": {
|
|
@@ -1,16 +1,17 @@
|
|
|
1
1
|
import { z } from "zod";
|
|
2
2
|
type EnumValues = readonly [string, ...string[]];
|
|
3
|
+
type NarrowEnum<V extends EnumValues> = string extends V[number] ? never : V;
|
|
3
4
|
/**
|
|
4
5
|
* A business-context-neutral sentinel returned when an enum field
|
|
5
6
|
* receives a value the consumer does not recognize.
|
|
6
7
|
*/
|
|
7
8
|
export declare const ENUM_FALLBACK: "UNRECOGNIZED_";
|
|
8
9
|
type EnumFallback = typeof ENUM_FALLBACK;
|
|
9
|
-
export declare function enumWithFallback<const V extends EnumValues>(values: V
|
|
10
|
+
export declare function enumWithFallback<const V extends EnumValues>(values: NarrowEnum<V>, options: {
|
|
10
11
|
optional: true;
|
|
11
12
|
}): z.ZodEffects<z.ZodOptional<z.ZodEnum<[...V, EnumFallback]>>, V[number] | EnumFallback | undefined, unknown>;
|
|
12
|
-
export declare function requiredEnumWithFallback<const V extends EnumValues>(values: V): z.ZodEffects<z.ZodEnum<[...V, "UNRECOGNIZED_"]>, "UNRECOGNIZED_" | V[number], unknown>;
|
|
13
|
-
export declare function optionalEnumWithFallback<const V extends EnumValues>(values: V): z.ZodEffects<z.ZodOptional<z.ZodEnum<[...V, "UNRECOGNIZED_"]>>, "UNRECOGNIZED_" | V[number] | undefined, unknown>;
|
|
14
|
-
export declare function requiredEnum<const V extends EnumValues>(values: V): z.ZodEnum<[...V]>;
|
|
15
|
-
export declare function optionalEnum<const V extends EnumValues>(values: V): z.ZodOptional<z.ZodEnum<[...V]>>;
|
|
13
|
+
export declare function requiredEnumWithFallback<const V extends EnumValues>(values: NarrowEnum<V>): z.ZodEffects<z.ZodEnum<[...V, "UNRECOGNIZED_"]>, "UNRECOGNIZED_" | V[number], unknown>;
|
|
14
|
+
export declare function optionalEnumWithFallback<const V extends EnumValues>(values: NarrowEnum<V>): z.ZodEffects<z.ZodOptional<z.ZodEnum<[...V, "UNRECOGNIZED_"]>>, "UNRECOGNIZED_" | V[number] | undefined, unknown>;
|
|
15
|
+
export declare function requiredEnum<const V extends EnumValues>(values: NarrowEnum<V>): z.ZodEnum<[...V]>;
|
|
16
|
+
export declare function optionalEnum<const V extends EnumValues>(values: NarrowEnum<V>): z.ZodOptional<z.ZodEnum<[...V]>>;
|
|
16
17
|
export {};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"enum.js","sourceRoot":"","sources":["../../../../../../packages/contract-core/src/lib/schemas/enum.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"enum.js","sourceRoot":"","sources":["../../../../../../packages/contract-core/src/lib/schemas/enum.ts"],"names":[],"mappings":";;;AAkCA,4CAuBC;AAED,4DAEC;AAED,4DAEC;AAED,oCAEC;AAED,oCAIC;AA3ED,6BAAwB;AAKxB;;;GAGG;AACU,QAAA,aAAa,GAAG,eAAwB,CAAC;AAyBtD,SAAgB,gBAAgB,CAC9B,MAAS,EACT,UAAkC,EAAE;IAEpC,IAAK,MAA4B,CAAC,QAAQ,CAAC,qBAAa,CAAC,EAAE,CAAC;QAC1D,MAAM,IAAI,KAAK,CACb,iCAAiC,qBAAa,kCAAkC,CACjF,CAAC;IACJ,CAAC;IAED,MAAM,YAAY,GAAG,OAAC,CAAC,IAAI,CAAC,CAAC,GAAG,MAAM,CAAC,CAAC,CAAC;IACzC,MAAM,YAAY,GAAG,OAAC,CAAC,IAAI,CAAC,CAAC,GAAG,MAAM,EAAE,qBAAa,CAAC,CAAC,CAAC;IACxD,MAAM,QAAQ,GAAG,OAAO,CAAC,QAAQ,IAAI,KAAK,CAAC;IAC3C,MAAM,MAAM,GAAG,QAAQ,CAAC,CAAC,CAAC,YAAY,CAAC,QAAQ,EAAE,CAAC,CAAC,CAAC,YAAY,CAAC;IAEjE,+DAA+D;IAC/D,OAAO,OAAC,CAAC,UAAU,CAA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