line-flex-message-preview 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +254 -2
- package/dist/define.d.ts +1 -0
- package/dist/define.js +4 -0
- package/dist/dom.d.ts +15 -0
- package/dist/dom.js +38 -0
- package/dist/element.d.ts +44 -0
- package/dist/element.js +174 -0
- package/dist/flex-message.bundle.js +132 -0
- package/dist/html.d.ts +30 -0
- package/dist/html.js +86 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.js +7 -0
- package/dist/profile.d.ts +75 -0
- package/dist/profile.js +86 -0
- package/dist/styles.d.ts +4 -0
- package/dist/styles.js +152 -0
- package/dist/tree.d.ts +29 -0
- package/dist/tree.js +503 -0
- package/dist/types.d.ts +174 -0
- package/dist/types.js +9 -0
- package/dist/validate.d.ts +46 -0
- package/dist/validate.js +473 -0
- package/dist/values.d.ts +24 -0
- package/dist/values.js +78 -0
- package/docs/measurements.md +119 -0
- package/package.json +62 -4
package/dist/validate.js
ADDED
|
@@ -0,0 +1,473 @@
|
|
|
1
|
+
// Offline structural validation of Flex Message JSON.
|
|
2
|
+
//
|
|
3
|
+
// This is a fast first check, not a replacement for LINE's own verdict: the
|
|
4
|
+
// authoritative answer is LINE's "validate message objects" endpoint (see
|
|
5
|
+
// scripts/validate-with-line.ts). Severities:
|
|
6
|
+
//
|
|
7
|
+
// error LINE rejects this (it answers 400), or it cannot be rendered.
|
|
8
|
+
// That includes unknown properties: LINE's parser refuses them
|
|
9
|
+
// with "unknown field".
|
|
10
|
+
// warning Something the preview cannot show faithfully, so the preview
|
|
11
|
+
// may differ from the phone; or a deprecated component.
|
|
12
|
+
import { isNonNegativeInteger, isObject, parseAngle, parseAspectRatio, parseColor, parseHttpsUrl, parseLength } from './values.js';
|
|
13
|
+
const KEYWORDS = ['none', 'xs', 'sm', 'md', 'lg', 'xl', 'xxl'];
|
|
14
|
+
const SIZES = ['xxs', 'xs', 'sm', 'md', 'lg', 'xl', 'xxl', '3xl', '4xl', '5xl'];
|
|
15
|
+
const enumOf = (...values) => ({ kind: 'enum', values });
|
|
16
|
+
const length = (keywords, percent = false) => ({ kind: 'length', keywords, percent });
|
|
17
|
+
const color = { kind: 'color' };
|
|
18
|
+
const integer = { kind: 'integer' };
|
|
19
|
+
const boolean = { kind: 'boolean' };
|
|
20
|
+
const string = { kind: 'string' };
|
|
21
|
+
const structure = { kind: 'structure' };
|
|
22
|
+
const positioned = {
|
|
23
|
+
position: enumOf('relative', 'absolute'),
|
|
24
|
+
offsetTop: length(KEYWORDS, true),
|
|
25
|
+
offsetBottom: length(KEYWORDS, true),
|
|
26
|
+
offsetStart: length(KEYWORDS, true),
|
|
27
|
+
offsetEnd: length(KEYWORDS, true),
|
|
28
|
+
};
|
|
29
|
+
const textStyle = {
|
|
30
|
+
size: length(SIZES),
|
|
31
|
+
color,
|
|
32
|
+
weight: enumOf('regular', 'bold'),
|
|
33
|
+
style: enumOf('normal', 'italic'),
|
|
34
|
+
decoration: enumOf('none', 'underline', 'line-through'),
|
|
35
|
+
};
|
|
36
|
+
/** Every property each component accepts, and the shape of its value. */
|
|
37
|
+
export const COMPONENT_RULES = {
|
|
38
|
+
box: {
|
|
39
|
+
type: string,
|
|
40
|
+
layout: enumOf('horizontal', 'vertical', 'baseline'),
|
|
41
|
+
contents: structure,
|
|
42
|
+
backgroundColor: color,
|
|
43
|
+
borderColor: color,
|
|
44
|
+
borderWidth: length(['none', 'light', 'normal', 'medium', 'semi-bold', 'bold']),
|
|
45
|
+
cornerRadius: length(KEYWORDS),
|
|
46
|
+
width: length(undefined, true),
|
|
47
|
+
maxWidth: length(undefined, true),
|
|
48
|
+
height: length(undefined, true),
|
|
49
|
+
maxHeight: length(undefined, true),
|
|
50
|
+
flex: integer,
|
|
51
|
+
spacing: length(KEYWORDS),
|
|
52
|
+
margin: length(KEYWORDS),
|
|
53
|
+
paddingAll: length(KEYWORDS, true),
|
|
54
|
+
paddingTop: length(KEYWORDS, true),
|
|
55
|
+
paddingBottom: length(KEYWORDS, true),
|
|
56
|
+
paddingStart: length(KEYWORDS, true),
|
|
57
|
+
paddingEnd: length(KEYWORDS, true),
|
|
58
|
+
action: structure,
|
|
59
|
+
justifyContent: enumOf('flex-start', 'center', 'flex-end', 'space-between', 'space-around', 'space-evenly'),
|
|
60
|
+
alignItems: enumOf('flex-start', 'center', 'flex-end'),
|
|
61
|
+
background: structure,
|
|
62
|
+
...positioned,
|
|
63
|
+
},
|
|
64
|
+
button: {
|
|
65
|
+
type: string,
|
|
66
|
+
action: structure,
|
|
67
|
+
flex: integer,
|
|
68
|
+
margin: length(KEYWORDS),
|
|
69
|
+
height: enumOf('sm', 'md'),
|
|
70
|
+
style: enumOf('primary', 'secondary', 'link'),
|
|
71
|
+
color,
|
|
72
|
+
gravity: enumOf('top', 'bottom', 'center'),
|
|
73
|
+
adjustMode: enumOf('shrink-to-fit'),
|
|
74
|
+
scaling: boolean,
|
|
75
|
+
...positioned,
|
|
76
|
+
},
|
|
77
|
+
image: {
|
|
78
|
+
type: string,
|
|
79
|
+
url: { kind: 'url' },
|
|
80
|
+
flex: integer,
|
|
81
|
+
margin: length(KEYWORDS),
|
|
82
|
+
align: enumOf('start', 'end', 'center'),
|
|
83
|
+
gravity: enumOf('top', 'bottom', 'center'),
|
|
84
|
+
size: length([...SIZES, 'full'], true),
|
|
85
|
+
aspectRatio: { kind: 'ratio' },
|
|
86
|
+
aspectMode: enumOf('fit', 'cover'),
|
|
87
|
+
backgroundColor: color,
|
|
88
|
+
action: structure,
|
|
89
|
+
animated: boolean,
|
|
90
|
+
...positioned,
|
|
91
|
+
},
|
|
92
|
+
video: {
|
|
93
|
+
type: string,
|
|
94
|
+
url: { kind: 'url' },
|
|
95
|
+
previewUrl: { kind: 'url' },
|
|
96
|
+
altContent: structure,
|
|
97
|
+
aspectRatio: { kind: 'ratio' },
|
|
98
|
+
action: structure,
|
|
99
|
+
},
|
|
100
|
+
icon: {
|
|
101
|
+
type: string,
|
|
102
|
+
url: { kind: 'url' },
|
|
103
|
+
margin: length(KEYWORDS),
|
|
104
|
+
size: length(SIZES),
|
|
105
|
+
aspectRatio: { kind: 'ratio' },
|
|
106
|
+
scaling: boolean,
|
|
107
|
+
...positioned,
|
|
108
|
+
},
|
|
109
|
+
text: {
|
|
110
|
+
type: string,
|
|
111
|
+
text: string,
|
|
112
|
+
contents: structure,
|
|
113
|
+
adjustMode: enumOf('shrink-to-fit'),
|
|
114
|
+
flex: integer,
|
|
115
|
+
margin: length(KEYWORDS),
|
|
116
|
+
align: enumOf('start', 'end', 'center'),
|
|
117
|
+
gravity: enumOf('top', 'bottom', 'center'),
|
|
118
|
+
wrap: boolean,
|
|
119
|
+
lineSpacing: length(),
|
|
120
|
+
action: structure,
|
|
121
|
+
maxLines: integer,
|
|
122
|
+
scaling: boolean,
|
|
123
|
+
...textStyle,
|
|
124
|
+
...positioned,
|
|
125
|
+
},
|
|
126
|
+
span: { type: string, text: string, ...textStyle },
|
|
127
|
+
separator: { type: string, margin: length(KEYWORDS), color },
|
|
128
|
+
filler: { type: string, flex: integer },
|
|
129
|
+
// Gone from LINE's documentation and OpenAPI document, but still accepted.
|
|
130
|
+
spacer: { type: string, size: enumOf('xs', 'sm', 'md', 'lg', 'xl', 'xxl') },
|
|
131
|
+
};
|
|
132
|
+
/** Component types LINE accepts that its OpenAPI document does not list. */
|
|
133
|
+
export const UNDOCUMENTED_COMPONENTS = ['spacer'];
|
|
134
|
+
/** What a baseline box may contain; LINE rejects anything else as "invalid box content type". */
|
|
135
|
+
const BASELINE_CONTENTS = ['icon', 'text', 'filler', 'spacer'];
|
|
136
|
+
/** LINE allows at most this many bubbles in a carousel. */
|
|
137
|
+
const MAX_CAROUSEL_BUBBLES = 12;
|
|
138
|
+
export const BUBBLE_PROPERTIES = ['type', 'size', 'direction', 'header', 'hero', 'body', 'footer', 'styles', 'action'];
|
|
139
|
+
export const BLOCK_STYLE_PROPERTIES = ['backgroundColor', 'separator', 'separatorColor'];
|
|
140
|
+
const BUBBLE_SIZES = ['nano', 'micro', 'deca', 'hecto', 'kilo', 'mega', 'giga'];
|
|
141
|
+
const BLOCKS = ['header', 'hero', 'body', 'footer'];
|
|
142
|
+
/** Properties LINE accepts that this renderer does not draw. */
|
|
143
|
+
const NOT_RENDERED = {
|
|
144
|
+
text: ['adjustMode', 'scaling'],
|
|
145
|
+
button: ['adjustMode', 'scaling'],
|
|
146
|
+
icon: ['scaling'],
|
|
147
|
+
image: ['animated'],
|
|
148
|
+
};
|
|
149
|
+
/** LINE allows boxes nested up to this many levels; the outermost box is level 1. */
|
|
150
|
+
const MAX_BOX_DEPTH = 20;
|
|
151
|
+
function pointer(path, key) {
|
|
152
|
+
return `${path}/${String(key).replace(/~/g, '~0').replace(/\//g, '~1')}`;
|
|
153
|
+
}
|
|
154
|
+
function describe(value) {
|
|
155
|
+
const text = typeof value === 'string' ? JSON.stringify(value) : String(JSON.stringify(value));
|
|
156
|
+
return text.length > 60 ? `${text.slice(0, 57)}...` : text;
|
|
157
|
+
}
|
|
158
|
+
class Validator {
|
|
159
|
+
issues = [];
|
|
160
|
+
add(severity, code, path, message) {
|
|
161
|
+
this.issues.push({ path, severity, code, message });
|
|
162
|
+
}
|
|
163
|
+
value(rule, value, path, name) {
|
|
164
|
+
const bad = (expected) => this.add('error', 'invalid-value', path, `${name} must be ${expected}; got ${describe(value)}.`);
|
|
165
|
+
switch (rule.kind) {
|
|
166
|
+
case 'enum':
|
|
167
|
+
if (typeof value !== 'string' || !rule.values.includes(value))
|
|
168
|
+
bad(`one of ${rule.values.join(', ')}`);
|
|
169
|
+
return;
|
|
170
|
+
case 'length': {
|
|
171
|
+
if (typeof value === 'string' && rule.keywords?.includes(value))
|
|
172
|
+
return;
|
|
173
|
+
const parsed = parseLength(value, { percent: rule.percent });
|
|
174
|
+
const allowNegative = name.startsWith('offset');
|
|
175
|
+
if (!parsed || (parsed.n < 0 && !allowNegative)) {
|
|
176
|
+
const units = rule.percent ? 'a px or % value' : 'a px value';
|
|
177
|
+
bad(rule.keywords ? `${units} or one of ${rule.keywords.join(', ')}` : units);
|
|
178
|
+
}
|
|
179
|
+
return;
|
|
180
|
+
}
|
|
181
|
+
case 'color':
|
|
182
|
+
if (!parseColor(value))
|
|
183
|
+
bad('a #RRGGBB or #RRGGBBAA colour');
|
|
184
|
+
return;
|
|
185
|
+
case 'url':
|
|
186
|
+
if (!parseHttpsUrl(value))
|
|
187
|
+
bad('an https URL of at most 2000 characters');
|
|
188
|
+
return;
|
|
189
|
+
case 'ratio': {
|
|
190
|
+
const ratio = parseAspectRatio(value);
|
|
191
|
+
if (!ratio)
|
|
192
|
+
bad('a "{width}:{height}" ratio');
|
|
193
|
+
else if (ratio[1] > ratio[0] * 3)
|
|
194
|
+
bad('a ratio whose height is at most three times its width');
|
|
195
|
+
return;
|
|
196
|
+
}
|
|
197
|
+
case 'integer':
|
|
198
|
+
if (!isNonNegativeInteger(value))
|
|
199
|
+
bad('a non-negative integer');
|
|
200
|
+
return;
|
|
201
|
+
case 'boolean':
|
|
202
|
+
if (typeof value !== 'boolean')
|
|
203
|
+
bad('true or false');
|
|
204
|
+
return;
|
|
205
|
+
case 'string':
|
|
206
|
+
if (typeof value !== 'string')
|
|
207
|
+
bad('a string');
|
|
208
|
+
return;
|
|
209
|
+
case 'structure':
|
|
210
|
+
return;
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
action(value, path) {
|
|
214
|
+
if (!isObject(value) || typeof value.type !== 'string') {
|
|
215
|
+
this.add('error', 'invalid-value', path, 'action must be an action object with a type.');
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
gradient(value, path) {
|
|
219
|
+
if (!isObject(value) || value.type !== 'linearGradient') {
|
|
220
|
+
this.add('error', 'invalid-value', path, 'background must be an object of type linearGradient.');
|
|
221
|
+
return;
|
|
222
|
+
}
|
|
223
|
+
const angle = parseAngle(value.angle);
|
|
224
|
+
if (angle === undefined || angle < 0 || angle >= 360) {
|
|
225
|
+
this.add('error', 'invalid-value', pointer(path, 'angle'), 'angle must be from "0deg" up to, but not including, "360deg".');
|
|
226
|
+
}
|
|
227
|
+
for (const key of ['startColor', 'endColor']) {
|
|
228
|
+
if (!parseColor(value[key]))
|
|
229
|
+
this.add('error', 'invalid-value', pointer(path, key), `${key} must be a #RRGGBB or #RRGGBBAA colour.`);
|
|
230
|
+
}
|
|
231
|
+
if (value.centerColor !== undefined && !parseColor(value.centerColor)) {
|
|
232
|
+
this.add('error', 'invalid-value', pointer(path, 'centerColor'), 'centerColor must be a #RRGGBB or #RRGGBBAA colour.');
|
|
233
|
+
}
|
|
234
|
+
if (value.centerPosition !== undefined && parseLength(value.centerPosition, { percent: true })?.unit !== '%') {
|
|
235
|
+
this.add('error', 'invalid-value', pointer(path, 'centerPosition'), 'centerPosition must be a percentage.');
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
component(node, path, where) {
|
|
239
|
+
if (!isObject(node)) {
|
|
240
|
+
this.add('error', 'invalid-type', path, 'A component must be an object.');
|
|
241
|
+
return;
|
|
242
|
+
}
|
|
243
|
+
const type = node.type;
|
|
244
|
+
const rules = typeof type === 'string' && Object.hasOwn(COMPONENT_RULES, type) ? COMPONENT_RULES[type] : undefined;
|
|
245
|
+
if (!rules || typeof type !== 'string') {
|
|
246
|
+
this.add('error', 'unknown-component', path, `Unknown component type ${describe(type)}.`);
|
|
247
|
+
return;
|
|
248
|
+
}
|
|
249
|
+
if (type === 'span' && !where.inText)
|
|
250
|
+
this.add('error', 'misplaced-component', path, 'A span can only be used in the contents of a text.');
|
|
251
|
+
if (type !== 'span' && where.inText)
|
|
252
|
+
this.add('error', 'misplaced-component', path, `The contents of a text can only hold spans; got ${type}.`);
|
|
253
|
+
if (type === 'icon' && where.parentLayout !== 'baseline') {
|
|
254
|
+
this.add('error', 'misplaced-component', path, 'An icon can only be used in a baseline box.');
|
|
255
|
+
}
|
|
256
|
+
if (type === 'video' && !where.hero) {
|
|
257
|
+
this.add('error', 'misplaced-component', path, 'A video can only be the hero of a bubble.');
|
|
258
|
+
}
|
|
259
|
+
if (where.parentLayout === 'baseline' && !BASELINE_CONTENTS.includes(type)) {
|
|
260
|
+
this.add('error', 'misplaced-component', path, `A baseline box can only hold ${BASELINE_CONTENTS.slice(0, 3).join(', ')}; got ${type}.`);
|
|
261
|
+
}
|
|
262
|
+
if (type === 'filler' || type === 'spacer') {
|
|
263
|
+
this.add('warning', 'deprecated', path, `${type} is deprecated; use padding, margin or justifyContent instead.`);
|
|
264
|
+
}
|
|
265
|
+
for (const [key, value] of Object.entries(node)) {
|
|
266
|
+
const rule = Object.hasOwn(rules, key) ? rules[key] : undefined;
|
|
267
|
+
if (!rule) {
|
|
268
|
+
this.add('error', 'unknown-property', pointer(path, key), `${type} has no property ${describe(key)}.`);
|
|
269
|
+
continue;
|
|
270
|
+
}
|
|
271
|
+
if (value === undefined)
|
|
272
|
+
continue;
|
|
273
|
+
this.value(rule, value, pointer(path, key), key);
|
|
274
|
+
if (key === 'action')
|
|
275
|
+
this.action(value, pointer(path, key));
|
|
276
|
+
if (key === 'background')
|
|
277
|
+
this.gradient(value, pointer(path, key));
|
|
278
|
+
}
|
|
279
|
+
for (const key of NOT_RENDERED[type] ?? []) {
|
|
280
|
+
if (node[key] !== undefined && node[key] !== false) {
|
|
281
|
+
this.add('warning', 'not-rendered', pointer(path, key), `${key} is accepted by LINE but not drawn by this preview.`);
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
const require = (key) => {
|
|
285
|
+
if (node[key] === undefined)
|
|
286
|
+
this.add('error', 'missing-property', path, `${type} requires ${key}.`);
|
|
287
|
+
};
|
|
288
|
+
switch (type) {
|
|
289
|
+
case 'box': {
|
|
290
|
+
require('layout');
|
|
291
|
+
const depth = where.depth + 1;
|
|
292
|
+
if (depth === MAX_BOX_DEPTH + 1)
|
|
293
|
+
this.add('error', 'too-deep', path, `Boxes can be nested at most ${MAX_BOX_DEPTH} levels deep.`);
|
|
294
|
+
if (!Array.isArray(node.contents)) {
|
|
295
|
+
this.add('error', node.contents === undefined ? 'missing-property' : 'invalid-type', path, 'box requires contents, an array of components.');
|
|
296
|
+
return;
|
|
297
|
+
}
|
|
298
|
+
node.contents.forEach((child, i) => {
|
|
299
|
+
this.component(child, pointer(pointer(path, 'contents'), i), { parentLayout: String(node.layout), depth });
|
|
300
|
+
});
|
|
301
|
+
return;
|
|
302
|
+
}
|
|
303
|
+
case 'text': {
|
|
304
|
+
const hasSpans = Array.isArray(node.contents) && node.contents.length > 0;
|
|
305
|
+
if (node.contents !== undefined && !Array.isArray(node.contents)) {
|
|
306
|
+
this.add('error', 'invalid-type', pointer(path, 'contents'), 'contents must be an array of spans.');
|
|
307
|
+
}
|
|
308
|
+
if (Array.isArray(node.contents)) {
|
|
309
|
+
node.contents.forEach((child, i) => this.component(child, pointer(pointer(path, 'contents'), i), { inText: true, depth: where.depth }));
|
|
310
|
+
}
|
|
311
|
+
if (!hasSpans && node.text === undefined)
|
|
312
|
+
this.add('error', 'missing-property', path, 'text requires text or contents.');
|
|
313
|
+
if (!hasSpans && node.text === '')
|
|
314
|
+
this.add('error', 'invalid-value', pointer(path, 'text'), 'text must not be empty.');
|
|
315
|
+
return;
|
|
316
|
+
}
|
|
317
|
+
case 'button':
|
|
318
|
+
require('action');
|
|
319
|
+
if (isObject(node.action) && (typeof node.action.label !== 'string' || node.action.label === '')) {
|
|
320
|
+
this.add('error', 'missing-property', pointer(path, 'action'), 'The action of a button requires a label.');
|
|
321
|
+
}
|
|
322
|
+
return;
|
|
323
|
+
case 'span':
|
|
324
|
+
if (node.text === undefined)
|
|
325
|
+
this.add('error', 'missing-property', path, 'span requires text.');
|
|
326
|
+
else if (node.text === '')
|
|
327
|
+
this.add('error', 'invalid-value', pointer(path, 'text'), 'text must not be empty.');
|
|
328
|
+
return;
|
|
329
|
+
case 'image':
|
|
330
|
+
case 'icon':
|
|
331
|
+
require('url');
|
|
332
|
+
return;
|
|
333
|
+
case 'video':
|
|
334
|
+
require('url');
|
|
335
|
+
require('previewUrl');
|
|
336
|
+
if (!isObject(node.altContent) || (node.altContent.type !== 'box' && node.altContent.type !== 'image')) {
|
|
337
|
+
this.add('error', node.altContent === undefined ? 'missing-property' : 'invalid-value', path, 'video requires altContent, a box or an image.');
|
|
338
|
+
}
|
|
339
|
+
else {
|
|
340
|
+
this.component(node.altContent, pointer(path, 'altContent'), { depth: where.depth });
|
|
341
|
+
}
|
|
342
|
+
this.add('warning', 'not-rendered', path, 'Video is not played in this preview; its altContent is shown instead.');
|
|
343
|
+
return;
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
bubble(node, path, inCarousel = false) {
|
|
347
|
+
if (!isObject(node) || node.type !== 'bubble') {
|
|
348
|
+
this.add('error', 'invalid-type', path, 'Expected an object of type bubble.');
|
|
349
|
+
return;
|
|
350
|
+
}
|
|
351
|
+
for (const key of Object.keys(node)) {
|
|
352
|
+
if (!BUBBLE_PROPERTIES.includes(key)) {
|
|
353
|
+
this.add('error', 'unknown-property', pointer(path, key), `bubble has no property ${describe(key)}.`);
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
if (node.size !== undefined)
|
|
357
|
+
this.value(enumOf(...BUBBLE_SIZES), node.size, pointer(path, 'size'), 'size');
|
|
358
|
+
if (node.direction !== undefined)
|
|
359
|
+
this.value(enumOf('ltr', 'rtl'), node.direction, pointer(path, 'direction'), 'direction');
|
|
360
|
+
if (node.action !== undefined)
|
|
361
|
+
this.action(node.action, pointer(path, 'action'));
|
|
362
|
+
if (BLOCKS.every((name) => node[name] === undefined)) {
|
|
363
|
+
this.add('error', 'missing-property', path, 'A bubble requires at least one of header, hero, body and footer.');
|
|
364
|
+
}
|
|
365
|
+
for (const name of BLOCKS) {
|
|
366
|
+
const block = node[name];
|
|
367
|
+
if (block === undefined)
|
|
368
|
+
continue;
|
|
369
|
+
const blockPath = pointer(path, name);
|
|
370
|
+
if (name !== 'hero' && (!isObject(block) || block.type !== 'box')) {
|
|
371
|
+
this.add('error', 'invalid-type', blockPath, `${name} must be a box.`);
|
|
372
|
+
continue;
|
|
373
|
+
}
|
|
374
|
+
if (name === 'hero' && (!isObject(block) || !['box', 'image', 'video'].includes(String(block.type)))) {
|
|
375
|
+
this.add('error', 'invalid-type', blockPath, 'hero must be a box, an image or a video.');
|
|
376
|
+
continue;
|
|
377
|
+
}
|
|
378
|
+
if (name === 'hero' && isObject(block) && block.type === 'video') {
|
|
379
|
+
// Both are LINE's rules, learnt from its validate endpoint.
|
|
380
|
+
if (inCarousel)
|
|
381
|
+
this.add('error', 'misplaced-component', blockPath, 'A video cannot be used in a carousel.');
|
|
382
|
+
if (!['kilo', 'mega', 'giga'].includes(String(node.size ?? 'mega'))) {
|
|
383
|
+
this.add('error', 'misplaced-component', blockPath, 'A video needs a bubble of size kilo, mega or giga.');
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
this.component(block, blockPath, { hero: name === 'hero', depth: 0 });
|
|
387
|
+
}
|
|
388
|
+
if (node.styles !== undefined) {
|
|
389
|
+
const stylesPath = pointer(path, 'styles');
|
|
390
|
+
if (!isObject(node.styles)) {
|
|
391
|
+
this.add('error', 'invalid-type', stylesPath, 'styles must be an object.');
|
|
392
|
+
return;
|
|
393
|
+
}
|
|
394
|
+
for (const [name, style] of Object.entries(node.styles)) {
|
|
395
|
+
const stylePath = pointer(stylesPath, name);
|
|
396
|
+
if (!BLOCKS.includes(name)) {
|
|
397
|
+
this.add('error', 'unknown-property', stylePath, `styles has no block ${describe(name)}.`);
|
|
398
|
+
continue;
|
|
399
|
+
}
|
|
400
|
+
if (!isObject(style)) {
|
|
401
|
+
this.add('error', 'invalid-type', stylePath, 'A block style must be an object.');
|
|
402
|
+
continue;
|
|
403
|
+
}
|
|
404
|
+
this.blockStyle(style, stylePath);
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
}
|
|
408
|
+
blockStyle(style, path) {
|
|
409
|
+
for (const [key, value] of Object.entries(style)) {
|
|
410
|
+
const keyPath = pointer(path, key);
|
|
411
|
+
if (key === 'backgroundColor' || key === 'separatorColor')
|
|
412
|
+
this.value(color, value, keyPath, key);
|
|
413
|
+
else if (key === 'separator')
|
|
414
|
+
this.value(boolean, value, keyPath, key);
|
|
415
|
+
else
|
|
416
|
+
this.add('error', 'unknown-property', keyPath, `A block style has no property ${describe(key)}.`);
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
container(node, path) {
|
|
420
|
+
if (!isObject(node)) {
|
|
421
|
+
this.add('error', 'invalid-type', path, 'Expected a bubble or a carousel.');
|
|
422
|
+
return;
|
|
423
|
+
}
|
|
424
|
+
if (node.type === 'bubble') {
|
|
425
|
+
this.bubble(node, path);
|
|
426
|
+
}
|
|
427
|
+
else if (node.type === 'carousel') {
|
|
428
|
+
if (!Array.isArray(node.contents) || node.contents.length === 0) {
|
|
429
|
+
this.add('error', 'missing-property', path, 'carousel requires contents, a non-empty array of bubbles.');
|
|
430
|
+
return;
|
|
431
|
+
}
|
|
432
|
+
const contentsPath = pointer(path, 'contents');
|
|
433
|
+
if (node.contents.length > MAX_CAROUSEL_BUBBLES) {
|
|
434
|
+
this.add('error', 'invalid-value', contentsPath, `A carousel can hold at most ${MAX_CAROUSEL_BUBBLES} bubbles; got ${node.contents.length}.`);
|
|
435
|
+
}
|
|
436
|
+
const sizes = new Set(node.contents.map((item) => (isObject(item) ? (item.size ?? 'mega') : undefined)));
|
|
437
|
+
if (sizes.size > 1)
|
|
438
|
+
this.add('error', 'invalid-value', contentsPath, 'The bubbles of a carousel must all have the same size.');
|
|
439
|
+
node.contents.forEach((item, i) => this.bubble(item, pointer(contentsPath, i), true));
|
|
440
|
+
}
|
|
441
|
+
else {
|
|
442
|
+
this.add('error', 'invalid-type', path, `Expected type bubble or carousel; got ${describe(node.type)}.`);
|
|
443
|
+
}
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
/**
|
|
447
|
+
* Checks a Flex message (`{ type: "flex", altText, contents }`) or a bare
|
|
448
|
+
* bubble / carousel. A string is parsed as JSON first. Returns every problem
|
|
449
|
+
* found; an empty array means nothing was found, not that LINE will accept it.
|
|
450
|
+
*/
|
|
451
|
+
export function validate(input) {
|
|
452
|
+
const validator = new Validator();
|
|
453
|
+
let value = input;
|
|
454
|
+
if (typeof value === 'string') {
|
|
455
|
+
try {
|
|
456
|
+
value = JSON.parse(value);
|
|
457
|
+
}
|
|
458
|
+
catch (error) {
|
|
459
|
+
validator.add('error', 'invalid-json', '', `Not valid JSON: ${error instanceof Error ? error.message : String(error)}`);
|
|
460
|
+
return validator.issues;
|
|
461
|
+
}
|
|
462
|
+
}
|
|
463
|
+
if (isObject(value) && value.type === 'flex') {
|
|
464
|
+
if (typeof value.altText !== 'string' || value.altText === '') {
|
|
465
|
+
validator.add('error', 'missing-property', '', 'A flex message requires altText, a non-empty string.');
|
|
466
|
+
}
|
|
467
|
+
validator.container(value.contents, '/contents');
|
|
468
|
+
}
|
|
469
|
+
else {
|
|
470
|
+
validator.container(value, '');
|
|
471
|
+
}
|
|
472
|
+
return validator.issues;
|
|
473
|
+
}
|
package/dist/values.d.ts
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
export interface Length {
|
|
2
|
+
n: number;
|
|
3
|
+
unit: 'px' | '%';
|
|
4
|
+
}
|
|
5
|
+
export declare function parseLength(value: unknown, opts?: {
|
|
6
|
+
percent?: boolean;
|
|
7
|
+
}): Length | undefined;
|
|
8
|
+
export declare function cssLength(length: Length): string;
|
|
9
|
+
/** `#RRGGBB` or `#RRGGBBAA`, returned lower-cased. */
|
|
10
|
+
export declare function parseColor(value: unknown): string | undefined;
|
|
11
|
+
/** `"90deg"` → `90`. */
|
|
12
|
+
export declare function parseAngle(value: unknown): number | undefined;
|
|
13
|
+
/** `"20:13"` → `[20, 13]`. Both sides must be positive. */
|
|
14
|
+
export declare function parseAspectRatio(value: unknown): [number, number] | undefined;
|
|
15
|
+
/**
|
|
16
|
+
* Accepts only absolute `https:` URLs (the only scheme LINE accepts for
|
|
17
|
+
* images) and returns the normalised form, in which quotes, angle brackets and
|
|
18
|
+
* whitespace are percent-encoded.
|
|
19
|
+
*/
|
|
20
|
+
export declare function parseHttpsUrl(value: unknown): string | undefined;
|
|
21
|
+
export declare function isNonNegativeInteger(value: unknown): value is number;
|
|
22
|
+
/** Own-property lookup, so that keys like `constructor` never match. */
|
|
23
|
+
export declare function lookup<T>(table: Readonly<Record<string, T>>, key: unknown): T | undefined;
|
|
24
|
+
export declare function isObject(value: unknown): value is Record<string, unknown>;
|
package/dist/values.js
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
// Parsers for the value shapes LINE accepts.
|
|
2
|
+
//
|
|
3
|
+
// Every value that reaches CSS or an attribute goes through one of these and is
|
|
4
|
+
// re-serialised from the parsed result. Raw input strings are never written
|
|
5
|
+
// into styles or markup, so a hostile message cannot break out of them.
|
|
6
|
+
const LENGTH = /^(-?\d+(?:\.\d+)?)(px|%)$/;
|
|
7
|
+
const COLOR = /^#[0-9a-f]{6}(?:[0-9a-f]{2})?$/i;
|
|
8
|
+
const ANGLE = /^(-?\d+(?:\.\d+)?)deg$/;
|
|
9
|
+
const RATIO = /^(\d+(?:\.\d+)?):(\d+(?:\.\d+)?)$/;
|
|
10
|
+
export function parseLength(value, opts = {}) {
|
|
11
|
+
if (typeof value !== 'string')
|
|
12
|
+
return undefined;
|
|
13
|
+
const m = LENGTH.exec(value);
|
|
14
|
+
if (!m)
|
|
15
|
+
return undefined;
|
|
16
|
+
const unit = m[2];
|
|
17
|
+
if (unit === '%' && !opts.percent)
|
|
18
|
+
return undefined;
|
|
19
|
+
const n = Number(m[1]);
|
|
20
|
+
return Number.isFinite(n) ? { n, unit } : undefined;
|
|
21
|
+
}
|
|
22
|
+
export function cssLength(length) {
|
|
23
|
+
return `${length.n}${length.unit}`;
|
|
24
|
+
}
|
|
25
|
+
/** `#RRGGBB` or `#RRGGBBAA`, returned lower-cased. */
|
|
26
|
+
export function parseColor(value) {
|
|
27
|
+
return typeof value === 'string' && COLOR.test(value) ? value.toLowerCase() : undefined;
|
|
28
|
+
}
|
|
29
|
+
/** `"90deg"` → `90`. */
|
|
30
|
+
export function parseAngle(value) {
|
|
31
|
+
if (typeof value !== 'string')
|
|
32
|
+
return undefined;
|
|
33
|
+
const m = ANGLE.exec(value);
|
|
34
|
+
if (!m)
|
|
35
|
+
return undefined;
|
|
36
|
+
const n = Number(m[1]);
|
|
37
|
+
return Number.isFinite(n) ? n : undefined;
|
|
38
|
+
}
|
|
39
|
+
/** `"20:13"` → `[20, 13]`. Both sides must be positive. */
|
|
40
|
+
export function parseAspectRatio(value) {
|
|
41
|
+
if (typeof value !== 'string')
|
|
42
|
+
return undefined;
|
|
43
|
+
const m = RATIO.exec(value);
|
|
44
|
+
if (!m)
|
|
45
|
+
return undefined;
|
|
46
|
+
const w = Number(m[1]);
|
|
47
|
+
const h = Number(m[2]);
|
|
48
|
+
return w > 0 && h > 0 && Number.isFinite(w) && Number.isFinite(h) ? [w, h] : undefined;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Accepts only absolute `https:` URLs (the only scheme LINE accepts for
|
|
52
|
+
* images) and returns the normalised form, in which quotes, angle brackets and
|
|
53
|
+
* whitespace are percent-encoded.
|
|
54
|
+
*/
|
|
55
|
+
export function parseHttpsUrl(value) {
|
|
56
|
+
if (typeof value !== 'string' || value.length > 2000)
|
|
57
|
+
return undefined;
|
|
58
|
+
let url;
|
|
59
|
+
try {
|
|
60
|
+
url = new URL(value);
|
|
61
|
+
}
|
|
62
|
+
catch {
|
|
63
|
+
return undefined;
|
|
64
|
+
}
|
|
65
|
+
if (url.protocol !== 'https:' || url.username !== '' || url.password !== '')
|
|
66
|
+
return undefined;
|
|
67
|
+
return url.href;
|
|
68
|
+
}
|
|
69
|
+
export function isNonNegativeInteger(value) {
|
|
70
|
+
return typeof value === 'number' && Number.isInteger(value) && value >= 0;
|
|
71
|
+
}
|
|
72
|
+
/** Own-property lookup, so that keys like `constructor` never match. */
|
|
73
|
+
export function lookup(table, key) {
|
|
74
|
+
return typeof key === 'string' && Object.hasOwn(table, key) ? table[key] : undefined;
|
|
75
|
+
}
|
|
76
|
+
export function isObject(value) {
|
|
77
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
78
|
+
}
|