Skip to main content

validateStateMachine

Check a state machine for structural problems: states as nodes, transitions as directed edges between them. CnStateMachineEditor runs it from its validate() method; a host can also run it itself, for example in a store before it publishes. The host's server stays the authority on what it accepts; this gives fast, itemised feedback first.

Signature​

import { validateStateMachine } from '@conduction/nextcloud-vue'

const issues = validateStateMachine({ states, transitions }, options)
ArgumentTypeDescription
graph.statesArray | stringThe states, each with an id. A JSON string is parsed.
graph.transitionsArray | stringThe transitions, each with a source and a target state id. A JSON string is parsed; malformed JSON counts as no transitions.
options.keysobjectRecord keys by role, when the host's records use other names. Defaults: { id: 'id', label: 'label', final: 'final', from: 'from', to: 'to', transitionLabel: 'label' }.
options.messagesobjectMessage templates by rule code, replacing the library's own. Placeholders are filled after translation.
options.translateFunctionApplied to a template from messages before its placeholders are filled, so a host passes its own t and keeps its own translations.

Return value​

An array of { type, code, message }. type is 'error' or 'warning'. An empty machine returns []: it is unfinished, not wrong.

Rules​

CodeTypePlaceholdersWhen
NO_FINAL_STATEerrorNo state has final === true.
UNREACHABLE_FINALerror{name}A final state cannot be reached from any starting state (one nothing leads into). When every state has an incoming transition there is no starting state, so nothing is reachable.
DANGLING_TRANSITIONerror{label}A transition points at a state that is not in states.
DUPLICATE_TRANSITIONwarning{from}, {to}Two transitions share a source and a target.
ORPHAN_STATEwarning{name}A state has no transitions at all, in a machine of more than one state.
CYCLE_NO_EXITwarning{names}A cycle none of whose states can reach a final state.

Example with a host's own keys and texts​

const issues = validateStateMachine(
{ states: statusTypes, transitions: template.transitions },
{
keys: { label: 'name', final: 'isFinal', from: 'fromStatus', to: 'toStatus' },
messages: { NO_FINAL_STATE: 'Workflow has no final status defined' },
translate: (text) => t('myapp', text),
},
)