qwack 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: 68b356c4e1298450d74b96b5ccb7c9f65a7cf39bfd8a8cb283436029ae7219fa
4
+ data.tar.gz: aa175dc9cd62399b9c60c030031315eb569dabb2a68c9d71d986591192187bd5
5
+ SHA512:
6
+ metadata.gz: b493dd31f15e44d5f92c907f04adb2a5a797f15f6e2e8fe05b4f8023ae4f0bb16049bbb04d416ea2f53cb6d06ad51139f2833ddc5c371fec12e3b8649349652e
7
+ data.tar.gz: d22f14d9decd84f70965c39faa15f2977de588675346379dd8a099a2616609b1ff7ef66d7c5528fcdced7cbc8443e3aaa75308ac55e5ad6fb1536a2d519cee2e
data/LICENSE.txt ADDED
@@ -0,0 +1,8 @@
1
+ Copyright 2022 Laurent Humez
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
8
+
data/README.md ADDED
File without changes
data/lib/qwack/base.rb ADDED
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative './errors'
4
+
5
+ module Qwack
6
+ # Base module for types
7
+ module Base
8
+ def validation_errors(input, path = 'root')
9
+ raise NotImplementedError
10
+ end
11
+
12
+ def validate!(input, path = 'root')
13
+ errors = validation_errors(input, path)
14
+ raise Qwack::Errors::ValidationError, errors unless errors.empty?
15
+
16
+ input
17
+ end
18
+
19
+ def mock(input)
20
+ raise NotImplementedError
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative './base'
4
+
5
+ module Qwack
6
+ # Base module for user defined arrays
7
+ module BaseArray
8
+ include Qwack::Base
9
+
10
+ def allowed_types
11
+ ancestors.reverse.each_with_object([]) do |ancestor, obj|
12
+ obj.append(*ancestor.own_allowed_types) \
13
+ if ancestor.respond_to?(:own_allowed_types)
14
+ end
15
+ end
16
+
17
+ def own_allowed_types
18
+ @own_allowed_types ||= []
19
+ end
20
+
21
+ def validation_errors(input, path = 'root')
22
+ return [{ path: path, name: name, desc: 'Is not an array', item: input }] \
23
+ unless input.is_a?(::Array)
24
+
25
+ all_allowed_types = allowed_types
26
+ input.each_with_object([]).with_index do |(item, errors), index|
27
+ all_allowed_types.each do |type|
28
+ errors.append(*type.validation_errors(item, "#{path}.#{index}"))
29
+ end
30
+ end
31
+ end
32
+
33
+ def mock(input = nil)
34
+ input || []
35
+ end
36
+
37
+ private
38
+
39
+ def allow_types(types)
40
+ own_allowed_types.append(*types)
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative './base'
4
+
5
+ module Qwack
6
+ # Base module for user defined enums
7
+ module BaseEnum
8
+ include Qwack::Base
9
+
10
+ def allowed_values
11
+ ancestors.reverse.each_with_object([]) do |ancestor, obj|
12
+ obj.append(*ancestor.own_allowed_values) \
13
+ if ancestor.respond_to?(:own_allowed_values)
14
+ end
15
+ end
16
+
17
+ def own_allowed_values
18
+ @own_allowed_values ||= []
19
+ end
20
+
21
+ def validation_errors(input, path = 'root')
22
+ return [{ path: path, name: name, desc: 'Is not an allowed value', item: input }] \
23
+ unless allowed_values.include?(input)
24
+
25
+ []
26
+ end
27
+
28
+ def mock(input = nil)
29
+ input || own_allowed_values&.first
30
+ end
31
+
32
+ private
33
+
34
+ def allow_values(values)
35
+ own_allowed_values.append(*values)
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,57 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative './base'
4
+
5
+ module Qwack
6
+ # Base module for user defined hashes
7
+ module BaseHash
8
+ include Qwack::Base
9
+
10
+ def attributes
11
+ ancestors.reverse.each_with_object({}) do |ancestor, obj|
12
+ obj.merge!(ancestor.own_attributes) if ancestor.respond_to?(:own_attributes)
13
+ end
14
+ end
15
+
16
+ def own_attributes
17
+ @own_attributes ||= {}
18
+ end
19
+
20
+ # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
21
+ def attribute_validation_errors(input, path, key, errors)
22
+ if !attributes.key?(key)
23
+ errors << { path: path, name: name, desc: 'Extraneous keys', item: key }
24
+ elsif !input.key?(key)
25
+ errors << { path: path, name: name, desc: 'Missing non-optional key', item: key } \
26
+ unless attributes[key][:optional] == true
27
+ elsif input[key].nil?
28
+ errors << { path: path, name: name, desc: 'Key cannot be null', item: key } \
29
+ unless attributes[key][:null] == true
30
+ else
31
+ errors.append(*attributes[key][:type].validation_errors(input[key], "#{path}.#{key}"))
32
+ end
33
+ end
34
+ # rubocop:enable Metrics/AbcSize, Metrics/MethodLength
35
+
36
+ def validation_errors(input, path = 'root')
37
+ return [{ path: path, name: name, desc: 'Is not a Hash', item: input }] \
38
+ unless input.is_a?(::Hash)
39
+
40
+ (attributes.keys + input.keys).uniq.each_with_object([]) do |key, errors|
41
+ attribute_validation_errors(input, path, key, errors)
42
+ end
43
+ end
44
+
45
+ def mock(input = nil)
46
+ attributes.each_with_object({}) do |(key, conf), obj|
47
+ obj[key] = conf[:type].mock(input&.fetch(key, nil) || conf[:mock])
48
+ end
49
+ end
50
+
51
+ private
52
+
53
+ def add_attribute(key, type:, **args)
54
+ own_attributes[key] = { type: type, **args }
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,52 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'pry'
4
+ require_relative './base'
5
+
6
+ module Qwack
7
+ # Base module for user defined types
8
+ module BaseType
9
+ include Qwack::Base
10
+
11
+ def validators
12
+ ancestors.reverse.each_with_object([]) do |ancestor, obj|
13
+ obj.append(*ancestor.own_validators) \
14
+ if ancestor.respond_to?(:own_validators)
15
+ end
16
+ end
17
+
18
+ def own_validators
19
+ @own_validators = [] unless instance_variable_defined?('@own_validators')
20
+
21
+ @own_validators
22
+ end
23
+
24
+ def own_default_mock
25
+ @own_default_mock = nil unless instance_variable_defined?('@own_default_mock')
26
+
27
+ @own_default_mock
28
+ end
29
+
30
+ def validation_errors(input, path = 'root')
31
+ validators.each_with_object([]) do |validator, errors|
32
+ error = validator.call(input)
33
+ errors << { path: path, name: name, desc: error, item: input } \
34
+ unless error.nil?
35
+ end
36
+ end
37
+
38
+ def mock(input = nil)
39
+ input || own_default_mock
40
+ end
41
+
42
+ private
43
+
44
+ def add_validator(validator)
45
+ own_validators.push(validator)
46
+ end
47
+
48
+ def default_mock(value)
49
+ @own_default_mock = value
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'pp'
4
+
5
+ module Qwack
6
+ module Errors
7
+ # Wrapper exception around validation errors
8
+ class ValidationError < StandardError
9
+ attr_reader :validation_errors
10
+
11
+ def initialize(validation_errors = [])
12
+ @validation_errors = validation_errors
13
+ super("Found the following errors:\n #{validation_errors.pretty_inspect}")
14
+ end
15
+ end
16
+ end
17
+ end
data/lib/qwack/meta.rb ADDED
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative './base_array'
4
+
5
+ module Qwack
6
+ # Shorthand for defining Array types
7
+ class Meta
8
+ def self.array_of(type)
9
+ Class.new do
10
+ extend Qwack::Base
11
+ extend Qwack::BaseArray
12
+ allow_types(type)
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../base'
4
+ require_relative '../base_type'
5
+
6
+ module Qwack
7
+ module Types
8
+ class Boolean
9
+ extend Qwack::BaseType
10
+
11
+ add_validator ->(input) { 'Not a boolean' unless [true, false].include?(input) }
12
+
13
+ default_mock false
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'date'
4
+ require_relative '../base_type'
5
+
6
+ module Qwack
7
+ module Types
8
+ class Date
9
+ extend Qwack::BaseType
10
+
11
+ add_validator lambda { |input|
12
+ begin
13
+ DateTime.parse(input)
14
+ nil
15
+ rescue StandardError
16
+ 'Not a date'
17
+ end
18
+ }
19
+
20
+ default_mock '1990-01-01T00:00:00+00'
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../base_type'
4
+
5
+ module Qwack
6
+ module Types
7
+ class Email
8
+ extend Qwack::BaseType
9
+
10
+ add_validator ->(input) { 'Not an email' unless input&.match?(URI::MailTo::EMAIL_REGEXP) }
11
+
12
+ default_mock 'name@domain'
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../base_type'
4
+
5
+ module Qwack
6
+ module Types
7
+ class Float
8
+ extend Qwack::BaseType
9
+
10
+ add_validator ->(input) { 'Not a float' unless input.is_a?(::Float) }
11
+
12
+ default_mock 0.0
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../base_type'
4
+
5
+ module Qwack
6
+ module Types
7
+ class Integer
8
+ extend Qwack::BaseType
9
+
10
+ add_validator ->(input) { 'Not an integer' unless input.is_a?(::Integer) }
11
+
12
+ default_mock 0
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../base_type'
4
+
5
+ module Qwack
6
+ module Types
7
+ class String
8
+ extend Qwack::BaseType
9
+
10
+ add_validator ->(input) { 'Not a string' unless input.is_a?(::String) }
11
+
12
+ default_mock ''
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Qwack
4
+ module Version
5
+ STRING = '0.1.0'
6
+ end
7
+ end
data/lib/qwack.rb ADDED
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative './qwack/base'
4
+ require_relative './qwack/base_array'
5
+ require_relative './qwack/base_enum'
6
+ require_relative './qwack/base_hash'
7
+ require_relative './qwack/base_type'
8
+ require_relative './qwack/errors'
9
+ require_relative './qwack/meta'
10
+ require_relative './qwack/types/boolean'
11
+ require_relative './qwack/types/date'
12
+ require_relative './qwack/types/email'
13
+ require_relative './qwack/types/float'
14
+ require_relative './qwack/types/integer'
15
+ require_relative './qwack/types/string'
metadata ADDED
@@ -0,0 +1,163 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: qwack
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Laurent Humez
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2022-09-28 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 1.15.0
20
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: '3.0'
23
+ type: :development
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ version: 1.15.0
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: '3.0'
33
+ - !ruby/object:Gem::Dependency
34
+ name: pry
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ type: :development
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ - !ruby/object:Gem::Dependency
48
+ name: rspec
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: 3.11.0
54
+ - - "<"
55
+ - !ruby/object:Gem::Version
56
+ version: '4.0'
57
+ type: :development
58
+ prerelease: false
59
+ version_requirements: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: 3.11.0
64
+ - - "<"
65
+ - !ruby/object:Gem::Version
66
+ version: '4.0'
67
+ - !ruby/object:Gem::Dependency
68
+ name: rubocop
69
+ requirement: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ version: 1.36.0
74
+ - - "<"
75
+ - !ruby/object:Gem::Version
76
+ version: '2.0'
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: 1.36.0
84
+ - - "<"
85
+ - !ruby/object:Gem::Version
86
+ version: '2.0'
87
+ - !ruby/object:Gem::Dependency
88
+ name: rubocop-rspec
89
+ requirement: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: 2.13.0
94
+ - - "<"
95
+ - !ruby/object:Gem::Version
96
+ version: '3.0'
97
+ type: :development
98
+ prerelease: false
99
+ version_requirements: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: 2.13.0
104
+ - - "<"
105
+ - !ruby/object:Gem::Version
106
+ version: '3.0'
107
+ description: |2
108
+ Qwack is an extensible, lightweight DLS to dynamically
109
+ verify and mock scalar types.
110
+ It is meant primilarily to handle parsed JSON objects
111
+ eg: from an API or a database field
112
+ email: lahumez@gmail.com
113
+ executables: []
114
+ extensions: []
115
+ extra_rdoc_files:
116
+ - LICENSE.txt
117
+ - README.md
118
+ files:
119
+ - LICENSE.txt
120
+ - README.md
121
+ - lib/qwack.rb
122
+ - lib/qwack/base.rb
123
+ - lib/qwack/base_array.rb
124
+ - lib/qwack/base_enum.rb
125
+ - lib/qwack/base_hash.rb
126
+ - lib/qwack/base_type.rb
127
+ - lib/qwack/errors.rb
128
+ - lib/qwack/meta.rb
129
+ - lib/qwack/types/boolean.rb
130
+ - lib/qwack/types/date.rb
131
+ - lib/qwack/types/email.rb
132
+ - lib/qwack/types/float.rb
133
+ - lib/qwack/types/integer.rb
134
+ - lib/qwack/types/string.rb
135
+ - lib/qwack/version.rb
136
+ homepage: https://github.com/godric/qwack
137
+ licenses:
138
+ - MIT
139
+ metadata:
140
+ homepage_uri: https://github.com/godric/qwack
141
+ source_code_uri: https://github.com/godric/qwack
142
+ bug_tracker_uri: https://github.com/godric/qwack/issues
143
+ rubygems_mfa_required: 'true'
144
+ post_install_message:
145
+ rdoc_options: []
146
+ require_paths:
147
+ - lib
148
+ required_ruby_version: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: '3.0'
153
+ required_rubygems_version: !ruby/object:Gem::Requirement
154
+ requirements:
155
+ - - ">="
156
+ - !ruby/object:Gem::Version
157
+ version: '0'
158
+ requirements: []
159
+ rubygems_version: 3.2.15
160
+ signing_key:
161
+ specification_version: 4
162
+ summary: Dynamic typing DLS for plain hashes.
163
+ test_files: []