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
4 changes: 3 additions & 1 deletion src/did/+did2/+build/Contents.m
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,9 @@
% fit_metric_unit, and the time-model rules absolute_reading_no_origin,
% reading_one_epoch_frame, reading_end_not_before_start, origin_acyclic,
% time_shared_or_inline, assertion_no_time, relative_reading_owner (its
% origin is another document, see checkTimeOrigins); the `dimensions` / `covariates` rules are checked by
% origin is another document, see checkTimeOrigins), and one_kind_cell
% (one value cell, the kind's own: [a quantity cell is named for its
% kind]); the `dimensions` / `covariates` rules are checked by
% the builder's own dimension and covariate checks whether or not the
% schema names them; ingredient_in_formulation is known but needs the
% formulation, see checkDoseIngredients) -- an UNKNOWN rule is an error,
Expand Down
2 changes: 1 addition & 1 deletion src/did/+did2/+build/document.m
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@
error('did2:build:badGap', '%s.', strjoin(problems, '; '));
end

checkRules(cache, className, ruleView(blocks, cellOwner, cellName), chain);
checkRules(cache, className, ruleView(blocks, cellOwner, cellName), chain, blocks);
if options.Validate
cache.validateDocument(doc);
end
Expand Down
128 changes: 127 additions & 1 deletion src/did/+did2/+build/private/checkRules.m
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
function checkRules(cache, className, doc, chain)
function checkRules(cache, className, doc, chain, raw)
Comment thread
github-advanced-security[bot] marked this conversation as resolved.
Fixed
%CHECKRULES The cross-field checks a per-field check cannot make.
%
% checkRules(CACHE, CLASSNAME, DOC, CHAIN) raises did2:build:ruleViolated when DOC
% breaks one of them. Two kinds, both listed here so a reader has one place
% to look:
%
% checkRules(CACHE, CLASSNAME, DOC, CHAIN, RAW) also gives the document as
% built, before did2.build.document's ruleView renamed its value cell to
% `value` (and `external` to `data_body`) for the rules below; RAW is DOC
% when not given. Only one_kind_cell reads it: it is about the cell's own
% name.
%
% 1. THE SCHEMA'S OWN `rules`, read from every class in the chain. Each rule
% the builder can check is implemented below BY NAME. A rule name the
% builder does not know is an ERROR (did2:build:unknownRule), not a skip:
Expand Down Expand Up @@ -108,6 +114,26 @@
% amending [a time states its relation
% to its origin])
%
% The kind-cell rule (Decided by Jess Haley, 2026-12-02 [a quantity cell
% is named for its kind], did-schema did_v2_go_forward_class_audit.md:
% "exactly one kind cell per statement, matching its value-kind
% superclass (a rule)"). It lands here before did-schema declares it
% (siblings first; did-schema #469). Its name is Proposed (not decided).
% one_kind_cell the document holds one value cell,
% under the name the schema gives the
% cell of its value kind (the kind its
% superclasses list, or the kind its own
% chain holds: did2.schema.cache/
% valueCellField), in that kind's block
% on a nested document: no field named
% for another value kind, and no second
% cell. A cell that is absent because
% the value is in data bodies (`external`
% true) or in a shared value document
% (`value_id`) is not a second cell;
% whether the value is somewhere is
% value_inline_or_body's question
%
% BOTH NUMERIC SHAPES are read ([one shape for measured numbers], Jess
% Haley, 2026-11-17, did-schema did_v2_numeric_shape_plan.md, step 1 of
% its section 9): a covariate, condition or parameter may carry the new
Expand Down Expand Up @@ -189,11 +215,15 @@
className {mustBeText}
doc (1,1) struct
chain = {}
raw = []
end

if isempty(chain)
chain = cache.classChain(className);
end
if isempty(raw)
raw = doc;
end
blocks = setdiff(fieldnames(doc), {'document_class', 'depends_on', 'files', 'file'}, 'stable');
edgeNames = {};
if isfield(doc, 'depends_on') && ~isempty(doc.depends_on)
Expand Down Expand Up @@ -394,6 +424,10 @@
% Jess Haley 2026-11-30 [time combinations refused by rule]:
% the per-document halves (see the header and timeRule)
timeRule(doc, ruleName, className);
case 'one_kind_cell'
% [a quantity cell is named for its kind] (Jess Haley,
% 2026-12-02): see the header
oneKindCell(cache, className, raw, ruleName);
case 'relative_reading_owner'
% [time combinations refused by rule], [time readings have no
% clock term]: whether the origin is an epoch or a device is a
Expand Down Expand Up @@ -1354,6 +1388,98 @@
end
end

function oneKindCell(cache, className, doc, ruleName)
% one_kind_cell (see the header): the value cells DOC holds, in either
% layout, are at most one, and that one is the cell the schema declares for
% the document's value kind. A cell is a field named `value` or named for a
% value kind (a class descending from `value`), at the top level of a flat
% document or directly in a block of a nested one; a cell embedded in
% another (a dose's ingredient `mass`) is part of that cell, not a second.
kind = '';
if isfield(doc, 'document_class') && isstruct(doc.document_class) ...
&& isfield(doc.document_class, 'superclasses')
kind = cache.documentValueKind(className, ...
superclassNames(doc.document_class.superclasses));
end
[want, ~, owner] = cache.valueCellField(className, kind);
named = kind;
if isempty(named)
named = owner;
end
flatDoc = ~isfield(doc, 'base') && isfield(doc, 'id');
if flatDoc
holders = {''};
else
holders = setdiff(fieldnames(doc), {'document_class', 'depends_on', 'files', 'file'}, 'stable');
end
found = {};
for h = 1:numel(holders)
if flatDoc
b = doc;
else
b = doc.(holders{h});
if ~isstruct(b) || ~isscalar(b)
continue;
end
end
names = fieldnames(b);
for k = 1:numel(names)
if strcmp(names{k}, 'value') || isValueKindName(cache, names{k})
if flatDoc
found{end+1} = names{k}; %#ok<AGROW>
else
found{end+1} = [holders{h} '.' names{k}]; %#ok<AGROW>
end
end
end
end
expected = want;
if ~flatDoc && ~isempty(owner)
expected = [owner '.' want];
end
if isempty(want)
% no value kind in the chain declares a cell: nothing to match (a class
% that declares the rule but takes no value kind is the schema's error,
% did-schema validate_v_eta R14)
return;
end
if numel(found) > 1
violated(ruleName, className, sprintf( ...
['a statement holds exactly one kind cell, `%s` (its value kind "%s"); ' ...
'this document holds %d: %s.'], expected, named, numel(found), ...
strjoin(strcat('`', found, '`'), ', ')));
end
if numel(found) == 1 && ~strcmp(found{1}, expected)
violated(ruleName, className, sprintf( ...
['the kind cell must match the value kind "%s" (`%s`); this document ' ...
'holds `%s`.'], named, expected, found{1}));
end
end

function names = superclassNames(sc)
% the class_name of each entry of a document_class.superclasses list
names = {};
if isempty(sc)
return;
end
if iscell(sc)
items = sc(:)';
else
items = num2cell(sc(:)');
end
for k = 1:numel(items)
if isstruct(items{k}) && isfield(items{k}, 'class_name')
names{end+1} = char(items{k}.class_name); %#ok<AGROW>
end
end
end

function tf = isValueKindName(cache, name)
% is NAME the name of a value kind (a class descending from `value`)
tf = ~strcmp(name, 'value') && cache.hasClass(name) ...
&& any(strcmp(cache.superclasses(name), 'value'));
end

function violated(ruleName, where, message)
error('did2:build:ruleViolated', '%s (%s): %s', ruleName, where, message);
end
Loading
Loading