#Guard Parameters?

1 messages · Page 1 of 1 (latest)

round flume
#

This is a question i guess for both stately and xstate. I see that in stately you can add parameters to an action, I'd like to be able to do the same for guards. Are guard parameters in xstate, and if so can I add them on a stately statechart?

round flume
#

Guard Parameters?

polar imp
#

It works in v4. Haven't seen how it'll work in v5.

The guard:

fieldHasValue: (ctx, event, { cond }) => {
  // @ts-expect-error Field does exist
  const field = cond.field;
  // @ts-expect-error Field does exist
  const expectedValue = cond.expected;

  return ctx[`${field}`] === expectedValue;
},

And usage:

{ 
  cond: {
    type: 'fieldHasValue',
    field: 'securityMethod',
    expected: 'new-account',
  },
  target: 'newAccount',
},
round flume
#

How would a user add them on stately ui?

summer basin
#

In v5 this works:

Guard:

fieldHasValue: ({context, guard}) => {
  const {field, expected} = guard.params;

  return context[field] === expected;
}

Usage:

{
  target: 'newAccount',
  guard: {
    type: 'fieldHasValue',
    params: {
      field: 'securityMethod',
      expected: 'new-account',
    },
  },
},
round flume
round flume
polar imp
worldly dawn
#

Guard parameters are in XState v4 and v5:

// XState v4
cond: { type: 'greaterThan', value: 4 }

// XState v5
guard: { type: 'greaterThan', params: { value: 4 } }
// note how params are necessary