Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Activity
Piotr Solnica
@solnic
like that:
\`\`\`
your code
\`\`\`
Aditya Tiwari
@aditya01933
require 'dry-validation'
UserSchema = Dry::Validation.Schema do

  key(:username).required(:unique?)
  key(:name).required
  key(:email).required
  key(:password).confirm
  configure do
      config.messages_file = 'config/validations/errors.yml'
      config.input_processor = :sanitizer
     def unique?(value)
         User.find_by(username: value).nil?  
     end
  end
end
UserUpdateSchema = Dry::Validation.Schema do

  optional(:username).required(:unique?)
  optional(:name).required
  configure do
   config.messages_file = 'config/validations/errors.yml'
   config.input_processor = :sanitizer
    def unique?(value)
       User.find_by(username: value).nil?
    end
  end
end
Piotr Solnica
@solnic
oh well
Aditya Tiwari
@aditya01933
@solnic its not perfect but better
Any solution please ???
I want to remove code duplication of configuration block and "unique" custom predicate
Piotr Solnica
@solnic
class MySchema < Dry::Validation::Schema
  configure do |config|
    config.messages = :i18n
  end

  def unique?(value)
    false
  end

  def self.define(&block)
    Dry::Validation.Schema(self, &block)
  end
end

UserSchema = MySchema.define do
  key(:email).required(:unique?)
  key(:name).required
end
@aditya01933 like that ^
Aditya Tiwari
@aditya01933
@sonic Perfect!!!!!
Thanks its exactly what i was looking for :)
Piotr Solnica
@solnic
cool :)
Aditya Tiwari
@aditya01933
@solnic one more small question..
Currently i m testing uniqueness of username like
 def unique?(value)
      User.find_by(username: value)
  end
How can i write unique? in such a way that it can test uniqueness of other keys like name, email etc too ...
?
Piotr Solnica
@solnic
there’s no built-in conveniences for this type of predicates yet
you could parameterize it for now
Aditya Tiwari
@aditya01933
like ?
Piotr Solnica
@solnic
unique?(field_name, value)
Aditya Tiwari
@aditya01933
ok
Piotr Solnica
@solnic
then key(:email).required(unique?: :email)
we’ll improve this soon, but for now that’s the only option
Aditya Tiwari
@aditya01933
ok
Thanks .. I am trying it
Chase Gilliam
@Ch4s3
Are there any examples of how to use dry-validation to validate params and/or in place of strong params?
Aditya Tiwari
@aditya01933
@Ch4s3 my above code is doing that
Piotr Solnica
@solnic
@Ch4s3 there’s nothing special about this, just replace it with dry-v schemas :)
Aditya Tiwari
@aditya01933
require 'dry-validation'

class MySchema < Dry::Validation::Schema
  configure do |config|
    config.messages_file = 'config/validations/errors.yml'
    config.input_processor = :sanitizer
  end

  def unique?(value)
    User.find_by(username: value)
  end

  def self.define(&block)
    Dry::Validation.Schema(self, &block)
  end
end

UserSchema = MySchema.define do
  key(:username).required(:unique?)
  key(:name).required 
  key(:email).required
  key(:password).required.confirm 
end


UserUpdateSchema = MySchema.define do    
  optional(:username).required(:unique?)
  optional(:name).required  
end
config.input_processor = :sanitizer
Chase Gilliam
@Ch4s3
are you including that or writing it directly in the controller?
Aditya Tiwari
@aditya01933
i am including it in controller. I am writing above code in separate file under app/validations folder
In my controller I have
def dry_validation  
      parameters = params[:user]
      @schema = UserSchema.(parameters)
      unless @schema.success?
       render json:{message: @schema.messages}, status: 401
      end
 end
Piotr Solnica
@solnic
you always want to memoize schema objects, defining a schema on each request won’t be efficient
Chase Gilliam
@Ch4s3
gotcha
Aditya Tiwari
@aditya01933
before_action :dry_validation, only: :create
Chase Gilliam
@Ch4s3
ok. that makes sense. So this is practical for per endpoint schemas then. That's awesome!
Aditya Tiwari
@aditya01933
Yeah!!
Piotr Solnica
@solnic
we could provide dry-validation-rails for nice integration with rails
Chase Gilliam
@Ch4s3
that would be interesting
Let me try out dry-validation with a few of the endpoints I use for ajax, and I'll see if I can document what works well for the use case
Aditya Tiwari
@aditya01933
@solnic I am trying this but not working
key(:username).required(:unique?, :username)

 def unique?(field, value)
      User.where("#{field} = ? ", value).count == 0    
  end
What is right way ?
Fran Worley
@fran-worley
@aditya01933 try this:
key(:username).required(unique?:  :username)
Aditya Tiwari
@aditya01933
@fran-worley syntax error, unexpected ':', expecting ')'
key(:username).required(:unique? :username)
Fran Worley
@fran-worley
Yeah, your colon is in the wrong place
it should read the way I wrote it: key(:username).required(unique?: :username) not key(:username).required(:unique? :username)
Aditya Tiwari
@aditya01933
ahhh .. what a mistake!!
Fran Worley
@fran-worley
@aditya01933 is it working now?
Aditya Tiwari
@aditya01933
yes perfectly !!
Thanks :)
Fran Worley
@fran-worley
great :)