Class: SchemaContract::Validator
- Inherits:
-
Object
- Object
- SchemaContract::Validator
show all
- Defined in:
- app/models/schema_contract/validator.rb
Defined Under Namespace
Classes: SchemaContractValidationError
Instance Method Summary
collapse
Constructor Details
#initialize(record_id) ⇒ Validator
Returns a new instance of Validator.
7
8
9
|
# File 'app/models/schema_contract/validator.rb', line 7
def initialize(record_id)
@record_id = record_id
end
|
Instance Method Details
#parsed_schema ⇒ Object
50
51
52
53
|
# File 'app/models/schema_contract/validator.rb', line 50
def parsed_schema
file_contents = File.read(schema_file)
JSON.parse(file_contents)
end
|
#record ⇒ Object
34
35
36
|
# File 'app/models/schema_contract/validator.rb', line 34
def record
@record ||= Validation.find(@record_id)
end
|
#schema_file ⇒ Object
38
39
40
41
42
43
44
45
46
47
48
|
# File 'app/models/schema_contract/validator.rb', line 38
def schema_file
@schema_file ||= begin
path = Settings.schema_contract[record.contract_name]
if path.nil?
@result = 'schema_not_found'
raise SchemaContractValidationError, "No schema file #{record.contract_name} found."
end
Rails.root.join(path)
end
end
|
#validate ⇒ Object
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
# File 'app/models/schema_contract/validator.rb', line 11
def validate
errors = JSON::Validator.fully_validate(parsed_schema, record.response)
if errors.any?
@result = 'schema_errors_found'
record.update(error_details: errors)
detailed_message = { error_type: 'Schema discrepancy found', record_id: @record_id, response: record.response,
details: errors }
raise SchemaContractValidationError, detailed_message
else
@result = 'success'
end
rescue SchemaContractValidationError => e
raise e
rescue => e
@result = 'error'
detailed_message = { error_type: 'Unknown', record_id: @record_id, details: e.message }
raise SchemaContractValidationError, detailed_message
ensure
record&.update(status: @result) if defined?(@record)
end
|