new: scaffold materializes a schema default only for a required field
Files changed: - CHANGES.md - VERSION - instructions/CONTRACT.md - tools/CONTRACT.md - tools/chemenu/commands/new_page.py - tools/chemenu/tests/test_new_page.py - types/type-spec.md
This commit is contained in:
1 parent
4284f101c8
commit
aa31d431fc
7 files changed
+118
-9
No files matched your search
@@ -11,6 +11,10 @@ deterministic and stored in /types/; the content is judgment and provided by the
|
||||
|
||||
Frontmatter defaults, enum validity, and required-ness all come from the
|
||||
type's `.schema.yaml` (via `TypeResolver`) - nothing here re-declares them.
|
||||
A schema `default:` is materialized only for a field the schema also lists
|
||||
in `required:` - an optional field's default is a reader-side assumption
|
||||
(what a missing field means), and writing it into every scaffolded page
|
||||
would turn that assumption into a stated claim instead (Gitea #109).
|
||||
Directory placement for subtype-driven types (currently just entities) also
|
||||
comes from the type-spec, via its `layout:` frontmatter (see
|
||||
`TypeResolver.get_layout`) - not a hand-maintained Python dict.
|
||||
@@ -74,12 +78,22 @@ def _build_frontmatter(
|
||||
`explicit` supplies every CLI-derived value the caller already has;
|
||||
fields not in `explicit` get a type-appropriate default (today's date for
|
||||
date-formatted fields, the scaffold placeholder for `summary`, the
|
||||
schema's own `default:` where declared, an empty list for arrays), or are
|
||||
omitted entirely if optional with no sensible default (e.g.
|
||||
`source_url`). This is what lets frontmatter shape - and scaffold-time
|
||||
defaults like `provenance: general` - follow the schema instead of being
|
||||
hand-declared per CLI command.
|
||||
schema's own `default:` where declared *and the field is required*, an
|
||||
empty list for arrays), or are omitted entirely if optional with no
|
||||
sensible default (e.g. `source_url`). This is what lets frontmatter
|
||||
shape - and scaffold-time defaults like `provenance: general` - follow
|
||||
the schema instead of being hand-declared per CLI command.
|
||||
|
||||
A `default:` on an *optional* field (e.g. `instruction.obligation`) is
|
||||
deliberately not materialized here: it documents what a reader should
|
||||
assume when the field is absent, not what the scaffold should write.
|
||||
Writing it anyway turned every scaffolded instruction into one that
|
||||
falsely claims `obligation: required` - a migration-only field - and
|
||||
the same read/write distinction is what the schema's own `default:`
|
||||
doc-comment (`types/instruction.schema.yaml`) already draws (Gitea
|
||||
#109).
|
||||
"""
|
||||
required = set((schema or {}).get("required") or [])
|
||||
frontmatter: Dict[str, Any] = {"type": type_path}
|
||||
for field_name, field_schema in (schema or {}).get("properties", {}).items():
|
||||
if field_name == "type":
|
||||
@@ -105,7 +119,7 @@ def _build_frontmatter(
|
||||
frontmatter[field_name] = resolved_author
|
||||
elif field_schema.get("format") == "date":
|
||||
frontmatter[field_name] = today
|
||||
elif "default" in field_schema:
|
||||
elif "default" in field_schema and field_name in required:
|
||||
frontmatter[field_name] = field_schema["default"]
|
||||
elif field_schema.get("type") == "array":
|
||||
frontmatter[field_name] = []
|
||||
|
||||
Reference in new issue
Block a user