feat: add template constraint validation (#86)#92
Merged
Conversation
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## main #92 +/- ##
===========================================
+ Coverage 46.76% 63.61% +16.85%
===========================================
Files 5 15 +10
Lines 355 2053 +1698
===========================================
+ Hits 166 1306 +1140
- Misses 168 537 +369
- Partials 21 210 +189
🚀 New features to boost your workflow:
|
Contributor
There was a problem hiding this comment.
Pull request overview
Adds first-class “template constraints” to the metadata model and core runtime so relationship cardinality rules can be validated, reported, and (optionally) enforced during relation creation and via a catalog-wide check.
Changes:
- Extend asset templates with
constraints(required/forbidden relations) and validate constraints during template directory load. - Add constraint evaluation/reporting, a NATS check subject, violation event publishing, and enforcement modes (
warn/enforce/disabled) wired through core config and CLI. - Update example templates, docs, and tests to cover constraint parsing, evaluation, and enforcement behaviors.
Reviewed changes
Copilot reviewed 18 out of 18 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| templates/vibration-sensor.yaml | Adds required partOf relation constraint targeting equipment. |
| templates/temp-sensor.yaml | Adds required partOf relation constraint targeting equipment. |
| templates/equipment.yaml | Introduces an equipment template used as a constraint target. |
| internal/core/testdata/valid_template.yaml | Updates test template to include a sample constraint section. |
| internal/core/metadata.go | Extends AssetTemplate with TemplateConstraints/RelationConstraint definitions. |
| internal/core/meta_handler.go | Adds constraints check subject and enforces/warns on relation-create violations with rollback. |
| internal/core/loader.go | Validates template constraints after directory load. |
| internal/core/loader_test.go | Adds assertions for loaded constraints and a negative test for unknown target_template. |
| internal/core/events.go | Adds constraint-violation event subject and publisher method. |
| internal/core/constraints.go | New evaluator/reporting logic plus template-constraint validation and error formatting. |
| internal/core/constraints_test.go | Tests evaluator behavior, enforce-mode rollback, warn-mode publishing, and catalog check. |
| internal/core/config.go | Adds constraints.enforcement config with defaults and validation. |
| internal/core/config_test.go | Tests constraints config defaults, YAML loading, and invalid value rejection. |
| docs/USER_GUIDE.md | Documents template constraints, enforcement modes, subjects, and CLI usage. |
| deploy/configs/core/config.staging.yaml | Sets constraints.enforcement: warn. |
| deploy/configs/core/config.prod.yaml | Sets constraints.enforcement: warn. |
| deploy/configs/core/config.dev.yaml | Sets constraints.enforcement: warn. |
| cmd/core/main.go | Adds --check-constraints CLI and wires config enforcement into MetaHandler. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+566
to
+570
| _ = h.store.DeleteRelation(relation.ID) | ||
| h.reply(msg, Response{Success: false, Error: err.Error()}) | ||
| return | ||
| } else if !h.allowConstraintViolations(violations) { | ||
| _ = h.store.DeleteRelation(relation.ID) |
Comment on lines
42
to
49
| path := filepath.Join(dir, entry.Name()) | ||
| if err := l.LoadFromFile(path); err != nil { | ||
| return fmt.Errorf("failed to load template (%s): %w", path, err) | ||
| } | ||
| } | ||
|
|
||
| return nil | ||
| return validateTemplateConstraints(l) | ||
| } |
Comment on lines
+100
to
+112
| if count > 0 { | ||
| zero := 0 | ||
| violations = append(violations, newConstraintViolation( | ||
| asset, | ||
| ConstraintForbiddenRelation, | ||
| constraint, | ||
| count, | ||
| nil, | ||
| &zero, | ||
| fmt.Sprintf("asset %s forbids %s relation(s) to template %s, found %d", | ||
| asset.ID, constraint.Type, constraint.TargetTemplate, count), | ||
| )) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Test