Class: EasyJSONMatcher::SchemaGenerator

Inherits:
Object
  • Object
show all
Includes:
AttributeTypeMethods
Defined in:
lib/easy_json_matcher/schema_generator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from AttributeTypeMethods

#has_boolean, #has_date, #has_number, #has_object, #has_string, #has_value

Constructor Details

#initialize(opts: [], global_opts: []) {|node| ... } ⇒ SchemaGenerator

Returns a new instance of SchemaGenerator.

Yields:



14
15
16
17
18
19
20
# File 'lib/easy_json_matcher/schema_generator.rb', line 14

def initialize(opts: [], global_opts: [])
  @glob_opts = global_opts
  @att_glob_opts = glob_opts.dup
  @att_glob_opts.delete(:strict)
  @node = node_generator(opts: opts, globals: global_opts)
  yield node if block_given?
end

Instance Attribute Details

#att_glob_optsObject (readonly)

Returns the value of attribute att_glob_opts.



12
13
14
# File 'lib/easy_json_matcher/schema_generator.rb', line 12

def att_glob_opts
  @att_glob_opts
end

#glob_optsObject (readonly)

Returns the value of attribute glob_opts.



12
13
14
# File 'lib/easy_json_matcher/schema_generator.rb', line 12

def glob_opts
  @glob_opts
end

#nodeObject (readonly)

Returns the value of attribute node.



12
13
14
# File 'lib/easy_json_matcher/schema_generator.rb', line 12

def node
  @node
end

Instance Method Details

#contains_array(key:, opts: [], with_content:) {|array_validator| ... } ⇒ Object

Yields:

  • (array_validator)


50
51
52
53
54
# File 'lib/easy_json_matcher/schema_generator.rb', line 50

def contains_array(key:, opts: [], with_content:)
  array_validator = ArrayValidator.new opts: opts, verify_content_as: with_content
  yield array_validator if block_given?
  node.add_validator key: key, validator: array_validator
end

#contains_node(key:, opts: []) {|generator| ... } ⇒ Object

Methods for adding specific attribute types ##############

Yields:

  • (generator)


44
45
46
47
48
# File 'lib/easy_json_matcher/schema_generator.rb', line 44

def contains_node(key:, opts: [])
  generator = node_generator(opts: opts)
  yield generator if block_given?
  node.add_validator key: key, validator: generator.node
end

#create_node(opts:) ⇒ Object



22
23
24
# File 'lib/easy_json_matcher/schema_generator.rb', line 22

def create_node(opts:)
  Node.new(opts: opts)
end

#generate_nodeObject

Methods for generating the schema #########################



63
64
65
# File 'lib/easy_json_matcher/schema_generator.rb', line 63

def generate_node
  node.generate_node
end

#generate_schemaObject



67
68
69
# File 'lib/easy_json_matcher/schema_generator.rb', line 67

def generate_schema
  Validator.new validate_with: generate_node
end

#has_attribute(key:, opts:) ⇒ Object



26
27
28
29
30
31
# File 'lib/easy_json_matcher/schema_generator.rb', line 26

def has_attribute(key:, opts:)
  opts = override_globals(local_opts: opts)
  opts = opts - [:strict]
  validator = ValidationChainFactory.get_chain(steps: opts)
  node.add_validator key: key, validator: validator
end

#has_schema(key:, name:) ⇒ Object



56
57
58
59
# File 'lib/easy_json_matcher/schema_generator.rb', line 56

def has_schema(key:, name:)
  schema = SchemaLibrary.get_schema(name: name)
  node.add_validator key: key, validator: schema
end

#node_generator(opts:, globals:) ⇒ Object



76
77
78
# File 'lib/easy_json_matcher/schema_generator.rb', line 76

def node_generator(opts:, globals:)
  NodeGenerator.new(opts: opts, global_opts: globals)
end

#override_globals(local_opts:) ⇒ Object

TODO pretty hacky but can be cleaned later



34
35
36
37
38
39
40
# File 'lib/easy_json_matcher/schema_generator.rb', line 34

def override_globals(local_opts:)
  if local_opts.include?(:not_required) && glob_opts.include?(:required)
     (local_opts + (glob_opts - [:required]))
  else
    local_opts + glob_opts
  end
end

#register(as:) ⇒ Object



71
72
73
74
# File 'lib/easy_json_matcher/schema_generator.rb', line 71

def register(as:)
  SchemaLibrary.add_schema(name: as, schema: generate_node)
  generate_schema
end