Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions .changeset/39722-field-rule-option-label.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
---
'@objectstack/lint': minor
'@objectstack/spec': minor
---

fix(lint)!: a field rule (`requiredWhen`, `readonlyWhen`, `visibleWhen`, an option's `visibleWhen`) that compares a choice field with an option label, where the record holds the option's value, is refused at validate, build and publish

Clause-②: yes (narrowing)
Metadata-protocol: narrowed

<!-- adr-0087: registered field-rule-option-label-refused -->

**BREAKING** accept-set narrowing. A `select`, `radio`, `multiselect` or `checkboxes` field stores the option's value (`urgent`), never its label (`Urgent`), and a field's rules are evaluated against the stored record. So a field rule comparing the field with a label never applied: a `requiredWhen` of `record.priority == 'Urgent'` never required the field, a `readonlyWhen` never locked it, a `visibleWhen` never showed it (nor, on a required field, required it), and an option's `visibleWhen` never offered the option, so a write picking it was refused. Written with `!=` or a negated `in`, each held on every record. All published with no finding.

- **Now:** `os validate`, `os build` and `os lint` report `field-rule-option-label` as an error, and the metadata publish door refuses an `object` or `object_extension` publish carrying it with `422 INVALID_METADATA`. The finding names the field, the label written and the value to write. Judged in a field's `requiredWhen`, `readonlyWhen` and `visibleWhen` and in each option's `visibleWhen`, on the object's own fields and on the fields an object extension adds, for a field read as `record.FIELD` or `previous.FIELD`. The spellings reached: `FIELD == 'L'`, `FIELD != 'L'`, `'L' == FIELD`, `'L' != FIELD`, and each member of `FIELD in [...]` (negated or not). New export: `FIELD_RULE_OPTION_LABEL` from `@objectstack/lint`.
- **Not refused:** a `parent.FIELD` read (the master record's field), the membership spelling `'L' in FIELD` on a `multiselect` or `checkboxes` field, a label that is also some option's value, a literal that is neither a label nor a value, a `readonly` field, a field bound to a shared picklist or declared by two object extensions, a label holding a quote or a backslash, and a predicate in a dialect other than CEL.
- **Stored metadata:** a stored object or extension carrying the shape still loads and its rule evaluates as before. Its next publish is refused until the predicate compares the option value or is dropped.

| Before (refused now) | After |
|:--|:--|
| `requiredWhen: "record.priority == 'Urgent'"` | `requiredWhen: "record.priority == 'urgent'"` |
| `readonlyWhen: "record.priority != 'Urgent'"` | `readonlyWhen: "record.priority != 'urgent'"` |
| `options: [{ value: 'phone', label: 'Phone', visibleWhen: "record.priority == 'Low'" }]` | `options: [{ value: 'phone', label: 'Phone', visibleWhen: "record.priority == 'low'" }]` |

Fix: rewrite the label literal to the option's value, as the refusal names it.
6 changes: 6 additions & 0 deletions packages/lint/src/authoring-rules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3512,6 +3512,12 @@ export const AUTHORING_RULES: readonly AuthoringRule[] = [
// carries, and the target's own rules the door's live objects carry are
// judged in the baseline too, so an extension write is charged with its
// entry's rules alone. A rule switched off (`active: false`) is not judged.
//
// [#39722] The same entry judges a field's own rules with the same reading,
// under its own id (`field-rule-option-label`): a field's `requiredWhen`,
// `readonlyWhen` and `visibleWhen` and each option's `visibleWhen`, on an
// object's fields and on the fields an extension entry adds. One entry, so
// the doors and commands above are its doors and commands too.
{
name: 'validateValidationRuleOptionLabels',
tier: 'gating',
Expand Down
5 changes: 4 additions & 1 deletion packages/lint/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1033,10 +1033,13 @@ export type {

// #39441 — a validation rule's predicate comparing a choice field with an
// option LABEL where the record holds the value (never fires, or refuses
// every write when negated).
// every write when negated). #39722 — the same comparand in a field's own
// rule (`requiredWhen`, `readonlyWhen`, `visibleWhen`, an option's
// `visibleWhen`), reported under its own id by the same rule.
export {
validateValidationRuleOptionLabels,
VALIDATION_CONDITION_OPTION_LABEL,
FIELD_RULE_OPTION_LABEL,
} from './validate-validation-rule-option-labels.js';
export type { ValidationRuleOptionLabelFinding } from './validate-validation-rule-option-labels.js';

Expand Down
2 changes: 2 additions & 0 deletions packages/lint/src/rule-explanations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,7 @@ import {
FLOW_TRIGGER_OBJECT_MISSING_EXPLANATION,
FLOW_CONDITION_OPTION_LABEL_EXPLANATION,
VALIDATION_CONDITION_OPTION_LABEL_EXPLANATION,
FIELD_RULE_OPTION_LABEL_EXPLANATION,
FLOW_TIME_RELATIVE_SHADOWED_EXPLANATION,
FLOW_TIME_RELATIVE_KEY_MISPLACED_EXPLANATION,
FLOW_TIME_RELATIVE_DATE_FIELD_INVALID_EXPLANATION,
Expand Down Expand Up @@ -734,6 +735,7 @@ export const RULE_EXPLANATIONS: Readonly<Record<string, RuleExplanation>> = Obje
[FLOW_TRIGGER_OBJECT_MISSING_EXPLANATION.rule]: FLOW_TRIGGER_OBJECT_MISSING_EXPLANATION,
[FLOW_CONDITION_OPTION_LABEL_EXPLANATION.rule]: FLOW_CONDITION_OPTION_LABEL_EXPLANATION,
[VALIDATION_CONDITION_OPTION_LABEL_EXPLANATION.rule]: VALIDATION_CONDITION_OPTION_LABEL_EXPLANATION,
[FIELD_RULE_OPTION_LABEL_EXPLANATION.rule]: FIELD_RULE_OPTION_LABEL_EXPLANATION,
[FLOW_TIME_RELATIVE_SHADOWED_EXPLANATION.rule]: FLOW_TIME_RELATIVE_SHADOWED_EXPLANATION,
[FLOW_TIME_RELATIVE_KEY_MISPLACED_EXPLANATION.rule]: FLOW_TIME_RELATIVE_KEY_MISPLACED_EXPLANATION,
[FLOW_TIME_RELATIVE_DATE_FIELD_INVALID_EXPLANATION.rule]: FLOW_TIME_RELATIVE_DATE_FIELD_INVALID_EXPLANATION,
Expand Down
24 changes: 24 additions & 0 deletions packages/lint/src/rule-explanations/publish-time-inert-refusals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,30 @@ export const VALIDATION_CONDITION_OPTION_LABEL_EXPLANATION: RuleExplanation = {
],
};

export const FIELD_RULE_OPTION_LABEL_EXPLANATION: RuleExplanation = {
rule: 'field-rule-option-label',
covers: 'why field rules use option values',
paragraphs: [
'A select, radio, multiselect or checkboxes field stores the option\'s `value`, never its `label`, and a ' +
'field\'s rules are evaluated against the record as stored (the server on every write, the form against ' +
'the values it holds). A rule that compares the field with a quoted label (`record.priority == \'Urgent\'` ' +
'where the option is `{ value: \'urgent\', label: \'Urgent\' }`) is false on every record: a ' +
'`requiredWhen` never requires the field, a `readonlyWhen` never locks it, a `visibleWhen` never shows it ' +
'(and so, on a required field, never requires it), and an option\'s `visibleWhen` never offers the option, ' +
'so every write that picks it is refused. Written with `!=` or a negated `in`, it is true on every record.',
'Reported in a field\'s `requiredWhen`, `readonlyWhen` and `visibleWhen` and in each option\'s ' +
'`visibleWhen`, on the object\'s own fields and on the fields an object extension adds. Only when the label ' +
'is the comparand of that field: `F == \'L\'`, `F != \'L\'`, `\'L\' == F`, `\'L\' != F`, or a member ' +
'of `F in [...]`, with `F` read as `record.F` or `previous.F`. The membership spelling `\'L\' in F` on a ' +
'multiselect or checkboxes field is not read.',
'Not reported: a `parent.F` read (the master record\'s field, another object\'s options), a label that is ' +
'also some option\'s value, a literal that is neither a label nor a value (a stored value may lie outside ' +
'the options), a readonly field, a field whose options come from a shared picklist or that two object ' +
'extensions both declare, a label holding a quote or a backslash, and a predicate in a dialect other than ' +
'CEL. Write the option\'s value in place of the label.',
],
};

export const FLOW_TIME_RELATIVE_SHADOWED_EXPLANATION: RuleExplanation = {
rule: 'flow-time-relative-shadowed',
covers: 'which trigger wins on a start with both',
Expand Down
Loading
Loading