@edjbarron/ux-guard 0.0.0-stage → 0.6.1
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 +201 -0
- package/NOTICE +2 -0
- package/README.md +239 -2
- package/bin/ux-guard.mjs +4 -0
- package/docs/cli-reference.md +145 -0
- package/docs/scoring-and-backslide.md +100 -0
- package/docs/test-suite.md +175 -0
- package/lib/authoritative.mjs +463 -0
- package/lib/baselines.mjs +1840 -0
- package/lib/catalog.mjs +197 -0
- package/lib/cli.mjs +1980 -0
- package/lib/config.mjs +209 -0
- package/lib/discovery.mjs +581 -0
- package/lib/journey-policy.mjs +590 -0
- package/lib/journeys.mjs +900 -0
- package/lib/observations.mjs +268 -0
- package/lib/patterns.mjs +247 -0
- package/lib/playwright.mjs +344 -0
- package/lib/run-contract.mjs +413 -0
- package/lib/shards.mjs +445 -0
- package/lib/targets.mjs +764 -0
- package/lib/verdicts.mjs +1769 -0
- package/lib/visuals.mjs +1263 -0
- package/lib/yaml.mjs +140 -0
- package/package.json +56 -4
- package/skills/ux-guard/SKILL.md +134 -0
package/lib/shards.mjs
ADDED
|
@@ -0,0 +1,445 @@
|
|
|
1
|
+
import {
|
|
2
|
+
existsSync,
|
|
3
|
+
lstatSync,
|
|
4
|
+
readdirSync,
|
|
5
|
+
readFileSync,
|
|
6
|
+
writeFileSync,
|
|
7
|
+
} from 'node:fs';
|
|
8
|
+
import { basename, join } from 'node:path';
|
|
9
|
+
import {
|
|
10
|
+
createJourneyReport,
|
|
11
|
+
createLifecycleJourney,
|
|
12
|
+
} from './journeys.mjs';
|
|
13
|
+
import {
|
|
14
|
+
canonicalJson,
|
|
15
|
+
canonicalize,
|
|
16
|
+
createRunEnvelope,
|
|
17
|
+
RunContractError,
|
|
18
|
+
sha256,
|
|
19
|
+
validateRunIdentity,
|
|
20
|
+
} from './run-contract.mjs';
|
|
21
|
+
|
|
22
|
+
export const JOURNEY_SHARD_KIND = 'ux-guard-journey-shard';
|
|
23
|
+
export const JOURNEY_SHARD_VERSION = 1;
|
|
24
|
+
|
|
25
|
+
const ID = /^[a-z0-9]+(?:[._-][a-z0-9]+)*$/;
|
|
26
|
+
const STATUSES = new Set([
|
|
27
|
+
'successful',
|
|
28
|
+
'failed',
|
|
29
|
+
'skipped',
|
|
30
|
+
'unexecutable',
|
|
31
|
+
'infrastructure-error',
|
|
32
|
+
]);
|
|
33
|
+
const ERROR_CATEGORIES = new Set(['assertion', 'infrastructure']);
|
|
34
|
+
|
|
35
|
+
export class JourneyShardError extends Error {
|
|
36
|
+
constructor(message) {
|
|
37
|
+
super(message);
|
|
38
|
+
this.name = 'JourneyShardError';
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export class JourneyShardUpgradeError extends JourneyShardError {
|
|
43
|
+
constructor(message) {
|
|
44
|
+
super(message);
|
|
45
|
+
this.name = 'JourneyShardUpgradeError';
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export function createJourneyShard({
|
|
50
|
+
runIdentity,
|
|
51
|
+
useCaseId,
|
|
52
|
+
journeyId,
|
|
53
|
+
project,
|
|
54
|
+
repeatIndex,
|
|
55
|
+
status,
|
|
56
|
+
journey = null,
|
|
57
|
+
errors = [],
|
|
58
|
+
} = {}) {
|
|
59
|
+
validateRunIdentity(runIdentity);
|
|
60
|
+
assertId(useCaseId, 'useCaseId');
|
|
61
|
+
assertId(journeyId, 'journeyId');
|
|
62
|
+
assertId(project, 'project');
|
|
63
|
+
if (!Number.isSafeInteger(repeatIndex) || repeatIndex < 0) {
|
|
64
|
+
throw new JourneyShardError('repeatIndex must be a non-negative safe integer');
|
|
65
|
+
}
|
|
66
|
+
if (!STATUSES.has(status)) throw new JourneyShardError('shard status is invalid');
|
|
67
|
+
const id = shardId({ useCaseId, journeyId, project, repeatIndex });
|
|
68
|
+
const shard = {
|
|
69
|
+
kind: JOURNEY_SHARD_KIND,
|
|
70
|
+
version: JOURNEY_SHARD_VERSION,
|
|
71
|
+
id,
|
|
72
|
+
runIdentity: structuredClone(runIdentity),
|
|
73
|
+
useCaseId,
|
|
74
|
+
journeyId,
|
|
75
|
+
project,
|
|
76
|
+
repeatIndex,
|
|
77
|
+
status,
|
|
78
|
+
journey: journey === null ? null : structuredClone(journey),
|
|
79
|
+
errors: structuredClone(errors),
|
|
80
|
+
};
|
|
81
|
+
return validateJourneyShard(shard);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export function validateJourneyShard(shard) {
|
|
85
|
+
assertMap(shard, 'shard');
|
|
86
|
+
assertExactKeys(
|
|
87
|
+
shard,
|
|
88
|
+
[
|
|
89
|
+
'kind',
|
|
90
|
+
'version',
|
|
91
|
+
'id',
|
|
92
|
+
'runIdentity',
|
|
93
|
+
'useCaseId',
|
|
94
|
+
'journeyId',
|
|
95
|
+
'project',
|
|
96
|
+
'repeatIndex',
|
|
97
|
+
'status',
|
|
98
|
+
'journey',
|
|
99
|
+
'errors',
|
|
100
|
+
],
|
|
101
|
+
'shard',
|
|
102
|
+
);
|
|
103
|
+
if (shard.kind !== JOURNEY_SHARD_KIND) {
|
|
104
|
+
throw new JourneyShardError(`shard.kind must be ${JOURNEY_SHARD_KIND}`);
|
|
105
|
+
}
|
|
106
|
+
if (shard.version !== JOURNEY_SHARD_VERSION) {
|
|
107
|
+
throw new JourneyShardUpgradeError(
|
|
108
|
+
`unsupported journey shard version ${String(shard.version)}; migrate to version ${JOURNEY_SHARD_VERSION}`,
|
|
109
|
+
);
|
|
110
|
+
}
|
|
111
|
+
try {
|
|
112
|
+
validateRunIdentity(shard.runIdentity);
|
|
113
|
+
} catch (error) {
|
|
114
|
+
if (
|
|
115
|
+
error instanceof RunContractError &&
|
|
116
|
+
/tool is not this ux-guard version|schemas is invalid/.test(error.message)
|
|
117
|
+
) {
|
|
118
|
+
throw new JourneyShardUpgradeError(
|
|
119
|
+
`trusted ux-guard cannot consume shard tool/schema identity: ${error.message}`,
|
|
120
|
+
);
|
|
121
|
+
}
|
|
122
|
+
throw error;
|
|
123
|
+
}
|
|
124
|
+
assertId(shard.useCaseId, 'shard.useCaseId');
|
|
125
|
+
assertId(shard.journeyId, 'shard.journeyId');
|
|
126
|
+
assertId(shard.project, 'shard.project');
|
|
127
|
+
if (!Number.isSafeInteger(shard.repeatIndex) || shard.repeatIndex < 0) {
|
|
128
|
+
throw new JourneyShardError('shard.repeatIndex is invalid');
|
|
129
|
+
}
|
|
130
|
+
const expectedId = shardId(shard);
|
|
131
|
+
if (shard.id !== expectedId) throw new JourneyShardError('shard.id is not derived from policy ids');
|
|
132
|
+
if (!STATUSES.has(shard.status)) throw new JourneyShardError('shard.status is invalid');
|
|
133
|
+
const measured = shard.status === 'successful' || shard.status === 'unexecutable';
|
|
134
|
+
if (measured) {
|
|
135
|
+
if (
|
|
136
|
+
!shard.journey ||
|
|
137
|
+
shard.journey.useCaseId !== shard.useCaseId ||
|
|
138
|
+
shard.journey.journeyId !== shard.journeyId ||
|
|
139
|
+
shard.journey.status !== shard.status
|
|
140
|
+
) {
|
|
141
|
+
throw new JourneyShardError('measured shard journey identity or status does not match');
|
|
142
|
+
}
|
|
143
|
+
} else if (shard.journey !== null) {
|
|
144
|
+
throw new JourneyShardError('failed, skipped, and infrastructure shards cannot claim measurements');
|
|
145
|
+
}
|
|
146
|
+
if (!Array.isArray(shard.errors)) throw new JourneyShardError('shard.errors must be an array');
|
|
147
|
+
if (measured && shard.errors.length) {
|
|
148
|
+
throw new JourneyShardError('successful and unexecutable shards cannot contain run errors');
|
|
149
|
+
}
|
|
150
|
+
if (['failed', 'infrastructure-error'].includes(shard.status) && shard.errors.length === 0) {
|
|
151
|
+
throw new JourneyShardError(`${shard.status} shard must contain an error`);
|
|
152
|
+
}
|
|
153
|
+
for (const [index, error] of shard.errors.entries()) {
|
|
154
|
+
const path = `shard.errors[${index}]`;
|
|
155
|
+
assertMap(error, path);
|
|
156
|
+
assertExactKeys(error, ['category', 'message'], path);
|
|
157
|
+
if (!ERROR_CATEGORIES.has(error.category)) {
|
|
158
|
+
throw new JourneyShardError(`${path}.category is invalid`);
|
|
159
|
+
}
|
|
160
|
+
assertText(error.message, `${path}.message`);
|
|
161
|
+
}
|
|
162
|
+
return shard;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
export function serializeJourneyShard(shard) {
|
|
166
|
+
validateJourneyShard(shard);
|
|
167
|
+
return `${JSON.stringify(canonicalize(shard), null, 2)}\n`;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
export function parseJourneyShard(text) {
|
|
171
|
+
let shard;
|
|
172
|
+
try {
|
|
173
|
+
shard = JSON.parse(text);
|
|
174
|
+
} catch {
|
|
175
|
+
throw new JourneyShardError('journey shard is not valid JSON');
|
|
176
|
+
}
|
|
177
|
+
return validateJourneyShard(shard);
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
export function shardId({ useCaseId, journeyId, project, repeatIndex }) {
|
|
181
|
+
for (const [label, value] of Object.entries({ useCaseId, journeyId, project })) {
|
|
182
|
+
assertId(value, label);
|
|
183
|
+
}
|
|
184
|
+
if (!Number.isSafeInteger(repeatIndex) || repeatIndex < 0) {
|
|
185
|
+
throw new JourneyShardError('repeatIndex must be a non-negative safe integer');
|
|
186
|
+
}
|
|
187
|
+
return `shard.${sha256(`${useCaseId}\0${journeyId}\0${project}\0${repeatIndex}`)}`;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
export function shardFileName(shard) {
|
|
191
|
+
validateShardIdentity(shard);
|
|
192
|
+
const digest = shardId(shard).slice('shard.'.length, 'shard.'.length + 24);
|
|
193
|
+
return `${safeSegment(shard.useCaseId)}--${safeSegment(shard.journeyId)}--${digest}.json`;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
export function writeJourneyShard(directory, shard) {
|
|
197
|
+
validateJourneyShard(shard);
|
|
198
|
+
validateShardDirectory(directory);
|
|
199
|
+
const path = join(directory, shardFileName(shard));
|
|
200
|
+
try {
|
|
201
|
+
writeFileSync(path, serializeJourneyShard(shard), { flag: 'wx', mode: 0o600 });
|
|
202
|
+
} catch (error) {
|
|
203
|
+
if (error?.code === 'EEXIST') {
|
|
204
|
+
throw new JourneyShardError(`immutable journey shard already exists: ${basename(path)}`);
|
|
205
|
+
}
|
|
206
|
+
throw new JourneyShardError(`journey shard could not be written: ${basename(path)}`);
|
|
207
|
+
}
|
|
208
|
+
return path;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
export function aggregateJourneyShards({
|
|
212
|
+
directory,
|
|
213
|
+
policy,
|
|
214
|
+
catalog,
|
|
215
|
+
runIdentity,
|
|
216
|
+
project = 'chromium',
|
|
217
|
+
repetitions = 1,
|
|
218
|
+
artifactPath = 'test-results/ux-guard/shards',
|
|
219
|
+
} = {}) {
|
|
220
|
+
validateRunIdentity(runIdentity);
|
|
221
|
+
assertId(project, 'project');
|
|
222
|
+
if (!Number.isSafeInteger(repetitions) || repetitions < 1) {
|
|
223
|
+
throw new JourneyShardError('repetitions must be a positive safe integer');
|
|
224
|
+
}
|
|
225
|
+
validateShardDirectory(directory);
|
|
226
|
+
const expected = expectedShards(policy, project, repetitions);
|
|
227
|
+
const files = readdirSync(directory).sort();
|
|
228
|
+
if (files.some((file) => !file.endsWith('.json'))) {
|
|
229
|
+
throw new JourneyShardError('shard directory contains an unexpected non-JSON artifact');
|
|
230
|
+
}
|
|
231
|
+
const byLogicalId = new Map();
|
|
232
|
+
const shardIds = new Set();
|
|
233
|
+
const manifest = [];
|
|
234
|
+
for (const file of files) {
|
|
235
|
+
if (file !== basename(file) || file.includes('..')) {
|
|
236
|
+
throw new JourneyShardError('shard path traversal is not allowed');
|
|
237
|
+
}
|
|
238
|
+
const path = join(directory, file);
|
|
239
|
+
const stats = lstatSync(path);
|
|
240
|
+
if (stats.isSymbolicLink() || !stats.isFile()) {
|
|
241
|
+
throw new JourneyShardError(`shard ${file} must be a regular file, not a symlink`);
|
|
242
|
+
}
|
|
243
|
+
const text = readFileSync(path, 'utf8');
|
|
244
|
+
const shard = parseJourneyShard(text);
|
|
245
|
+
if (file !== shardFileName(shard)) {
|
|
246
|
+
throw new JourneyShardError(`shard ${file} does not match its collision-resistant id`);
|
|
247
|
+
}
|
|
248
|
+
if (shardIds.has(shard.id)) throw new JourneyShardError(`duplicate shard id ${shard.id}`);
|
|
249
|
+
shardIds.add(shard.id);
|
|
250
|
+
assertExpectedRunIdentity(shard.runIdentity, runIdentity, file);
|
|
251
|
+
const logicalId = logicalShardId(shard);
|
|
252
|
+
if (!expected.has(logicalId)) {
|
|
253
|
+
throw new JourneyShardError(`unexpected shard ${shard.useCaseId}/${shard.journeyId}`);
|
|
254
|
+
}
|
|
255
|
+
if (byLogicalId.has(logicalId)) {
|
|
256
|
+
throw new JourneyShardError(`duplicate expected shard ${logicalId}`);
|
|
257
|
+
}
|
|
258
|
+
byLogicalId.set(logicalId, shard);
|
|
259
|
+
manifest.push({ id: shard.id, path: file, sha256: sha256(text) });
|
|
260
|
+
}
|
|
261
|
+
const missing = [...expected.keys()].filter((id) => !byLogicalId.has(id));
|
|
262
|
+
if (missing.length) {
|
|
263
|
+
throw new JourneyShardError(`missing expected shards: ${missing.join(', ')}`);
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
const collapsed = [];
|
|
267
|
+
const statuses = [];
|
|
268
|
+
const runErrors = [];
|
|
269
|
+
for (const required of requiredVariants(policy)) {
|
|
270
|
+
const repeated = [];
|
|
271
|
+
for (let repeatIndex = 0; repeatIndex < repetitions; repeatIndex += 1) {
|
|
272
|
+
repeated.push(
|
|
273
|
+
byLogicalId.get(
|
|
274
|
+
logicalShardId({
|
|
275
|
+
useCaseId: required.useCaseId,
|
|
276
|
+
journeyId: required.journeyId,
|
|
277
|
+
project,
|
|
278
|
+
repeatIndex,
|
|
279
|
+
}),
|
|
280
|
+
),
|
|
281
|
+
);
|
|
282
|
+
}
|
|
283
|
+
const reference = stableShardPayload(repeated[0]);
|
|
284
|
+
for (const shard of repeated.slice(1)) {
|
|
285
|
+
if (canonicalJson(stableShardPayload(shard)) !== canonicalJson(reference)) {
|
|
286
|
+
throw new JourneyShardError(
|
|
287
|
+
`unstable repeated measurement for ${required.useCaseId}/${required.journeyId}`,
|
|
288
|
+
);
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
const shard = repeated[0];
|
|
292
|
+
statuses.push(shard.status);
|
|
293
|
+
if (shard.journey) collapsed.push(shard.journey);
|
|
294
|
+
else {
|
|
295
|
+
collapsed.push(
|
|
296
|
+
createLifecycleJourney({
|
|
297
|
+
useCaseId: shard.useCaseId,
|
|
298
|
+
journeyId: shard.journeyId,
|
|
299
|
+
status: shard.status,
|
|
300
|
+
}),
|
|
301
|
+
);
|
|
302
|
+
}
|
|
303
|
+
shard.errors.forEach((error, index) => {
|
|
304
|
+
runErrors.push({
|
|
305
|
+
id: `error.${shard.useCaseId}.${shard.journeyId}.${index + 1}`,
|
|
306
|
+
category: error.category,
|
|
307
|
+
message: error.message,
|
|
308
|
+
journeyRef: `journey.${shard.useCaseId}.${shard.journeyId}`,
|
|
309
|
+
});
|
|
310
|
+
});
|
|
311
|
+
}
|
|
312
|
+
const manifestHash = sha256(canonicalJson(manifest));
|
|
313
|
+
const run = createRunEnvelope(runIdentity, {
|
|
314
|
+
statuses,
|
|
315
|
+
errors: runErrors,
|
|
316
|
+
artifacts: [
|
|
317
|
+
{
|
|
318
|
+
id: 'journey-shards',
|
|
319
|
+
path: artifactPath,
|
|
320
|
+
sha256: manifestHash,
|
|
321
|
+
},
|
|
322
|
+
],
|
|
323
|
+
});
|
|
324
|
+
return createJourneyReport(collapsed, { catalog, policy, run });
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
function assertExpectedRunIdentity(actual, expected, file) {
|
|
328
|
+
if (canonicalJson(actual) === canonicalJson(expected)) return;
|
|
329
|
+
if (
|
|
330
|
+
canonicalJson(actual.tool) !== canonicalJson(expected.tool) ||
|
|
331
|
+
canonicalJson(actual.policy) !== canonicalJson(expected.policy)
|
|
332
|
+
) {
|
|
333
|
+
throw new JourneyShardUpgradeError(
|
|
334
|
+
`trusted ux-guard cannot consume ${file}: tool/schema or policy generation differs from the trusted base`,
|
|
335
|
+
);
|
|
336
|
+
}
|
|
337
|
+
throw new JourneyShardError(
|
|
338
|
+
`shard ${file} provenance, browser/profile, or adapter identity does not match the expected run`,
|
|
339
|
+
);
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
function expectedShards(policy, project, repetitions) {
|
|
343
|
+
const expected = new Map();
|
|
344
|
+
for (const required of requiredVariants(policy)) {
|
|
345
|
+
for (let repeatIndex = 0; repeatIndex < repetitions; repeatIndex += 1) {
|
|
346
|
+
const identity = {
|
|
347
|
+
useCaseId: required.useCaseId,
|
|
348
|
+
journeyId: required.journeyId,
|
|
349
|
+
project,
|
|
350
|
+
repeatIndex,
|
|
351
|
+
};
|
|
352
|
+
expected.set(logicalShardId(identity), identity);
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
return expected;
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
function requiredVariants(policy) {
|
|
359
|
+
if (!Array.isArray(policy?.coverage)) {
|
|
360
|
+
throw new JourneyShardError('policy.coverage must be an array');
|
|
361
|
+
}
|
|
362
|
+
const variants = policy.coverage.flatMap((useCase) =>
|
|
363
|
+
useCase.requiredVariants.map((variant) => ({
|
|
364
|
+
useCaseId: useCase.useCaseId,
|
|
365
|
+
journeyId: variant.journeyId,
|
|
366
|
+
})),
|
|
367
|
+
);
|
|
368
|
+
const ids = new Set();
|
|
369
|
+
for (const variant of variants) {
|
|
370
|
+
const id = `${variant.useCaseId}\0${variant.journeyId}`;
|
|
371
|
+
if (ids.has(id)) throw new JourneyShardError(`policy repeats required variant ${id}`);
|
|
372
|
+
ids.add(id);
|
|
373
|
+
}
|
|
374
|
+
return variants.sort(
|
|
375
|
+
(left, right) =>
|
|
376
|
+
left.useCaseId.localeCompare(right.useCaseId) ||
|
|
377
|
+
left.journeyId.localeCompare(right.journeyId),
|
|
378
|
+
);
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
function stableShardPayload(shard) {
|
|
382
|
+
return {
|
|
383
|
+
status: shard.status,
|
|
384
|
+
journey: shard.journey,
|
|
385
|
+
errors: shard.errors,
|
|
386
|
+
};
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
function logicalShardId({ useCaseId, journeyId, project, repeatIndex }) {
|
|
390
|
+
return `${useCaseId}/${journeyId}/${project}/${repeatIndex}`;
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
function validateShardDirectory(directory) {
|
|
394
|
+
if (typeof directory !== 'string' || !directory) {
|
|
395
|
+
throw new JourneyShardError('shard directory must be a path');
|
|
396
|
+
}
|
|
397
|
+
if (!existsSync(directory)) throw new JourneyShardError('shard directory does not exist');
|
|
398
|
+
const stats = lstatSync(directory);
|
|
399
|
+
if (stats.isSymbolicLink() || !stats.isDirectory()) {
|
|
400
|
+
throw new JourneyShardError('shard directory must be a real directory, not a symlink');
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
function validateShardIdentity(shard) {
|
|
405
|
+
assertId(shard.useCaseId, 'useCaseId');
|
|
406
|
+
assertId(shard.journeyId, 'journeyId');
|
|
407
|
+
assertId(shard.project, 'project');
|
|
408
|
+
if (!Number.isSafeInteger(shard.repeatIndex) || shard.repeatIndex < 0) {
|
|
409
|
+
throw new JourneyShardError('repeatIndex is invalid');
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
function safeSegment(value) {
|
|
414
|
+
if (!ID.test(value)) throw new JourneyShardError('unsafe shard path segment');
|
|
415
|
+
return value;
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
function assertMap(value, path) {
|
|
419
|
+
if (value === null || typeof value !== 'object' || Array.isArray(value)) {
|
|
420
|
+
throw new JourneyShardError(`${path} must be an object`);
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
function assertExactKeys(value, expected, path) {
|
|
425
|
+
const actual = Object.keys(value).sort();
|
|
426
|
+
const wanted = [...expected].sort();
|
|
427
|
+
if (
|
|
428
|
+
actual.length !== wanted.length ||
|
|
429
|
+
actual.some((key, index) => key !== wanted[index])
|
|
430
|
+
) {
|
|
431
|
+
throw new JourneyShardError(`${path} must contain ${wanted.join(', ')} only`);
|
|
432
|
+
}
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
function assertId(value, path) {
|
|
436
|
+
if (typeof value !== 'string' || !ID.test(value)) {
|
|
437
|
+
throw new JourneyShardError(`${path} is not a safe id`);
|
|
438
|
+
}
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
function assertText(value, path) {
|
|
442
|
+
if (typeof value !== 'string' || !value.trim()) {
|
|
443
|
+
throw new JourneyShardError(`${path} must be non-empty text`);
|
|
444
|
+
}
|
|
445
|
+
}
|