sinatra-schema 0.0.3 → 0.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- MGZiZDNiMDcyM2EzNzM1MjdmZWUxOTkzZGNjY2QxNmVmN2RmODRiMg==
4
+ OTg4NGUxMjY3YWRjOGI3ZTQ4NDI3ZGU5ZTIyNGU2ZmM1OWQ1ZWUxOQ==
5
5
  data.tar.gz: !binary |-
6
- ODY0YjViYzM3YmJhYjIzODc3Y2U3ZmMwMzJmM2Q5YzRiN2M4ZjNmYw==
6
+ MGVlODRhMWE1YzgxZjJhNGYyOWYzYzMyNDMwZWQxNzdiMzZlOTVmNg==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- MDQwY2YwMzU3MTg3ZDkzMGNiMDY2MTFlNjM0MWFkNjYzNzdhZTQzMWVlYjg1
10
- NzdhNzk2NjZlM2YyNjQ1MzQxZjA5ZTNiNWQ0NGI5MjYxYjI3MTUzYjYxODMx
11
- NDJhYzMyNDI2NDZjZGJlNjVhYzQxMTQ0OTdjOTRkN2Q1ZmQ3NmY=
9
+ MzRmMjhjOGEwODY5ZjU5YWU5OTM0MWExMTgwYzllYmVkMjAxNjMwMmI5NTlj
10
+ ZTg3YmVjOGZjNGRmZDgwMmE1NTcyMWUwMTk2YjIwNGU2ZTIxODNmYmQ0NWVm
11
+ ZGViNGRhYzgzMzVkNjhmNGMxNGY3M2JlYmE0NDMwMzNmMzk5OTY=
12
12
  data.tar.gz: !binary |-
13
- YjE1MWY4MTVhYTMxYzEyODU0Mjk3Y2NiY2I5YTA5YjBkMTlmOGE4MmZlOTMw
14
- NDlkNzU2MmU2OTg0ZGNiYzdkNWRkZjAwYmQ5OWE0MzNhNGE0MDVjZmVkZWFk
15
- NzM3MDNkZmEwOGI1OTY4ZWQ3NTg2ODY0MjBiODRlNDU5YTZlOGE=
13
+ Njg2Mjg4MGM5YzFiNjY5YWNhZGM4YjIxYThlYzgyNDQyYTg4NzMzZWUxMThj
14
+ MjJkMTU1MzQzNDNjZGNlN2ZiZGNjMGViZjc0NzAzYzk3NjI3M2QyMjkxYmUz
15
+ OWM3MjdjNDIyOGQ0ZWE2M2NmZTVkN2RhOTc1NzAwMDEyZDNiZjg=
@@ -40,7 +40,7 @@ module Sinatra
40
40
 
41
41
  def ref(id)
42
42
  unless definition = resource.defs[id] || Sinatra::Schema::Root.instance.find_definition(id)
43
- raise "Unknown reference: #{id}"
43
+ raise BadReference.new(id)
44
44
  end
45
45
  add definition, true
46
46
  end
@@ -0,0 +1,24 @@
1
+ module Sinatra
2
+ module Schema
3
+ class Error < StandardError
4
+ end
5
+
6
+ class BadReference < Error
7
+ def initialize(id)
8
+ super("Could not resolve property ref #{id}")
9
+ end
10
+ end
11
+
12
+ class BadRequest < Error
13
+ def initialize(msg)
14
+ super(msg)
15
+ end
16
+ end
17
+
18
+ class BadParams < Error
19
+ def initialize(msg)
20
+ super(msg)
21
+ end
22
+ end
23
+ end
24
+ end
@@ -23,8 +23,6 @@ module Sinatra
23
23
  res = instance_exec(schema_params, &link.action_block)
24
24
  link.resource.validate_response!(res)
25
25
  res
26
- rescue RuntimeError => e
27
- halt(400)
28
26
  end
29
27
  end
30
28
  end
@@ -8,7 +8,7 @@ module Sinatra
8
8
  when "application/x-www-form-urlencoded"
9
9
  cast_regular_params(properties)
10
10
  else
11
- raise "Cannot handle media type #{request.media_type}"
11
+ raise BadRequest.new("Unsupported media type: #{request.media_type}")
12
12
  end
13
13
  end
14
14
 
@@ -21,12 +21,12 @@ module Sinatra
21
21
  request.body.rewind # leave it ready for other calls
22
22
  supplied_params = MultiJson.decode(body)
23
23
  unless supplied_params.is_a?(Hash)
24
- raise "Invalid request, expecting a hash"
24
+ raise BadRequest.new("Invalid JSON request, please encode params as a hash")
25
25
  end
26
26
 
27
27
  indifferent_params(supplied_params)
28
28
  rescue MultiJson::ParseError
29
- raise "Invalid JSON"
29
+ raise BadRequest.new("Invalid JSON encoded in request")
30
30
  end
31
31
 
32
32
  def cast_regular_params(properties, root=params)
@@ -4,12 +4,12 @@ module Sinatra
4
4
  def validate_params!(params, properties)
5
5
  missing = properties.keys.map(&:to_s).sort - params.keys.map(&:to_s).sort
6
6
  unless missing.empty?
7
- raise "Missing properties: #{missing}"
7
+ raise BadParams.new("Missing expected params: #{missing.join(',')}")
8
8
  end
9
9
 
10
10
  extra = params.keys.map(&:to_s).sort - properties.keys.map(&:to_s).sort
11
11
  unless extra.empty?
12
- raise "Unexpected properties: #{extra}"
12
+ raise BadParams.new("Unexpected params: #{extra.join(',')}")
13
13
  end
14
14
 
15
15
  properties.each do |id, definition|
@@ -18,7 +18,7 @@ module Sinatra
18
18
  validate_params!(params[id], definition)
19
19
  else
20
20
  unless definition.valid?(params[id])
21
- raise "Bad param: #{id}"
21
+ raise BadParams.new("Incorrect format for param '#{id}'. Please encode it as #{definition.type}")
22
22
  end
23
23
  end
24
24
  end
@@ -1,5 +1,5 @@
1
1
  module Sinatra
2
2
  module Schema
3
- VERSION = "0.0.3"
3
+ VERSION = "0.0.4"
4
4
  end
5
5
  end
@@ -4,6 +4,7 @@ require "singleton"
4
4
  require "multi_json"
5
5
 
6
6
  require "sinatra/schema/definition"
7
+ require "sinatra/schema/error"
7
8
  require "sinatra/schema/link"
8
9
  require "sinatra/schema/param_parsing"
9
10
  require "sinatra/schema/param_validation"
@@ -19,6 +20,15 @@ module Sinatra
19
20
  def self.registered(app)
20
21
  app.helpers ParamParsing
21
22
  app.helpers ParamValidation
23
+
24
+ app.error(Sinatra::Schema::BadRequest) do |e|
25
+ halt(400, MultiJson.encode(error: e.message))
26
+ end
27
+
28
+ app.error(Sinatra::Schema::BadParams) do |e|
29
+ halt(422, MultiJson.encode(error: e.message))
30
+ end
31
+
22
32
  app.get "/schema" do
23
33
  content_type("application/schema+json")
24
34
  response.headers["Cache-Control"] = "public, max-age=3600"
@@ -45,5 +45,12 @@ describe Sinatra::Schema::DSL::Definitions do
45
45
  assert_equal 1, other.defs.size
46
46
  assert_equal 1, resource.properties.size
47
47
  end
48
+
49
+ it "raises when we can't resolve the ref" do
50
+ assert_raises(Sinatra::Schema::BadReference) do
51
+ dsl.ref :foobar
52
+ end
53
+
54
+ end
48
55
  end
49
56
  end
@@ -21,8 +21,21 @@ describe Sinatra::Schema do
21
21
  MultiJson.decode(last_response.body))
22
22
  end
23
23
 
24
- it "validates input" do
25
- post "/accounts", foo: "bar"
26
- assert_equal 400, last_response.status
24
+ describe "error handling" do
25
+ before do
26
+ header "Content-Type", "application/json"
27
+ end
28
+
29
+ it "converts BadRequest into a 400" do
30
+ post "/accounts", "{"
31
+ assert_equal 400, last_response.status
32
+ assert_equal({ "error" => "Invalid JSON encoded in request" }, last_json)
33
+ end
34
+
35
+ it "converts BadParams into a 422" do
36
+ post "/accounts", "{}"
37
+ assert_equal 422, last_response.status
38
+ assert_equal({ "error" => "Missing expected params: email" }, last_json)
39
+ end
27
40
  end
28
41
  end
@@ -16,11 +16,17 @@ describe Sinatra::Schema::ParamParsing do
16
16
  end
17
17
 
18
18
  it "handles invalid json" do
19
- assert_raises(RuntimeError, "Invalid JSON") do
19
+ assert_raises(Sinatra::Schema::BadRequest) do
20
20
  post "/", "{"
21
21
  end
22
22
  end
23
23
 
24
+ it "handles params not serialized as a hash" do
25
+ assert_raises(Sinatra::Schema::BadRequest) do
26
+ post "/", MultiJson.encode(["lolllll"])
27
+ end
28
+ end
29
+
24
30
  it "considers an empty body an empty hash" do
25
31
  post "/", ""
26
32
  assert_equal Hash.new, last_json
@@ -73,7 +79,7 @@ describe Sinatra::Schema::ParamParsing do
73
79
  end
74
80
 
75
81
  it "errors out on other formats" do
76
- assert_raises(RuntimeError, "Cannot handle media type application/xml") do
82
+ assert_raises(Sinatra::Schema::BadRequest) do
77
83
  header "Content-Type", "application/xml"
78
84
  post "/", "<omg />"
79
85
  end
@@ -14,28 +14,28 @@ describe Sinatra::Schema::ParamValidation do
14
14
 
15
15
  it "errors out on unexpected params" do
16
16
  $properties = {}
17
- assert_raises(RuntimeError, "omg just realized this doesn't do shit") do
17
+ assert_raises(Sinatra::Schema::BadParams) do
18
18
  post "/", MultiJson.encode(foo: "bar")
19
19
  end
20
20
  end
21
21
 
22
22
  it "errors out on missing params" do
23
23
  $properties = { foo: Sinatra::Schema::Definition.new(type: "string") }
24
- assert_raises(RuntimeError) do
24
+ assert_raises(Sinatra::Schema::BadParams) do
25
25
  post "/", "{}"
26
26
  end
27
27
  end
28
28
 
29
29
  it "errors out on wrong format" do
30
30
  $properties = { bool: Sinatra::Schema::Definition.new(type: "boolean") }
31
- assert_raises(RuntimeError) do
31
+ assert_raises(Sinatra::Schema::BadParams) do
32
32
  post "/", MultiJson.encode(bool: "omg")
33
33
  end
34
34
  end
35
35
 
36
36
  it "supports nested params" do
37
37
  $properties = { foo: { bar: Sinatra::Schema::Definition.new(type: "boolean") }}
38
- assert_raises(RuntimeError) do
38
+ assert_raises(Sinatra::Schema::BadParams) do
39
39
  post "/", MultiJson.encode(foo: { bar: "omg" })
40
40
  puts last_response.body
41
41
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sinatra-schema
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Pedro Belo
@@ -138,6 +138,7 @@ files:
138
138
  - lib/sinatra/schema/dsl/definitions.rb
139
139
  - lib/sinatra/schema/dsl/links.rb
140
140
  - lib/sinatra/schema/dsl/resources.rb
141
+ - lib/sinatra/schema/error.rb
141
142
  - lib/sinatra/schema/link.rb
142
143
  - lib/sinatra/schema/param_parsing.rb
143
144
  - lib/sinatra/schema/param_validation.rb