easy_json_matcher 0.1.1 → 0.2.1

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
  SHA1:
3
- metadata.gz: e4a01a086ab4017866d0ada74af47467a6daa903
4
- data.tar.gz: 84a4731a80838262adc023b7150771998edcbe15
3
+ metadata.gz: 8247cb3e9791930357fc71b2adccd2207722d96c
4
+ data.tar.gz: a016110a42e4c8b82b0b58e06b9c0a858d20af14
5
5
  SHA512:
6
- metadata.gz: 0aab586ee4777ef78bff7e762f8342898a4768b914426a2528b55bab53ca894091846eeec79556d56fcd2bc73f927000262825513c4bb33a364ca07ed5301b96
7
- data.tar.gz: 8cbe1988516116eea04c3482a7e6b15061e6a0c1f2cd416b175a40dd98bed9902e190fe2531d7a839ba622bba10c897cf9bad87a65bcc63270308d4f55a39c52
6
+ metadata.gz: ac9579c176287786a0f09bfe575f9362aa9d3ab723cf8ad6a94c18f546be62ec7f7cb08c1e9052f888129c0547ff8c2c82706e6c52042eb6ebe490acea22549c
7
+ data.tar.gz: 79cf3770d9f3898dfec75acfff000133e88f2e636e7a9f2ec802fd9386995aad72c73c39836d6584518213b52f2060916a29f0eb570e508ae7857c071be24ecf
@@ -14,7 +14,7 @@ module EasyJSONMatcher
14
14
  yield self if block_given?
15
15
  end
16
16
 
17
- def has_attribute(key:, opts: {})
17
+ def has_attribute(key:, opts: {}, custom_validator: nil)
18
18
  node.add_validator(_create_validator(key, opts))
19
19
  end
20
20
 
@@ -1,12 +1,13 @@
1
1
  module EasyJSONMatcher
2
2
  class Validator
3
3
 
4
- attr_reader :content, :required, :errors
4
+ attr_reader :content, :required, :errors, :custom_validator
5
5
  attr_accessor :key
6
6
 
7
7
  def initialize(options: {})
8
8
  @key = options[:key]
9
9
  @required = options[:required]
10
+ @custom_validator = options[:custom_validator]
10
11
  @errors = []
11
12
  _post_initialise(options)
12
13
  end
@@ -24,6 +25,7 @@ module EasyJSONMatcher
24
25
  else
25
26
  _validate #Hook
26
27
  end
28
+ _run_custom_validator if custom_validator
27
29
  _no_errors?
28
30
  end
29
31
 
@@ -83,6 +85,16 @@ module EasyJSONMatcher
83
85
  ValidatorFactory.get_instance(type: type, opts: opts)
84
86
  end
85
87
 
88
+ def _custom_validator?
89
+ custom_validator
90
+ end
91
+
92
+ def _run_custom_validator
93
+ if error_message = custom_validator.call(content)
94
+ errors << error_message
95
+ end
96
+ end
97
+
86
98
  def _no_errors?
87
99
  errors.empty?
88
100
  end
@@ -0,0 +1,99 @@
1
+ module EasyJSONMatcher
2
+ class Validator
3
+
4
+ attr_reader :content, :required, :errors, :custom_validator
5
+ attr_accessor :key
6
+
7
+ def initialize(options: {})
8
+ @key = options[:key]
9
+ @required = options[:required]
10
+ @custom_validator = options[:custom_validator]
11
+ @errors = []
12
+ _post_initialise(options)
13
+ end
14
+
15
+ # Hook. Allows further setup to be carried out by subclasses
16
+ def _post_initialise(options); end
17
+
18
+ def valid?(candidate)
19
+ if key
20
+ return false unless _check_content_type(candidate)
21
+ end
22
+ _set_content(candidate) #Hook
23
+ if content.nil?
24
+ _check_required?
25
+ else
26
+ _validate #Hook
27
+ end
28
+ _run_custom_validator if custom_validator
29
+ _no_errors?
30
+ end
31
+
32
+ # Hook. Overriden in Node
33
+ def reset!
34
+ errors.clear
35
+ end
36
+
37
+ # Hook
38
+ # Protected method that Validators use to implement their validation logic.
39
+ # Called by #valid?
40
+ def _validate
41
+ raise NotImplementedError.new "Validators must override _validate"
42
+ end
43
+
44
+ # Hook
45
+ # Protected method that Validators use to set their content from the candidate.
46
+ def _set_content(candidate)
47
+ @content = key ? candidate[key] : candidate
48
+ end
49
+
50
+ # Hook.
51
+ # This method returns the errors that this validator has found in the candidate.
52
+ def get_errors
53
+ error_message = {}
54
+ error_message[key.to_sym] = errors
55
+ error_message
56
+ end
57
+
58
+ # This method makees sure that the candidate behaves like a Hash, and not a
59
+ # value or an array.
60
+ def _check_content_type(candidate)
61
+ # TODO perhaps this should raise an error instead of returning false?
62
+ # if the value that has arrived at this point doesn't behave like a Hash then it
63
+ # is in the wrong place.
64
+ begin
65
+ candidate[key]
66
+ rescue TypeError
67
+ return false
68
+ end
69
+ true
70
+ end
71
+
72
+ def _check_required?
73
+ if required
74
+ errors << "Value was not present"
75
+ return true
76
+ else
77
+ return false
78
+ end
79
+ end
80
+
81
+ def _create_validator(type:, opts: {})
82
+ ValidatorFactory.get_instance(type: type, opts: opts)
83
+ end
84
+
85
+ def _custom_validator?
86
+ custom_validator
87
+ end
88
+
89
+ def _run_custom_validator
90
+ if error_message = custom_validator.call(content)
91
+ errors << error_message
92
+ end
93
+ end
94
+
95
+ def _no_errors?
96
+ errors.empty?
97
+ end
98
+ end
99
+ end
@@ -1,3 +1,3 @@
1
- module JsonapiMatcher
2
- VERSION = "0.1.1".freeze
1
+ module EasyJSONMatcher
2
+ VERSION = "0.2.1".freeze
3
3
  end
@@ -0,0 +1,3 @@
1
+ module EasyJSONMatcher
2
+ VERSION = "0.2.1".freeze
3
+ end
@@ -0,0 +1,26 @@
1
+ require 'test_helper'
2
+
3
+ class CustomValidationsTest < ActiveSupport::TestCase
4
+
5
+ test 'As a user, I want to be able to specify custom validations' do
6
+ test_schema = EasyJSONMatcher::SchemaGenerator.new do |s|
7
+ s.has_string key: :a_string, opts: { custom_validator: ->(candidate) { "String did not say 'hi'" unless candidate == 'hi' } }
8
+ end.generate_schema
9
+
10
+ should_not_validate = {
11
+ a_string: 'go away'
12
+ }.to_json
13
+
14
+ assert_not(test_schema.valid? should_not_validate)
15
+
16
+ should_validate = {
17
+ a_string: 'hi'
18
+ }.to_json
19
+
20
+
21
+ test_schema.reset!
22
+
23
+ assert(test_schema.valid?(should_validate), test_schema.get_errors)
24
+
25
+ end
26
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: easy_json_matcher
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - WJD Hamilton
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-03-23 00:00:00.000000000 Z
11
+ date: 2016-04-01 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Test your JSON output in Ruby, with a DSL that makes reasoning about
14
14
  your JSON very straightforward. See the Homepage for docs.
@@ -34,10 +34,13 @@ files:
34
34
  - lib/easy_json_matcher/string_validator.rb
35
35
  - lib/easy_json_matcher/validation_error.rb
36
36
  - lib/easy_json_matcher/validator.rb
37
+ - lib/easy_json_matcher/validator.rb~
37
38
  - lib/easy_json_matcher/validator_factory.rb
38
39
  - lib/easy_json_matcher/value_validator.rb
39
40
  - lib/easy_json_matcher/version.rb
41
+ - lib/easy_json_matcher/version.rb~
40
42
  - lib/tasks/jsonapi_matcher_tasks.rake
43
+ - test/custom_validations_test.rb
41
44
  - test/easy_json_matcher_test.rb
42
45
  - test/error_messages_test.rb
43
46
  - test/global_validation_options_test.rb
@@ -73,6 +76,7 @@ signing_key:
73
76
  specification_version: 4
74
77
  summary: Easily test your JSON output with templates and Schemas
75
78
  test_files:
79
+ - test/custom_validations_test.rb
76
80
  - test/easy_json_matcher_test.rb
77
81
  - test/error_messages_test.rb
78
82
  - test/global_validation_options_test.rb