Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Activity
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?
Luca Guidi
@jodosha
Andy Holland
@AMHOL
Thanks @jodosha, that's cool :)
Still laughing about the confirmation validation stuff in Rails they mentioned at the beginning
Also, did they pronounce Hanami right? If they did then I'm pronouncing it wrong lol
Luca Guidi
@jodosha
@AMHOL Hanami is a japanese word, I pronounce it ha-nah-me
Andy Holland
@AMHOL
:+1:
I always read it as ha-nam-eye
lol
Luca Guidi
@jodosha
:D
James Hamilton
@wjdhamilton
@AMHOL I laughed at that too. Ruby 5 have had a lot of blog posts about small changes in Rails 5 over the past few months. They're probably wishing that it would get released so they can start talking about the finished product!
Andy Holland
@AMHOL
@wjdhamilton Yeah, crazy how Rails focused everyone is
And how's it going?
James Hamilton
@wjdhamilton
@AMHOL yeah, it's almost like DHH has his own cult. But then, a lot of people probably came to Ruby through Rails
Sorry, trying to type here with my daughter on my knee. She keeps trying to help me and it's not very helpful
Jeff Dickey
@jdickey
that's her job :D