Coder Social home page Coder Social logo

Comments (2)

solnic avatar solnic commented on July 25, 2024

Hey there, thanks for the report. There a couple of issues here. First of all root.filled? is not needed when you have root.hash?, which implies that if it's there and it's a hash then it's definitely filled. Secondly, calling multiple predicates without composing them logically with & / | operators is not supported; in general every block must return a rule and it's going to be composed with the outer one. The exception is key construct, which is special, as it establishes a set of rules that can be applied to its value, that's why you can define nested rules for many keys.

In other words:

class Schema < Dry::Validation::Schema
  key(:foo) do |foo|
    # this won't work because the last rule returned is composed with the outer `key` rule:
    foo.filled?
    foo.size?(10..100)

    # you want this:
    foo.filled? & foo.size?(10..100)
  end
end

And here's a working example of what you tried to do:

require 'json'
require 'dry-validation'
require 'dry/validation/schema/form'

class StatSchema < Dry::Validation::Schema::Form
  key(:stats) do |stats|
    stats.hash? do
      stats.key(:strength) do |strength|
        strength.key(:description, &:filled?)
      end
    end
  end
end

json = JSON.parse <<-EOT
{
    "stats": {
        "strength": {
            "description": "Increases physical damage"
        }
    }
}
EOT

schema = StatSchema.new

puts schema.messages(json).inspect

# #<Dry::Validation::Schema::Result params={:stats=>{:strength=>{:description=>"Increases physical damage"}}} errors=[]>

Notice that I used Schema::Form (although we may want to introduce Schema::JSON too!) which is needed for json input, as keys are stringified and you may want to use coercions too, which are automatically configured based on your type expectations. Also notice that each is not needed as its purpose is to be used with arrays, not hashes.

Oh and it's using master, as I improved many things, I'll release 0.2.1 later today.

Having said all of that, I have to admit that we could do something to provide meaningful feedback when rules are defined incorrectly, I believe it should be possible. In example we could check if somebody is trying to use each against an input that is expected to be something else than an array and in that case simply raise a meaningful error. The same applies to defining multiple predicates without composing them logically with operators, in that case we also want to raise an error, alternatively we may just assume that all predicates should be composed with & implicitly, but I'm not sure if that's a good strategy since it wouldn't be explicit from the dsl pov and may confuse people. Remember that rules are applied using predicate logic, so foo.str? & foo.size?(10..100) won't check the size when foo is not a string, this is a huge difference between dry-validation and most (or all) of the other validation libs in Ruby.

I hope this is helpful and thanks for trying out this lib :)

from dry-validation.

zackp30 avatar zackp30 commented on July 25, 2024

Oh wow! Thanks for the informative and quick reply. I'll keep this in mind, fantastic library by the way! Using it to validate JSON (for now) for a card game I'm creating with some friends. Thanks again! 😄

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.