goltergaul / definition Goto Github PK
View Code? Open in Web Editor NEWSimple and composable validation and coercion of data structures
License: MIT License
Simple and composable validation and coercion of data structures
License: MIT License
$ 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)
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
... like string length checks (min/max), non empty strings checks, gt, lt and maybe more
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:
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.
❤️
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 :)
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'",
Currently you cannot define defaults for Key Definitions
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.