itly-plugin-schema-validator 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 4019396633434a7415feac1efa74be0672c5db11f054d2fcac3d8849230b353d
4
+ data.tar.gz: 1b81d30c1d7ba450578a9cd238806406f478cd9a7ec9c78f6cbf3f2d2d784001
5
+ SHA512:
6
+ metadata.gz: 9475e517a4f4c7b855d2f7374fc6d1e4b94661b46287af0aa28bbfcc8aa04d3f8572cda7ae9b159f3983c5a94892f4d20b03ce70205921550756c61c8fa97819
7
+ data.tar.gz: 227e09dd3c772e8315eed6b44b76ba6077154624786c9b87f7115af56918f0bc0599f39e3fe03ae682677eee9a13c4d61fbad06ebbd744fd2b22a58e1684c7a6
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --require spec_helper
data/Gemfile ADDED
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ # Specify your gem's dependencies in itly-plugin-schema-validator.gemspec
6
+ gemspec
7
+
8
+ gem 'rake', '~> 13.0'
9
+
10
+ if ENV['LOCAL_ITLY_GEM']
11
+ # TODO: before publication to RubyGems, switch to version 1
12
+ gem 'itly-sdk', '~> 0.1', path: '../sdk'
13
+ end
14
+
15
+ gem 'rbs', '~> 1.0'
16
+ gem 'rspec'
17
+ gem 'steep', '~> 0.41'
data/Steepfile ADDED
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ target :lib do
4
+ signature 'sig'
5
+
6
+ check 'lib'
7
+
8
+ library 'logger', 'set', 'itly-sdk'
9
+ end
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'bundler/setup'
5
+ require 'itly/plugin-schema_validator'
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require 'irb'
15
+ IRB.start(__FILE__)
data/bin/rspec ADDED
@@ -0,0 +1,18 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ # Configure and load RBS
5
+ unless ENV['DISABLE_TYPE_CHECKING']
6
+ ENV['RBS_TEST_TARGET'] = 'Itly::*'
7
+ ENV['RBS_TEST_LOGLEVEL'] = 'warn'
8
+ ENV['RBS_TEST_DOUBLE_SUITE'] = 'rspec'
9
+ ENV['RBS_TEST_OPT'] = '-I./sig -I../sdk/sig'
10
+
11
+ require 'rbs/test/setup'
12
+ end
13
+
14
+ # Start RSpec
15
+ require 'rspec/core'
16
+
17
+ ENV['RSPEC_RUN_FROM_SCRIPT'] = 'true'
18
+ RSpec::Core::Runner.invoke
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'lib/itly/plugin/schema_validator/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'itly-plugin-schema-validator'
7
+ spec.version = Itly::Plugin::SchemaValidator::VERSION
8
+ spec.authors = ['Iteratively', 'Benjamin Bouchet', 'Justin Fiedler', 'Andrey Sokolov']
9
+ spec.email = ['support@iterative.ly']
10
+
11
+ spec.summary = 'SchemaValidator plugin for Iteratively SDK for Ruby'
12
+ spec.description = 'Track and validate analytics with a unified, extensible interface ' \
13
+ 'that works with all your 3rd party analytics providers.'
14
+ spec.homepage = 'https://github.com/iterativelyhq/itly-sdk-ruby'
15
+ spec.license = 'MIT'
16
+ spec.required_ruby_version = Gem::Requirement.new('>= 2.6.0')
17
+
18
+ spec.metadata['allowed_push_host'] = 'https://rubygems.org/'
19
+
20
+ spec.metadata['homepage_uri'] = spec.homepage
21
+ spec.metadata['source_code_uri'] = 'https://github.com/iterativelyhq/itly-sdk-ruby/plugin-schema-validator'
22
+
23
+ # Specify which files should be added to the gem when it is released.
24
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
25
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
26
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(?:test|spec|features)/}) }
27
+ end
28
+ spec.require_paths = ['lib']
29
+
30
+ spec.add_dependency 'itly-sdk', '~> 0.1'
31
+ spec.add_dependency 'json_schemer', '~> 0.2'
32
+ end
@@ -0,0 +1,3 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'plugin-schema_validator'
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ if ENV['LOCAL_ITLY_GEM']
4
+ lib = File.expand_path('../../../sdk/lib', File.dirname(__FILE__))
5
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
6
+ end
7
+
8
+ require_relative 'plugin/schema_validator/schema_validator'
9
+ require_relative 'plugin/schema_validator/version'
@@ -0,0 +1,138 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'itly-sdk'
4
+ require 'json_schemer'
5
+
6
+ class Itly
7
+ class Plugin
8
+ ##
9
+ # Schema Validator plugin class for Itly SDK
10
+ #
11
+ # Automatically loaded at runtime in any new +Itly+ object
12
+ #
13
+ class SchemaValidator < Plugin
14
+ attr_reader :disabled
15
+
16
+ ##
17
+ # Instantiate a new Plugin::SchemaValidator
18
+ #
19
+ # @param [Hash] schemas: schemas for validation. Example:
20
+ #
21
+ # Plugin::SchemaValidator.new schema: {
22
+ # schema_1: {field: 'value, ...},
23
+ # schema_2: {field: 'value, ...}
24
+ # }
25
+ # @param [TrueClass/FalseClass] disabled: set to true to disable the plugin. Default to false
26
+ #
27
+ def initialize(schemas:, disabled: false)
28
+ super()
29
+ @schemas = schemas
30
+ @disabled = disabled
31
+ @validators = {}
32
+ end
33
+
34
+ ##
35
+ # Initialize the Plugin::SchemaValidator object
36
+ #
37
+ # @param [Itly::PluginOptions] options: plugin options
38
+ #
39
+ def load(options:)
40
+ super
41
+ # Get options
42
+ @logger = options.logger
43
+
44
+ # Log
45
+ @logger&.info "#{id}: load()"
46
+
47
+ @logger&.info "#{id}: plugin is disabled!" if @disabled
48
+ end
49
+
50
+ ##
51
+ # Validate an Event
52
+ #
53
+ # Call +event+ on all plugins and collect their return values.
54
+ #
55
+ # @param [Event] event: the event to validate
56
+ #
57
+ # @return [Itly::ValidationResponse] a Itly::ValidationResponse object generated
58
+ # by the plugins or nil to indicate that there were no error
59
+ #
60
+ def validate(event:)
61
+ super
62
+ return unless enabled?
63
+
64
+ # Log
65
+ log = Itly::Loggers.vars_to_log event: event
66
+ @logger&.info "#{id}: validate(#{log})"
67
+
68
+ # Check that we have a schema for this event
69
+ if @schemas[event.name.to_sym].nil?
70
+ raise Itly::ValidationError, "Event '#{event.name}' not found in tracking plan."
71
+ end
72
+
73
+ # Lazily initialize and cache validator
74
+ @validators[event.name.to_sym] ||= JSONSchemer.schema(@schemas[event.name.to_sym])
75
+
76
+ # Validation
77
+ properties = deeply_stringify_keys event.properties
78
+ result = @validators[event.name.to_sym].validate properties
79
+
80
+ return_validation_responses event, result
81
+ end
82
+
83
+ ##
84
+ # Get the plugin ID
85
+ #
86
+ # @return [String] plugin id
87
+ #
88
+ def id
89
+ 'schema_validator'
90
+ end
91
+
92
+ private
93
+
94
+ def enabled?
95
+ !@disabled
96
+ end
97
+
98
+ def return_validation_responses(event, result)
99
+ return if result.count.zero?
100
+
101
+ message = "Passed in '#{event.name}' properties did not validate against your tracking plan. "\
102
+ "Error#{'s' if result.count > 1}: "
103
+
104
+ message += result.collect do |error|
105
+ if error['details']
106
+ hash_to_message error['details']
107
+ else
108
+ "#{error['data']} #{error['data_pointer']}"
109
+ end
110
+ end.join '. '
111
+
112
+ Itly::ValidationResponse.new valid: false, plugin_id: id, message: message
113
+ end
114
+
115
+ def deeply_stringify_keys(hash)
116
+ stringified_hash = {}
117
+ hash.each do |k, v|
118
+ stringified_hash[k.to_s] = \
119
+ case v
120
+ when Hash
121
+ deeply_stringify_keys(v)
122
+ when Array
123
+ v.map { |i| i.is_a?(Hash) ? deeply_stringify_keys(i) : i }
124
+ else
125
+ v
126
+ end
127
+ end
128
+ stringified_hash
129
+ end
130
+
131
+ def hash_to_message(hash)
132
+ hash.collect do |k, v|
133
+ "#{k}: #{v.join ', '}"
134
+ end.join '. '
135
+ end
136
+ end
137
+ end
138
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Itly
4
+ class Plugin
5
+ class SchemaValidator < Plugin
6
+ VERSION = '0.1.0'
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,3 @@
1
+ class JSONSchemer
2
+ def validate: (propertiesHash) -> untyped
3
+ end
@@ -0,0 +1,3 @@
1
+ # Part of the gems
2
+ class MonitorMixin
3
+ end
@@ -0,0 +1,21 @@
1
+ class Itly
2
+ class Plugin
3
+ class SchemaValidator < Plugin
4
+ VERSION: String
5
+
6
+ attr_reader disabled: bool
7
+
8
+ def load: (options: Itly::PluginOptions options) -> void
9
+ def validate: (event: Itly::Event event) -> Itly::ValidationResponse?
10
+ def id: () -> String
11
+
12
+ private
13
+
14
+ def initialize: (schemas: propertiesHash schemas, ?disabled: bool disabled) -> void
15
+ def enabled?: () -> bool
16
+ def return_validation_responses: (Itly::Event event, untyped result) -> Itly::ValidationResponse?
17
+ def deeply_stringify_keys: (propertiesHash hash) -> Hash[String, untyped]
18
+ def hash_to_message: (propertiesHash hash) -> String
19
+ end
20
+ end
21
+ end
metadata ADDED
@@ -0,0 +1,92 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: itly-plugin-schema-validator
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Iteratively
8
+ - Benjamin Bouchet
9
+ - Justin Fiedler
10
+ - Andrey Sokolov
11
+ autorequire:
12
+ bindir: bin
13
+ cert_chain: []
14
+ date: 2021-06-15 00:00:00.000000000 Z
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: itly-sdk
18
+ requirement: !ruby/object:Gem::Requirement
19
+ requirements:
20
+ - - "~>"
21
+ - !ruby/object:Gem::Version
22
+ version: '0.1'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: '0.1'
30
+ - !ruby/object:Gem::Dependency
31
+ name: json_schemer
32
+ requirement: !ruby/object:Gem::Requirement
33
+ requirements:
34
+ - - "~>"
35
+ - !ruby/object:Gem::Version
36
+ version: '0.2'
37
+ type: :runtime
38
+ prerelease: false
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - "~>"
42
+ - !ruby/object:Gem::Version
43
+ version: '0.2'
44
+ description: Track and validate analytics with a unified, extensible interface that
45
+ works with all your 3rd party analytics providers.
46
+ email:
47
+ - support@iterative.ly
48
+ executables: []
49
+ extensions: []
50
+ extra_rdoc_files: []
51
+ files:
52
+ - ".rspec"
53
+ - Gemfile
54
+ - Steepfile
55
+ - bin/console
56
+ - bin/rspec
57
+ - bin/setup
58
+ - itly-plugin-schema-validator.gemspec
59
+ - lib/itly/plugin-schema-validator.rb
60
+ - lib/itly/plugin-schema_validator.rb
61
+ - lib/itly/plugin/schema_validator/schema_validator.rb
62
+ - lib/itly/plugin/schema_validator/version.rb
63
+ - sig/lib/json_schemer.rbs
64
+ - sig/lib/monitor_mixin.rbs
65
+ - sig/schema_validator.rbs
66
+ homepage: https://github.com/iterativelyhq/itly-sdk-ruby
67
+ licenses:
68
+ - MIT
69
+ metadata:
70
+ allowed_push_host: https://rubygems.org/
71
+ homepage_uri: https://github.com/iterativelyhq/itly-sdk-ruby
72
+ source_code_uri: https://github.com/iterativelyhq/itly-sdk-ruby/plugin-schema-validator
73
+ post_install_message:
74
+ rdoc_options: []
75
+ require_paths:
76
+ - lib
77
+ required_ruby_version: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: 2.6.0
82
+ required_rubygems_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ requirements: []
88
+ rubygems_version: 3.0.3.1
89
+ signing_key:
90
+ specification_version: 4
91
+ summary: SchemaValidator plugin for Iteratively SDK for Ruby
92
+ test_files: []