Audit code for consistency with checkpoint #1 of the architecture plan

This commit is contained in:
2026-07-03 07:38:00 -05:00
parent 26595e0105
commit 2690cdd959
2 changed files with 49 additions and 6 deletions

View File

@@ -12,6 +12,9 @@ func ValidateDocument(doc *SourceDocument) error {
if isBlank(doc.ID) {
return fmt.Errorf("source document id must not be empty")
}
if hasSurroundingWhitespace(doc.ID) {
return fmt.Errorf("source document id %q must not contain leading or trailing whitespace", doc.ID)
}
if isBlank(doc.Kind) {
return fmt.Errorf("source document kind must not be empty")
}
@@ -27,20 +30,22 @@ func ValidateDocument(doc *SourceDocument) error {
seenUnitIDs := make(map[string]struct{}, len(doc.Units))
for i, unit := range doc.Units {
unitID := strings.TrimSpace(unit.ID)
if unitID == "" {
if isBlank(unit.ID) {
return fmt.Errorf("source unit[%d].id must not be empty", i)
}
if hasSurroundingWhitespace(unit.ID) {
return fmt.Errorf("source unit[%d].id %q must not contain leading or trailing whitespace", i, unit.ID)
}
if isBlank(unit.Kind) {
return fmt.Errorf("source unit[%d].kind must not be empty", i)
}
if isBlank(unit.Text) {
return fmt.Errorf("source unit[%d].text must not be empty", i)
}
if _, ok := seenUnitIDs[unitID]; ok {
return fmt.Errorf("source unit id %q is duplicated", unitID)
if _, ok := seenUnitIDs[unit.ID]; ok {
return fmt.Errorf("source unit id %q is duplicated", unit.ID)
}
seenUnitIDs[unitID] = struct{}{}
seenUnitIDs[unit.ID] = struct{}{}
}
return nil
@@ -53,12 +58,21 @@ func ValidateRef(doc *SourceDocument, ref SourceRef) error {
if isBlank(ref.SourceID) {
return fmt.Errorf("source ref source_id must not be empty")
}
if hasSurroundingWhitespace(ref.SourceID) {
return fmt.Errorf("source ref source_id %q must not contain leading or trailing whitespace", ref.SourceID)
}
if isBlank(ref.StartUnitID) {
return fmt.Errorf("source ref start_unit_id must not be empty")
}
if hasSurroundingWhitespace(ref.StartUnitID) {
return fmt.Errorf("source ref start_unit_id %q must not contain leading or trailing whitespace", ref.StartUnitID)
}
if isBlank(ref.EndUnitID) {
return fmt.Errorf("source ref end_unit_id must not be empty")
}
if hasSurroundingWhitespace(ref.EndUnitID) {
return fmt.Errorf("source ref end_unit_id %q must not contain leading or trailing whitespace", ref.EndUnitID)
}
if ref.SourceID != doc.ID {
return fmt.Errorf("source ref source_id %q does not match document id %q", ref.SourceID, doc.ID)
}
@@ -93,3 +107,7 @@ func UnitIndex(doc *SourceDocument, unitID string) (int, bool) {
func isBlank(value string) bool {
return strings.TrimSpace(value) == ""
}
func hasSurroundingWhitespace(value string) bool {
return strings.TrimSpace(value) != value
}