json_schema_rails 0.0.1

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.
Files changed (123) hide show
  1. checksums.yaml +7 -0
  2. data/.editorconfig +16 -0
  3. data/.gitignore +11 -0
  4. data/.rspec +1 -0
  5. data/Gemfile +14 -0
  6. data/Guardfile +7 -0
  7. data/MIT-LICENSE +20 -0
  8. data/README.md +121 -0
  9. data/Rakefile +17 -0
  10. data/json_schema_rails.gemspec +29 -0
  11. data/lib/generators/schema/action/USAGE +22 -0
  12. data/lib/generators/schema/action/action_generator.rb +52 -0
  13. data/lib/generators/schema/action/templates/schema.yml.erb +31 -0
  14. data/lib/generators/schema/controller/USAGE +13 -0
  15. data/lib/generators/schema/controller/controller_generator.rb +27 -0
  16. data/lib/generators/schema/controller/templates/schema.yml.erb +7 -0
  17. data/lib/json_schema_rails/errors.rb +44 -0
  18. data/lib/json_schema_rails/helpers.rb +38 -0
  19. data/lib/json_schema_rails/loaders/base.rb +72 -0
  20. data/lib/json_schema_rails/loaders/directory.rb +39 -0
  21. data/lib/json_schema_rails/loaders/hyper_schema.rb +68 -0
  22. data/lib/json_schema_rails/loaders.rb +7 -0
  23. data/lib/json_schema_rails/railtie.rb +25 -0
  24. data/lib/json_schema_rails/schema_validator.rb +91 -0
  25. data/lib/json_schema_rails/version.rb +3 -0
  26. data/lib/json_schema_rails.rb +43 -0
  27. data/lib/tasks/schema_tasks.rake +37 -0
  28. data/schemas/core-schema.json +150 -0
  29. data/schemas/hyper-schema.json +168 -0
  30. data/schemas/strict/core-schema.json +151 -0
  31. data/schemas/strict/hyper-schema.json +169 -0
  32. data/spec/dummy_apps/rails3/README.rdoc +261 -0
  33. data/spec/dummy_apps/rails3/Rakefile +7 -0
  34. data/spec/dummy_apps/rails3/app/assets/images/rails.png +0 -0
  35. data/spec/dummy_apps/rails3/app/assets/javascripts/application.js +13 -0
  36. data/spec/dummy_apps/rails3/app/assets/stylesheets/application.css +13 -0
  37. data/spec/dummy_apps/rails3/app/controllers/application_controller.rb +3 -0
  38. data/spec/dummy_apps/rails3/app/controllers/posts_controller.rb +11 -0
  39. data/spec/dummy_apps/rails3/app/helpers/application_helper.rb +2 -0
  40. data/spec/dummy_apps/rails3/app/schemas/posts/create.yml +20 -0
  41. data/spec/dummy_apps/rails3/app/schemas/posts/update.json +29 -0
  42. data/spec/dummy_apps/rails3/app/views/layouts/application.html.erb +14 -0
  43. data/spec/dummy_apps/rails3/config/application.rb +68 -0
  44. data/spec/dummy_apps/rails3/config/boot.rb +6 -0
  45. data/spec/dummy_apps/rails3/config/database.yml +25 -0
  46. data/spec/dummy_apps/rails3/config/environment.rb +5 -0
  47. data/spec/dummy_apps/rails3/config/environments/development.rb +37 -0
  48. data/spec/dummy_apps/rails3/config/environments/production.rb +67 -0
  49. data/spec/dummy_apps/rails3/config/environments/test.rb +37 -0
  50. data/spec/dummy_apps/rails3/config/initializers/backtrace_silencers.rb +7 -0
  51. data/spec/dummy_apps/rails3/config/initializers/inflections.rb +15 -0
  52. data/spec/dummy_apps/rails3/config/initializers/mime_types.rb +5 -0
  53. data/spec/dummy_apps/rails3/config/initializers/secret_token.rb +7 -0
  54. data/spec/dummy_apps/rails3/config/initializers/session_store.rb +8 -0
  55. data/spec/dummy_apps/rails3/config/initializers/wrap_parameters.rb +14 -0
  56. data/spec/dummy_apps/rails3/config/locales/en.yml +5 -0
  57. data/spec/dummy_apps/rails3/config/routes.rb +60 -0
  58. data/spec/dummy_apps/rails3/config.ru +4 -0
  59. data/spec/dummy_apps/rails3/db/seeds.rb +7 -0
  60. data/spec/dummy_apps/rails3/doc/README_FOR_APP +2 -0
  61. data/spec/dummy_apps/rails3/public/404.html +26 -0
  62. data/spec/dummy_apps/rails3/public/422.html +26 -0
  63. data/spec/dummy_apps/rails3/public/500.html +25 -0
  64. data/spec/dummy_apps/rails3/public/favicon.ico +0 -0
  65. data/spec/dummy_apps/rails3/public/index.html +241 -0
  66. data/spec/dummy_apps/rails3/public/robots.txt +5 -0
  67. data/spec/dummy_apps/rails3/script/rails +6 -0
  68. data/spec/dummy_apps/rails4/README.rdoc +28 -0
  69. data/spec/dummy_apps/rails4/Rakefile +6 -0
  70. data/spec/dummy_apps/rails4/app/assets/images/.keep +0 -0
  71. data/spec/dummy_apps/rails4/app/assets/javascripts/application.js +13 -0
  72. data/spec/dummy_apps/rails4/app/assets/javascripts/posts.js +2 -0
  73. data/spec/dummy_apps/rails4/app/assets/stylesheets/application.css +15 -0
  74. data/spec/dummy_apps/rails4/app/assets/stylesheets/posts.css +4 -0
  75. data/spec/dummy_apps/rails4/app/controllers/application_controller.rb +5 -0
  76. data/spec/dummy_apps/rails4/app/controllers/concerns/.keep +0 -0
  77. data/spec/dummy_apps/rails4/app/controllers/posts_controller.rb +11 -0
  78. data/spec/dummy_apps/rails4/app/helpers/application_helper.rb +2 -0
  79. data/spec/dummy_apps/rails4/app/mailers/.keep +0 -0
  80. data/spec/dummy_apps/rails4/app/models/.keep +0 -0
  81. data/spec/dummy_apps/rails4/app/models/concerns/.keep +0 -0
  82. data/spec/dummy_apps/rails4/app/schemas/posts/create.yml +20 -0
  83. data/spec/dummy_apps/rails4/app/schemas/posts/update.json +29 -0
  84. data/spec/dummy_apps/rails4/app/views/layouts/application.html.erb +14 -0
  85. data/spec/dummy_apps/rails4/bin/bundle +3 -0
  86. data/spec/dummy_apps/rails4/bin/rails +4 -0
  87. data/spec/dummy_apps/rails4/bin/rake +4 -0
  88. data/spec/dummy_apps/rails4/config/application.rb +29 -0
  89. data/spec/dummy_apps/rails4/config/boot.rb +5 -0
  90. data/spec/dummy_apps/rails4/config/environment.rb +5 -0
  91. data/spec/dummy_apps/rails4/config/environments/development.rb +34 -0
  92. data/spec/dummy_apps/rails4/config/environments/production.rb +80 -0
  93. data/spec/dummy_apps/rails4/config/environments/test.rb +39 -0
  94. data/spec/dummy_apps/rails4/config/initializers/backtrace_silencers.rb +7 -0
  95. data/spec/dummy_apps/rails4/config/initializers/cookies_serializer.rb +3 -0
  96. data/spec/dummy_apps/rails4/config/initializers/filter_parameter_logging.rb +4 -0
  97. data/spec/dummy_apps/rails4/config/initializers/inflections.rb +16 -0
  98. data/spec/dummy_apps/rails4/config/initializers/mime_types.rb +4 -0
  99. data/spec/dummy_apps/rails4/config/initializers/session_store.rb +3 -0
  100. data/spec/dummy_apps/rails4/config/initializers/wrap_parameters.rb +9 -0
  101. data/spec/dummy_apps/rails4/config/locales/en.yml +23 -0
  102. data/spec/dummy_apps/rails4/config/routes.rb +58 -0
  103. data/spec/dummy_apps/rails4/config/secrets.yml +22 -0
  104. data/spec/dummy_apps/rails4/config.ru +4 -0
  105. data/spec/dummy_apps/rails4/lib/assets/.keep +0 -0
  106. data/spec/dummy_apps/rails4/log/.keep +0 -0
  107. data/spec/dummy_apps/rails4/public/404.html +67 -0
  108. data/spec/dummy_apps/rails4/public/422.html +67 -0
  109. data/spec/dummy_apps/rails4/public/500.html +66 -0
  110. data/spec/dummy_apps/rails4/public/favicon.ico +0 -0
  111. data/spec/fixtures/hyper_schema.yml +42 -0
  112. data/spec/fixtures/schemas/invalid_schema.yml +15 -0
  113. data/spec/fixtures/schemas/posts/create.yml +20 -0
  114. data/spec/fixtures/schemas/posts/search.json +13 -0
  115. data/spec/fixtures/schemas/posts/update.yaml +20 -0
  116. data/spec/fixtures/schemas/syntax_error.json +5 -0
  117. data/spec/lib/json_schema_rails/loaders/directory_spec.rb +59 -0
  118. data/spec/lib/json_schema_rails/loaders/hyper_schema_spec.rb +40 -0
  119. data/spec/lib/json_schema_rails/schema_validator_spec.rb +57 -0
  120. data/spec/lib/json_schema_rails_spec.rb +123 -0
  121. data/spec/railtie/controllers/posts_controller_spec.rb +43 -0
  122. data/spec/spec_helper.rb +28 -0
  123. metadata +406 -0
@@ -0,0 +1,67 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>The change you wanted was rejected (422)</title>
5
+ <meta name="viewport" content="width=device-width,initial-scale=1">
6
+ <style>
7
+ body {
8
+ background-color: #EFEFEF;
9
+ color: #2E2F30;
10
+ text-align: center;
11
+ font-family: arial, sans-serif;
12
+ margin: 0;
13
+ }
14
+
15
+ div.dialog {
16
+ width: 95%;
17
+ max-width: 33em;
18
+ margin: 4em auto 0;
19
+ }
20
+
21
+ div.dialog > div {
22
+ border: 1px solid #CCC;
23
+ border-right-color: #999;
24
+ border-left-color: #999;
25
+ border-bottom-color: #BBB;
26
+ border-top: #B00100 solid 4px;
27
+ border-top-left-radius: 9px;
28
+ border-top-right-radius: 9px;
29
+ background-color: white;
30
+ padding: 7px 12% 0;
31
+ box-shadow: 0 3px 8px rgba(50, 50, 50, 0.17);
32
+ }
33
+
34
+ h1 {
35
+ font-size: 100%;
36
+ color: #730E15;
37
+ line-height: 1.5em;
38
+ }
39
+
40
+ div.dialog > p {
41
+ margin: 0 0 1em;
42
+ padding: 1em;
43
+ background-color: #F7F7F7;
44
+ border: 1px solid #CCC;
45
+ border-right-color: #999;
46
+ border-left-color: #999;
47
+ border-bottom-color: #999;
48
+ border-bottom-left-radius: 4px;
49
+ border-bottom-right-radius: 4px;
50
+ border-top-color: #DADADA;
51
+ color: #666;
52
+ box-shadow: 0 3px 8px rgba(50, 50, 50, 0.17);
53
+ }
54
+ </style>
55
+ </head>
56
+
57
+ <body>
58
+ <!-- This file lives in public/422.html -->
59
+ <div class="dialog">
60
+ <div>
61
+ <h1>The change you wanted was rejected.</h1>
62
+ <p>Maybe you tried to change something you didn't have access to.</p>
63
+ </div>
64
+ <p>If you are the application owner check the logs for more information.</p>
65
+ </div>
66
+ </body>
67
+ </html>
@@ -0,0 +1,66 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>We're sorry, but something went wrong (500)</title>
5
+ <meta name="viewport" content="width=device-width,initial-scale=1">
6
+ <style>
7
+ body {
8
+ background-color: #EFEFEF;
9
+ color: #2E2F30;
10
+ text-align: center;
11
+ font-family: arial, sans-serif;
12
+ margin: 0;
13
+ }
14
+
15
+ div.dialog {
16
+ width: 95%;
17
+ max-width: 33em;
18
+ margin: 4em auto 0;
19
+ }
20
+
21
+ div.dialog > div {
22
+ border: 1px solid #CCC;
23
+ border-right-color: #999;
24
+ border-left-color: #999;
25
+ border-bottom-color: #BBB;
26
+ border-top: #B00100 solid 4px;
27
+ border-top-left-radius: 9px;
28
+ border-top-right-radius: 9px;
29
+ background-color: white;
30
+ padding: 7px 12% 0;
31
+ box-shadow: 0 3px 8px rgba(50, 50, 50, 0.17);
32
+ }
33
+
34
+ h1 {
35
+ font-size: 100%;
36
+ color: #730E15;
37
+ line-height: 1.5em;
38
+ }
39
+
40
+ div.dialog > p {
41
+ margin: 0 0 1em;
42
+ padding: 1em;
43
+ background-color: #F7F7F7;
44
+ border: 1px solid #CCC;
45
+ border-right-color: #999;
46
+ border-left-color: #999;
47
+ border-bottom-color: #999;
48
+ border-bottom-left-radius: 4px;
49
+ border-bottom-right-radius: 4px;
50
+ border-top-color: #DADADA;
51
+ color: #666;
52
+ box-shadow: 0 3px 8px rgba(50, 50, 50, 0.17);
53
+ }
54
+ </style>
55
+ </head>
56
+
57
+ <body>
58
+ <!-- This file lives in public/500.html -->
59
+ <div class="dialog">
60
+ <div>
61
+ <h1>We're sorry, but something went wrong.</h1>
62
+ </div>
63
+ <p>If you are the application owner check the logs for more information.</p>
64
+ </div>
65
+ </body>
66
+ </html>
File without changes
@@ -0,0 +1,42 @@
1
+ ---
2
+ "$schema": "http://json-schema.org/draft-04/hyper-schema#"
3
+ title: Test Schema API
4
+ type: object
5
+ links:
6
+ - href: http://example.com
7
+ rel: self
8
+ properties:
9
+ posts:
10
+ type: object
11
+ links:
12
+ - method: POST
13
+ href: "/posts"
14
+ rel: create
15
+ schema:
16
+ type: object
17
+ properties:
18
+ post:
19
+ "$ref": "#/definitions/post"
20
+ - method: PUT
21
+ href: "/posts/{id}"
22
+ rel: update
23
+ schema:
24
+ type: object
25
+ properties:
26
+ post:
27
+ "$ref": "#/definitions/post"
28
+
29
+ definitions:
30
+ post:
31
+ type: object
32
+ additionalProperties: false
33
+ required:
34
+ - title
35
+ - body
36
+ properties:
37
+ title:
38
+ type: string
39
+ body:
40
+ type: string
41
+ author:
42
+ type: string
@@ -0,0 +1,15 @@
1
+ ---
2
+ "$schema": "http://json-schema.org/draft-04/schema#"
3
+ title: Invalid Schema
4
+ type: object
5
+ required:
6
+ - foo
7
+ - bar
8
+ properties:
9
+ foo:
10
+ type: unknown
11
+ bar:
12
+ type: object
13
+ propertie: # Misspelled!
14
+ baz:
15
+ type: string
@@ -0,0 +1,20 @@
1
+ ---
2
+ "$schema": "http://json-schema.org/draft-04/schema#"
3
+ title: New Post
4
+ type: object
5
+ required:
6
+ - post
7
+ properties:
8
+ post:
9
+ type: object
10
+ additionalProperties: false
11
+ required:
12
+ - title
13
+ - body
14
+ properties:
15
+ title:
16
+ type: string
17
+ body:
18
+ type: string
19
+ author:
20
+ type: string
@@ -0,0 +1,13 @@
1
+ {
2
+ "$schema": "http://json-schema.org/draft-04/schema#",
3
+ "title": "Search Posts",
4
+ "type": "object",
5
+ "required": [
6
+ "query"
7
+ ],
8
+ "properties": {
9
+ "query": {
10
+ "type": "string"
11
+ }
12
+ }
13
+ }
@@ -0,0 +1,20 @@
1
+ ---
2
+ "$schema": "http://json-schema.org/draft-04/schema#"
3
+ title: Update Post
4
+ type: object
5
+ required:
6
+ - post
7
+ properties:
8
+ post:
9
+ type: object
10
+ additionalProperties: false
11
+ required:
12
+ - title
13
+ - body
14
+ properties:
15
+ title:
16
+ type: string
17
+ body:
18
+ type: string
19
+ author:
20
+ type: string
@@ -0,0 +1,5 @@
1
+ {
2
+ "$schema": "http://json-schema.org/draft-04/schema#",
3
+ "title": "Syntax Error Schema",
4
+ bad bad bad
5
+ }
@@ -0,0 +1,59 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe JsonSchemaRails::Loaders::Directory do
4
+ let(:loader) { JsonSchemaRails::Loaders::Directory.new(TEST_SCHEMAS_DIR) }
5
+
6
+ describe "#load_schema" do
7
+ subject { loader.load_schema(schema_name) }
8
+
9
+ context "with .yml file" do
10
+ let(:schema_name) { "posts/create" }
11
+ it { should be_a JsonSchema::Schema }
12
+ end
13
+
14
+ context "with .yaml file" do
15
+ let(:schema_name) { "posts/update" }
16
+ it { should be_a JsonSchema::Schema }
17
+ end
18
+
19
+ context "with .json file" do
20
+ let(:schema_name) { "posts/search" }
21
+ it { should be_a JsonSchema::Schema }
22
+ end
23
+
24
+ context "with nonexist file" do
25
+ let(:schema_name) { "posts/unknown" }
26
+ it { should be_nil }
27
+ end
28
+
29
+ context "when extname specified" do
30
+ before { loader.extnames = ".json" }
31
+
32
+ context "with specified extname file" do
33
+ let(:schema_name) { "posts/search" }
34
+ it { should be_a JsonSchema::Schema }
35
+ end
36
+
37
+ context "with not specified extname file" do
38
+ let(:schema_name) { "posts/create" }
39
+ it { should be_nil }
40
+ end
41
+ end
42
+ end
43
+
44
+ describe "#load_schema!" do
45
+ subject { -> { loader.load_schema!(schema_name) } }
46
+
47
+ context "with exist file" do
48
+ let(:schema_name) { "posts/create" }
49
+ it "returns schema" do
50
+ expect(subject.call).to be_a JsonSchema::Schema
51
+ end
52
+ end
53
+
54
+ context "with nonexist file" do
55
+ let(:schema_name) { "posts/unknown" }
56
+ it { should raise_error JsonSchemaRails::SchemaNotFound }
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,40 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe JsonSchemaRails::Loaders::HyperSchema do
4
+ let(:loader) { JsonSchemaRails::Loaders::HyperSchema.new(TEST_HYPER_SCHEMA_FILE) }
5
+
6
+ describe "#load_schema" do
7
+ subject { loader.load_schema(schema_name) }
8
+
9
+ context "with known static route" do
10
+ let(:schema_name) { "POST/posts" }
11
+ it { should be_a JsonSchema::Schema }
12
+ end
13
+
14
+ context "with known dynamic route" do
15
+ let(:schema_name) { "PUT/posts/123" }
16
+ it { should be_a JsonSchema::Schema }
17
+ end
18
+
19
+ context "with nonexist route" do
20
+ let(:schema_name) { "GET/unknown" }
21
+ it { should be_nil }
22
+ end
23
+ end
24
+
25
+ describe "#load_schema!" do
26
+ subject { -> { loader.load_schema!(schema_name) } }
27
+
28
+ context "with exist schema" do
29
+ let(:schema_name) { "POST/posts" }
30
+ it "returns schema" do
31
+ expect(subject.call).to be_a JsonSchema::Schema
32
+ end
33
+ end
34
+
35
+ context "with nonexist schema" do
36
+ let(:schema_name) { "POST/unknown" }
37
+ it { should raise_error JsonSchemaRails::SchemaNotFound }
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,57 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe JsonSchemaRails::SchemaValidator do
4
+ let(:validator) { JsonSchemaRails::SchemaValidator.new }
5
+
6
+ describe "#use_core_schemas!" do
7
+ context "with default option" do
8
+ before { validator.use_core_schemas! }
9
+
10
+ it "has 2 schemas" do
11
+ expect(validator.meta_schemas.size).to eq 2
12
+ end
13
+
14
+ it "uses strict schemas" do
15
+ schema = validator.meta_schemas.first
16
+ expect(schema.additional_properties).to eq false
17
+ end
18
+ end
19
+
20
+ context "with loose option" do
21
+ before { validator.use_core_schemas!(loose: true) }
22
+
23
+ it "has 2 schemas" do
24
+ expect(validator.meta_schemas.size).to eq 2
25
+ end
26
+
27
+ it "uses loose schemas" do
28
+ schema = validator.meta_schemas.first
29
+ expect(schema.additional_properties).to eq true
30
+ end
31
+ end
32
+ end
33
+
34
+ describe "#validate" do
35
+ before { validator.use_core_schemas! }
36
+ subject { -> { validator.validate(schema_file) } }
37
+
38
+ context "with valid schema" do
39
+ let(:schema_file) { File.join(TEST_SCHEMAS_DIR, "posts/create.yml") }
40
+
41
+ it "returns true" do
42
+ expect(subject.call).to eq true
43
+ end
44
+ end
45
+
46
+ context "with invalid schema" do
47
+ let(:schema_file) { File.join(TEST_SCHEMAS_DIR, "invalid_schema.yml") }
48
+
49
+ it "raises ValidationError with 2 errors" do
50
+ expect(subject).to raise_error do |e|
51
+ expect(e).to be_a JsonSchemaRails::ValidationError
52
+ expect(e.errors.size).to eq 2
53
+ end
54
+ end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,123 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe JsonSchemaRails do
4
+ before do
5
+ JsonSchemaRails.schema_loader = JsonSchemaRails::Loaders::Directory.new(TEST_SCHEMAS_DIR)
6
+ end
7
+
8
+ describe "#validate!" do
9
+ subject { -> { JsonSchemaRails.validate! schema_name, data } }
10
+
11
+ context "with valid data" do
12
+ let(:schema_name) { "posts/create" }
13
+ let(:data) { { "post" => { "title" => "Hello, world!", "body" => "Test Body" } } }
14
+ it { should_not raise_error }
15
+ end
16
+
17
+ context "with invalid data" do
18
+ let(:schema_name) { "posts/create" }
19
+ let(:data) { { "post" => { "title" => "Hello, world!" } } }
20
+ it { should raise_error JsonSchemaRails::ValidationError }
21
+ end
22
+
23
+ context "with unknown schema" do
24
+ let(:schema_name) { "posts/unknown" }
25
+ let(:data) { {} }
26
+ it { should raise_error JsonSchemaRails::SchemaNotFound }
27
+ end
28
+
29
+ context "with syntax errors in schema" do
30
+ let(:schema_name) { "syntax_error" }
31
+ let(:data) { {} }
32
+ it { should raise_error JsonSchemaRails::SchemaParseError }
33
+ end
34
+ end
35
+
36
+ describe "#validate" do
37
+ subject { -> { JsonSchemaRails.validate schema_name, data } }
38
+
39
+ context "with valid data" do
40
+ let(:schema_name) { "posts/create" }
41
+ let(:data) { { "post" => { "title" => "Hello, world!", "body" => "Test Body" } } }
42
+
43
+ it "returns a tuple of true validation result and empty errors" do
44
+ result, errors = subject.call
45
+ expect(result).to eq true
46
+ expect(errors).to be_empty
47
+ end
48
+ end
49
+
50
+ context "with invalid data" do
51
+ let(:schema_name) { "posts/create" }
52
+ let(:data) { { "post" => { "title" => "Hello, world!" } } }
53
+
54
+ it "returns a tuple of false validation result and errors" do
55
+ result, errors = subject.call
56
+ expect(result).to eq false
57
+ expect(errors).not_to be_empty
58
+ end
59
+ end
60
+
61
+ context "with unknown schema" do
62
+ let(:schema_name) { "posts/unknown" }
63
+ let(:data) { {} }
64
+ it { should raise_error JsonSchemaRails::SchemaNotFound }
65
+ end
66
+
67
+ context "with syntax errors in schema" do
68
+ let(:schema_name) { "syntax_error" }
69
+ let(:data) { {} }
70
+ it { should raise_error JsonSchemaRails::SchemaParseError }
71
+ end
72
+ end
73
+
74
+ describe "#lookup_schema!" do
75
+ subject { -> { JsonSchemaRails.lookup_schema!(schema_name) } }
76
+
77
+ context "with known schema" do
78
+ let(:schema_name) { "posts/create" }
79
+
80
+ it "returns the schema" do
81
+ expect(subject.call).to be_a JsonSchema::Schema
82
+ end
83
+ end
84
+
85
+ context "with unknown schema" do
86
+ let(:schema_name) { "posts/unknown" }
87
+ it { should raise_error JsonSchemaRails::SchemaNotFound }
88
+ end
89
+
90
+ context "with multiple schema names" do
91
+ let(:schema_name) { ["posts/unknown", "posts/create"] }
92
+
93
+ it "returns the first found one" do
94
+ expect(subject.call).to be_a JsonSchema::Schema
95
+ end
96
+ end
97
+ end
98
+
99
+ describe "#lookup_schema" do
100
+ subject { JsonSchemaRails.lookup_schema(schema_name) }
101
+
102
+ context "with known schema" do
103
+ let(:schema_name) { "posts/create" }
104
+
105
+ it "returns the schema" do
106
+ should be_a JsonSchema::Schema
107
+ end
108
+ end
109
+
110
+ context "with unknown schema" do
111
+ let(:schema_name) { "posts/unknown" }
112
+ it { should be_nil }
113
+ end
114
+
115
+ context "with multiple schema names" do
116
+ let(:schema_name) { ["posts/unknown", "posts/create"] }
117
+
118
+ it "returns the first found one" do
119
+ should be_a JsonSchema::Schema
120
+ end
121
+ end
122
+ end
123
+ end
@@ -0,0 +1,43 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe PostsController, type: :controller do
4
+ describe "POST #create" do
5
+ context "with good parameters" do
6
+ let(:params) { { "post" => { "title" => "Hello, world!", "body" => "Howdy!" } } }
7
+
8
+ it "passes the validation" do
9
+ post :create, params
10
+ expect(response).to be_success
11
+ end
12
+ end
13
+
14
+ context "with bad parameters" do
15
+ let(:params) { { "post" => { "title" => "Hello, world!" } } }
16
+
17
+ it "fails the validation" do
18
+ post :create, params
19
+ expect(response.status).to eq 400
20
+ end
21
+ end
22
+ end
23
+
24
+ describe "PUT #update" do
25
+ context "with good parameters" do
26
+ let(:params) { { "id" => 123, "post" => { "title" => "Hello, world!", "body" => "Howdy!" } } }
27
+
28
+ it "passes the validation" do
29
+ put :update, params
30
+ expect(response).to be_success
31
+ end
32
+ end
33
+
34
+ context "with bad parameters" do
35
+ let(:params) { { "id" => 123, "post" => { "title" => "Hello, world!" } } }
36
+
37
+ it "fails the validation" do
38
+ put :update, params
39
+ expect(response.status).to eq 400
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,28 @@
1
+ # Configure Rails Environment
2
+ ENV["RAILS_ENV"] = "test"
3
+ require "rails/version"
4
+ require File.expand_path("../dummy_apps/rails#{Rails::VERSION::MAJOR}/config/environment.rb", __FILE__)
5
+ require "rspec/rails"
6
+
7
+ Rails.backtrace_cleaner.remove_silencers!
8
+
9
+ TEST_SCHEMAS_DIR = File.expand_path("../fixtures/schemas", __FILE__)
10
+ TEST_HYPER_SCHEMA_FILE = File.expand_path("../fixtures/hyper_schema.yml", __FILE__)
11
+
12
+ RSpec.configure do |config|
13
+ if config.files_to_run.one?
14
+ config.default_formatter = 'doc'
15
+ end
16
+
17
+ config.order = :random
18
+ Kernel.srand config.seed
19
+
20
+ config.expect_with :rspec do |expectations|
21
+ expectations.syntax = :expect
22
+ end
23
+
24
+ config.mock_with :rspec do |mocks|
25
+ mocks.syntax = :expect
26
+ mocks.verify_partial_doubles = true
27
+ end
28
+ end