GithubHelp home page GithubHelp logo

Comments (5)

lasseebert avatar lasseebert commented on May 30, 2024

I should add that in this example, I don't care about validations, only coercion.

from validations.

jodosha avatar jodosha commented on May 30, 2024

@lasseebert I apologize for taking so long to reply.

Coercion can't be performed at the initialization time because of it can potentially raise TypeError.
Because params are instantiated before your implementation of #call is invoked, in case of failed coercion, your app will return server side error, without you are even able to control the flow.

Would you please explore the lazy coercion alternative? Thanks!

from validations.

lasseebert avatar lasseebert commented on May 30, 2024

If coercion is treated as some kind of special validation (not run on valid?) it could instead be run (and memoized) the first time that attribute is accessed.

Something like

# lib/lotus/validations.rb
module Lotus
  module Validations
    # ...
    module ClassMethods
      def attribute(name, options = {})
        attributes[name.to_sym] = validate_options!(name, options)

        class_eval %{
          def #{ name }
            @coerced[:#{ name }]
          end
        }
      end
      # ...
    end

    def initialize(attributes)
      @attributes = attributes
      @coerced = Hash.new { |hash, key| hash[key] = coerce(attributes[key]) if attributes[key] }
    end
    # ...
  end
end

I'm not sure that this is the way to go, but for me it was a gotcha that coercion is treated as a validation. Specially in the case when coercion is the only validation I'm using, then it does not make sense to call valid? before using the attributes.

I guess that when using lotus-validations on it's own, it makes more sense to that you have to call some method before anything happens. My case was from a lotus application.

from validations.

lasseebert avatar lasseebert commented on May 30, 2024

Very nice, thank you very much! :)

from validations.

jodosha avatar jodosha commented on May 30, 2024

@lasseebert 🎩 expect a new release on sunday.

from validations.

Related Issues (20)

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.