Scheemer uses Dry::Schema to enable us to write consistent looking structural parameter validation and data accessing on our services.
Scheemer::DSL ties together the parameters key translation and structural validation.
klass=Class.newdoextendScheemer::DSLschemadorequired(:root).hashdorequired(:someValue).filled(:string)endendon_missingpath: "book.author.name",fallback_to: {"Stephen King"}endrecord=klass.new({root: {someValue: "testing"}})record.some_value# => "testing"record.book.dig(:author,:name)# => "Stephen King"When using the DSL, it's possible to inject extra parameters into your custom validations through the constructor. This can be helpful in cases where validations require access to outside data (e.g. database records, user session).
klass=Class.newdoextendScheemer::DSLschemadorequired(:root).hashdorequired(:someValue).filled(:string)endenddefvalidate!(data)pdata[:records]# => [ ... ]pdata[:some_extra_value]# => "may be for validation"endendrecord=klass.new({root: {someValue: "testing"}},{records: [1,2],some_extra_value: "may be for validation"})Scheemer::Params handles the parameters key translation.
klass=Class.newdoextendScheemer::Params::DSLdefinitialize(...)super
...
endendrecord=klass.new({someValue: "testing"})record.some_value# => "testing"Scheemer::Schema::DSL handles the structural validation.
klass=Class.newdoextendScheemer::Schema::DSLschemadorequired(:name).filled(:string)endattr_reader:contentsdefinitialize(params)@contents=self.class.validate_schema!(params)endendklass.new({name: 1})# => Error:'{:name=>['must be a string"]}"$ docker-compose run scheemer /bin/sh
$ docker-compose run scheemer rspec
$ docker-compose build scheemerInstall the gem and add to the application's Gemfile by executing:
$ bundle add aion-dk/scheemerIf bundler is not being used to manage dependencies, install the gem by executing:
$ gem install aion-dk/scheemerThe gem is available as open source under the terms of the MIT License.