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 +4 -4
- data/lib/schema/active_model_validations.rb +29 -0
- data/lib/schema/associations/schema_creator.rb +1 -1
- data/lib/schema/model.rb +17 -2
- data/lib/schema/parsers/common.rb +1 -1
- data/lib/schema-model.rb +13 -0
- data/schema-model.gemspec +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5d996d080555c7553965069f80fc44c902ef72831635bdab8ae6515883e00334
|
4
|
+
data.tar.gz: da062d6c1a477c63382116961a7a8634cd13444a8a1797c38067d34d44d1faf0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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+)?(
|
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
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.
|
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-
|
11
|
+
date: 2022-05-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: inheritance-helper
|