Skip to content

Validate unit badge ID with badge_id_valid and improved error messages#297

Merged
stefan-burke merged 4 commits intomainfrom
terragon/fix-unit-id-validation-error-zlhklb
Oct 13, 2025
Merged

Validate unit badge ID with badge_id_valid and improved error messages#297
stefan-burke merged 4 commits intomainfrom
terragon/fix-unit-id-validation-error-zlhklb

Conversation

@stefan-burke
Copy link
Copy Markdown
Member

@stefan-burke stefan-burke commented Oct 13, 2025

Summary

  • Replaces a custom badge-id validation callback with a standard Rails validation method badge_id_valid to provide clearer error messages.
  • Adds a dedicated badge existence check via badge_id_valid that surfaces a localized error when a non-existent badge ID is used.
  • Adds validation for blank IDs so an empty badge ID also surfaces a validation error.
  • Updates UI error display to show a list of relevant validation errors.

Changes

Model

  • Remove before_create :validate_badge_id callback.
  • Add validation :badge_id_valid, on: :create, if: -> { unit_badges_enabled? }.
  • Implement new method badge_id_valid to verify Badge.exists?(id: id) and add a localized error on :id if not found; also returns an error when id is blank.
  • Minor normalization tweak in normalize_id to guard against blank IDs (no behavioral change).

Views

  • Update app/views/shared/_save_message.html.erb to render a list of errors when provided, improving visibility of validation issues.

Tests

  • Update spec/models/unit_spec.rb to reference the new #badge_id_valid validation (renamed from #validate_badge_id).
  • Minor test adjustments in spec/services/s3_backup_service_spec.rb to accommodate test helper changes and ensure directories exist when creating test artifacts.

Test plan

  • With unit badges enabled, attempting to create a Unit with a non-existent badge ID should fail with the message defined by units.validations.invalid_badge_id on the id field.
  • Ensure that when id is blank, the badge existence check is not triggered.
  • Verify that the UI save message area renders a list of validation errors when present.

Notes

  • The i18n key units.validations.invalid_badge_id should be defined to provide the user-friendly error message.
  • No user-facing API changes beyond improved validation messaging and error display.
  • Additional minor test adjustments were included to align with the new validation approach.

🌿 Generated by Terry


ℹ️ Tag @terragon-labs to ask questions and address PR feedback

📎 Task: https://www.terragonlabs.com/task/6c5d54cd-3d8f-4cb1-a6c4-1599a73e07cc

stefan-burke and others added 2 commits October 13, 2025 00:09
…xistence

Replaced the before_create callback :validate_badge_id with a custom validation method :badge_id_exists on create. This change ensures badge ID validation fits Rails validation conventions and removes unnecessary use of throw(:abort). Adjusted normalize_id method logic and updated related specs accordingly.

Co-authored-by: terragon-labs[bot] <terragon-labs[bot]@users.noreply.github.com>
Updated the Unit model's badge ID validation method to properly handle presence and existence checks. Replaced badge_id_exists with badge_id_valid to ensure ID presence before validating badge existence. Adjusted unit spec to reflect the method rename and validation logic.

Co-authored-by: terragon-labs[bot] <terragon-labs[bot]@users.noreply.github.com>
@stefan-burke stefan-burke changed the title Improve unit ID validation error messages for badges Improve unit badge ID validation and error messaging Oct 13, 2025
stefan-burke and others added 2 commits October 13, 2025 00:32
The mock_tar_compression helper was failing because FileUtils.touch
doesn't create parent directories. Added FileUtils.mkdir_p to ensure
the parent directory exists before attempting to create the file.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
@stefan-burke stefan-burke changed the title Improve unit badge ID validation and error messaging Validate unit badge ID with badge_id_valid and improved error messages Oct 13, 2025
@stefan-burke stefan-burke merged commit 917e626 into main Oct 13, 2025
16 checks passed
@stefan-burke stefan-burke deleted the terragon/fix-unit-id-validation-error-zlhklb branch October 13, 2025 13:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant