English | 日本語
Typed parameter objects and form objects for Rails.
Supports Ruby >= 3.2 and Rails / ActiveModel >= 7.2, < 9.0.
StructuredParams solves these challenges:
- API endpoints: Type checking, validation, and automatic casting of request parameters
- Form objects: Validation and conversion of complex form inputs to models
Built on ActiveModel, making nested objects and arrays easy to handle.
- ✅ API Parameter Validation - Type-safe request validation
- ✅ Form Objects - Encapsulate complex form logic
- ✅ Nested Structure Support - Automatic casting for objects and arrays
- ✅ Strong Parameters Integration - Auto-generate permit lists
- ✅ ActiveModel Compatible - Support for validations, serialization, and other standard features
- ✅ RBS Type Definitions - Type-safe development experience
# Gemfilegem'structured_params'# config/initializers/structured_params.rbStructuredParams.register_typesclassAddressParams < StructuredParams::Paramsattribute:street,:stringattribute:city,:stringendclassUserParams < StructuredParams::Paramsattribute:name,:stringattribute:age,:integerattribute:score,:integerattribute:tags,:array,value_type: :string# Primitive arrayattribute:address,:object,value_class: AddressParams# Nested object# validate raw string before type castingvalidates_raw:score,format: {with: /\A\d+\z/,message: 'must be numeric string'}validates:name,presence: truevalidates:age,numericality: {greater_than: 0}validates:score,numericality: {greater_than_or_equal_to: 0}end# Use in API controllerdefcreateuser_params=UserParams.new(params)ifuser_params.valid?User.create!(user_params.attributes)elserenderjson: {errors: user_params.errors},status: :unprocessable_entityendendStructuredParams supports primitive arrays via value_type. They are permitted using the Strong Parameters array format (tags: []).
classUserParams < StructuredParams::Paramsattribute:tags,:array,value_type: :stringend# Equivalent Strong Parameters:# params.permit(tags: [])classUserRegistrationForm < StructuredParams::Paramsattribute:name,:stringattribute:email,:stringattribute:terms_accepted,:booleanvalidates:name,:email,presence: truevalidates:terms_accepted,acceptance: trueend# Use in controller# Passing ActionController::Parameters to a Form class automatically calls# params.require(:user_registration).permit(...) internally.# Use a class name ending with `Form` to enable this behavior.defcreateform=UserRegistrationForm.new(params)ifform.valid?User.create!(form.attributes)redirect_toroot_pathelserender:newendend- Installation and Setup - Getting started with StructuredParams
- Basic Usage - Parameter classes, nested objects, and arrays
- Validation - Using ActiveModel validations with nested structures
- Strong Parameters - Automatic permit list generation
- Error Handling - Flat and structured error formats
- Serialization - Converting parameters to hashes and JSON
- Form Objects - Form object pattern with Rails views
Bug reports and pull requests are welcome on GitHub at https://github.com/Syati/structured_params.
The gem is available as open source under the terms of the MIT License.