@clipboard-health/contract-core 0.1.1 → 0.2.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
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
# @clipboard-health/contract-core <!-- omit from toc -->
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Shared Zod schemas for Clipboard Health's contracts.
|
|
4
4
|
|
|
5
5
|
## Table of contents <!-- omit from toc -->
|
|
6
6
|
|
|
7
7
|
- [Install](#install)
|
|
8
8
|
- [Usage](#usage)
|
|
9
|
+
- [Zod schemas](#zod-schemas)
|
|
9
10
|
- [Local development commands](#local-development-commands)
|
|
10
11
|
|
|
11
12
|
## Install
|
|
@@ -16,6 +17,59 @@ npm install @clipboard-health/contract-core
|
|
|
16
17
|
|
|
17
18
|
## Usage
|
|
18
19
|
|
|
20
|
+
### Zod schemas
|
|
21
|
+
|
|
22
|
+
<!-- prettier-ignore -->
|
|
23
|
+
```ts
|
|
24
|
+
// ./examples/schemas.ts
|
|
25
|
+
|
|
26
|
+
import { apiErrors, booleanString, nonEmptyString, uuid } from "@clipboard-health/contract-core";
|
|
27
|
+
import { type ZodError } from "zod";
|
|
28
|
+
|
|
29
|
+
function logError(error: unknown) {
|
|
30
|
+
console.error((error as ZodError).issues[0]!.message);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
apiErrors.parse({
|
|
34
|
+
errors: [
|
|
35
|
+
{
|
|
36
|
+
code: "NotFound",
|
|
37
|
+
detail: "Resource 'b146a790-9ed1-499f-966d-6c4905dc667f' not found",
|
|
38
|
+
id: "6191a8a0-96ff-4d4b-8e0f-746a5ab215f9",
|
|
39
|
+
status: "404",
|
|
40
|
+
title: "Not Found",
|
|
41
|
+
},
|
|
42
|
+
],
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
booleanString.parse("true");
|
|
46
|
+
|
|
47
|
+
try {
|
|
48
|
+
booleanString.parse("invalid");
|
|
49
|
+
} catch (error) {
|
|
50
|
+
logError(error);
|
|
51
|
+
// => Invalid enum value. Expected 'true' | 'false', received 'invalid'
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
nonEmptyString.parse("hello");
|
|
55
|
+
try {
|
|
56
|
+
nonEmptyString.parse("");
|
|
57
|
+
} catch (error) {
|
|
58
|
+
logError(error);
|
|
59
|
+
// => String must contain at least 1 character(s)
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
// UUID validation examples
|
|
63
|
+
uuid.parse("b8d617bb-edef-4262-a6e3-6cc807fa1b26");
|
|
64
|
+
try {
|
|
65
|
+
uuid.parse("invalid");
|
|
66
|
+
} catch (error) {
|
|
67
|
+
logError(error);
|
|
68
|
+
// => Invalid UUID format
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
```
|
|
72
|
+
|
|
19
73
|
## Local development commands
|
|
20
74
|
|
|
21
75
|
See [`package.json`](./package.json) `scripts` for a list of commands.
|
package/package.json
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@clipboard-health/contract-core",
|
|
3
|
-
"description": "
|
|
4
|
-
"version": "0.
|
|
3
|
+
"description": "Shared Zod schemas for Clipboard Health's contracts.",
|
|
4
|
+
"version": "0.2.0",
|
|
5
5
|
"dependencies": {
|
|
6
6
|
"tslib": "2.8.0",
|
|
7
7
|
"zod": "3.23.8"
|
|
8
8
|
},
|
|
9
9
|
"devDependencies": {
|
|
10
|
-
"@clipboard-health/testing-core": "0.
|
|
10
|
+
"@clipboard-health/testing-core": "0.2.0"
|
|
11
11
|
},
|
|
12
12
|
"keywords": [],
|
|
13
13
|
"license": "MIT",
|
|
@@ -15,6 +15,9 @@
|
|
|
15
15
|
"publishConfig": {
|
|
16
16
|
"access": "public"
|
|
17
17
|
},
|
|
18
|
+
"scripts": {
|
|
19
|
+
"embed": "embedme README.md"
|
|
20
|
+
},
|
|
18
21
|
"type": "commonjs",
|
|
19
22
|
"typings": "./src/index.d.ts",
|
|
20
23
|
"types": "./src/index.d.ts"
|
|
@@ -2,7 +2,7 @@ import { z } from "zod";
|
|
|
2
2
|
/**
|
|
3
3
|
* @see {@link https://jsonapi.org/format/#error-objects Error Objects}
|
|
4
4
|
*/
|
|
5
|
-
declare const apiError: z.ZodObject<{
|
|
5
|
+
export declare const apiError: z.ZodObject<{
|
|
6
6
|
code: z.ZodString;
|
|
7
7
|
detail: z.ZodOptional<z.ZodString>;
|
|
8
8
|
id: z.ZodString;
|
|
@@ -65,4 +65,3 @@ export declare const apiErrors: z.ZodObject<{
|
|
|
65
65
|
* @see {@link https://jsonapi.org/format/#error-objects Error Objects}
|
|
66
66
|
*/
|
|
67
67
|
export type ApiError = z.infer<typeof apiError>;
|
|
68
|
-
export {};
|
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.apiErrors = void 0;
|
|
3
|
+
exports.apiErrors = exports.apiError = void 0;
|
|
4
4
|
const zod_1 = require("zod");
|
|
5
5
|
const nonEmptyString_1 = require("./nonEmptyString");
|
|
6
6
|
/**
|
|
7
7
|
* @see {@link https://jsonapi.org/format/#error-objects Error Objects}
|
|
8
8
|
*/
|
|
9
|
-
|
|
9
|
+
exports.apiError = zod_1.z.object({
|
|
10
10
|
code: nonEmptyString_1.nonEmptyString,
|
|
11
11
|
detail: zod_1.z.string().optional(),
|
|
12
12
|
id: nonEmptyString_1.nonEmptyString,
|
|
@@ -17,6 +17,6 @@ const apiError = zod_1.z.object({
|
|
|
17
17
|
* @see {@link https://jsonapi.org/format/#errors Errors}
|
|
18
18
|
*/
|
|
19
19
|
exports.apiErrors = zod_1.z.object({
|
|
20
|
-
errors: apiError.array().min(0).max(100),
|
|
20
|
+
errors: exports.apiError.array().min(0).max(100),
|
|
21
21
|
});
|
|
22
22
|
//# sourceMappingURL=apiError.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"apiError.js","sourceRoot":"","sources":["../../../../../../packages/contract-core/src/lib/schemas/apiError.ts"],"names":[],"mappings":";;;AAAA,6BAAwB;AAExB,qDAAkD;AAElD;;GAEG;
|
|
1
|
+
{"version":3,"file":"apiError.js","sourceRoot":"","sources":["../../../../../../packages/contract-core/src/lib/schemas/apiError.ts"],"names":[],"mappings":";;;AAAA,6BAAwB;AAExB,qDAAkD;AAElD;;GAEG;AACU,QAAA,QAAQ,GAAG,OAAC,CAAC,MAAM,CAAC;IAC/B,IAAI,EAAE,+BAAc;IACpB,MAAM,EAAE,OAAC,CAAC,MAAM,EAAE,CAAC,QAAQ,EAAE;IAC7B,EAAE,EAAE,+BAAc;IAClB,MAAM,EAAE,+BAAc;IACtB,KAAK,EAAE,+BAAc;CACtB,CAAC,CAAC;AAEH;;GAEG;AACU,QAAA,SAAS,GAAG,OAAC,CAAC,MAAM,CAAC;IAChC,MAAM,EAAE,gBAAQ,CAAC,KAAK,EAAE,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,GAAG,CAAC;CACzC,CAAC,CAAC"}
|
package/src/lib/schemas/uuid.js
CHANGED
|
@@ -10,5 +10,7 @@ exports.uuid = zod_1.z
|
|
|
10
10
|
.string()
|
|
11
11
|
.trim()
|
|
12
12
|
// eslint-disable-next-line unicorn/better-regex
|
|
13
|
-
.regex(/^[0-9A-F]{8}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{12}$/i
|
|
13
|
+
.regex(/^[0-9A-F]{8}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{12}$/i, {
|
|
14
|
+
message: "Invalid UUID format",
|
|
15
|
+
});
|
|
14
16
|
//# sourceMappingURL=uuid.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"uuid.js","sourceRoot":"","sources":["../../../../../../packages/contract-core/src/lib/schemas/uuid.ts"],"names":[],"mappings":";;;AAAA,6BAAwB;AAExB;;;GAGG;AACU,QAAA,IAAI,GAAG,OAAC;KAClB,MAAM,EAAE;KACR,IAAI,EAAE;IACP,gDAAgD;KAC/C,KAAK,CAAC,iEAAiE,CAAC,CAAC"}
|
|
1
|
+
{"version":3,"file":"uuid.js","sourceRoot":"","sources":["../../../../../../packages/contract-core/src/lib/schemas/uuid.ts"],"names":[],"mappings":";;;AAAA,6BAAwB;AAExB;;;GAGG;AACU,QAAA,IAAI,GAAG,OAAC;KAClB,MAAM,EAAE;KACR,IAAI,EAAE;IACP,gDAAgD;KAC/C,KAAK,CAAC,iEAAiE,EAAE;IACxE,OAAO,EAAE,qBAAqB;CAC/B,CAAC,CAAC"}
|