xstate 6.0.0-alpha.4 → 6.0.0-alpha.41
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/README.md +19 -17
- package/bin/xstate.js +36 -0
- package/dist/StateMachine-8e73d782.cjs.js +1200 -0
- package/dist/StateMachine-db4e6d4b.development.cjs.js +1232 -0
- package/dist/StateMachine-ee89d49f.esm.js +1197 -0
- package/dist/StateMachine-f60abe05.development.esm.js +1229 -0
- package/dist/createActor-20615484.development.cjs.js +5717 -0
- package/dist/createActor-a3f7750f.esm.js +5578 -0
- package/dist/createActor-c97c04c9.development.esm.js +5620 -0
- package/dist/createActor-f3afd774.cjs.js +5675 -0
- package/dist/declarations/src/State.d.ts +17 -4
- package/dist/declarations/src/StateMachine.d.ts +37 -16
- package/dist/declarations/src/StateNode.d.ts +5 -2
- package/dist/declarations/src/actions.d.ts +10 -7
- package/dist/declarations/src/actors/callback.d.ts +2 -0
- package/dist/declarations/src/actors/logic.d.ts +7 -24
- package/dist/declarations/src/actors/observable.d.ts +3 -0
- package/dist/declarations/src/actors/promise.d.ts +6 -2
- package/dist/declarations/src/createActor.d.ts +64 -20
- package/dist/declarations/src/createMachine.d.ts +21 -13
- package/dist/declarations/src/createMachineFromConfig.d.ts +64 -30
- package/dist/declarations/src/durable/index.d.ts +133 -0
- package/dist/declarations/src/effectDescriptor.d.ts +81 -0
- package/dist/declarations/src/fsm.d.ts +77 -0
- package/dist/declarations/src/index.d.ts +15 -8
- package/dist/declarations/src/inspection.d.ts +11 -1
- package/dist/declarations/src/machineVersion.types.d.ts +38 -0
- package/dist/declarations/src/machineVersions.d.ts +100 -0
- package/dist/declarations/src/runtimeHelpers.d.ts +35 -0
- package/dist/declarations/src/schema.types.d.ts +5 -0
- package/dist/declarations/src/serialize.d.ts +9 -22
- package/dist/declarations/src/setup.d.ts +317 -64
- package/dist/declarations/src/spawn.d.ts +5 -5
- package/dist/declarations/src/stateUtils.d.ts +19 -7
- package/dist/declarations/src/system.d.ts +74 -11
- package/dist/declarations/src/transitionActions.d.ts +14 -2
- package/dist/declarations/src/types.d.ts +318 -134
- package/dist/declarations/src/types.v6.d.ts +306 -52
- package/dist/declarations/src/utils.d.ts +5 -3
- package/dist/declarations/src/validation/index.d.ts +37 -0
- package/dist/declarations/src/validation.types.d.ts +22 -0
- package/dist/effectDescriptor-57ae11fa.esm.js +250 -0
- package/dist/effectDescriptor-b7c48ceb.development.esm.js +250 -0
- package/dist/effectDescriptor-ba9e14f0.development.cjs.js +257 -0
- package/dist/effectDescriptor-e2bb61e2.cjs.js +257 -0
- package/dist/index-7fd92683.cjs.js +941 -0
- package/dist/index-a8ce72f1.esm.js +932 -0
- package/dist/index-b5b1b2bc.development.esm.js +932 -0
- package/dist/index-d42efc07.development.cjs.js +941 -0
- package/dist/xstate-actors.cjs.js +6 -5
- package/dist/xstate-actors.development.cjs.js +6 -5
- package/dist/xstate-actors.development.esm.js +2 -1
- package/dist/xstate-actors.esm.js +2 -1
- package/dist/xstate-actors.umd.min.js +1 -1
- package/dist/xstate-actors.umd.min.js.map +1 -1
- package/dist/xstate-durable.cjs.d.mts +2 -0
- package/dist/xstate-durable.cjs.d.ts +2 -0
- package/dist/xstate-durable.cjs.js +327 -0
- package/dist/xstate-durable.cjs.mjs +5 -0
- package/dist/xstate-durable.development.cjs.js +327 -0
- package/dist/xstate-durable.development.cjs.mjs +5 -0
- package/dist/xstate-durable.development.esm.js +321 -0
- package/dist/xstate-durable.esm.js +321 -0
- package/dist/xstate-durable.umd.min.js +2 -0
- package/dist/xstate-durable.umd.min.js.map +1 -0
- package/dist/xstate-graph.cjs.js +16 -19
- package/dist/xstate-graph.development.cjs.js +16 -19
- package/dist/xstate-graph.development.esm.js +13 -16
- package/dist/xstate-graph.esm.js +13 -16
- package/dist/xstate-graph.umd.min.js +1 -1
- package/dist/xstate-graph.umd.min.js.map +1 -1
- package/dist/xstate-validation.cjs.d.mts +2 -0
- package/dist/xstate-validation.cjs.d.ts +2 -0
- package/dist/xstate-validation.cjs.js +277 -0
- package/dist/xstate-validation.cjs.mjs +5 -0
- package/dist/xstate-validation.development.cjs.js +277 -0
- package/dist/xstate-validation.development.cjs.mjs +5 -0
- package/dist/xstate-validation.development.esm.js +271 -0
- package/dist/xstate-validation.esm.js +271 -0
- package/dist/xstate-validation.umd.min.js +2 -0
- package/dist/xstate-validation.umd.min.js.map +1 -0
- package/dist/xstate.cjs.js +1361 -412
- package/dist/xstate.cjs.mjs +9 -0
- package/dist/xstate.development.cjs.js +1361 -412
- package/dist/xstate.development.cjs.mjs +9 -0
- package/dist/xstate.development.esm.js +1332 -392
- package/dist/xstate.esm.js +1332 -392
- package/dist/xstate.umd.min.js +1 -1
- package/dist/xstate.umd.min.js.map +1 -1
- package/durable/package.json +8 -0
- package/package.json +39 -3
- package/validation/package.json +8 -0
- package/dist/StateMachine-445a8480.cjs.js +0 -732
- package/dist/StateMachine-4deb4090.development.esm.js +0 -746
- package/dist/StateMachine-f310bc72.esm.js +0 -729
- package/dist/StateMachine-f83f01f9.development.cjs.js +0 -749
- package/dist/index-918ab496.cjs.js +0 -4397
- package/dist/index-9cce3480.esm.js +0 -4342
- package/dist/index-ccbe7c0b.development.esm.js +0 -4381
- package/dist/index-ebaf6fd8.development.cjs.js +0 -4436
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
export * from "./declarations/src/validation/index.js";
|
|
2
|
+
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoieHN0YXRlLXZhbGlkYXRpb24uY2pzLmQubXRzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi9kZWNsYXJhdGlvbnMvc3JjL3ZhbGlkYXRpb24vaW5kZXguZC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSJ9
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
export * from "./declarations/src/validation/index.js";
|
|
2
|
+
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoieHN0YXRlLXZhbGlkYXRpb24uY2pzLmQudHMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuL2RlY2xhcmF0aW9ucy9zcmMvdmFsaWRhdGlvbi9pbmRleC5kLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUFBIn0=
|
|
@@ -0,0 +1,277 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
const actorValidationErrorSymbol = Symbol.for('xstate.actorValidationError');
|
|
6
|
+
/** A structured failure produced by `standardSchemaValidator()`. */
|
|
7
|
+
class ActorValidationError extends Error {
|
|
8
|
+
constructor(options) {
|
|
9
|
+
super(getMessage(options), {
|
|
10
|
+
cause: options.cause
|
|
11
|
+
});
|
|
12
|
+
this[actorValidationErrorSymbol] = true;
|
|
13
|
+
this.reason = void 0;
|
|
14
|
+
this.boundary = void 0;
|
|
15
|
+
this.logicId = void 0;
|
|
16
|
+
this.stateNodeId = void 0;
|
|
17
|
+
this.eventType = void 0;
|
|
18
|
+
this.eventOrigin = void 0;
|
|
19
|
+
this.childId = void 0;
|
|
20
|
+
this.issues = void 0;
|
|
21
|
+
this.cause = void 0;
|
|
22
|
+
this.name = 'ActorValidationError';
|
|
23
|
+
this.reason = options.reason;
|
|
24
|
+
this.boundary = options.boundary;
|
|
25
|
+
this.logicId = options.logicId;
|
|
26
|
+
this.stateNodeId = options.stateNodeId;
|
|
27
|
+
this.eventType = options.eventType;
|
|
28
|
+
this.eventOrigin = options.eventOrigin;
|
|
29
|
+
this.childId = options.childId;
|
|
30
|
+
this.issues = options.issues;
|
|
31
|
+
this.cause = options.cause;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
function isActorValidationError(value) {
|
|
35
|
+
return !!(value && typeof value === 'object' && actorValidationErrorSymbol in value);
|
|
36
|
+
}
|
|
37
|
+
/** Creates a synchronous, assertion-only Standard Schema validator. */
|
|
38
|
+
function standardSchemaValidator(options = {}) {
|
|
39
|
+
const unknownEvents = options.unknownEvents ?? 'error';
|
|
40
|
+
const unknownEmitted = options.unknownEmitted ?? 'error';
|
|
41
|
+
const checkTarget = target => {
|
|
42
|
+
if (!target.schema) {
|
|
43
|
+
if (target.boundary === 'event' && unknownEvents === 'error') {
|
|
44
|
+
return createError(target, 'unknownEvent');
|
|
45
|
+
}
|
|
46
|
+
if (target.boundary === 'emitted' && unknownEmitted === 'error') {
|
|
47
|
+
return createError(target, 'unknownEmitted');
|
|
48
|
+
}
|
|
49
|
+
return undefined;
|
|
50
|
+
}
|
|
51
|
+
let result;
|
|
52
|
+
try {
|
|
53
|
+
result = target.schema['~standard'].validate(target.value);
|
|
54
|
+
} catch (cause) {
|
|
55
|
+
return createError(target, 'schemaThrew', undefined, cause);
|
|
56
|
+
}
|
|
57
|
+
if (isPromiseLike(result)) {
|
|
58
|
+
void Promise.resolve(result).catch(() => {});
|
|
59
|
+
return createError(target, 'asyncValidationUnsupported');
|
|
60
|
+
}
|
|
61
|
+
if (result.issues) {
|
|
62
|
+
return createError(target, 'invalid', result.issues);
|
|
63
|
+
}
|
|
64
|
+
return undefined;
|
|
65
|
+
};
|
|
66
|
+
const checkEvent = (logic, event, eventOrigin) => {
|
|
67
|
+
const schemas = getSchemas(logic);
|
|
68
|
+
if (!schemas?.events || event.type.startsWith('xstate.') || event.type.startsWith('@xstate.')) {
|
|
69
|
+
return undefined;
|
|
70
|
+
}
|
|
71
|
+
return checkTarget({
|
|
72
|
+
schema: schemas.events[event.type],
|
|
73
|
+
value: getPayload(event),
|
|
74
|
+
boundary: 'event',
|
|
75
|
+
logicId: getLogicId(logic),
|
|
76
|
+
eventType: event.type,
|
|
77
|
+
eventOrigin
|
|
78
|
+
});
|
|
79
|
+
};
|
|
80
|
+
const checkEmitted = (logic, effects) => {
|
|
81
|
+
const schemas = getSchemas(logic);
|
|
82
|
+
if (!schemas?.emitted) {
|
|
83
|
+
return undefined;
|
|
84
|
+
}
|
|
85
|
+
for (const effect of effects) {
|
|
86
|
+
if (effect.kind !== 'emit') {
|
|
87
|
+
continue;
|
|
88
|
+
}
|
|
89
|
+
const error = checkTarget({
|
|
90
|
+
schema: schemas.emitted[effect.event.type],
|
|
91
|
+
value: getPayload(effect.event),
|
|
92
|
+
boundary: 'emitted',
|
|
93
|
+
logicId: getLogicId(logic),
|
|
94
|
+
eventType: effect.event.type
|
|
95
|
+
});
|
|
96
|
+
if (error) {
|
|
97
|
+
return error;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
return undefined;
|
|
101
|
+
};
|
|
102
|
+
const checkMachineResult = (machine, snapshot) => {
|
|
103
|
+
let error = checkTarget({
|
|
104
|
+
schema: machine.schemas?.context,
|
|
105
|
+
value: snapshot.context,
|
|
106
|
+
boundary: 'context',
|
|
107
|
+
logicId: machine.id
|
|
108
|
+
});
|
|
109
|
+
if (error) {
|
|
110
|
+
return error;
|
|
111
|
+
}
|
|
112
|
+
for (const stateNode of snapshot.nodes) {
|
|
113
|
+
// the root state node reuses the machine-level `schemas` object, which is
|
|
114
|
+
// already validated above and elsewhere; those are not state-local
|
|
115
|
+
if (stateNode === machine.root) {
|
|
116
|
+
continue;
|
|
117
|
+
}
|
|
118
|
+
error = checkTarget({
|
|
119
|
+
schema: stateNode.schemas?.context,
|
|
120
|
+
value: snapshot.context,
|
|
121
|
+
boundary: 'state.context',
|
|
122
|
+
logicId: machine.id,
|
|
123
|
+
stateNodeId: stateNode.id
|
|
124
|
+
});
|
|
125
|
+
if (error) {
|
|
126
|
+
return error;
|
|
127
|
+
}
|
|
128
|
+
if (stateNode.schemas?.input) {
|
|
129
|
+
error = checkTarget({
|
|
130
|
+
schema: stateNode.schemas.input,
|
|
131
|
+
value: snapshot._stateInputs[stateNode.id],
|
|
132
|
+
boundary: 'state.input',
|
|
133
|
+
logicId: machine.id,
|
|
134
|
+
stateNodeId: stateNode.id
|
|
135
|
+
});
|
|
136
|
+
if (error) {
|
|
137
|
+
return error;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
for (const childId of Object.keys(machine.schemas?.children ?? {})) {
|
|
142
|
+
const child = snapshot.children[childId];
|
|
143
|
+
if (child !== undefined) {
|
|
144
|
+
error = checkTarget({
|
|
145
|
+
schema: machine.schemas.children[childId],
|
|
146
|
+
value: child,
|
|
147
|
+
boundary: 'child',
|
|
148
|
+
logicId: machine.id,
|
|
149
|
+
childId
|
|
150
|
+
});
|
|
151
|
+
if (error) {
|
|
152
|
+
return error;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
for (const timer of Object.values(snapshot.timers)) {
|
|
157
|
+
if (timer.type === '@xstate.raise') {
|
|
158
|
+
error = checkEvent(machine, timer.event, 'raised');
|
|
159
|
+
if (error) {
|
|
160
|
+
return error;
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
return undefined;
|
|
165
|
+
};
|
|
166
|
+
return {
|
|
167
|
+
check(request) {
|
|
168
|
+
const {
|
|
169
|
+
logic
|
|
170
|
+
} = request;
|
|
171
|
+
const schemas = getSchemas(logic);
|
|
172
|
+
if (request.kind === 'input') {
|
|
173
|
+
return checkTarget({
|
|
174
|
+
schema: schemas?.input,
|
|
175
|
+
value: request.input,
|
|
176
|
+
boundary: 'input',
|
|
177
|
+
logicId: getLogicId(logic)
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
if (request.kind === 'event') {
|
|
181
|
+
return checkEvent(logic, request.event, request.eventOrigin);
|
|
182
|
+
}
|
|
183
|
+
const {
|
|
184
|
+
snapshot,
|
|
185
|
+
effects
|
|
186
|
+
} = request;
|
|
187
|
+
if (isStateMachine(logic)) {
|
|
188
|
+
const error = checkMachineResult(logic, snapshot);
|
|
189
|
+
if (error) {
|
|
190
|
+
return error;
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
if (snapshot.status === 'done') {
|
|
194
|
+
const error = checkTarget({
|
|
195
|
+
schema: schemas?.output,
|
|
196
|
+
value: snapshot.output,
|
|
197
|
+
boundary: 'output',
|
|
198
|
+
logicId: getLogicId(logic)
|
|
199
|
+
});
|
|
200
|
+
if (error) {
|
|
201
|
+
return error;
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
return checkEmitted(logic, effects);
|
|
205
|
+
}
|
|
206
|
+
};
|
|
207
|
+
}
|
|
208
|
+
function createError(request, reason, issues, cause) {
|
|
209
|
+
return new ActorValidationError({
|
|
210
|
+
reason,
|
|
211
|
+
boundary: request.boundary,
|
|
212
|
+
...(request.logicId === undefined ? {} : {
|
|
213
|
+
logicId: request.logicId
|
|
214
|
+
}),
|
|
215
|
+
...(request.stateNodeId === undefined ? {} : {
|
|
216
|
+
stateNodeId: request.stateNodeId
|
|
217
|
+
}),
|
|
218
|
+
...(request.eventType === undefined ? {} : {
|
|
219
|
+
eventType: request.eventType
|
|
220
|
+
}),
|
|
221
|
+
...(request.eventOrigin === undefined ? {} : {
|
|
222
|
+
eventOrigin: request.eventOrigin
|
|
223
|
+
}),
|
|
224
|
+
...(request.childId === undefined ? {} : {
|
|
225
|
+
childId: request.childId
|
|
226
|
+
}),
|
|
227
|
+
...(issues === undefined ? {} : {
|
|
228
|
+
issues
|
|
229
|
+
}),
|
|
230
|
+
...(cause === undefined ? {} : {
|
|
231
|
+
cause
|
|
232
|
+
})
|
|
233
|
+
});
|
|
234
|
+
}
|
|
235
|
+
function isStateMachine(logic) {
|
|
236
|
+
return 'root' in logic && 'schemas' in logic;
|
|
237
|
+
}
|
|
238
|
+
function getSchemas(logic) {
|
|
239
|
+
if (isStateMachine(logic)) {
|
|
240
|
+
return logic.schemas;
|
|
241
|
+
}
|
|
242
|
+
return logic.config?.schemas;
|
|
243
|
+
}
|
|
244
|
+
function getLogicId(logic) {
|
|
245
|
+
return 'id' in logic && typeof logic.id === 'string' ? logic.id : undefined;
|
|
246
|
+
}
|
|
247
|
+
function getPayload(event) {
|
|
248
|
+
const {
|
|
249
|
+
type: _,
|
|
250
|
+
...payload
|
|
251
|
+
} = event;
|
|
252
|
+
return payload;
|
|
253
|
+
}
|
|
254
|
+
function getMessage(options) {
|
|
255
|
+
const subject = options.eventType !== undefined ? ` "${options.eventType}"` : options.childId !== undefined ? ` "${options.childId}"` : '';
|
|
256
|
+
switch (options.reason) {
|
|
257
|
+
case 'unknownEvent':
|
|
258
|
+
return `Unknown event${subject}`;
|
|
259
|
+
case 'unknownEmitted':
|
|
260
|
+
return `Unknown emitted event${subject}`;
|
|
261
|
+
case 'asyncValidationUnsupported':
|
|
262
|
+
return `Async schema validation is unsupported for ${options.boundary}${subject}`;
|
|
263
|
+
case 'transformationUnsupported':
|
|
264
|
+
return `Schema transformations are unsupported for ${options.boundary}${subject}`;
|
|
265
|
+
case 'schemaThrew':
|
|
266
|
+
return `Schema threw while validating ${options.boundary}${subject}`;
|
|
267
|
+
case 'invalid':
|
|
268
|
+
return `Invalid ${options.boundary}${subject}`;
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
function isPromiseLike(value) {
|
|
272
|
+
return !!value && typeof value.then === 'function';
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
exports.ActorValidationError = ActorValidationError;
|
|
276
|
+
exports.isActorValidationError = isActorValidationError;
|
|
277
|
+
exports.standardSchemaValidator = standardSchemaValidator;
|
|
@@ -0,0 +1,277 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
const actorValidationErrorSymbol = Symbol.for('xstate.actorValidationError');
|
|
6
|
+
/** A structured failure produced by `standardSchemaValidator()`. */
|
|
7
|
+
class ActorValidationError extends Error {
|
|
8
|
+
constructor(options) {
|
|
9
|
+
super(getMessage(options), {
|
|
10
|
+
cause: options.cause
|
|
11
|
+
});
|
|
12
|
+
this[actorValidationErrorSymbol] = true;
|
|
13
|
+
this.reason = void 0;
|
|
14
|
+
this.boundary = void 0;
|
|
15
|
+
this.logicId = void 0;
|
|
16
|
+
this.stateNodeId = void 0;
|
|
17
|
+
this.eventType = void 0;
|
|
18
|
+
this.eventOrigin = void 0;
|
|
19
|
+
this.childId = void 0;
|
|
20
|
+
this.issues = void 0;
|
|
21
|
+
this.cause = void 0;
|
|
22
|
+
this.name = 'ActorValidationError';
|
|
23
|
+
this.reason = options.reason;
|
|
24
|
+
this.boundary = options.boundary;
|
|
25
|
+
this.logicId = options.logicId;
|
|
26
|
+
this.stateNodeId = options.stateNodeId;
|
|
27
|
+
this.eventType = options.eventType;
|
|
28
|
+
this.eventOrigin = options.eventOrigin;
|
|
29
|
+
this.childId = options.childId;
|
|
30
|
+
this.issues = options.issues;
|
|
31
|
+
this.cause = options.cause;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
function isActorValidationError(value) {
|
|
35
|
+
return !!(value && typeof value === 'object' && actorValidationErrorSymbol in value);
|
|
36
|
+
}
|
|
37
|
+
/** Creates a synchronous, assertion-only Standard Schema validator. */
|
|
38
|
+
function standardSchemaValidator(options = {}) {
|
|
39
|
+
const unknownEvents = options.unknownEvents ?? 'error';
|
|
40
|
+
const unknownEmitted = options.unknownEmitted ?? 'error';
|
|
41
|
+
const checkTarget = target => {
|
|
42
|
+
if (!target.schema) {
|
|
43
|
+
if (target.boundary === 'event' && unknownEvents === 'error') {
|
|
44
|
+
return createError(target, 'unknownEvent');
|
|
45
|
+
}
|
|
46
|
+
if (target.boundary === 'emitted' && unknownEmitted === 'error') {
|
|
47
|
+
return createError(target, 'unknownEmitted');
|
|
48
|
+
}
|
|
49
|
+
return undefined;
|
|
50
|
+
}
|
|
51
|
+
let result;
|
|
52
|
+
try {
|
|
53
|
+
result = target.schema['~standard'].validate(target.value);
|
|
54
|
+
} catch (cause) {
|
|
55
|
+
return createError(target, 'schemaThrew', undefined, cause);
|
|
56
|
+
}
|
|
57
|
+
if (isPromiseLike(result)) {
|
|
58
|
+
void Promise.resolve(result).catch(() => {});
|
|
59
|
+
return createError(target, 'asyncValidationUnsupported');
|
|
60
|
+
}
|
|
61
|
+
if (result.issues) {
|
|
62
|
+
return createError(target, 'invalid', result.issues);
|
|
63
|
+
}
|
|
64
|
+
return undefined;
|
|
65
|
+
};
|
|
66
|
+
const checkEvent = (logic, event, eventOrigin) => {
|
|
67
|
+
const schemas = getSchemas(logic);
|
|
68
|
+
if (!schemas?.events || event.type.startsWith('xstate.') || event.type.startsWith('@xstate.')) {
|
|
69
|
+
return undefined;
|
|
70
|
+
}
|
|
71
|
+
return checkTarget({
|
|
72
|
+
schema: schemas.events[event.type],
|
|
73
|
+
value: getPayload(event),
|
|
74
|
+
boundary: 'event',
|
|
75
|
+
logicId: getLogicId(logic),
|
|
76
|
+
eventType: event.type,
|
|
77
|
+
eventOrigin
|
|
78
|
+
});
|
|
79
|
+
};
|
|
80
|
+
const checkEmitted = (logic, effects) => {
|
|
81
|
+
const schemas = getSchemas(logic);
|
|
82
|
+
if (!schemas?.emitted) {
|
|
83
|
+
return undefined;
|
|
84
|
+
}
|
|
85
|
+
for (const effect of effects) {
|
|
86
|
+
if (effect.kind !== 'emit') {
|
|
87
|
+
continue;
|
|
88
|
+
}
|
|
89
|
+
const error = checkTarget({
|
|
90
|
+
schema: schemas.emitted[effect.event.type],
|
|
91
|
+
value: getPayload(effect.event),
|
|
92
|
+
boundary: 'emitted',
|
|
93
|
+
logicId: getLogicId(logic),
|
|
94
|
+
eventType: effect.event.type
|
|
95
|
+
});
|
|
96
|
+
if (error) {
|
|
97
|
+
return error;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
return undefined;
|
|
101
|
+
};
|
|
102
|
+
const checkMachineResult = (machine, snapshot) => {
|
|
103
|
+
let error = checkTarget({
|
|
104
|
+
schema: machine.schemas?.context,
|
|
105
|
+
value: snapshot.context,
|
|
106
|
+
boundary: 'context',
|
|
107
|
+
logicId: machine.id
|
|
108
|
+
});
|
|
109
|
+
if (error) {
|
|
110
|
+
return error;
|
|
111
|
+
}
|
|
112
|
+
for (const stateNode of snapshot.nodes) {
|
|
113
|
+
// the root state node reuses the machine-level `schemas` object, which is
|
|
114
|
+
// already validated above and elsewhere; those are not state-local
|
|
115
|
+
if (stateNode === machine.root) {
|
|
116
|
+
continue;
|
|
117
|
+
}
|
|
118
|
+
error = checkTarget({
|
|
119
|
+
schema: stateNode.schemas?.context,
|
|
120
|
+
value: snapshot.context,
|
|
121
|
+
boundary: 'state.context',
|
|
122
|
+
logicId: machine.id,
|
|
123
|
+
stateNodeId: stateNode.id
|
|
124
|
+
});
|
|
125
|
+
if (error) {
|
|
126
|
+
return error;
|
|
127
|
+
}
|
|
128
|
+
if (stateNode.schemas?.input) {
|
|
129
|
+
error = checkTarget({
|
|
130
|
+
schema: stateNode.schemas.input,
|
|
131
|
+
value: snapshot._stateInputs[stateNode.id],
|
|
132
|
+
boundary: 'state.input',
|
|
133
|
+
logicId: machine.id,
|
|
134
|
+
stateNodeId: stateNode.id
|
|
135
|
+
});
|
|
136
|
+
if (error) {
|
|
137
|
+
return error;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
for (const childId of Object.keys(machine.schemas?.children ?? {})) {
|
|
142
|
+
const child = snapshot.children[childId];
|
|
143
|
+
if (child !== undefined) {
|
|
144
|
+
error = checkTarget({
|
|
145
|
+
schema: machine.schemas.children[childId],
|
|
146
|
+
value: child,
|
|
147
|
+
boundary: 'child',
|
|
148
|
+
logicId: machine.id,
|
|
149
|
+
childId
|
|
150
|
+
});
|
|
151
|
+
if (error) {
|
|
152
|
+
return error;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
for (const timer of Object.values(snapshot.timers)) {
|
|
157
|
+
if (timer.type === '@xstate.raise') {
|
|
158
|
+
error = checkEvent(machine, timer.event, 'raised');
|
|
159
|
+
if (error) {
|
|
160
|
+
return error;
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
return undefined;
|
|
165
|
+
};
|
|
166
|
+
return {
|
|
167
|
+
check(request) {
|
|
168
|
+
const {
|
|
169
|
+
logic
|
|
170
|
+
} = request;
|
|
171
|
+
const schemas = getSchemas(logic);
|
|
172
|
+
if (request.kind === 'input') {
|
|
173
|
+
return checkTarget({
|
|
174
|
+
schema: schemas?.input,
|
|
175
|
+
value: request.input,
|
|
176
|
+
boundary: 'input',
|
|
177
|
+
logicId: getLogicId(logic)
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
if (request.kind === 'event') {
|
|
181
|
+
return checkEvent(logic, request.event, request.eventOrigin);
|
|
182
|
+
}
|
|
183
|
+
const {
|
|
184
|
+
snapshot,
|
|
185
|
+
effects
|
|
186
|
+
} = request;
|
|
187
|
+
if (isStateMachine(logic)) {
|
|
188
|
+
const error = checkMachineResult(logic, snapshot);
|
|
189
|
+
if (error) {
|
|
190
|
+
return error;
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
if (snapshot.status === 'done') {
|
|
194
|
+
const error = checkTarget({
|
|
195
|
+
schema: schemas?.output,
|
|
196
|
+
value: snapshot.output,
|
|
197
|
+
boundary: 'output',
|
|
198
|
+
logicId: getLogicId(logic)
|
|
199
|
+
});
|
|
200
|
+
if (error) {
|
|
201
|
+
return error;
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
return checkEmitted(logic, effects);
|
|
205
|
+
}
|
|
206
|
+
};
|
|
207
|
+
}
|
|
208
|
+
function createError(request, reason, issues, cause) {
|
|
209
|
+
return new ActorValidationError({
|
|
210
|
+
reason,
|
|
211
|
+
boundary: request.boundary,
|
|
212
|
+
...(request.logicId === undefined ? {} : {
|
|
213
|
+
logicId: request.logicId
|
|
214
|
+
}),
|
|
215
|
+
...(request.stateNodeId === undefined ? {} : {
|
|
216
|
+
stateNodeId: request.stateNodeId
|
|
217
|
+
}),
|
|
218
|
+
...(request.eventType === undefined ? {} : {
|
|
219
|
+
eventType: request.eventType
|
|
220
|
+
}),
|
|
221
|
+
...(request.eventOrigin === undefined ? {} : {
|
|
222
|
+
eventOrigin: request.eventOrigin
|
|
223
|
+
}),
|
|
224
|
+
...(request.childId === undefined ? {} : {
|
|
225
|
+
childId: request.childId
|
|
226
|
+
}),
|
|
227
|
+
...(issues === undefined ? {} : {
|
|
228
|
+
issues
|
|
229
|
+
}),
|
|
230
|
+
...(cause === undefined ? {} : {
|
|
231
|
+
cause
|
|
232
|
+
})
|
|
233
|
+
});
|
|
234
|
+
}
|
|
235
|
+
function isStateMachine(logic) {
|
|
236
|
+
return 'root' in logic && 'schemas' in logic;
|
|
237
|
+
}
|
|
238
|
+
function getSchemas(logic) {
|
|
239
|
+
if (isStateMachine(logic)) {
|
|
240
|
+
return logic.schemas;
|
|
241
|
+
}
|
|
242
|
+
return logic.config?.schemas;
|
|
243
|
+
}
|
|
244
|
+
function getLogicId(logic) {
|
|
245
|
+
return 'id' in logic && typeof logic.id === 'string' ? logic.id : undefined;
|
|
246
|
+
}
|
|
247
|
+
function getPayload(event) {
|
|
248
|
+
const {
|
|
249
|
+
type: _,
|
|
250
|
+
...payload
|
|
251
|
+
} = event;
|
|
252
|
+
return payload;
|
|
253
|
+
}
|
|
254
|
+
function getMessage(options) {
|
|
255
|
+
const subject = options.eventType !== undefined ? ` "${options.eventType}"` : options.childId !== undefined ? ` "${options.childId}"` : '';
|
|
256
|
+
switch (options.reason) {
|
|
257
|
+
case 'unknownEvent':
|
|
258
|
+
return `Unknown event${subject}`;
|
|
259
|
+
case 'unknownEmitted':
|
|
260
|
+
return `Unknown emitted event${subject}`;
|
|
261
|
+
case 'asyncValidationUnsupported':
|
|
262
|
+
return `Async schema validation is unsupported for ${options.boundary}${subject}`;
|
|
263
|
+
case 'transformationUnsupported':
|
|
264
|
+
return `Schema transformations are unsupported for ${options.boundary}${subject}`;
|
|
265
|
+
case 'schemaThrew':
|
|
266
|
+
return `Schema threw while validating ${options.boundary}${subject}`;
|
|
267
|
+
case 'invalid':
|
|
268
|
+
return `Invalid ${options.boundary}${subject}`;
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
function isPromiseLike(value) {
|
|
272
|
+
return !!value && typeof value.then === 'function';
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
exports.ActorValidationError = ActorValidationError;
|
|
276
|
+
exports.isActorValidationError = isActorValidationError;
|
|
277
|
+
exports.standardSchemaValidator = standardSchemaValidator;
|