Optional and bounded List properties — design #2671
dfrnt-hoijnet
started this conversation in
Ideas
Replies: 1 comment
|
The main direction involves to relax List to become optional with a mandatory option on the field, in multiple steps. The min/max cardinality may not be needed initially. These are the proposed PRs:
The advantage is the |
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
A
Listproperty is the only collection family that is mandatory today: the subject must hold exactly one edge to a cons structure, where[]is expressed as an edge tordf:nil.Set,ArrayandTableare absence-tolerant — no edge means an empty collection. The mandatory check lives inrefute_cardinality_(list(C))insrc/core/document/instance.pl, which demands cardinality one on the property edge.Two places in the codebase already assume
Listbehaves like the other collections and are inconsistent with the checker:is_required_propertyinsrc/core/api/api_jsonschema.plreports everyListas non-required, and the migration machinery lets aListproperty be added without a default (src/core/document/migration.pl,check_children_property_defaults). The change makes both assumptions true rather than patching either side.Task
Relax the unbounded
Listfamily so a missing field is legal, and giveListthe@min_cardinality/@max_cardinality/@cardinalitybounds vocabulary thatSetandCardinalityalready carry. Bounds count elements of the list. The presence of any bound in the spec makes the field required —min_cardinalitynever tolerates absence, somin: 0is the explicit spelling of today's "at least an empty list" contract.Goal
Optional lists and element bounds without a new keyword and without a separate presence flag: one rule — bounds imply required — covers optional, required-emptyable, required-non-empty and bounded lists. Every document valid under today's schema stays valid; the relaxation is a pure weakening of instance checking, not a data migration.
Semantics
Absence is tolerated if the spec carries no cardinality bound. When the edge is present, the element count must satisfy the bounds;
[]is a present field with zero elements. Within a bounded spec an unsetmindefaults to0and an unsetmaxtoinf;@cardinalityis shorthand for min = max = N. Edges on the property remain capped at one regardless of bounds — a second edge to a cons chain is always a violation.[][a][a,b][a,b,c]{"@type":"List"}unboundedmin:0min:1min:2,max:3max:2@cardinality:2Two consequences are worth stating in the docs.
{"@type":"List"}relaxes from mandatory to optional — the behaviour change — and the old contract is recovered withmin:0. And a bounded list is always required: "optional with a maximum length" is unexpressible, because any bound asserts presence. That is the price of the one-rule model; required-non-empty (min:1) covers the common real intent.Thus must be implemented in going from 12.0 --> 12.1 and would have to wait until 12.2 otherwise. Thus let's do it now during release candidate series.
Storage representation is unchanged:
[]still writes therdf:niledge and reads back[]; an absent field writes nothing and reads back with the key omitted. The two are representation-distinct but only[]satisfies a bounded spec. Writes are not normalised — documents keep the form they were submitted in.Migrations
Weakening direction: unbounded → bounded is a strengthening (presence becomes required); bounded → unbounded and loosening bounds are weakenings.
type_weakenandclass_property_weakenedcompare property spec dicts, so bound changes surface automatically once frames serialisemin/maxforList— the comparison itself needs the bound tuple extended intotype_weaken. Existing stored data is never touched by the unbounded relaxation: it is the same triples under a looser check.Clients
Optional[List[T]]maps to an unboundedListspec — which also fixes today's nested{"@type":"Optional","@class":{"@type":"List",...}}output the engine cannot parse.List[T]maps tomin:0(required, emptyable), preserving the constructor-required contract. Bounds beyond that pass through as dict specs.woql.jsonandcompile_wfare untouched — this is a schema-family change, not a WOQL predicate.Listfields needs checking on the Rust side before release; unbounded lists should emit nullable.Verification items before implementation
Listmay appear in@keyfields today; if it can, a bounded (required) list in a key keeps thekey_missing_required_fielderror while unbounded joins the tolerated set.@oneOf/TaggedUnion members: the existing optional-member ban needs to treat an unbounded list member correctly — an optional member can never be the chosen branch, which may deserve its own rejection.TableandArraybounds are deliberately out of scope; their storage shapes differ, and the machinery generalises later if wanted.Acceptance criteria
Listfield inserts successfully and reads back with the key omitted;{"@type":"List","@min_cardinality":0}restores today's mandatory-emptyable contract.All reactions