Class: Itly::Plugin::SchemaValidator

Inherits:
Itly::Plugin show all
Defined in:
lib/itly/plugin/schema_validator/schema_validator.rb,
lib/itly/plugin/schema_validator/version.rb

Overview

Schema Validator plugin class for Itly SDK

Automatically loaded at runtime in any new Itly object

Constant Summary collapse

VERSION =
'0.1.1'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(schemas:, disabled: false) ⇒ SchemaValidator

Instantiate a new Plugin::SchemaValidator

Parameters:

  • schemas: (Hash)

    schemas for validation. Example:

    Plugin::SchemaValidator.new schema:

    schema_1: {field: 'value, ...,
    schema_2: 'value, ...
    

    }

  • disabled: (TrueClass/FalseClass) (defaults to: false)

    set to true to disable the plugin. Default to false



27
28
29
30
31
32
# File 'lib/itly/plugin/schema_validator/schema_validator.rb', line 27

def initialize(schemas:, disabled: false)
  super()
  @schemas = schemas
  @disabled = disabled
  @validators = {}
end

Instance Attribute Details

#disabledObject (readonly)

Returns the value of attribute disabled.



14
15
16
# File 'lib/itly/plugin/schema_validator/schema_validator.rb', line 14

def disabled
  @disabled
end

Instance Method Details

#idString

Get the plugin ID

Returns:

  • (String)

    plugin id



88
89
90
# File 'lib/itly/plugin/schema_validator/schema_validator.rb', line 88

def id
  'schema_validator'
end

#load(options:) ⇒ Object

Initialize the Plugin::SchemaValidator object

Parameters:

  • options: (Itly::PluginOptions)

    plugin options



39
40
41
42
43
44
45
46
47
48
# 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:) ⇒ Itly::ValidationResponse

Validate an Event

Call event on all plugins and collect their return values.

by the plugins or nil to indicate that there were no error

Parameters:

  • event: (Event)

    the event to validate

Returns:

  • (Itly::ValidationResponse)

    a Itly::ValidationResponse object generated



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# 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