Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Activity
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 :)
Aditya Tiwari
@aditya01933
thanks
James Hamilton
@wjdhamilton
Hi @solnic! in your talk last year you mentioned a gem called Autocurry. Is it available yet?
Piotr Solnica
@solnic
@wjdhamilton no, sorry, Barcelona was too beautiful to sit in my hotel room and work on another gem and when I went back home I got distracted by so many things that I kind-of forgot about it…I’m planning to create a new dry gem with various fp-features, not sure if it makes sense to have a separate gem just for auto-curry behavior, we’ll see
Tim Riley
@timriley
@nepalez dry-rb.org deployed :)
James Hamilton
@wjdhamilton
@solnic no problem, Barcelona is worth seeing! I got engaged there. I have an idea which requires a set of functions to be copied and curried during runtime. It's just in my head right now, so it can probably wait for your gem to arrive, but when I saw you refer to Autocurry I thought it worth checking out.
Andrew Kozin
@nepalez
@timriley thank you!
OMG! I've totally forgotten I was pushing into the fork, not dry-rb itself
Tim Riley
@timriley
@nepalez oops! I’ll deploy again with your changes now :)
And it’s up.
Andrew Kozin
@nepalez
@timriley yeah, this works! thank you!
Chase Gilliam
@Ch4s3
@solnic If I wanted to jump in a and do some work on dry-rb gems, where would be a good place to start?