Skip to content

Commit

Permalink
fix(core): Relax validation of custom process states
Browse files Browse the repository at this point in the history
The last release caused an error to be thrown on bootstrap
in the case of unreachable states. There are however valid
cases for making states unreachable, so now we will just print
a warning.
  • Loading branch information
michaelbromley committed Nov 20, 2023
1 parent f01533a commit cf301eb
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ describe('FSM validateTransitionDefinition()', () => {

const result = validateTransitionDefinition(valid, 'Start');

expect(result.valid).toBe(false);
expect(result.valid).toBe(true);
expect(result.error).toBe('The following states are unreachable: Unreachable');
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,15 @@ export function validateTransitionDefinition<T extends string>(
};
}

if (!allStatesReached()) {
return {
valid: false,
error: `The following states are unreachable: ${Object.entries(result)
.filter(([s, v]) => !(v as ValidationResult).reachable)
.map(([s]) => s)
.join(', ')}`,
};
} else {
return {
valid: true,
};
}
const error = !allStatesReached()
? `The following states are unreachable: ${Object.entries(result)
.filter(([s, v]) => !(v as ValidationResult).reachable)
.map(([s]) => s)
.join(', ')}`
: undefined;

return {
valid: true,
error,
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ export class FulfillmentStateMachine {
Logger.error(`The fulfillment process has an invalid configuration:`);
throw new Error(validationResult.error);
}
if (validationResult.valid && validationResult.error) {
Logger.warn(`Fulfillment process: ${validationResult.error}`);
}
return {
transitions: allTransitions,
onTransitionStart: async (fromState, toState, data) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ export class OrderStateMachine {
Logger.error(`The order process has an invalid configuration:`);
throw new Error(validationResult.error);
}
if (validationResult.valid && validationResult.error) {
Logger.warn(`Order process: ${validationResult.error}`);
}
return {
transitions: allTransitions,
onTransitionStart: async (fromState, toState, data) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ export class PaymentStateMachine {
Logger.error(`The payment process has an invalid configuration:`);
throw new Error(validationResult.error);
}
if (validationResult.valid && validationResult.error) {
Logger.warn(`Payment process: ${validationResult.error}`);
}
return {
transitions: allTransitions,
onTransitionStart: async (fromState, toState, data) => {
Expand Down

0 comments on commit cf301eb

Please sign in to comment.