Skip to content

Commit 70c68dd

Browse files
committed
Remove excessive raw schemas and simplify composite tokens resolving
1 parent b98c345 commit 70c68dd

File tree

2 files changed

+177
-298
lines changed

2 files changed

+177
-298
lines changed

src/schema.ts

Lines changed: 106 additions & 179 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import {
55
dimensionValue,
66
durationValue,
77
fontFamilyValue,
8+
fontWeightValue,
9+
numberValue,
810
strokeStyleValue,
911
} from "./dtcg.schema";
1012

@@ -17,114 +19,65 @@ export type {
1719
StrokeStyleValue,
1820
} from "./dtcg.schema";
1921

20-
const ColorSchema = z.object({
22+
const colorSchema = z.object({
2123
type: z.literal("color"),
2224
value: colorValue,
2325
});
2426

25-
const RawColorSchema = z.object({
26-
type: z.literal("color"),
27-
value: z.union([colorValue, z.string()]),
28-
});
29-
30-
const DimensionSchema = z.object({
27+
const dimensionSchema = z.object({
3128
type: z.literal("dimension"),
3229
value: dimensionValue,
3330
});
3431

35-
const RawDimensionSchema = z.object({
36-
type: z.literal("dimension"),
37-
value: z.union([dimensionValue, z.string()]),
38-
});
39-
40-
const DurationSchema = z.object({
32+
const durationSchema = z.object({
4133
type: z.literal("duration"),
4234
value: durationValue,
4335
});
4436

45-
const RawDurationSchema = z.object({
46-
type: z.literal("duration"),
47-
value: z.union([durationValue, z.string()]),
48-
});
49-
50-
const NumberSchema = z.object({
37+
const numberSchema = z.object({
5138
type: z.literal("number"),
52-
value: z.number(),
39+
value: numberValue,
5340
});
5441

55-
const RawNumberSchema = z.object({
56-
type: z.literal("number"),
57-
value: z.union([z.number(), z.string()]),
58-
});
59-
60-
const CubicBezierSchema = z.object({
42+
const cubicBezierSchema = z.object({
6143
type: z.literal("cubicBezier"),
6244
value: cubicBezierValue,
6345
});
6446

65-
const RawCubicBezierSchema = z.object({
66-
type: z.literal("cubicBezier"),
67-
value: z.union([cubicBezierValue, z.string()]),
68-
});
69-
70-
const FontFamilySchema = z.object({
47+
const fontFamilySchema = z.object({
7148
type: z.literal("fontFamily"),
7249
value: fontFamilyValue,
7350
});
7451

75-
const RawFontFamilySchema = z.object({
76-
type: z.literal("fontFamily"),
77-
value: z.union([fontFamilyValue, z.string()]),
78-
});
79-
80-
const FontWeightValueSchema = z.union([z.number(), z.string()]);
81-
82-
const FontWeightSchema = z.object({
83-
type: z.literal("fontWeight"),
84-
value: FontWeightValueSchema,
85-
});
86-
87-
const RawFontWeightSchema = z.object({
52+
const fontWeightSchema = z.object({
8853
type: z.literal("fontWeight"),
89-
value: z.union([FontWeightValueSchema, z.string()]),
54+
value: fontWeightValue,
9055
});
9156

92-
const StrokeStyleSchema = z.object({
57+
const strokeStyleSchema = z.object({
9358
type: z.literal("strokeStyle"),
9459
value: strokeStyleValue,
9560
});
9661

97-
const RawStrokeStyleSchema = z.object({
98-
type: z.literal("strokeStyle"),
99-
value: z.union([strokeStyleValue, z.string()]),
100-
});
101-
102-
const TransitionValueSchema = z.object({
103-
duration: durationValue,
104-
delay: durationValue,
105-
timingFunction: cubicBezierValue,
106-
});
107-
108-
export type TransitionValue = z.infer<typeof TransitionValueSchema>;
109-
110-
const TransitionSchema = z.object({
62+
const transitionSchema = z.object({
11163
type: z.literal("transition"),
112-
value: TransitionValueSchema,
64+
value: z.object({
65+
duration: durationValue,
66+
delay: durationValue,
67+
timingFunction: cubicBezierValue,
68+
}),
11369
});
11470

115-
const RawTransitionSchema = z.object({
71+
const rawTransitionSchema = z.object({
11672
type: z.literal("transition"),
117-
value: z.union([
118-
z.object({
119-
duration: z.union([durationValue, z.string()]),
120-
delay: z.union([durationValue, z.string()]),
121-
timingFunction: z.union([cubicBezierValue, z.string()]),
122-
}),
123-
z.string(), // token reference
124-
]),
73+
value: z.object({
74+
duration: z.union([durationValue, z.string()]),
75+
delay: z.union([durationValue, z.string()]),
76+
timingFunction: z.union([cubicBezierValue, z.string()]),
77+
}),
12578
});
12679

127-
export const ShadowItemSchema = z.object({
80+
export const shadowItemSchema = z.object({
12881
color: colorValue,
12982
offsetX: dimensionValue,
13083
offsetY: dimensionValue,
@@ -133,18 +86,12 @@ export const ShadowItemSchema = z.object({
13386
inset: z.boolean().optional(),
13487
});
13588

136-
export type ShadowItem = z.infer<typeof ShadowItemSchema>;
137-
138-
const ShadowValueSchema = z.array(ShadowItemSchema);
139-
140-
export type ShadowValue = z.infer<typeof ShadowValueSchema>;
141-
142-
const ShadowSchema = z.object({
89+
const shadowSchema = z.object({
14390
type: z.literal("shadow"),
144-
value: ShadowValueSchema,
91+
value: z.array(shadowItemSchema),
14592
});
14693

147-
const RawShadowItemSchema = z.object({
94+
const rawShadowItemSchema = z.object({
14895
color: z.union([colorValue, z.string()]),
14996
offsetX: z.union([dimensionValue, z.string()]),
15097
offsetY: z.union([dimensionValue, z.string()]),
@@ -153,135 +100,115 @@ const RawShadowItemSchema = z.object({
153100
inset: z.boolean().optional(),
154101
});
155102

156-
const RawShadowSchema = z.object({
103+
const rawShadowSchema = z.object({
157104
type: z.literal("shadow"),
158-
value: z.union([
159-
z.array(RawShadowItemSchema),
160-
z.string(), // token reference
161-
]),
162-
});
163-
164-
const BorderValueSchema = z.object({
165-
color: colorValue,
166-
width: dimensionValue,
167-
style: strokeStyleValue,
105+
value: z.array(rawShadowItemSchema),
168106
});
169107

170-
export type BorderValue = z.infer<typeof BorderValueSchema>;
171-
172-
const BorderSchema = z.object({
108+
const borderSchema = z.object({
173109
type: z.literal("border"),
174-
value: BorderValueSchema,
110+
value: z.object({
111+
color: colorValue,
112+
width: dimensionValue,
113+
style: strokeStyleValue,
114+
}),
175115
});
176116

177-
const RawBorderSchema = z.object({
117+
const rawBorderSchema = z.object({
178118
type: z.literal("border"),
179-
value: z.union([
180-
z.object({
181-
color: z.union([colorValue, z.string()]),
182-
width: z.union([dimensionValue, z.string()]),
183-
style: z.union([strokeStyleValue, z.string()]),
184-
}),
185-
z.string(), // token reference
186-
]),
187-
});
188-
189-
const TypographyValueSchema = z.object({
190-
fontFamily: fontFamilyValue,
191-
fontSize: dimensionValue,
192-
fontWeight: FontWeightValueSchema,
193-
letterSpacing: dimensionValue,
194-
lineHeight: z.number(),
119+
value: z.object({
120+
color: z.union([colorValue, z.string()]),
121+
width: z.union([dimensionValue, z.string()]),
122+
style: z.union([strokeStyleValue, z.string()]),
123+
}),
195124
});
196125

197-
export type TypographyValue = z.infer<typeof TypographyValueSchema>;
198-
199-
const TypographySchema = z.object({
126+
const typographySchema = z.object({
200127
type: z.literal("typography"),
201-
value: TypographyValueSchema,
128+
value: z.object({
129+
fontFamily: fontFamilyValue,
130+
fontSize: dimensionValue,
131+
fontWeight: fontWeightValue,
132+
letterSpacing: dimensionValue,
133+
lineHeight: z.number(),
134+
}),
202135
});
203136

204-
const RawTypographySchema = z.object({
137+
const rawTypographySchema = z.object({
205138
type: z.literal("typography"),
206-
value: z.union([
207-
z.object({
208-
fontFamily: z.union([fontFamilyValue, z.string()]),
209-
fontSize: z.union([dimensionValue, z.string()]),
210-
fontWeight: z.union([FontWeightValueSchema, z.string()]),
211-
letterSpacing: z.union([dimensionValue, z.string()]),
212-
lineHeight: z.union([z.number(), z.string()]),
213-
}),
214-
z.string(), // token reference
215-
]),
216-
});
217-
218-
const GradientPosition = z.number().min(0).max(1);
219-
220-
const GradientValueSchema = z.array(
221-
z.object({
222-
color: colorValue,
223-
position: GradientPosition,
139+
value: z.object({
140+
fontFamily: z.union([fontFamilyValue, z.string()]),
141+
fontSize: z.union([dimensionValue, z.string()]),
142+
fontWeight: z.union([fontWeightValue, z.string()]),
143+
letterSpacing: z.union([dimensionValue, z.string()]),
144+
lineHeight: z.union([z.number(), z.string()]),
224145
}),
225-
);
226-
227-
export type GradientValue = z.infer<typeof GradientValueSchema>;
146+
});
228147

229-
const GradientSchema = z.object({
148+
const gradientSchema = z.object({
230149
type: z.literal("gradient"),
231-
value: GradientValueSchema,
150+
value: z.array(
151+
z.object({
152+
color: colorValue,
153+
position: z.number(),
154+
}),
155+
),
232156
});
233157

234-
const RawGradientSchema = z.object({
158+
const rawGradientSchema = z.object({
235159
type: z.literal("gradient"),
236-
value: z.union([
237-
z.array(
238-
z.object({
239-
color: z.union([colorValue, z.string()]),
240-
position: GradientPosition,
241-
}),
242-
),
243-
z.string(), // token reference
244-
]),
160+
value: z.array(
161+
z.object({
162+
color: z.union([colorValue, z.string()]),
163+
position: z.number(),
164+
}),
165+
),
245166
});
246167

247168
export const ValueSchema = z.union([
248169
// primitive tokens
249-
ColorSchema,
250-
DimensionSchema,
251-
DurationSchema,
252-
CubicBezierSchema,
253-
NumberSchema,
254-
FontFamilySchema,
255-
FontWeightSchema,
256-
StrokeStyleSchema,
170+
colorSchema,
171+
dimensionSchema,
172+
durationSchema,
173+
cubicBezierSchema,
174+
numberSchema,
175+
fontFamilySchema,
176+
fontWeightSchema,
177+
strokeStyleSchema,
257178
// composite tokens
258-
TransitionSchema,
259-
ShadowSchema,
260-
BorderSchema,
261-
TypographySchema,
262-
GradientSchema,
179+
transitionSchema,
180+
shadowSchema,
181+
borderSchema,
182+
typographySchema,
183+
gradientSchema,
263184
]);
264185

265-
export type Value = z.infer<typeof ValueSchema>;
266-
267186
export const RawValueSchema = z.union([
268187
// primitive tokens
269-
RawColorSchema,
270-
RawDimensionSchema,
271-
RawDurationSchema,
272-
RawCubicBezierSchema,
273-
RawNumberSchema,
274-
RawFontFamilySchema,
275-
RawFontWeightSchema,
276-
RawStrokeStyleSchema,
188+
colorSchema,
189+
dimensionSchema,
190+
durationSchema,
191+
cubicBezierSchema,
192+
numberSchema,
193+
fontFamilySchema,
194+
fontWeightSchema,
195+
strokeStyleSchema,
277196
// composite tokens
278-
RawTransitionSchema,
279-
RawShadowSchema,
280-
RawBorderSchema,
281-
RawTypographySchema,
282-
RawGradientSchema,
197+
rawTransitionSchema,
198+
rawShadowSchema,
199+
rawBorderSchema,
200+
rawTypographySchema,
201+
rawGradientSchema,
283202
]);
284203

204+
export type TransitionValue = z.infer<typeof transitionSchema>["value"];
205+
export type ShadowItem = z.infer<typeof shadowItemSchema>;
206+
export type ShadowValue = z.infer<typeof shadowSchema>["value"];
207+
export type BorderValue = z.infer<typeof borderSchema>["value"];
208+
export type TypographyValue = z.infer<typeof typographySchema>["value"];
209+
export type GradientValue = z.infer<typeof gradientSchema>["value"];
210+
211+
export type Value = z.infer<typeof ValueSchema>;
285212
export type RawValue = z.infer<typeof RawValueSchema>;
286213

287214
/* make sure Value and Raw Value are in sync */

0 commit comments

Comments
 (0)