Questions tagged [dry-validation]

A dry-rb gem that allows powerful data validation based on predicate logic in ruby.

Introduction Page

Unlike other, well known, validation solutions in Ruby, dry-validation takes a different approach and focuses a lot on explicitness, clarity and precision of validation logic. It is designed to work with any data input, whether it’s a simple hash, an array or a complex object with deeply nested data.

It is based on the idea that each validation is encapsulated by a simple, stateless predicate that receives some input and returns either true or false. Those predicates are encapsulated by rules which can be composed together using predicate logic. This means you can use the common logic operators to build up a validation schema.

Validations can be described with great precision, dry-validation eliminates ambiguous concepts like presence validation where we can’t really say whether some attribute or key is missing or it’s just that the value is nil.

In dry-validation type-safety is a first-class feature, something that’s completely missing in other validation libraries, and it’s an important and useful feature. It means you can compose a validation that relies on the type of a given value. For example it makes no sense to validate each element of an array when it turns out to be an empty string.

13 questions
4
votes
3 answers

How to validate date string with dry-validation gem?

I want to allow date formatted string with dry-validation gem, but I can't. class NewUserContract < Dry::Validation::Contract params do optional(:date).filled(:date) end end contract = NewUserContract.new contract.call(date: Date.today) #=>…
Junichi Ito
  • 2,438
  • 1
  • 23
  • 46
3
votes
1 answer

Dry validation i18n message for array validation

Let say I have define a dry-validation like this: class ApplicationContract < Dry::Validation::Contract config.messages.backend = :i18n config.messages.load_paths << 'config/errors.yml' params do required(:todo).schema do …
SoT
  • 898
  • 1
  • 15
  • 36
3
votes
1 answer

Automatically truncate a string

Taking the example from dry-validation: require "dry-validation" module Types include Dry::Types.module Name = Types::String.constructor do |str| str ? str.strip.chomp : str end end SignUpForm = Dry::Validation.Params do configure do …
Paulo Fidalgo
  • 21,709
  • 7
  • 99
  • 115
2
votes
1 answer

Default value as positional argument to settings is deprecated and will be removed in the next major version

I am going to maintain a rails application locally on Mac M1 based computer. OS Version: Monterey 12.1 Ruby version 2.6.6 Rails version 4.2.11 I am getting this error and can't figure out what is actually causing this error to raise. The error…
2
votes
1 answer

How prevent dry-validation from executing a rule if schema validation did not pass?

class PostSchema < Dry::Validation::Contract params do required(:title).value(:string, size: 20) required(:content).value(:string, size: 50) end rule do # prevent this rule from executing if schema validation did not pass …
Uysim Ty
  • 131
  • 2
  • 5
2
votes
1 answer

Getting error cannot load such file -- dry/types/compat/form_types (LoadError) while updating bundle in my project

Getting error /home/sachin/.rvm/gems/ruby-2.3.4/gems/activesupport-4.2.11.1/lib/active_support/dependencies.rb:274:in `require': cannot load such file -- dry/types/compat/form_types (LoadError) While trying 'bundle update' in one of my project. i…
Sachin
  • 353
  • 1
  • 10
1
vote
1 answer

Ruby on Rails: dry-schema dry-validation not validating presence

module QleKinds class CreateParamsValidator < MyCustomClass define do required(:title).value(:filled?) end end end I'm using dry-validation and dry-schema in a rails application and implementing the advice to check how to check…
zasman
  • 446
  • 1
  • 8
  • 28
0
votes
0 answers

How to set default value for Dry::Validation::Contract

I have the following contract and I would like to somehow set apikey to default to whatever is set in ENV.fetch('MY_ENV') so that users don't have to pass apikey param to every contract. I'd like it be injected automatically somehow if…
jedi
  • 2,003
  • 5
  • 28
  • 66
0
votes
0 answers

Dry-validation/Dry-schema uninitialized constant Dry::Schema::PredicateRegistry

I'm attempting to update the ruby version of an empty, stock API (which is forked to create new API) from Ruby 2.6.3 to Ruby 2.7.3 using TrailBlazer. Currently, I'm having issues with the Dry gems, specifically Dry-Validation (1.8.0), Dry-Schema…
0
votes
1 answer

Rails dry-validation passed when params missing and rise an error when it's present

In my Rails 7 app I want to use dry-validation gem to validate incoming JSONs inside create method of my MandateController. Basically based on that JSON request: "sdd_setup_request": { "return_url": "https://example.com/return", "data": { …
mr_muscle
  • 2,536
  • 18
  • 61
0
votes
1 answer

dry-rails how to use contract validation

after adding the gem dry-rails to my rails application, I have defined a contract: module Users module Contracts class New < ApplicationContract params do required(:todo).schema do required(:title).filled(:string) …
SoT
  • 898
  • 1
  • 15
  • 36
0
votes
1 answer

Creating nested/reusable validators in Ruby with dry-validation

Let's say I want to set up a validation contract for addresses, but then I also want to set up a validator for users, and for coffee shops; both of which include an address, is it possible to re-use the AddressContract in UserContract and…
aidan
  • 9,310
  • 8
  • 68
  • 82
0
votes
1 answer

Dry-Validation - no implicit conversion of Float into String on rules

I'm trying to validate the value of a key into my schema. But I'm getting a no implicit conversion of Float into String because the value is a float and I'm using a regex to validate the format. VALIDATION_PARAMETERS =…
Guillaume
  • 1,437
  • 2
  • 15
  • 17