-
Notifications
You must be signed in to change notification settings - Fork 4
/
MetadataAttribute.ts
168 lines (159 loc) · 3.58 KB
/
MetadataAttribute.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import { z } from 'zod';
import { nonEmptyStringSchema } from './primitives.js';
/**
* The type of a metadata attribute.
*/
export enum MetadataAttributeType {
BOOLEAN = 'Boolean',
DATE = 'Date',
NUMBER = 'Number',
STRING = 'String',
JSON = 'JSON',
}
export type BooleanAttribute = {
/**
* A JS boolean value serialized as string. It's consumer responsibility to parse it.
*/
value: 'true' | 'false';
/**
* Union discriminant.
*/
type: MetadataAttributeType.BOOLEAN;
/**
* The attribute's unique identifier.
*/
key: string;
};
/**
* @internal
*/
export const BooleanAttributeSchema = z.object({
type: z.literal(MetadataAttributeType.BOOLEAN),
key: nonEmptyStringSchema("The attribute's unique identifier."),
value: z
.enum(['true', 'false'])
.describe("A JS boolean value serialized as string. It's consumer responsibility to parse it."),
});
export type DateAttribute = {
/**
* A valid ISO 8601 date string. It's consumer responsibility to parse it.
*/
value: string;
/**
* Union discriminant.
*/
type: MetadataAttributeType.DATE;
/**
* The attribute's unique identifier.
*/
key: string;
};
/**
* @internal
*/
export const DateAttributeSchema = z.object({
type: z.literal(MetadataAttributeType.DATE),
key: nonEmptyStringSchema("The attribute's unique identifier."),
value: z
.string()
.datetime()
.describe("A valid ISO 8601 date string. It's consumer responsibility to parse it."),
});
export type NumberAttribute = {
/**
* A valid JS number serialized as string. It's consumer responsibility to parse it.
*
* @example
* ```ts
* '42'
*
* '42n'
*
* '42.42'
* ```
*/
value: string;
/**
* Union discriminant.
*/
type: MetadataAttributeType.NUMBER;
/**
* The attribute's unique identifier.
*/
key: string;
};
/**
* @internal
*/
export const NumberAttributeSchema = z.object({
type: z.literal(MetadataAttributeType.NUMBER),
key: nonEmptyStringSchema("The attribute's unique identifier."), // TODO generalize and share
value: nonEmptyStringSchema(
"A valid JS number serialized as string. It's consumer responsibility to parse it.",
),
});
export type StringAttribute = {
/**
* Any string value.
*/
value: string;
/**
* Union discriminant.
*/
type: MetadataAttributeType.STRING;
/**
* The attribute's unique identifier.
*/
key: string;
};
/**
* @internal
*/
export const StringAttributeSchema = z.object({
type: z.literal(MetadataAttributeType.STRING),
key: nonEmptyStringSchema("The attribute's unique identifier."),
value: nonEmptyStringSchema('A string value.'),
});
export type JSONAttribute = {
/**
* A JSON string. It's consumer responsibility to validate and parse it.
*/
value: string;
/**
* Union discriminant.
*/
type: MetadataAttributeType.JSON;
/**
* Union discriminant.
*/
/**
* The attribute's unique identifier.
*/
key: string;
};
/**
* @internal
*/
export const JSONAttributeSchema = z.object({
type: z.literal(MetadataAttributeType.JSON),
key: nonEmptyStringSchema("The attribute's unique identifier."),
value: nonEmptyStringSchema(
"A JSON string. It's consumer responsibility to validate and parse it.",
),
});
/**
* A Lens metadata attribute.
*/
export type MetadataAttribute =
| BooleanAttribute
| DateAttribute
| NumberAttribute
| StringAttribute
| JSONAttribute;
export const MetadataAttributeSchema = z.discriminatedUnion('type', [
BooleanAttributeSchema,
DateAttributeSchema,
NumberAttributeSchema,
StringAttributeSchema,
JSONAttributeSchema,
]);