class Itly::Plugin::SchemaValidator

Schema Validator plugin class for Itly SDK

Automatically loaded at runtime in any new Itly object

Constants

VERSION

Attributes

disabled[R]

Public Class Methods

new(schemas:, disabled: false) click to toggle source

Instantiate a new Plugin::SchemaValidator

@param [Hash] schemas: schemas for validation. Example:

Plugin::SchemaValidator.new schema: {
  schema_1: {field: 'value, ...},
  schema_2: {field: 'value, ...}
}

@param [TrueClass/FalseClass] disabled: set to true to disable the plugin. Default to false

Calls superclass method
# File lib/itly/plugin/schema_validator/schema_validator.rb, line 27
def initialize(schemas:, disabled: false)
  super()
  @schemas = schemas
  @disabled = disabled
  @validators = {}
end

Public Instance Methods

id() click to toggle source

Get the plugin ID

@return [String] plugin id

# File lib/itly/plugin/schema_validator/schema_validator.rb, line 88
def id
  'schema_validator'
end
load(options:) click to toggle source

Initialize the Plugin::SchemaValidator object

@param [Itly::PluginOptions] options: plugin options

Calls superclass method
# File lib/itly/plugin/schema_validator/schema_validator.rb, line 39
def load(options:)
  super
  # Get options
  @logger = options.logger

  # Log
  @logger&.info "#{id}: load()"

  @logger&.info "#{id}: plugin is disabled!" if @disabled
end
validate(event:) click to toggle source

Validate an Event

Call event on all plugins and collect their return values.

@param [Event] event: the event to validate

@return [Itly::ValidationResponse] a Itly::ValidationResponse object generated by the plugins or nil to indicate that there were no error

Calls superclass method
# File lib/itly/plugin/schema_validator/schema_validator.rb, line 60
def validate(event:)
  super
  return unless enabled?

  # Log
  log = Itly::Loggers.vars_to_log event: event
  @logger&.info "#{id}: validate(#{log})"

  # Check that we have a schema for this event
  if @schemas[event.name.to_sym].nil?
    raise Itly::ValidationError, "Event '#{event.name}' not found in tracking plan."
  end

  # Lazily initialize and cache validator
  @validators[event.name.to_sym] ||= JSONSchemer.schema(@schemas[event.name.to_sym])

  # Validation
  properties = deeply_stringify_keys event.properties
  result = @validators[event.name.to_sym].validate properties

  return_validation_responses event, result
end

Private Instance Methods

deeply_stringify_keys(hash) click to toggle source
# File lib/itly/plugin/schema_validator/schema_validator.rb, line 115
def deeply_stringify_keys(hash)
  stringified_hash = {}
  hash.each do |k, v|
    stringified_hash[k.to_s] = \
      case v
      when Hash
        deeply_stringify_keys(v)
      when Array
        v.map { |i| i.is_a?(Hash) ? deeply_stringify_keys(i) : i }
      else
        v
      end
  end
  stringified_hash
end
enabled?() click to toggle source
# File lib/itly/plugin/schema_validator/schema_validator.rb, line 94
def enabled?
  !@disabled
end
hash_to_message(hash) click to toggle source
# File lib/itly/plugin/schema_validator/schema_validator.rb, line 131
def hash_to_message(hash)
  hash.collect do |k, v|
    "#{k}: #{v.join ', '}"
  end.join '. '
end
return_validation_responses(event, result) click to toggle source
# File lib/itly/plugin/schema_validator/schema_validator.rb, line 98
def return_validation_responses(event, result)
  return if result.count.zero?

  message = "Passed in '#{event.name}' properties did not validate against your tracking plan. "\
    "Error#{'s' if result.count > 1}: "

  message += result.collect do |error|
    if error['details']
      hash_to_message error['details']
    else
      "#{error['data']} #{error['data_pointer']}"
    end
  end.join '. '

  Itly::ValidationResponse.new valid: false, plugin_id: id, message: message
end