media_types-validation 0.1.0 → 0.2.0
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 +4 -4
- data/CHANGELOG.md +11 -0
- data/Gemfile.lock +1 -1
- data/README.md +12 -6
- data/lib/media_types/validation.rb +4 -4
- data/lib/media_types/validation/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7af4e0eddfb876d2fcf698fab8bd96deac9ac5c34a3b1c60567c21119597db51
|
4
|
+
data.tar.gz: 1c6d6482a8a4701ff65dc7deec8d5568bfcdf2ae5fa0d62d7b4fa53dcb5e35d5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e7404580d33f3875931fc4199ed3127048132c03c473599a145b77439e43d4276bf44af5f34ec69b6c6764ca251570877f093fd2e00480ea7a3e5d2ff1be7f38
|
7
|
+
data.tar.gz: c1cb7e6869e220d9574801430578f63e624fe31f051175bb6cc8e7d4a05922388a1672e5be471191dbd1d89840d4b064cb37153ea58fafcd79f214b5114e447a
|
data/CHANGELOG.md
ADDED
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -24,7 +24,10 @@ Or install it yourself as:
|
|
24
24
|
|
25
25
|
## Usage
|
26
26
|
|
27
|
-
|
27
|
+
If you add the `MediaTypes::Validation` controller concern, `validate_json_with_media_type` becomes available during
|
28
|
+
actions. This does _not_ validate only `JSON` output, but stringifies and then parses the body as `JSON`, so the
|
29
|
+
limitations of `JSON` apply. This step is necessary in order to make sure `rails` types and others are first correctly
|
30
|
+
casted (and formatted).
|
28
31
|
|
29
32
|
```ruby
|
30
33
|
require 'media_types/validation'
|
@@ -51,19 +54,22 @@ class BookController < ApiController
|
|
51
54
|
end
|
52
55
|
```
|
53
56
|
|
57
|
+
By default, this method only outputs to `stderr` when something is wrong; see configuration below if you want to assign
|
58
|
+
your own behaviour, such as adding a `Warn` header, or raising a server error.
|
59
|
+
|
54
60
|
### Configuration
|
55
61
|
|
56
62
|
In an initializer you can set procs in order to change the default behaviour:
|
57
63
|
|
58
64
|
```ruby
|
59
|
-
MediaTypes::Validation.configure do
|
60
|
-
|
61
|
-
|
62
|
-
warn
|
65
|
+
MediaTypes::Validation.configure do
|
66
|
+
self.json_invalid_media_proc = proc do |media_type:, err:, body:|
|
67
|
+
response['Warn'] = '199 media type %s is invalid (%s)' % [media_type, err]
|
68
|
+
warn response['Warn'] + "\n" + body
|
63
69
|
end
|
64
70
|
|
65
71
|
# Or alternatively you can always raise
|
66
|
-
|
72
|
+
self.raise_on_json_invalid_media = true
|
67
73
|
end
|
68
74
|
```
|
69
75
|
|
@@ -10,10 +10,10 @@ module MediaTypes
|
|
10
10
|
|
11
11
|
extend ActiveSupport::Concern
|
12
12
|
|
13
|
-
|
13
|
+
mattr_accessor :json_invalid_media_proc, :raise_on_json_invalid_media
|
14
14
|
|
15
|
-
def self.configure
|
16
|
-
|
15
|
+
def self.configure(&block)
|
16
|
+
instance_exec self, &block
|
17
17
|
end
|
18
18
|
|
19
19
|
def validate_json_with_media_type(body, media_type:)
|
@@ -42,7 +42,7 @@ module MediaTypes
|
|
42
42
|
json_valid_media_or_throw?(body, media_type: media_type)
|
43
43
|
rescue ::MediaTypes::Scheme::ValidationError => err
|
44
44
|
if json_invalid_media_proc.respond_to?(:call)
|
45
|
-
|
45
|
+
instance_exec(media_type: media_type, err: err, body: body, &json_invalid_media_proc)
|
46
46
|
else
|
47
47
|
message = format(
|
48
48
|
'[media type validation] The data being sent as %<media_type>s is invalid:' + "\n" \
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: media_types-validation
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Derk-Jan Karrenbeld
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-02-
|
11
|
+
date: 2019-02-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -109,6 +109,7 @@ files:
|
|
109
109
|
- ".idea/modules.xml"
|
110
110
|
- ".idea/vcs.xml"
|
111
111
|
- ".travis.yml"
|
112
|
+
- CHANGELOG.md
|
112
113
|
- CODE_OF_CONDUCT.md
|
113
114
|
- Gemfile
|
114
115
|
- Gemfile.lock
|