class Snowly::EachValidator
Attributes
errors[R]
request[R]
Public Class Methods
new(payload)
click to toggle source
# File lib/snowly/each_validator.rb, line 10 def initialize(payload) @request = Request.new payload @errors = [] end
Public Instance Methods
as_hash()
click to toggle source
# File lib/snowly/each_validator.rb, line 32 def as_hash { event_id: request.as_hash['event_id'], errors: errors, content: request.as_hash } end
protocol_schema()
click to toggle source
# File lib/snowly/each_validator.rb, line 28 def protocol_schema @protocol_schema ||= ProtocolSchemaFinder.new.schema end
valid?()
click to toggle source
If request is valid @return [true, false] if valid
# File lib/snowly/each_validator.rb, line 17 def valid? @errors == [] end
validate()
click to toggle source
Entry point for validation.
# File lib/snowly/each_validator.rb, line 22 def validate validate_root validate_associated valid? end
Private Instance Methods
associated_contexts()
click to toggle source
@return [Hash] all contexts content and schema definitions
# File lib/snowly/each_validator.rb, line 39 def associated_contexts load_contexts request.as_hash['contexts'] end
associated_elements()
click to toggle source
@return [Array<Hash>] all associated content
# File lib/snowly/each_validator.rb, line 49 def associated_elements (Array(associated_contexts) + Array(associated_unstruct_event)).compact end
associated_unstruct_event()
click to toggle source
@return [Hash] all unstructured events content and schema definitions
# File lib/snowly/each_validator.rb, line 44 def associated_unstruct_event load_unstruct_event request.as_hash['unstruct_event'] end
load_contexts(hash)
click to toggle source
Performs initial validation for associated contexts and loads their contents and definitions. @return [Array<Hash>]
# File lib/snowly/each_validator.rb, line 55 def load_contexts(hash) return unless hash response = [] unless hash['data'] @errors << "All custom contexts must be contain a `data` element" and return end schema = SchemaCache.instance[hash['schema']] response << { content: hash['data'], definition: schema, schema_name: hash['schema'] } unless hash['data'].is_a? Array @errors << "All custom contexts must be wrapped in an Array" and return end hash['data'].each do |data_item| schema = SchemaCache.instance[data_item['schema']] response << { content: data_item['data'], definition: schema, schema_name: data_item['schema'] } end response end
load_unstruct_event(hash)
click to toggle source
Performs initial validation for associated unstructured events and loads their contents and definitions. @return [Array<Hash>]
# File lib/snowly/each_validator.rb, line 79 def load_unstruct_event(hash) return unless hash response = [] unless hash['data'] @errors << "All custom unstruct event must be contain a `data` element" and return end outer_data = hash['data'] inner_data = outer_data['data'] response << { content: outer_data, definition: SchemaCache.instance[hash['schema']], schema_name: hash['schema'] } response << { content: inner_data, definition: SchemaCache.instance[outer_data['schema']], schema_name: outer_data['schema'] } response end
register_missing_schema(name)
click to toggle source
# File lib/snowly/each_validator.rb, line 73 def register_missing_schema(name) @errors << "#{ name } wasn't found in any resolvers." end
validate_associated()
click to toggle source
Validates associated contexts and unstructured events
# File lib/snowly/each_validator.rb, line 93 def validate_associated return unless associated_elements missing_schemas, valid_elements = associated_elements.partition{ |el| el[:definition].blank? } missing_schemas.each { |element| register_missing_schema(element[:schema_name]) } valid_elements.each do |element| this_error = JSON::Validator.fully_validate JSON.parse(element[:definition]), element[:content] @errors += this_error if this_error.count > 0 end end
validate_root()
click to toggle source
Validates root attributes for the events table
# File lib/snowly/each_validator.rb, line 104 def validate_root this_error = JSON::Validator.fully_validate protocol_schema, request.as_hash @errors += this_error if this_error.count > 0 end