Off the Rails: Validating non-model classes with…ActiveModel?
summary
Builds a single validator class that accepts Hash/Struct/Data and runs ActiveModel validations (built‑in and custom), using TDD and by reading validators plus ActiveSupport callbacks so options like if and allow_blank behave correctly. Handles presence, confirmation, arrays, shoulda‑matchers quirks, and a mutating‑hash bug fixed by swapping delegator for method_missing. The implementation lives in a repo as a gem scaffold with commit checkpoints and has run in production for over a year.
AI-written summary · about
chapters (20)
- 0:00Introduction
- 1:48Rails validators outside models
- 6:20Goals for flexible validator
- 7:03Proof of concept: presence validator
- 10:54Fixing initial POC errors
- 12:21Rails validator options like allow_nil
- 14:05Delegating validation methods
- 15:07Conditional validations with if/unless
- 16:09Hash keys to method calls
- 17:29Additional Rails validators
- 18:11Clearing validators and state
- 19:58Nested data and custom validators
- 21:23Class-level validate method
- 22:27Confirmation validator quirks
- 23:10Shoulda Matchers integration
- 24:14Q&A: Hash mutation and use cases
- 26:00Q&A: Real-world validation scenarios
- 27:02Q&A: Event sourcing and validation
- 28:46Q&A: Storing registered validations
- 30:08Closing
related talks
24mValidate Me! - Demystifying Rails Validators
44mDo You Need That Validation? Let Me Call You Back About It
31mActiveSupport and ActiveModel
26mHow to Upstream Your Code to Rails
26mDon't Rewrite Your Framework
16mTDD Workshop: Mocking, Stubbing, and Faking External Services
41mAn Intervention for ActiveRecord
42mPowerful Rails Features You Might Not Know
