Giter Site home page Giter Site logo

respect's People

Contributors

nicolasdespres avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

respect's Issues

CompositeSchema should allow to pass options to composed schema.

Consider the following CompositeSchema:

module Respect
  class PointSchema < CompositeSchema
    def schema_definition
      HashSchema.define do |s|
        s.float "x"
        s.float "y"
      end
    end
  end
end

when using it like that:

HashSchema.define do |s|
  s.point :center
end

The user should be able to pass options to the underlying x and y properties like that:

HashSchema.define do |s|
  s.point :center, x: { greater_than: 4 }, y: { in: 42.5..51.5 }
end

Recursive composite schema must work too. Consider:

HashSchema.define do |s|
  s.circle "area", radius: { greater_than: 10.0 }, center: { x: greater_than: 5.0 }
end

For array we could use the index for key (items and extra items) and :all (item).

Should we check schema definition validity before at definition time?

  • Forbid zero for 'divisible_by' and 'multiple_of'?
    A ZeroDivisionError is raised at validation time instead of a InvalidSchemaError at
    description time.
  • Valid format argument?
    For most of the argument it is not that important since it fails afterward
    at validation time. It will be safer for user to fail with an InvalidSchemaError
    at description time but we will probably loose in extensibility and flexibility.
    Ruby traditional approach would be not to check.
  • Forbid unknown option?
    Same remarks as above.

Cleanup dumpers API.

Right now it is not easy to define how a new Schema sub-class should be dump using the various available dumpers. The ideal would be to only have to overwrite one method.

Main Example - Schema Validation doesn't match DSL

The DSL now looks like this:

schema = Respect::HashSchema.define do |s|
s.string "name"
s.integer "age", greater_than: 18
s.string "homepage", format: :uri
end

But the schema validation will always be false:
schema.validate?({ "name" => "My name", "age" => 20, "email" => "[email protected]" })
schema.validate?({ "name" => "My name", "age" => 15, "email" => "[email protected]" })

The last parameter/key is "email", and it should be "homepage" to match the DSL. Of course, you'll need an appropriate URI value to go with it.

Main example needs a require statement

Before using Respect, you need to require it. The first 2 lines should look like this:

require 'respect'

schema = Respect::HashSchema.define do |s|
...

That way, someone can just copy the code off the page, paste it directly into irb or rails console, and it just works out of the box.

Add :blank boolean option.

Should be used like that:

StringSchema.new(blank: false).validate?("")  #=> false
StringSchema.new(blank: true).validate?("")  #=> true

Must:

  • Use ActiveSupport blank? method.
  • How to we convert it in json-schema.org?

More sugar for `HashDef#[]` and `HashDef#[]=`

Users should be able to write something like that:

# Check "HTTP_VERSION" is "HTTP/1.1".
h["HTTP_VERSION"] = "HTTP/1.1"
# Check "rack.version" is an array equal to [ 1, 1 ].
h["rack.version"] = [ 1, 1 ]
# Check "HTTP_USER_AGENT" matches /^Mozilla/.
h["HTTP_USER_AGENT"] =~ /^Mozilla/
# Check "rack.run_once" is boolean equal to false.
h["rack.run_once"] = false
# Check "action_dispatch.parameter_filter" is an array including :password.
h["action_dispatch.parameter_filter"].include_item :password
# Check "rack.session" is a hash containing a "session_id" key with a string value of minimum length 16.
h["rack.session"].has_property("session_id").string(min_length: 16)
# Check "rack.session" is a hash containing a _csrf_token" key with a string value of minimum length 44.
h["rack.session"].has_property("_csrf_token").string(min_length: 44)
# Check "rack.session.options" is a hash with the given hash schema.
h["rack.session.options"].object strict: false do |s|
  # A "secret" key with a string value of minimum lenght 60.
  s.string "secret", min_length: 60
  # A "httponly" key with a boolean value equal to true
  s.boolean "httponly", equal_to: true
  # A "rack.request.cookie_hash" hash with the following schema.
  s.headers "rack.request.cookie_hash" do |h|
    h["_blog_session"].string min_length: 218
  end
end
# Check "action_controller.instance" value is a ActionController::Base instance.
# This check won't be documented.
h["action_controller.instance"].is_a ActionController::Base, doc: false
# Check "__internal__" is a string equal to "foo" and won't be documented.
h["__internal__"].must_be("foo", doc: false)
# Check "REQUEST_URI" is string formatted as a URI.
h.uri "REQUEST_URI"

This would be useful for headers specification.

Example DSL has a minor error

The example DSL reads as follows:
schema = Respect::HashSchema.define do |s|
s.string "name"
s.integer "age", greater_than: 18
s.string "homepage", format: :email
end

But there's a minor mistake that causes the schema validation to always return false.
The last value in the DSL should read:
s.string "email", format: :email

Then the schema validation examples lower down the page work as advertised.

Add a better loggin system in back-end.

Right now there is the poorest logging system in dumpers: Kernel.warn. We should use a regular logger API and let the user provide its own. We could also introduce a Logger module which add the logger API to an object/class implementing print.

`puts JSON.pretty_generate(schema.to_h)` doesn't actually work

In Ruby 2.1, I get:

lib/respect/schema.rb:252:in `to_json': unknown format '#<JSON::Ext::Generator::State:0x007fd32b89b5d8>' (ArgumentError)
    from /opt/rubies/2.1.4-github1/lib/ruby/2.1.0/json/common.rb:285:in `generate'
    from /opt/rubies/2.1.4-github1/lib/ruby/2.1.0/json/common.rb:285:in `pretty_generate'
    from scheme.rb:11:in `<main>'

I think the def to_json implementation assumes some other value for format.

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.