Comments (6)
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.
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.
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.
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.
And thank you for direction to solve!
from dry-validation.
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)
- Contract do not find schema keys defined with nested Dry Params. HOT 6
- Inconsistent behavior in nested schemas via passed in blocks HOT 8
- Incorrect validation of rule keys substring of schema keys
- Injecting dependencies using dry-auto_inject with reserved names
- `rule.each` produces error when input is `nil` HOT 2
- Building contract failed when or-ing types HOT 2
- Validation passes when array type is invalid HOT 1
- NoMethodError is raised when validating non-hash objects with a Dry::Validation::Contract that has config.validate_keys set to true HOT 4
- Dry::Validation::MissingMessageError after update dry stack HOT 2
- Weird issue since 1.10 dry-schema release HOT 3
- JSON schema contract failing to recognise valid input HOT 2
- Rule validation does not show in errors when the key validated is in an array of hashes HOT 1
- Mixing of `~>` and `<` in gemspec versioning HOT 1
- Documentation missmatch: `predicates_as_macros` is not available until v1.2
- `Dry::Validation::Result` lacks an `#output` method similar to `Dry::Schema::Result` HOT 4
- `Dry::Validation::Contract` behaves different when defined with a `params Dry::Schema::Params(parent: RawSchema)` vs. `params ParamsSchema` HOT 2
- errors(full: true), for nested input, could be better HOT 1
- False-positive when validating a nested datetime in an array of hashes HOT 2
- Consider release a new version with updated dependencies HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from dry-validation.