
Product
Introducing Rust Support in Socket
Socket now supports Rust and Cargo, offering package search for all users and experimental SBOM generation for enterprise projects.
GuideRail is a opinionated powerful factory library for Ruby that transforms various data sources into safe, immutable Data
objects. By leveraging the robust validation capabilities of dry-schema
, it provides a clear and declarative way to define, validate, and instantiate your data structures.
It acts as a "guide rail," ensuring that data flowing into your application (e.g., from controller params or API responses) is valid and conforms to a defined structure before being used in your domain logic or views.
dry-schema
for comprehensive data validation and coercion. The generated Data object can be guaranteed to have the expected attribute. ( If you specify required )Data
class, preventing accidental state mutations.renderable
mode provides nil
-safe default values, perfect for views.monadic
mode provides integration with functional programming paradigms by returning Dry::Monads::Result
objects.Add this line to your application's Gemfile:
gem 'guide-rail'
And then execute:
$ bundle install
or
$ bundle add guide-rail
require "guide-rail"
SimpleNonNullableSchema = Dry::Schema.Params do
required(:name).filled(:string)
end
class SimpleNonNullableCreator
extend GuideRail
schema SimpleNonNullableSchema
class_name :SimpleNonNullable
end
SimpleNonNullableCreator.from(name: nil) # => #<Dry::Schema::Result{name: nil} errors={name: ["must be filled"]} path=[]>
SimpleNonNullableCreator.from(name: "John") # => #<data SimpleNonNullable name="John">
SimpleNonNullableCreator.from({}) # => #<Dry::Schema::Result{} errors={name: ["is missing"]} path=[]>
renderable
optionSimpleNullableSchema = Dry::Schema.Params do
required(:name).maybe(:string)
end
class SimpleRenderableCreator
extend GuideRail
schema SimpleNullableSchema
class_name :SimpleRenderable
renderable true
end
SimpleRenderableCreator.from(name: nil) # => #<data SimpleRenderable name="">
SimpleRenderableCreator.from(name: "John") # => #<data SimpleRenderable name="John">
SimpleRenderableCreator.from({}) # => #<Dry::Schema::Result{} errors={name: ["is missing"]} path=[]>
You can give a block to Creator class, the generated Data class can be extended by the block ( applying to define
class method ).
This can be used to define decorator methods and conversion processes, so the generated Data class can also be used as a so-called ViewModel.
OptionalSchema = Dry::Schema.Params do
optional(:name).filled(:string)
end
class OptionalAndYieldAccepter
extend GuideRail
class_name :ExtendedData
schema OptionalSchema
renderable true
yield_block do
alias_method :name_orig, :name
define_method :name do
"decorated #{name_orig}"
end
end
end
OptionalAndYieldAccepter.from({}).name.start_with? "decorated" # => true
SimpleNonNullableSchema = Dry::Schema.Params do
required(:name).filled(:string)
end
class SimpleNonNullableMonadicCreator
extend GuideRail
schema SimpleNonNullableSchema
class_name :SimpleNonNullableMonadic
monadic true
end
SimpleNonNullableMonadicCreator.from({}).either(
->(e) { e.value! },
->(e) { e.errors.to_h }
)
# => {name: ["is missing"]}
After checking out the repo, run bin/setup
to install dependencies. Then, run rake test
to run the tests. You can also run bin/console
for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run bundle exec rake install
. To release a new version, update the version number in version.rb
, and then run bundle exec rake release
, which will create a git tag for the version, push git commits and the created tag, and push the .gem
file to rubygems.org.
Bug reports and pull requests are welcome on GitHub at https://github.com/wtnabe/guide-rail.
FAQs
Unknown package
We found that guide-rail demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Socket now supports Rust and Cargo, offering package search for all users and experimental SBOM generation for enterprise projects.
Product
Socket’s precomputed reachability slashes false positives by flagging up to 80% of vulnerabilities as irrelevant, with no setup and instant results.
Product
Socket is launching experimental protection for Chrome extensions, scanning for malware and risky permissions to prevent silent supply chain attacks.