easy_json_matcher 0.0.1 → 0.0.2.pre.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/easy_json_matcher/array_validator.rb +45 -0
- data/lib/easy_json_matcher/date_validator.rb +1 -1
- data/lib/easy_json_matcher/exceptions.rb +9 -0
- data/lib/easy_json_matcher/number_validator.rb +3 -1
- data/lib/easy_json_matcher/schema_generator.rb +33 -7
- data/lib/easy_json_matcher/schema_library.rb +35 -0
- data/lib/easy_json_matcher/validator.rb +14 -1
- data/lib/easy_json_matcher/validator_factory.rb +8 -3
- data/lib/easy_json_matcher/version.rb +1 -1
- data/lib/easy_json_matcher.rb +0 -22
- data/test/dummy/log/test.log +9595 -0
- data/test/{jsonapi_matcher_test.rb → easy_json_matcher_test.rb} +13 -21
- data/test/managing_schemas_test.rb +68 -0
- data/test/required_validation_test.rb +37 -0
- data/test/validating_arrays_test.rb +68 -0
- metadata +14 -8
- data/test/date_format_test.rb +0 -0
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 08815b61dd12963ba183f268f8ed79ef4871e942
|
4
|
+
data.tar.gz: 70c09b58d1a58df1310b78f92ac7f12f8ea6c22e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d2415e588006089aab89ef53870839493258f6bd5d71d9573d515c6ee10aacbb3f6dd3db1b979d5c843a07e52356d75456e72242b2ee2706093c8da78a2c1d6c
|
7
|
+
data.tar.gz: 954f6c4a709230a0e8d8250e6a3a4f020fbd723071a741fb478a968bd008077c6cc587b42819d179038fce6ae48db62089227614fe2fdbc2db7f669b3fcb5a42
|
@@ -1,8 +1,53 @@
|
|
1
1
|
require 'easy_json_matcher/validator'
|
2
2
|
module EasyJSONMatcher
|
3
3
|
class ArrayValidator < Validator
|
4
|
+
attr_reader :validators, :validator_results
|
5
|
+
|
4
6
|
def _validate
|
7
|
+
return false unless _content_is_array?
|
8
|
+
_validate_content
|
9
|
+
end
|
10
|
+
|
11
|
+
def _content_is_array?
|
5
12
|
content.is_a? Array
|
6
13
|
end
|
14
|
+
|
15
|
+
def _validate_content
|
16
|
+
validators.each do |val|
|
17
|
+
_run_validator(val)
|
18
|
+
end
|
19
|
+
_validation_result
|
20
|
+
end
|
21
|
+
|
22
|
+
def should_only_contain(type:,opts: {})
|
23
|
+
_clear_validators
|
24
|
+
_add_validator(_create_validator(type: type, opts: opts))
|
25
|
+
end
|
26
|
+
|
27
|
+
def _clear_validators
|
28
|
+
validators.clear
|
29
|
+
end
|
30
|
+
|
31
|
+
def _add_validator(validator)
|
32
|
+
validators << validator
|
33
|
+
end
|
34
|
+
|
35
|
+
def validators
|
36
|
+
@validators ||= []
|
37
|
+
end
|
38
|
+
|
39
|
+
def _run_validator(v)
|
40
|
+
content.each do |value|
|
41
|
+
validator_results << v.valid?(value)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
def _validation_result
|
46
|
+
!validator_results.include? false
|
47
|
+
end
|
48
|
+
|
49
|
+
def validator_results
|
50
|
+
@validator_results ||= []
|
51
|
+
end
|
7
52
|
end
|
8
53
|
end
|
@@ -0,0 +1,9 @@
|
|
1
|
+
require 'easy_json_matcher'
|
2
|
+
module EasyJSONMatcher
|
3
|
+
# Override of Ruby error for namespacing
|
4
|
+
class Error < RuntimeError; end
|
5
|
+
|
6
|
+
# Exception thrown by SchemaLibrary when a Schema is requested that has not
|
7
|
+
# already been registered
|
8
|
+
class MissingSchemaException < Error; end
|
9
|
+
end
|
@@ -1,5 +1,7 @@
|
|
1
1
|
require 'easy_json_matcher/validator_factory'
|
2
2
|
require 'easy_json_matcher/node'
|
3
|
+
require 'easy_json_matcher/schema_library'
|
4
|
+
require 'easy_json_matcher/exceptions'
|
3
5
|
module EasyJSONMatcher
|
4
6
|
class SchemaGenerator
|
5
7
|
|
@@ -12,22 +14,46 @@ module EasyJSONMatcher
|
|
12
14
|
end
|
13
15
|
|
14
16
|
def contains_node(key:, opts: {})
|
15
|
-
generator = _node_generator
|
17
|
+
generator = _node_generator _validator_opts(key, opts)
|
16
18
|
yield generator if block_given?
|
17
19
|
node.add_validator generator.generate_node
|
18
20
|
end
|
19
21
|
|
20
22
|
def has_attribute(key:, opts: {})
|
21
|
-
node.add_validator(_create_validator(
|
23
|
+
node.add_validator(_create_validator(key, opts))
|
22
24
|
end
|
23
25
|
|
24
|
-
def
|
26
|
+
def contains_schema(schema_name:, opts: {})
|
27
|
+
key = opts[:key] || schema_name
|
28
|
+
schema = _create_validator(key, _prep_schema_opts(schema_name, opts))
|
29
|
+
_set_validator_key(schema, opts[:key] || schema_name)
|
30
|
+
node.add_validator schema
|
31
|
+
end
|
32
|
+
|
33
|
+
def _prep_schema_opts(schema_name, opts)
|
34
|
+
opts[:type] = :schema
|
35
|
+
opts[:name] = schema_name
|
36
|
+
opts
|
37
|
+
end
|
38
|
+
|
39
|
+
def _set_validator_key(validator, key)
|
40
|
+
validator.key = key
|
41
|
+
end
|
42
|
+
|
43
|
+
def contains_array(key:, opts: {})
|
44
|
+
opts.merge!({type: :array})
|
45
|
+
array_validator = _create_validator(key, opts)
|
46
|
+
yield array_validator if block_given?
|
47
|
+
node.add_validator array_validator
|
48
|
+
end
|
49
|
+
|
50
|
+
def _validator_opts(key, opts)
|
25
51
|
opts[:key] = key
|
26
52
|
opts
|
27
53
|
end
|
28
54
|
|
29
|
-
def _create_validator(opts)
|
30
|
-
ValidatorFactory.
|
55
|
+
def _create_validator(key, opts)
|
56
|
+
ValidatorFactory.get_instance type: opts[:type], opts: _validator_opts(key, opts)
|
31
57
|
end
|
32
58
|
|
33
59
|
def _node_generator(opts = {})
|
@@ -39,11 +65,11 @@ module EasyJSONMatcher
|
|
39
65
|
end
|
40
66
|
|
41
67
|
def register(schema_name:)
|
42
|
-
|
68
|
+
SchemaLibrary.add_schema(name: schema_name, schema: generate_node)
|
43
69
|
end
|
44
70
|
|
45
71
|
def node
|
46
|
-
@node ||= Node.new(opts:
|
72
|
+
@node ||= Node.new(opts: _validator_opts(name, {}))
|
47
73
|
end
|
48
74
|
end
|
49
75
|
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'easy_json_matcher'
|
2
|
+
module EasyJSONMatcher
|
3
|
+
class SchemaLibrary
|
4
|
+
|
5
|
+
SCHEMAS = {}
|
6
|
+
private_constant :SCHEMAS
|
7
|
+
class << self
|
8
|
+
def available_schemas
|
9
|
+
schemas.keys
|
10
|
+
end
|
11
|
+
|
12
|
+
def schema_for(name)
|
13
|
+
schemas[name]
|
14
|
+
end
|
15
|
+
|
16
|
+
def add_schema(name:, schema:)
|
17
|
+
schemas[name] = schema
|
18
|
+
end
|
19
|
+
|
20
|
+
def schemas
|
21
|
+
SCHEMAS
|
22
|
+
end
|
23
|
+
|
24
|
+
def get_schema(name)
|
25
|
+
_find_and_clone_schema(name) or raise MissingSchemaException.new("No schema with #{name} has been registered")
|
26
|
+
end
|
27
|
+
|
28
|
+
def _find_and_clone_schema(name)
|
29
|
+
s = SCHEMAS[name]
|
30
|
+
return s.dup if s
|
31
|
+
nil
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -1,19 +1,32 @@
|
|
1
1
|
module EasyJSONMatcher
|
2
2
|
class Validator
|
3
3
|
|
4
|
-
attr_reader :content, :
|
4
|
+
attr_reader :content, :required
|
5
|
+
attr_accessor :key
|
5
6
|
|
6
7
|
def initialize(options: {})
|
7
8
|
@key = options[:key]
|
9
|
+
@required = options[:required]
|
8
10
|
end
|
9
11
|
|
10
12
|
def valid?(candidate)
|
11
13
|
_set_content(candidate)
|
14
|
+
if content.nil?
|
15
|
+
return true unless _check_required?
|
16
|
+
end
|
12
17
|
_validate
|
13
18
|
end
|
14
19
|
|
15
20
|
def _set_content(candidate)
|
16
21
|
@content = key ? candidate[key.to_s] : candidate
|
17
22
|
end
|
23
|
+
|
24
|
+
def _check_required?
|
25
|
+
required
|
26
|
+
end
|
27
|
+
|
28
|
+
def _create_validator(type:, opts: {})
|
29
|
+
ValidatorFactory.get_instance(type: type, opts: opts)
|
30
|
+
end
|
18
31
|
end
|
19
32
|
end
|
@@ -9,9 +9,14 @@ module EasyJSONMatcher
|
|
9
9
|
class ValidatorFactory
|
10
10
|
|
11
11
|
class << self
|
12
|
-
def
|
13
|
-
|
14
|
-
|
12
|
+
def get_instance(type:, opts: {})
|
13
|
+
raise "Type must be specified" unless type
|
14
|
+
if type == :schema
|
15
|
+
SchemaLibrary.get_schema(opts[:name])
|
16
|
+
else
|
17
|
+
validator_class = get_type(type)
|
18
|
+
validator_class.new options: opts
|
19
|
+
end
|
15
20
|
end
|
16
21
|
|
17
22
|
def get_type(name)
|
data/lib/easy_json_matcher.rb
CHANGED
@@ -2,26 +2,4 @@ require 'easy_json_matcher/node'
|
|
2
2
|
require 'easy_json_matcher/schema_generator'
|
3
3
|
module EasyJSONMatcher
|
4
4
|
|
5
|
-
SCHEMAS = {}
|
6
|
-
private_constant :SCHEMAS
|
7
|
-
class << self
|
8
|
-
def available_schemas
|
9
|
-
schemas.keys
|
10
|
-
end
|
11
|
-
|
12
|
-
def schema_for(name)
|
13
|
-
schemas[name]
|
14
|
-
end
|
15
|
-
|
16
|
-
def add_schema(name:, schema:)
|
17
|
-
schemas[name] = schema
|
18
|
-
end
|
19
|
-
|
20
|
-
def schemas
|
21
|
-
SCHEMAS
|
22
|
-
end
|
23
|
-
end
|
24
|
-
|
25
|
-
# Override of Ruby error for namespacing
|
26
|
-
class Error < RuntimeError; end
|
27
5
|
end
|