Skip to content

Fix: Use validation context for drafts instead of skipping validations#59

Open
bellisabell wants to merge 1 commit intomainfrom
bell/fix-draft-validation
Open

Fix: Use validation context for drafts instead of skipping validations#59
bellisabell wants to merge 1 commit intomainfrom
bell/fix-draft-validation

Conversation

@bellisabell
Copy link
Member

Problem

In DraftController#update, drafts were saved with save!(validate: false) which bypasses all model validations. This is dangerous because it allows any data to be saved to the database, including data that violates length constraints or other integrity checks.

Solution

Use Rails validation contexts to selectively skip only the presence validations when saving drafts:

  1. Changed save!(validate: false) to save!(context: :draft)
  2. Added draft_context? method to Post model
  3. Made presence validations (subject presence, body_cannot_be_empty) conditional with unless: :draft_context?
  4. Length validation still enforced - subject max 255 chars is always validated

Benefits

  • Drafts can still be saved without subject/body (as before)
  • Data integrity is preserved (length limits enforced)
  • More maintainable than validate: false
  • Follows Rails best practices for conditional validations

Closes #23

Replace save!(validate: false) with save!(context: :draft) to maintain
data integrity while allowing drafts to be saved without subject/body.

- Add draft_context? method to Post model
- Make presence validations conditional on validation context
- Length validation still enforced (max 255 chars for subject)

Closes #23
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.

Bug: save!(validate: false) in DraftController bypasses all model validations

2 participants