Skip to content

Add and enforce validation struct tags for existing and future struct fields #52

Description

@thep2p

Problem

According to CLAUDE.md, the project uses github.com/go-playground/validator/v10 for struct validation. However, several public structs currently lack validation tags, which can lead to runtime panics when invalid instances are created.

Current Issues

  1. BootstrapEntry struct (bootstrap/bootstrap.go:23-26) lacks validation tags:

    type BootstrapEntry struct {
        Identity    model.Identity
        LookupTable core.MutableLookupTable
    }

    Users can create invalid BootstrapEntry instances with nil lookup tables, leading to potential panics at runtime.

  2. Missing validation enforcement - No systematic way to ensure new structs include appropriate validation tags

Impact

  • Runtime safety: Without validation tags, invalid structs can be created and cause panics
  • Developer experience: Errors occur at runtime instead of at construction time
  • Code quality: Inconsistent validation patterns across the codebase

Proposed Solution

1. Add Validation Tags to Existing Structs

Identify and add validation tags to all public structs that should be validated:

type BootstrapEntry struct {
    Identity    model.Identity              `validate:"required"`
    LookupTable core.MutableLookupTable     `validate:"required"`
}

Note: If model.Identity and core.MutableLookupTable are value types (not pointers), document why validation isn't needed or if zero-value is acceptable.

2. Create Validation Guidelines

Add a section to CLAUDE.md or create a CONTRIBUTING.md with guidelines:

  • When to add validation tags
  • How to validate structs (call validator in constructors)
  • Examples of common validation patterns
  • Exception cases (e.g., internal-only structs, value types where zero-value is valid)

3. Potential Linting/Static Analysis

Consider adding a linter rule or custom tool to:

  • Detect public structs without validation tags
  • Verify validation is called in constructors
  • Flag potential validation issues in code review

Acceptance Criteria

  • Audit all public structs and identify which need validation tags
  • Add validation tags to existing structs (e.g., BootstrapEntry)
  • Update documentation with validation guidelines
  • Add validation calls in constructors where appropriate
  • Consider adding linter rule or static analysis tool
  • Add tests to verify validation works correctly

Examples of Structs to Audit

  • bootstrap.BootstrapEntry
  • Network layer message types
  • Identity and addressing types
  • Configuration structs
  • Any public API types that could be constructed by users

References

Priority

Cosmetic / Low Priority - This is a code quality improvement that enhances safety but doesn't block current functionality. Can be addressed incrementally.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions