Giter Club home page Giter Club logo

dry-validation-matchers's Introduction

Dry::Validation::Matchers

RSpec matchers for Dry::Validation.

Installation

Add this line to your application's Gemfile:

gem 'dry-validation-matchers'

And then execute:

$ bundle

Or install it yourself as:

$ gem install dry-validation-matchers

Usage

RSpec.describe "Integration with RSpec", type: %i[dry_validation] do

  subject(:schema_class) do
    Class.new(Dry::Validation::Contract) do
      register_macro(:email) do
        key.failure('must_be_a_valid_email') if value.is_a?(String) &&
            !value.match?(/\A([\w+\-].?)+@[a-z\d\-]+(\.[a-z]+)*\.[a-z]+\z/i)
      end

      register_macro(:precision) do |macro:|
        num = macro.args[0]
        key.failure("cant_have_more_than_#{num}_decimal_numbers") if value && value.to_s.split('.').last.size > num
      end

      params do
        required(:username).filled
        required(:first_name)
        required(:age).filled(:integer)
        required(:last_name).filled(:string)
        optional(:mobile).filled
        optional(:email)
        optional(:decimal_value)
      end

      rule(:email).validate(:email)
      rule(:decimal_value).validate(precision: 5)
    end
  end

  it { is_expected.to validate(:username, :required).filled }
  it { is_expected.to validate(:mobile, :optional).filled }
  it { is_expected.to validate(:email, :optional) }
  it { is_expected.to validate(:email, :optional).macro_use?(:email) }
  it { is_expected.to validate(:decimal_value, :optional).macro_use?(precision: 5) }
end

See spec/acceptance/rspec_spec.rb as well.

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake spec 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 tags, and push the .gem file to rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/bloom-solutions/dry-validation-matchers. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.

License

The gem is available as open source under the terms of the MIT License.

dry-validation-matchers's People

Contributors

flintoff avatar musaffa avatar paulfri avatar ramontayag avatar shamox avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

dry-validation-matchers's Issues

Requesting a BigDecimal fix version on the 1.1.x series

I'm trying to upgrade a codebase to ruby 2.7 (2.6 is EOL), and I'm using this library. I believe it provides real benefit, in quickly and simply defining validation tests, massively reducing the amount of duplicated code that would be necessary to perform the same job, and reducing the risks of tests not being defined or being incomplete.

However, 1.1.1 is not ruby 2.7 compatible, and upgrading to 1.2.x requires an upgrade to dry-validation in to the 1.3 series, that will take weeks of refactoring.

A fix that would buy me a little time is releasing a fix for the BigDecimal problem off the 1.1.x series. I've got a branch, and you can see the changes at https://github.com/bloom-solutions/dry-validation-matchers/compare/v1.1.1...trammel:fix-bigdecimal-legacy-version?expand=1

Could you possibly release a new version of the old 1.1.x series with this fix?

Undefined method `configure' for RSpec:Module (NoMethodError)

Issue happened on:

dry-validation-matchers 1.2.2
Ruby 3.0.0p0 (2020-12-25 revision 95aff21468) [arm64-darwin21]
Rails 7.0.4.3
Gemfile depends on rspec-rails gem only and not depends on rspec-core directly
dry-validation-matchers gem was defined after rspec-rails gem

Issue trigger:

run rails c
or
run rails s -b 0.0.0.0

Error log when exception raised:

/Users/username/.rvm/gems/ruby-3.0.0/gems/dry-validation-matchers-1.2.2/lib/dry/validation/matchers/integrations/rspec.rb:2:in `<main>': undefined method `configure' for RSpec:Module (NoMethodError)
	from /Users/username/.rvm/gems/ruby-3.0.0/gems/bootsnap-1.16.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:32:in `require'
	from /Users/username/.rvm/gems/ruby-3.0.0/gems/bootsnap-1.16.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:32:in `require'
	from /Users/username/.rvm/gems/ruby-3.0.0/gems/zeitwerk-2.6.7/lib/zeitwerk/kernel.rb:38:in `require'
	from /Users/username/.rvm/gems/ruby-3.0.0/gems/dry-validation-matchers-1.2.2/lib/dry/validation/matchers.rb:5:in `<main>'
	from /Users/username/.rvm/gems/ruby-3.0.0/gems/bootsnap-1.16.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:32:in `require'
	from /Users/username/.rvm/gems/ruby-3.0.0/gems/bootsnap-1.16.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:32:in `require'
	from /Users/username/.rvm/gems/ruby-3.0.0/gems/zeitwerk-2.6.7/lib/zeitwerk/kernel.rb:38:in `require'
	from /Users/username/.rvm/gems/ruby-3.0.0/gems/dry-validation-matchers-1.2.2/lib/dry-validation-matchers.rb:1:in `<main>'
	from /Users/username/.rvm/gems/ruby-3.0.0/gems/bootsnap-1.16.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:32:in `require'
	from /Users/username/.rvm/gems/ruby-3.0.0/gems/bootsnap-1.16.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:32:in `require'
	from /Users/username/.rvm/gems/ruby-3.0.0/gems/zeitwerk-2.6.7/lib/zeitwerk/kernel.rb:38:in `require'
	from /Users/username/.rvm/gems/ruby-3.0.0/gems/bundler-2.3.23/lib/bundler/runtime.rb:60:in `block (2 levels) in require'
	from /Users/username/.rvm/gems/ruby-3.0.0/gems/bundler-2.3.23/lib/bundler/runtime.rb:55:in `each'
	from /Users/username/.rvm/gems/ruby-3.0.0/gems/bundler-2.3.23/lib/bundler/runtime.rb:55:in `block in require'
	from /Users/username/.rvm/gems/ruby-3.0.0/gems/bundler-2.3.23/lib/bundler/runtime.rb:44:in `each'
	from /Users/username/.rvm/gems/ruby-3.0.0/gems/bundler-2.3.23/lib/bundler/runtime.rb:44:in `require'
	from /Users/username/.rvm/gems/ruby-3.0.0/gems/bundler-2.3.23/lib/bundler.rb:187:in `require'
	from /Users/username/Documents/hyrd/repo/hyrd-api/config/application.rb:7:in `<main>'
	from /Users/username/.rvm/gems/ruby-3.0.0/gems/bootsnap-1.16.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:32:in `require'
	from /Users/username/.rvm/gems/ruby-3.0.0/gems/bootsnap-1.16.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:32:in `require'
	from /Users/username/.rvm/gems/ruby-3.0.0/gems/railties-7.0.4.2/lib/rails/commands/server/server_command.rb:137:in `block in perform'
	from <internal:kernel>:90:in `tap'
	from /Users/username/.rvm/gems/ruby-3.0.0/gems/railties-7.0.4.2/lib/rails/commands/server/server_command.rb:134:in `perform'
	from /Users/username/.rvm/gems/ruby-3.0.0/gems/thor-1.2.1/lib/thor/command.rb:27:in `run'
	from /Users/username/.rvm/gems/ruby-3.0.0/gems/thor-1.2.1/lib/thor/invocation.rb:127:in `invoke_command'
	from /Users/username/.rvm/gems/ruby-3.0.0/gems/thor-1.2.1/lib/thor.rb:392:in `dispatch'
	from /Users/username/.rvm/gems/ruby-3.0.0/gems/railties-7.0.4.2/lib/rails/command/base.rb:87:in `perform'
	from /Users/username/.rvm/gems/ruby-3.0.0/gems/railties-7.0.4.2/lib/rails/command.rb:48:in `invoke'
	from /Users/username/.rvm/gems/ruby-3.0.0/gems/railties-7.0.4.2/lib/rails/commands.rb:18:in `<main>'
	from /Users/username/.rvm/gems/ruby-3.0.0/gems/bootsnap-1.16.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:32:in `require'
	from /Users/username/.rvm/gems/ruby-3.0.0/gems/bootsnap-1.16.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:32:in `require'
	from bin/rails:4:in `<main>'

Analysis:

  • Based on the conditions mentioned above, the rspec-rails behavior was extending RSpec module, that makes the RSpec module defined but the configure method was not defined (ref)

Solution:

Alternative solution on this issue:

  • adding rspec-core to the gemfile and defined it before dry-validation-helper
    or
  • defining dry-validation-helper gem before rspec-rails gem
    Both needs to update the README.md to avoid this issue

Missing maybe? matchers

Hi there,

I just wanted to point out that this gem is missing the maybe matchers:

it { should validate(:field, :required).maybe(:int?) }

Is this something y'all are interested in adding? Are you open to a PR that this matcher?

Faun

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.