easy_json_matcher 0.0.1 → 0.0.2.pre.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: aac9b53dc002894d4415c88923b7f544248cd5b0
4
- data.tar.gz: f349d7f0b70b34acf537985638f92a97c7a3d27f
3
+ metadata.gz: 08815b61dd12963ba183f268f8ed79ef4871e942
4
+ data.tar.gz: 70c09b58d1a58df1310b78f92ac7f12f8ea6c22e
5
5
  SHA512:
6
- metadata.gz: bb28de637e864eb7288282cdeb00dff395c7590bb392fdf108a75b086e6fa8ad215359256cddbee96753678648d8232ec95e0e2b93435498aea1c7b976b9d80f
7
- data.tar.gz: cb7da7f775d5d35786f54f9de63d3cbdfb0ac3dae8e42293abd9ee6fa39b8d622d674985ffa9b6ec09eb97c622aaf2fc10cc36dc0695a235d0928c50814191d5
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
@@ -8,7 +8,7 @@ module EasyJSONMatcher
8
8
  def initialize(opts = {})
9
9
  super(opts)
10
10
  @date_format = opts[:format]
11
- @string_validator = ValidatorFactory.create({type: :string})
11
+ @string_validator = _create_validator(type: :string)
12
12
  end
13
13
 
14
14
  def _validate
@@ -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
@@ -6,7 +6,9 @@ module EasyJSONMatcher
6
6
  begin
7
7
  Kernel::Float(content)
8
8
  true
9
- rescue ArgumentError => e
9
+ rescue ArgumentError
10
+ false
11
+ rescue TypeError
10
12
  false
11
13
  end
12
14
  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 validator_opts(key, opts)
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(validator_opts(key, opts)))
23
+ node.add_validator(_create_validator(key, opts))
22
24
  end
23
25
 
24
- def validator_opts(key, opts)
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.create opts
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
- EasyJSONMatcher.add_schema(name: schema_name, schema: generate_node)
68
+ SchemaLibrary.add_schema(name: schema_name, schema: generate_node)
43
69
  end
44
70
 
45
71
  def node
46
- @node ||= Node.new(opts: validator_opts(name, {}))
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, :key
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 create(opts)
13
- validator_class = get_type(opts[:type])
14
- validator_class.new options: opts
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)
@@ -1,3 +1,3 @@
1
1
  module JsonapiMatcher
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2-1"
3
3
  end
@@ -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