-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathvalidate.ts
More file actions
147 lines (120 loc) · 4.39 KB
/
validate.ts
File metadata and controls
147 lines (120 loc) · 4.39 KB
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
import { Type } from "@sinclair/typebox";
import { TypeCompiler } from "@sinclair/typebox/compiler";
import type { Op } from "./ops.ts";
import type { RenderOptions, RenderResult, Term } from "./term.ts";
/* ── Range helpers (match bit-packing in pack()) ──────────────────── */
const u8 = Type.Integer({ minimum: 0, maximum: 255 });
const u16 = Type.Integer({ minimum: 0, maximum: 65535 });
/* RGBA color packed as (a << 24 | r << 16 | g << 8 | b). When alpha >= 128,
* bit 31 is set and JavaScript interprets the value as a negative int32.
* Accept both signed and unsigned representations of the same bit pattern. */
const rgba = Type.Integer({ minimum: -0x80000000, maximum: 0xFFFFFFFF });
/* ── Sizing axis (discriminated union) ────────────────────────────── */
const Fit = Type.Object({
type: Type.Literal("fit"),
min: Type.Optional(Type.Number()),
max: Type.Optional(Type.Number()),
});
const Grow = Type.Object({
type: Type.Literal("grow"),
min: Type.Optional(Type.Number()),
max: Type.Optional(Type.Number()),
});
const Percent = Type.Object({
type: Type.Literal("percent"),
value: Type.Number(),
});
const Fixed = Type.Object({
type: Type.Literal("fixed"),
value: Type.Number(),
});
const SizingAxis = Type.Union([Fit, Grow, Percent, Fixed]);
/* ── Sub-objects ──────────────────────────────────────────────────── */
const Padding = Type.Object({
left: Type.Optional(u8),
right: Type.Optional(u8),
top: Type.Optional(u8),
bottom: Type.Optional(u8),
});
const Layout = Type.Object({
width: Type.Optional(SizingAxis),
height: Type.Optional(SizingAxis),
padding: Type.Optional(Padding),
gap: Type.Optional(u16),
direction: Type.Optional(
Type.Union([Type.Literal("ltr"), Type.Literal("ttb")]),
),
alignX: Type.Optional(u8),
alignY: Type.Optional(u8),
});
const CornerRadius = Type.Object({
tl: Type.Optional(u8),
tr: Type.Optional(u8),
bl: Type.Optional(u8),
br: Type.Optional(u8),
});
const Border = Type.Object({
color: rgba,
left: Type.Optional(u8),
right: Type.Optional(u8),
top: Type.Optional(u8),
bottom: Type.Optional(u8),
});
const Clip = Type.Object({
horizontal: Type.Optional(Type.Boolean()),
vertical: Type.Optional(Type.Boolean()),
});
const Floating = Type.Object({
x: Type.Optional(Type.Number()),
y: Type.Optional(Type.Number()),
parent: Type.Optional(Type.Integer({ minimum: 0 })),
attachTo: Type.Optional(u8),
attachPoints: Type.Optional(u8),
zIndex: Type.Optional(u16),
});
/* ── Op types (discriminated on `directive`) ──────────────────────── */
const CloseElement = Type.Object({ directive: Type.Literal(0x04) });
const OpenElement = Type.Object({
directive: Type.Literal(0x02),
id: Type.String(),
layout: Type.Optional(Layout),
bg: Type.Optional(rgba),
cornerRadius: Type.Optional(CornerRadius),
border: Type.Optional(Border),
clip: Type.Optional(Clip),
floating: Type.Optional(Floating),
});
const TextOp = Type.Object({
directive: Type.Literal(0x03),
content: Type.String(),
color: Type.Optional(rgba),
fontSize: Type.Optional(u8),
fontId: Type.Optional(u8),
wrap: Type.Optional(u8),
attrs: Type.Optional(u8),
});
const Ops = Type.Array(Type.Union([OpenElement, TextOp, CloseElement]));
/* ── Compiled validator ───────────────────────────────────────────── */
const compiled = TypeCompiler.Compile(Ops);
export function validate(ops: unknown): ops is Op[] {
return compiled.Check(ops);
}
export function assert(ops: unknown): asserts ops is Op[] {
if (!compiled.Check(ops)) {
let errors = [...compiled.Errors(ops)];
let msg = errors
.slice(0, 5)
.map((e) => `${e.path}: ${e.message}`)
.join("\n");
throw new TypeError(`Invalid ops:\n${msg}`);
}
}
/* ── Term wrapper ─────────────────────────────────────────────────── */
export function validated(term: Term): Term {
return {
render(ops: Op[], options?: RenderOptions): RenderResult {
assert(ops);
return term.render(ops, options);
},
};
}