activerecord_json_validator 1.3.0 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d9dce365377ebd51d3f5b8156e3cf928507014ed1278c0ca794675b4fecd3d2c
4
- data.tar.gz: b1f58a7b215df70ff616dd80ee31dd24dd8fa0b0020189ee99318fb577a38621
3
+ metadata.gz: 0f5b518bda8ab28520f561dc840f47223e252530a9182ef31a164c12fb831451
4
+ data.tar.gz: 79e01fcce3c8cb0e27f6e3e24df4db54a312f6cef7285f38cec05689e897d042
5
5
  SHA512:
6
- metadata.gz: 3b0f0f8e654453f06fd16d31324b8d3cfa069411e206cbc3b58575987984bda9ece9156411121d5202c701fd4dcece6ed5ab7b40eac5d5116a02f3347f2e0a4c
7
- data.tar.gz: 355f0fec168526d3e881efb0b9ef5434089d2bc531adeb6f6dc9af61ea96dbb4a30600d744babd82dd57df2b6157d36a1d4e9ed837af902295d8419b7e5bfe91
6
+ metadata.gz: b4e5420f0c6aaad0e1e13ed296bc598e88b16974bfbaa7e55cc1680ad4ca499ba0b570de3ee26d72a993cbb9e712370f7e23fcf14e56e46ba40d7c601c5695f1
7
+ data.tar.gz: 93cf5acde18b71f512f2f68eaec1ba8191a721e1ace7853604d9e6ac4c6c7d03ae88a19c402a4c8b8545e8bfa5546de4ced97dfa032536f18747a5658419b752
data/README.md CHANGED
@@ -22,16 +22,31 @@ gem 'activerecord_json_validator'
22
22
  ## Usage
23
23
 
24
24
  ### JSON Schema
25
+ Schemas must use be a JSON string or use string keys.
25
26
 
26
27
  ```json
27
- {
28
+ '{
28
29
  "type": "object",
29
- "$schema": "http://json-schema.org/draft-04/schema",
30
+ "$schema": "http://json-schema.org/draft-04/schema#",
30
31
  "properties": {
31
32
  "city": { "type": "string" },
32
33
  "country": { "type": "string" }
33
34
  },
34
35
  "required": ["country"]
36
+ }'
37
+ ```
38
+
39
+ or
40
+
41
+ ```ruby
42
+ {
43
+ "type" => "object",
44
+ "$schema" => "http://json-schema.org/draft-04/schema#",
45
+ "properties" => {
46
+ "city" => { "type" => "string" },
47
+ "country" => { "type" => "string" }
48
+ },
49
+ "required" => ["country"]
35
50
  }
36
51
  ```
37
52
 
@@ -69,13 +84,12 @@ user.profile_invalid_json # => '{invalid JSON":}'
69
84
  |------------|-----------------------------------------------------
70
85
  | `:schema` | The JSON schema to validate the data against (see **Schema** section)
71
86
  | `:message` | The ActiveRecord message added to the record errors (see **Message** section)
72
- | `:options` | A `Hash` of [`json-schema`](https://github.com/ruby-json-schema/json-schema)-supported options to pass to the validator
87
+ | `:options` | A `Hash` of [`json_schemer`](https://github.com/davishmcclurg/json_schemer#options)-supported options to pass to the validator
73
88
 
74
89
  ##### Schema
75
90
 
76
- `ActiveRecord::JSONValidator` uses the `json-schema` gem to validate the JSON
77
- data against a JSON schema. You can use [any value](https://github.com/ruby-json-schema/json-schema/tree/master#usage) that
78
- `JSON::Validator.validate` would take as the `schema` argument.
91
+ `ActiveRecord::JSONValidator` uses the [json_schemer](https://github.com/davishmcclurg/json_schemer) gem to validate the JSON
92
+ data against a JSON schema.
79
93
 
80
94
  Additionally, you can use a `Symbol` or a `Proc`. Both will be executed in the
81
95
  context of the validated record (`Symbol` will be sent as a method and the
@@ -27,6 +27,6 @@ Gem::Specification.new do |spec|
27
27
  spec.add_development_dependency 'phare'
28
28
  spec.add_development_dependency 'rubocop', '~> 0.28'
29
29
 
30
- spec.add_dependency 'json-schema', '~> 2.8'
30
+ spec.add_dependency 'json_schemer', '~> 0.2.18'
31
31
  spec.add_dependency 'activerecord', '>= 4.2.0', '< 7'
32
32
  end
@@ -14,8 +14,8 @@ class JsonValidator < ActiveModel::EachValidator
14
14
 
15
15
  # Validate the JSON value with a JSON schema path or String
16
16
  def validate_each(record, attribute, value)
17
- # Validate value with JSON::Validator
18
- errors = ::JSON::Validator.fully_validate(schema(record), validatable_value(value), options.fetch(:options))
17
+ # Validate value with JSON Schemer
18
+ errors = JSONSchemer.schema(schema(record), options.fetch(:options)).validate(value).to_a
19
19
 
20
20
  # Everything is good if we don’t have any errors and we got valid JSON value
21
21
  return if errors.empty? && record.send(:"#{attribute}_invalid_json").blank?
@@ -49,7 +49,7 @@ protected
49
49
  end
50
50
  end
51
51
 
52
- # Return a valid schema for JSON::Validator.fully_validate, recursively calling
52
+ # Return a valid schema, recursively calling
53
53
  # itself until it gets a non-Proc/non-Symbol value.
54
54
  def schema(record, schema = nil)
55
55
  schema ||= options.fetch(:schema)
@@ -61,12 +61,6 @@ protected
61
61
  end
62
62
  end
63
63
 
64
- def validatable_value(value)
65
- return value if value.is_a?(String)
66
-
67
- ::ActiveSupport::JSON.encode(value)
68
- end
69
-
70
64
  def message(errors)
71
65
  message = options.fetch(:message)
72
66
 
@@ -2,6 +2,6 @@
2
2
 
3
3
  module ActiveRecord
4
4
  module JSONValidator
5
- VERSION = '1.3.0'
5
+ VERSION = '2.0.0'
6
6
  end
7
7
  end
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'active_record'
4
- require 'json-schema'
4
+ require 'json_schemer'
5
5
 
6
6
  require 'active_record/json_validator/version'
7
7
  require 'active_record/json_validator/validator'
@@ -42,7 +42,7 @@ describe JsonValidator do
42
42
 
43
43
  describe :validate_each do
44
44
  let(:validator) { JsonValidator.new(options) }
45
- let(:options) { { attributes: [attribute], options: { strict: true } } }
45
+ let(:options) { { attributes: [attribute], options: { format: true } } }
46
46
  let(:validate_each!) { validator.validate_each(record, attribute, value) }
47
47
 
48
48
  # Doubles
@@ -51,16 +51,18 @@ describe JsonValidator do
51
51
  let(:record_errors) { double(:errors) }
52
52
  let(:value) { double(:value) }
53
53
  let(:schema) { double(:schema) }
54
- let(:validatable_value) { double(:validatable_value) }
54
+ let(:schema_validator) { double(:schema_validator) }
55
+ let(:raw_errors) { double(:raw_errors) }
55
56
  let(:validator_errors) { double(:validator_errors) }
56
57
 
57
58
  before do
58
59
  expect(validator).to receive(:schema).with(record).and_return(schema)
59
- expect(validator).to receive(:validatable_value).with(value).and_return(validatable_value)
60
- expect(::JSON::Validator).to receive(:fully_validate).with(schema, validatable_value, options[:options]).and_return(validator_errors)
60
+ expect(JSONSchemer).to receive(:schema).with(schema, options[:options]).and_return(schema_validator)
61
+ expect(schema_validator).to receive(:validate).with(value).and_return(raw_errors)
62
+ expect(raw_errors).to receive(:to_a).and_return(validator_errors)
61
63
  end
62
64
 
63
- context 'with JSON::Validator errors' do
65
+ context 'with JSON Schemer errors' do
64
66
  before do
65
67
  expect(validator_errors).to receive(:empty?).and_return(false)
66
68
  expect(record).not_to receive(:"#{attribute}_invalid_json")
@@ -70,7 +72,7 @@ describe JsonValidator do
70
72
  specify { validate_each! }
71
73
  end
72
74
 
73
- context 'without JSON::Validator errors but with invalid JSON data' do
75
+ context 'without JSON Schemer errors but with invalid JSON data' do
74
76
  before do
75
77
  expect(validator_errors).to receive(:empty?).and_return(true)
76
78
  expect(record).to receive(:"#{attribute}_invalid_json").and_return('foo"{]')
@@ -80,7 +82,7 @@ describe JsonValidator do
80
82
  specify { validate_each! }
81
83
  end
82
84
 
83
- context 'without JSON::Validator errors and valid JSON data' do
85
+ context 'without JSON Schemer errors and valid JSON data' do
84
86
  before do
85
87
  expect(validator_errors).to receive(:empty?).and_return(true)
86
88
  expect(record).to receive(:"#{attribute}_invalid_json").and_return(nil)
@@ -155,22 +157,6 @@ describe JsonValidator do
155
157
  end
156
158
  end
157
159
 
158
- describe :validatable_value do
159
- let(:validator) { JsonValidator.new(options) }
160
- let(:options) { { attributes: [:foo] } }
161
- let(:validatable_value) { validator.send(:validatable_value, value) }
162
-
163
- context 'with non-String value' do
164
- let(:value) { { foo: 'bar' } }
165
- it { expect(validatable_value).to eql('{"foo":"bar"}') }
166
- end
167
-
168
- context 'with String value' do
169
- let(:value) { '{\"foo\":\"bar\"}' }
170
- it { expect(validatable_value).to eql(value) }
171
- end
172
- end
173
-
174
160
  describe :message do
175
161
  let(:validator) { JsonValidator.new(options) }
176
162
  let(:options) { { attributes: [:foo], message: message_option } }
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activerecord_json_validator
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.0
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rémi Prévost
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-08-27 00:00:00.000000000 Z
11
+ date: 2021-06-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -129,19 +129,19 @@ dependencies:
129
129
  - !ruby/object:Gem::Version
130
130
  version: '0.28'
131
131
  - !ruby/object:Gem::Dependency
132
- name: json-schema
132
+ name: json_schemer
133
133
  requirement: !ruby/object:Gem::Requirement
134
134
  requirements:
135
135
  - - "~>"
136
136
  - !ruby/object:Gem::Version
137
- version: '2.8'
137
+ version: 0.2.18
138
138
  type: :runtime
139
139
  prerelease: false
140
140
  version_requirements: !ruby/object:Gem::Requirement
141
141
  requirements:
142
142
  - - "~>"
143
143
  - !ruby/object:Gem::Version
144
- version: '2.8'
144
+ version: 0.2.18
145
145
  - !ruby/object:Gem::Dependency
146
146
  name: activerecord
147
147
  requirement: !ruby/object:Gem::Requirement
@@ -211,7 +211,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
211
211
  - !ruby/object:Gem::Version
212
212
  version: '0'
213
213
  requirements: []
214
- rubygems_version: 3.0.3
214
+ rubygems_version: 3.1.6
215
215
  signing_key:
216
216
  specification_version: 4
217
217
  summary: ActiveRecord::JSONValidator makes it easy to validate JSON attributes with