schema-model 0.6.5 → 0.6.8

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9793fc154e42915262f3c54f42c5d753ff5b232c7e337ab072ac89f1b3a8c03a
4
- data.tar.gz: 4f7c3f9f18c96a2194c1046015e428699d07a7d0c276edbada3dfeed2018b639
3
+ metadata.gz: 5d996d080555c7553965069f80fc44c902ef72831635bdab8ae6515883e00334
4
+ data.tar.gz: da062d6c1a477c63382116961a7a8634cd13444a8a1797c38067d34d44d1faf0
5
5
  SHA512:
6
- metadata.gz: 1d888604b7606cdee7c428a8312deb91b950bc74dd44c5a3cfa76ca8e4bead6a31722557f243bf3ac463aac621d3fcdb66a739f3020afca1d3beaaedb174d386
7
- data.tar.gz: 01a24bfa955b24aa2c4223a6b4abd35eb792fb8fbb74cb346ccc205c18c97c76062ec340f2614735b65839aa677e43acb071d6f53dd7c79660f5781d02257dcd
6
+ metadata.gz: b29a0e7d32895ffe1c941860cb9f1af7efcfa4bc0a82a8ea86956d5c322f5fb1255301ee1702555546cc5d662c4981c2145167c9f453bdb15ce21fd34002ce1d
7
+ data.tar.gz: 4bac466d153605319ae50dc62a028e42b07ffe93ae589f4ed99dcedd54ade7a899f51acfbef45b35f395f77f8f2450cbcbdf2f57caf6d7afcfa1f75b66f2f22b
@@ -10,11 +10,40 @@ module Schema
10
10
  base.schema_include OverrideParsingErrors
11
11
  end
12
12
 
13
+ def valid_model!
14
+ unless valid?
15
+ raise ValidationException.new(
16
+ "invalid values for attributes #{errors.map(&:attribute).join(', ')}",
17
+ self,
18
+ errors
19
+ )
20
+ end
21
+ end
22
+
23
+ def valid!
24
+ parsed!
25
+ valid_model!
26
+ end
27
+
13
28
  # no-doc
14
29
  module OverrideParsingErrors
15
30
  def parsing_errors
16
31
  @parsing_errors ||= ActiveModel::Errors.new(self)
17
32
  end
33
+
34
+ def parsed?
35
+ parsing_errors.empty?
36
+ end
37
+
38
+ def parsed!
39
+ unless parsed?
40
+ raise ParsingException.new(
41
+ "schema parsing failed for attributes #{parsing_errors.errors.map(&:attribute).join(', ')}",
42
+ self,
43
+ parsing_errors
44
+ )
45
+ end
46
+ end
18
47
  end
19
48
  end
20
49
  end
@@ -20,7 +20,7 @@ module Schema
20
20
  def create_schema(base_schema, data, error_name = nil)
21
21
  if data.is_a?(Hash)
22
22
  unless (schema_class = get_schema_class(base_schema, data))
23
- add_parsing_error(base_schema, error_name, UNKNOWN)
23
+ add_parsing_error(base_schema, error_name, UNKNOWN) if base_schema.class.capture_unknown_attributes?
24
24
  return nil
25
25
  end
26
26
  schema = schema_class.from_hash(data)
data/lib/schema/model.rb CHANGED
@@ -25,6 +25,10 @@ module Schema
25
25
 
26
26
  # no-doc
27
27
  module ClassMethods
28
+ def self.include(base)
29
+ base.capture_unknown_attributes = true
30
+ end
31
+
28
32
  def schema
29
33
  {}.freeze
30
34
  end
@@ -40,10 +44,21 @@ module Schema
40
44
 
41
45
  def schema_config
42
46
  {
43
- schema_includes: []
47
+ schema_includes: [],
48
+ capture_unknown_attributes: true
44
49
  }.freeze
45
50
  end
46
51
 
52
+ def capture_unknown_attributes=(v)
53
+ config = schema_config.dup
54
+ config[:capture_unknown_attributes] = v
55
+ redefine_class_method(:schema_config, config.freeze)
56
+ end
57
+
58
+ def capture_unknown_attributes?
59
+ schema_config[:capture_unknown_attributes]
60
+ end
61
+
47
62
  def attribute(name, type, options = {})
48
63
  options[:aliases] = [options[:alias]] if options.key?(:alias)
49
64
 
@@ -148,7 +163,7 @@ STR
148
163
  def update_model_attributes(schema, data)
149
164
  data.each do |key, value|
150
165
  unless schema.key?(key)
151
- parsing_errors.add(key, ::Schema::ParsingErrors::UNKNOWN_ATTRIBUTE)
166
+ parsing_errors.add(key, ::Schema::ParsingErrors::UNKNOWN_ATTRIBUTE) if self.class.capture_unknown_attributes?
152
167
  next
153
168
  end
154
169
 
@@ -7,7 +7,7 @@ module Schema
7
7
  # Schema::Parsers::Common are parser methods for basic types
8
8
  module Common
9
9
  INTEGER_REGEX = /^[-+]?(?:[1-9]\d*|0)(?:\.0+)?$/.freeze
10
- FLOAT_REGEX = /^[-+]?(?:[1-9]\d*|0)(?:\.\d+)?(e-\d+)?$/.freeze
10
+ FLOAT_REGEX = /^[-+]?(?:[1-9]\d*|0)(?:\.\d+)?([Ee]-?\d+)?$/.freeze
11
11
  BOOLEAN_REGEX = /^(?:1|t|true|on|y|yes)$/i.freeze
12
12
 
13
13
  # rubocop:disable Metrics/CyclomaticComplexity
data/lib/schema-model.rb CHANGED
@@ -4,6 +4,19 @@ autoload :SchemaValidator, 'schema_validator'
4
4
 
5
5
  # Schema is a series of tools for transforming data into models
6
6
  module Schema
7
+ class SchemaException < StandardError
8
+ attr_reader :schema,
9
+ :errors
10
+
11
+ def initialize(msg, schema, errors)
12
+ super(msg)
13
+ @schema = schema
14
+ @errors = errors
15
+ end
16
+ end
17
+ class ParsingException < SchemaException; end
18
+ class ValidationException < SchemaException; end
19
+
7
20
  autoload :ActiveModelValidations, 'schema/active_model_validations'
8
21
  autoload :All, 'schema/all'
9
22
  autoload :ArrayHeaders, 'schema/array_headers'
data/schema-model.gemspec CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = 'schema-model'
5
- s.version = '0.6.5'
5
+ s.version = '0.6.8'
6
6
  s.licenses = ['MIT']
7
7
  s.summary = 'Schema Model'
8
8
  s.description = 'Easy way to create models from payloads'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: schema-model
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.5
4
+ version: 0.6.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Doug Youch
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-05-25 00:00:00.000000000 Z
11
+ date: 2022-05-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: inheritance-helper