Code Monkey home page Code Monkey logo

definition's Issues

undefined method `Pathname' for main:Object

$ gem install definition
Fetching: definition-0.5.1.gem (100%)
Successfully installed definition-0.5.1
Parsing documentation for definition-0.5.1
Installing ri documentation for definition-0.5.1
Done installing documentation for definition after 0 seconds
1 gem installed
$ irb
2.5.1 :001 > require "definition"
Traceback (most recent call last):
9: from /home/nih/.rvm/rubies/ruby-2.5.1/bin/irb:11:in <main>' 8: from (irb):1 7: from /home/nih/.rvm/rubies/ruby-2.5.1/lib/ruby/2.5.0/rubygems/core_ext/kernel_require.rb:39:in require'
6: from /home/nih/.rvm/rubies/ruby-2.5.1/lib/ruby/2.5.0/rubygems/core_ext/kernel_require.rb:135:in rescue in require' 5: from /home/nih/.rvm/rubies/ruby-2.5.1/lib/ruby/2.5.0/rubygems/core_ext/kernel_require.rb:135:in require'
4: from /home/nih/.rvm/gems/ruby-2.5.1/gems/definition-0.5.1/lib/definition.rb:8:in <top (required)>' 3: from /home/nih/.rvm/rubies/ruby-2.5.1/lib/ruby/2.5.0/rubygems/core_ext/kernel_require.rb:70:in require'
2: from /home/nih/.rvm/rubies/ruby-2.5.1/lib/ruby/2.5.0/rubygems/core_ext/kernel_require.rb:70:in require' 1: from /home/nih/.rvm/gems/ruby-2.5.1/gems/definition-0.5.1/lib/definition/i18n.rb:3:in <top (required)>'
NoMethodError (undefined method `Pathname' for main:Object)

Add translation support

Error messages should be easy to map to a source like for example a form field or a path in a json document. Also one needs to generate a translated error message for each error

Allow returning a custom error message from Lambda Definitions

If a Lambda definition fails then it currently returns a rather generic error message that only mentions the name of the lambda. This is not providing enough information to users to correct the bad input in some cases though. for example when building a definition that checks if a provided array includes only uniqe values then you might want to point out which value is not unique:

::Definition::Lambda(:unique_values) do |array|
  next unless array.uniq.size == array.size

  conform_with(array)
end

In this case something like this would be nice:

::Definition::Lambda(:unique_values) do |array|
  duplicates = array.select{ |e| array.count(e) > 1 }.uniq

  if duplicates.empty?
    conform_with(array)
  else
    fail_with("Array contains duplicate values: #{duplicates.join(', ')}")
  end
end

At the same time the version without fail_with should still work

Other usecases that come to mind:

  • An array needs to contain some specific items and you want to tell the user which ones are missing
  • You do validation across multiple fields, e.g. If field A is present, then field B must be present as well

error hash is missing error when using Or Definition with nested schemas

definition = Definition.Or(
  Definition.Keys do
    required(:person, Definition.Keys do
      required(:first_name, Definition.Type(String))
    end) 
  end,
  Definition.Keys do
    required(:social_security_number, Definition.Type(String))
  end
)

definition.conform({ person: { first_name: 1 } }).error_hash => {
  "person": null,
  "social_security_number": [
    "<Definition::ConformError \n\t message: \"hash is missing key :social_security_number\", \n\t json_pointer: \"/social_security_number\">"
  ]
}

the returned errors are correct, just the error_hash representation not.

Confusing API of ConformError

The #to_s method of ConformError suggests an API that doesn't exist: (also there's a typo)
image

I'd suggest that the class should define those two methods or be stringified in a different way.

Provide something like dry-initializer solves

Provide functionality to define and validate the initialization arguments of a class in an easy way. e.g.

class MyClass
  include Definition::Initializer

  required :username, Definition.Type(String)
  optional :scores, Definition.Type(Array)

  def hello
    puts "Hello #{username}"
  end
end

instance = MyClass.new(username: "Fred", scores: [1,2,3])
instance.hello # => "Hello Fred"

this would only support keyword arguments though

Feedback welcome :)

Keys Definition fails if input value is nil

schema = Definition.Keys do
  required(:foo, Definition.Type(String))
end

schema.conform(nil) => undefined method `keys' for nil:NilClass
    [ 0] "/home/dog/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/definition-0.6.0/lib/definition/types/keys.rb:99:in `add_extra_key_errors'",
    [ 1] "/home/dog/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/definition-0.6.0/lib/definition/types/keys.rb:87:in `conform'",
    [ 2] "/home/dog/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/definition-0.6.0/lib/definition/types/keys.rb:72:in `conform'",
    [ 3] "/home/dog/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/definition-0.6.0/lib/definition/types/keys.rb:143:in `block in conform_definitions'",
    [ 4] "/home/dog/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/definition-0.6.0/lib/definition/types/keys.rb:140:in `each'",
    [ 5] "/home/dog/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/definition-0.6.0/lib/definition/types/keys.rb:140:in `each_with_object'",
    [ 6] "/home/dog/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/definition-0.6.0/lib/definition/types/keys.rb:140:in `conform_definitions'",
    [ 7] "/home/dog/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/definition-0.6.0/lib/definition/types/keys.rb:113:in `conform_all_keys'",
    [ 8] "/home/dog/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/definition-0.6.0/lib/definition/types/keys.rb:89:in `conform'",
    [ 9] "/home/dog/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/definition-0.6.0/lib/definition/types/keys.rb:72:in `conform'",

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.