Giter Site home page Giter Site logo

Comments (6)

solnic avatar solnic commented on June 16, 2024 1

Transaction and Contract are not meant to be used in the same class, I'm not sure if this causes the issue but I figured I should mention that. Both implement #call so this doesn't make sense.

from dry-validation.

sbezugliy avatar sbezugliy commented on June 16, 2024

The same for interactor setting default value as next

# frozen_string_literal: true

require "base64"

module Interactors
  module Base64
    class EncodeInteractor < Dry::Validation::Contract
      extend Dry::Initializer
      include Dry::Transaction

      schema Schemas::Base64::DataSchema

      param :data, default: proc { "" }

      step :encode

      private

      def encode(input)
        Success(cypher: ::Base64.encode64(input[:data]))
      end
    end
  end
end

from dry-validation.

sbezugliy avatar sbezugliy commented on June 16, 2024

Hmm... Understood. I did find this as interesting use-case, mixing both inside of same context. I will analyze one more time, to combine them, due to I want to get thin context between serializers and backend classes, and to realise conveyor and railway approaches, with strong fault traceability.

from dry-validation.

sbezugliy avatar sbezugliy commented on June 16, 2024

Or I will extract validators to additional layer, but wanted to make smaller count of levels. Also I don't use models, but only backend classes and libs, so implementation of validators at the level of contracts/interactors have sense for current case.

from dry-validation.

sbezugliy avatar sbezugliy commented on June 16, 2024

And thank you for direction to solve!

from dry-validation.

sbezugliy avatar sbezugliy commented on June 16, 2024

Solved it in this way.

schema:

# frozen_string_literal: true

module Schemas
  module Base64
    DataSchema = Dry::Schema.Params do
      required(:data).filled(:string)
    end
  end
end

contracts:

module Contracts
  class BaseContract < Dry::Validation::Contract
    include Dry::Logic
    include Dry::Monads[:result]
    Dry::Validation.load_extensions :monads
  end
end
# frozen_string_literal: true

module Contracts
  module Base64
    class EncodeContract < BaseContract
      schema Schemas::Base64::DataSchema
    end
  end
end

interactor:

# frozen_string_literal: true

module Interactors
  class BaseInteractor
    extend Dry::Initializer
    include Dry::Transaction
    include Dry::Logic
    include Dry::Monads[:result]
  end
end
# frozen_string_literal: true

require "base64"

module Interactors
  module Base64
    class EncodeInteractor < BaseInteractor
      param :data, default: proc { "" }

      step :contract
      step :encode

      private

      def contract(input)
        Contracts::Base64::EncodeContract.new.call(input).to_monad
      end

      def encode(input)
        Success(cypher: ::Base64.encode64(input[:data]))
      end
    end
  end
end

It's simples chain of interaction, but current layout is well covers my requirements for interactor of 10 steps with some branch actions.
Next also I'll add layer of dry-matchers to convert dry-monades messages to formatted JSON API responses.

from dry-validation.

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.