Class: CabezaDeTermo::JsonSpec::JsonValidator

Inherits:
JsonExpectationsRunner show all
Defined in:
lib/cabeza-de-termo/json-spec/walkers/validator/json-validator.rb

Instance Method Summary collapse

Methods inherited from JsonExpectationsRunner

#has_errors?, #in_isolation, #in_isolation_walk_each_field, #in_isolation_walk_on, #raise_validation_error, #unexpected_fields_from_current_value_holder, #walk_each_defined_field_of, #walk_each_field_name, #walk_each_field_value, #walk_expectation, #walk_expectation_runner, #walk_expectations_of, #walk_json_any_of, #walk_json_anything, #walk_json_each, #walk_json_each_field, #walk_json_field, #walk_json_field_name, #walk_json_list, #walk_json_object, #walk_json_scalar, #walk_json_spec, #walk_on, #walk_with_new_walker

Methods included from ValueHoldersStackBehaviour

#new_value_holder_on, #pop_value_holder, #push_value_holder, #walk_with_json, #walk_with_value_holder, #with_value_holder

Methods inherited from ExpressionWalker

#supressing, #walk_expectation, #walk_expectation_runner, #walk_json_any_of, #walk_json_anything, #walk_json_each, #walk_json_each_field, #walk_json_field, #walk_json_field_name, #walk_json_list, #walk_json_object, #walk_json_scalar, #walk_json_spec, #walk_on

Constructor Details

#initializeJsonValidator

Returns a new instance of JsonValidator.



7
8
9
10
11
12
13
# File 'lib/cabeza-de-termo/json-spec/walkers/validator/json-validator.rb', line 7

def initialize()
	super()

	@errors = []
	@unexpected_field_errors = []
	@treat_unexpected_fields_as_errors = false
end

Instance Method Details

#add_all_errors(errors) ⇒ Object



116
117
118
119
120
# File 'lib/cabeza-de-termo/json-spec/walkers/validator/json-validator.rb', line 116

def add_all_errors(errors)
	errors.each do |error|
		add_error(error)
	end
end

#add_all_unexpected_fields(errors) ⇒ Object



126
127
128
129
130
# File 'lib/cabeza-de-termo/json-spec/walkers/validator/json-validator.rb', line 126

def add_all_unexpected_fields(errors)
	errors.each do |error|
		add_unexpected_field(error)
	end
end

#add_error(error) ⇒ Object

Logging



112
113
114
# File 'lib/cabeza-de-termo/json-spec/walkers/validator/json-validator.rb', line 112

def add_error(error)
	@errors << error
end

#add_unexpected_field(error) ⇒ Object



122
123
124
# File 'lib/cabeza-de-termo/json-spec/walkers/validator/json-validator.rb', line 122

def add_unexpected_field(error)
	@unexpected_field_errors << error
end

#errorsObject

Accessing



59
60
61
# File 'lib/cabeza-de-termo/json-spec/walkers/validator/json-validator.rb', line 59

def errors()
	@errors
end

#errors_countObject

Counting



49
50
51
# File 'lib/cabeza-de-termo/json-spec/walkers/validator/json-validator.rb', line 49

def errors_count()
	errors.size
end

#has_unexpected_fields?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/cabeza-de-termo/json-spec/walkers/validator/json-validator.rb', line 39

def has_unexpected_fields?()
	!no_unexpected_fields?
end

#new_json_expectations_runnerObject

Instance creation



102
103
104
# File 'lib/cabeza-de-termo/json-spec/walkers/validator/json-validator.rb', line 102

def new_json_expectations_runner()
	self.class.new
end

#new_validation_error(value_holder, message) ⇒ Object



106
107
108
# File 'lib/cabeza-de-termo/json-spec/walkers/validator/json-validator.rb', line 106

def new_validation_error(value_holder, message)
	JsonValidatorError.new(value_holder.accessors_chain, message)
end

#no_errors?Boolean

Asking

Returns:

  • (Boolean)


31
32
33
# File 'lib/cabeza-de-termo/json-spec/walkers/validator/json-validator.rb', line 31

def no_errors?()
	errors.empty?
end

#no_unexpected_fields?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/cabeza-de-termo/json-spec/walkers/validator/json-validator.rb', line 35

def no_unexpected_fields?()
	unexpected_fields.empty?
end

#on_json_any_of_failed_with_all_runners(expectations_runners) ⇒ Object



84
85
86
87
88
89
90
91
92
# File 'lib/cabeza-de-termo/json-spec/walkers/validator/json-validator.rb', line 84

def on_json_any_of_failed_with_all_runners(expectations_runners)
	expectations_runners.each do |each_expectations_runner|
		add_all_errors(each_expectations_runner.errors)
	end

	expectations_runners.each do |each_expectations_runner|
		add_all_unexpected_fields(each_expectations_runner.unexpected_fields)
	end
end

#on_json_any_of_was_satisfied_with_runner(expectations_runner) ⇒ Object



80
81
82
# File 'lib/cabeza-de-termo/json-spec/walkers/validator/json-validator.rb', line 80

def on_json_any_of_was_satisfied_with_runner(expectations_runner)
	add_all_unexpected_fields(expectations_runner.unexpected_fields)
end

#on_unwalked_field(field_name, value_holder) ⇒ Object

Hooks



69
70
71
72
73
74
75
76
77
78
# File 'lib/cabeza-de-termo/json-spec/walkers/validator/json-validator.rb', line 69

def on_unwalked_field(field_name, value_holder)
	error = new_validation_error(
					value_holder,
					"An unexpected '#{field_name}' field was found."
				)

	add_unexpected_field(error)

	add_error(error) if treat_unexpected_fields_as_errors?
end

#on_walked_expectation(expectation, value_holder, was_satisfied) ⇒ Object



94
95
96
97
98
# File 'lib/cabeza-de-termo/json-spec/walkers/validator/json-validator.rb', line 94

def on_walked_expectation(expectation, value_holder, was_satisfied)
	return if was_satisfied

	add_error( new_validation_error(value_holder, expectation.failed_message_on(value_holder)) )
end

#treat_unexpected_fields_as_errors(boolean = true) ⇒ Object

Configuring



23
24
25
26
27
# File 'lib/cabeza-de-termo/json-spec/walkers/validator/json-validator.rb', line 23

def treat_unexpected_fields_as_errors(boolean = true)
	@treat_unexpected_fields_as_errors = boolean

	self
end

#treat_unexpected_fields_as_errors?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/cabeza-de-termo/json-spec/walkers/validator/json-validator.rb', line 43

def treat_unexpected_fields_as_errors?()
	@treat_unexpected_fields_as_errors
end

#unexpected_fieldsObject



63
64
65
# File 'lib/cabeza-de-termo/json-spec/walkers/validator/json-validator.rb', line 63

def unexpected_fields()
	@unexpected_field_errors
end

#unexpected_fields_countObject



53
54
55
# File 'lib/cabeza-de-termo/json-spec/walkers/validator/json-validator.rb', line 53

def unexpected_fields_count()
	unexpected_fields.size
end

#validate(json_expression, json) ⇒ Object



15
16
17
18
19
# File 'lib/cabeza-de-termo/json-spec/walkers/validator/json-validator.rb', line 15

def validate(json_expression, json)
	walk_with_json(json_expression, json)

	self
end