Skip to content

Conditionally required

conditionallyRequired enforces that a set of fields becomes required when a controlling field matches a specific value.

If the condition is met and any required field is missing or rejected, the rule emits a boundary‑level error for each missing field. This rule is ideal for type‑driven shapes, mode‑dependent configurations, and any scenario where additional fields become mandatory only under certain conditions.

Signature

export function conditionallyRequired(
    typeKey: string,
    typeValue: unknown,
    ...required: string[]
): BoundaryRule

Parameters

Parameter Data type Description
typeKey string The controlling field whose final value determines whether requirements apply.
typeValue unknown The value that triggers the requirement set.
required string[] The fields that must be accepted when the condition is met.

Events

Event code Description
boundary.does.require-field A required field was missing or rejected under the condition.

Design rationale

  • Activates only when the controlling field’s final value equals the specified trigger value.
  • Emits one error per missing field, ensuring precise contributor feedback.
  • Evaluates required fields strictly by acceptance (decision.code === "accept").
  • Does not modify field values or decisions — it only adds boundary‑level issues.
  • Supports clear, declarative modeling of conditional schemas.

Invoke

conditionallyRequired runs during the boundary decision phase. It is not affected by normalization mode or parsing rules.

The rule activates when:

  • fields[typeKey]?.final strictly equals typeValue.
  • Any of the required fields are missing or not accepted.

Examples

Condition met — all required fields present

conditionallyRequired("kind", "email", "address", "verified");

// kind = "email"
// address accepted
// verified accepted
// → OK (no boundary issues)

Condition met — missing required fields

conditionallyRequired("kind", "email", "address", "verified");

// kind = "email"
// address accepted
// verified missing
// → boundary.does.require-field at $.verified

Condition not met — rule does nothing

conditionallyRequired("kind", "email", "address");

// kind = "sms"
// → no required‑field checks performed