medusajs/medusa · error · MedusaError

Rule must have an attribute, an operator and a value

Error message

Rule must have an attribute, an operator and a value

What it means

validateRule checks that a shipping-option rule object has attribute, operator, and value; missing any of the three throws INVALID_DATA. Note value must be truthy, so empty-string or 0 values are also rejected.

Source

Thrown at packages/modules/fulfillment/src/utils/utils.ts:100

    const { attribute, operator, value } = rule
    const contextValue = pickValueFromObject(attribute, context)

    return operatorsPredicate[operator](
      `${contextValue}`,
      value as string & string[]
    )
  }

  return loopComparator.apply(rules, [predicate])
}

/**
 * Validate contextValue rule object
 * @param rule
 */
export function validateRule(rule: Record<string, unknown>): boolean {
  if (!rule.attribute || !rule.operator || !rule.value) {
    throw new MedusaError(
      MedusaError.Types.INVALID_DATA,
      "Rule must have an attribute, an operator and a value"
    )
  }

  if (!isString(rule.attribute)) {
    throw new MedusaError(
      MedusaError.Types.INVALID_DATA,
      "Rule attribute must be a string"
    )
  }

  if (!isString(rule.operator)) {
    throw new MedusaError(
      MedusaError.Types.INVALID_DATA,
      "Rule operator must be a string"
    )
  }

View on GitHub (pinned to 5e06e544a2)

Solutions

  1. Ensure every rule has non-empty attribute, operator, and value
  2. Guard before submit: rule.attribute && rule.operator && rule.value !== undefined && rule.value !== ''
  3. For in/nin use a non-empty array as value

Example fix

// before
rules: [{ attribute: 'cart_total', operator: 'gt' }]
// after
rules: [{ attribute: 'cart_total', operator: 'gt', value: 100 }]
Defensive patterns

Strategy: validation

Validate before calling

const isCompleteRule = (r: any) => !!r?.attribute && !!r?.operator && r?.value !== undefined && r?.value !== '' && !(Array.isArray(r.value) && !r.value.length)

Type guard

const isCompleteRule = (r: any): r is { attribute: string; operator: string; value: unknown } => !!r?.attribute && !!r?.operator && !!r?.value

Prevention

When it happens

Trigger: create/updateShippingOptions with rules like { attribute: 'iso', operator: 'eq' } (no value), or passing rules: [null] / partially built objects.

Common situations: Dynamic rule builders that submit incomplete forms; JSON payloads where value key is omitted.

Understand the failure class

Background: Schema validation failed / invalid input schema: payload rejected because its shape doesn't match the expected schema — this error's family across 28 libraries.

Related errors


AI-assisted analysis of medusajs/medusa@5e06e544a2 (2026-08-27). Data as JSON: /api/errors/85a626cfab556194. Report an issue: GitHub.