@clipboard-health/contract-core 1.5.10 → 1.5.12

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
@@ -22,8 +22,15 @@ npm install @clipboard-health/contract-core
22
22
  <embedex source="packages/contract-core/examples/schemas.ts">
23
23
 
24
24
  ```ts
25
- import { apiErrors, booleanString, nonEmptyString, uuid } from "@clipboard-health/contract-core";
26
- import { type ZodError } from "zod";
25
+ import {
26
+ apiErrors,
27
+ booleanString,
28
+ nonEmptyString,
29
+ optionalEnumWithFallback,
30
+ requiredEnumWithFallback,
31
+ uuid,
32
+ } from "@clipboard-health/contract-core";
33
+ import { type z, type ZodError } from "zod";
27
34
 
28
35
  function logError(error: unknown) {
29
36
  console.error((error as ZodError).issues[0]!.message);
@@ -66,6 +73,52 @@ try {
66
73
  logError(error);
67
74
  // => Invalid UUID format
68
75
  }
76
+
77
+ // Enum with fallback examples
78
+ /* -- required -- */
79
+ const requiredStatusEnumSchema = requiredEnumWithFallback(
80
+ ["unspecified", "pending", "completed", "failed"],
81
+ "unspecified",
82
+ );
83
+ // type RequiredStatusEnum = "unspecified" | "pending" | "completed" | "failed"
84
+ type RequiredStatusEnum = z.infer<typeof requiredStatusEnumSchema>;
85
+
86
+ const completedStatus: RequiredStatusEnum = requiredStatusEnumSchema.parse("completed");
87
+ // => "completed"
88
+ console.log(completedStatus);
89
+
90
+ const additionalStatus = requiredStatusEnumSchema.parse("additional");
91
+ // => "unspecified"
92
+ console.log(additionalStatus);
93
+
94
+ try {
95
+ // eslint-disable-next-line unicorn/no-useless-undefined
96
+ requiredStatusEnumSchema.parse(undefined);
97
+ } catch (error) {
98
+ logError(error);
99
+ // => Validation error
100
+ }
101
+
102
+ /* -- optional -- */
103
+ const optionalStatusEnumSchema = optionalEnumWithFallback(
104
+ ["unspecified", "pending", "completed", "failed"],
105
+ "unspecified",
106
+ );
107
+ // type OptionalStatusEnum = "unspecified" | "pending" | "completed" | "failed" | undefined
108
+ type OptionalStatusEnum = z.infer<typeof optionalStatusEnumSchema>;
109
+
110
+ const failedStatus: OptionalStatusEnum = optionalStatusEnumSchema.parse("failed");
111
+ // => "failed"
112
+ console.log(failedStatus);
113
+
114
+ const extraStatus = optionalStatusEnumSchema.parse("extra");
115
+ // => "unspecified"
116
+ console.log(extraStatus);
117
+
118
+ // eslint-disable-next-line unicorn/no-useless-undefined
119
+ const undefinedStatus = optionalStatusEnumSchema.parse(undefined);
120
+ // => undefined
121
+ console.log(undefinedStatus);
69
122
  ```
70
123
 
71
124
  </embedex>
package/package.json CHANGED
@@ -1,13 +1,13 @@
1
1
  {
2
2
  "name": "@clipboard-health/contract-core",
3
3
  "description": "Shared Zod schemas for Clipboard's contracts.",
4
- "version": "1.5.10",
4
+ "version": "1.5.12",
5
5
  "bugs": "https://github.com/ClipboardHealth/core-utils/issues",
6
6
  "dependencies": {
7
7
  "tslib": "2.8.1"
8
8
  },
9
9
  "devDependencies": {
10
- "@clipboard-health/testing-core": "1.5.10",
10
+ "@clipboard-health/testing-core": "1.5.12",
11
11
  "zod": "3.25.76"
12
12
  },
13
13
  "keywords": [
@@ -0,0 +1,8 @@
1
+ import { z } from "zod";
2
+ type EnumValues = [string, ...string[]];
3
+ export declare function enumWithFallback<const V extends EnumValues, const F extends V[number]>(values: V, fallback: F, options: {
4
+ optional: true;
5
+ }): z.ZodEffects<z.ZodOptional<z.ZodEnum<V>>, V[number] | undefined, unknown>;
6
+ export declare const optionalEnumWithFallback: <const V extends EnumValues, const F extends V[number]>(values: V, fallback: F) => z.ZodEffects<z.ZodOptional<z.ZodEnum<V>>, V[number] | undefined, unknown>;
7
+ export declare const requiredEnumWithFallback: <const V extends EnumValues, const F extends V[number]>(values: V, fallback: F) => z.ZodEffects<z.ZodEnum<V>, V[number], unknown>;
8
+ export {};
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.requiredEnumWithFallback = exports.optionalEnumWithFallback = void 0;
4
+ exports.enumWithFallback = enumWithFallback;
5
+ const zod_1 = require("zod");
6
+ function enumWithFallback(values, fallback, options = {}) {
7
+ const Enum = zod_1.z.enum(values);
8
+ const optional = options.optional ?? false;
9
+ const schema = optional ? Enum.optional() : Enum;
10
+ return zod_1.z.preprocess((value) => {
11
+ if (value === undefined) {
12
+ return optional ? undefined : value;
13
+ }
14
+ return Enum.safeParse(value).success ? value : fallback;
15
+ }, schema);
16
+ }
17
+ const optionalEnumWithFallback = (values, fallback) => enumWithFallback(values, fallback, { optional: true });
18
+ exports.optionalEnumWithFallback = optionalEnumWithFallback;
19
+ const requiredEnumWithFallback = (values, fallback) => enumWithFallback(values, fallback, { optional: false });
20
+ exports.requiredEnumWithFallback = requiredEnumWithFallback;
21
+ //# sourceMappingURL=enum.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"enum.js","sourceRoot":"","sources":["../../../../../../packages/contract-core/src/lib/schemas/enum.ts"],"names":[],"mappings":";;;AAwBA,4CAeC;AAvCD,6BAAwB;AAwBxB,SAAgB,gBAAgB,CAC9B,MAAS,EACT,QAAW,EACX,UAAkC,EAAE;IAEpC,MAAM,IAAI,GAAG,OAAC,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;IAC5B,MAAM,QAAQ,GAAG,OAAO,CAAC,QAAQ,IAAI,KAAK,CAAC;IAC3C,MAAM,MAAM,GAAG,QAAQ,CAAC,CAAC,CAAC,IAAI,CAAC,QAAQ,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC;IAEjD,OAAO,OAAC,CAAC,UAAU,CAAC,CAAC,KAAK,EAAE,EAAE;QAC5B,IAAI,KAAK,KAAK,SAAS,EAAE,CAAC;YACxB,OAAO,QAAQ,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,KAAK,CAAC;QACtC,CAAC;QACD,OAAO,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,QAAQ,CAAC;IAC1D,CAAC,EAAE,MAAM,CAAC,CAAC;AACb,CAAC;AAEM,MAAM,wBAAwB,GAAG,CACtC,MAAS,EACT,QAAW,EACX,EAAE,CAAC,gBAAgB,CAAC,MAAM,EAAE,QAAQ,EAAE,EAAE,QAAQ,EAAE,IAAI,EAAE,CAAC,CAAC;AAH/C,QAAA,wBAAwB,4BAGuB;AAErD,MAAM,wBAAwB,GAAG,CACtC,MAAS,EACT,QAAW,EACX,EAAE,CAAC,gBAAgB,CAAC,MAAM,EAAE,QAAQ,EAAE,EAAE,QAAQ,EAAE,KAAK,EAAE,CAAC,CAAC;AAHhD,QAAA,wBAAwB,4BAGwB"}
@@ -1,5 +1,6 @@
1
1
  export * from "./apiError";
2
2
  export * from "./booleanString";
3
+ export * from "./enum";
3
4
  export * from "./money";
4
5
  export * from "./nonEmptyString";
5
6
  export * from "./objectId";
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  const tslib_1 = require("tslib");
4
4
  tslib_1.__exportStar(require("./apiError"), exports);
5
5
  tslib_1.__exportStar(require("./booleanString"), exports);
6
+ tslib_1.__exportStar(require("./enum"), exports);
6
7
  tslib_1.__exportStar(require("./money"), exports);
7
8
  tslib_1.__exportStar(require("./nonEmptyString"), exports);
8
9
  tslib_1.__exportStar(require("./objectId"), exports);
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../../../packages/contract-core/src/lib/schemas/index.ts"],"names":[],"mappings":";;;AAAA,qDAA2B;AAC3B,0DAAgC;AAChC,kDAAwB;AACxB,2DAAiC;AACjC,qDAA2B;AAC3B,iDAAuB"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../../../packages/contract-core/src/lib/schemas/index.ts"],"names":[],"mappings":";;;AAAA,qDAA2B;AAC3B,0DAAgC;AAChC,iDAAuB;AACvB,kDAAwB;AACxB,2DAAiC;AACjC,qDAA2B;AAC3B,iDAAuB"}