anyvali 0.1.6 → 0.1.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:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 3059151c336de14c296db20a2e44782184b88ae4210d34fc4b5b6316d7be8d15
|
|
4
|
+
data.tar.gz: 6905cd9c596343340335eff8bd3875fe1989caefa861e8e34952e9e194ce3b28
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 910cb9fdb00548fa64363d2ebe0800d62e074fc9af8e865e2e81f2340c35d949d8a68e5e956583df7a773a01fe5154da57f534eb3b9fc34d0e386f2bec9ac313
|
|
7
|
+
data.tar.gz: e8b45fbf0833f3924ac94c7cd09e1b4d972ba5a8de2b300e34be9a0171414fb412e75e32184d10fbc40329e2ded92bdbc9cd2764df05a0a8c011af1b5a7511d6
|
|
@@ -19,6 +19,11 @@ module AnyVali
|
|
|
19
19
|
|
|
20
20
|
def apply_single(value, config, kind)
|
|
21
21
|
case config
|
|
22
|
+
when "string"
|
|
23
|
+
# Generic, portable coercion source (spec 5.1). The only portable
|
|
24
|
+
# source is "string"; infer the target from the schema kind. On a
|
|
25
|
+
# string-kind schema this is a no-op (the value is already a string).
|
|
26
|
+
coerce_from_string_to_kind(value, kind)
|
|
22
27
|
when "string->int"
|
|
23
28
|
coerce_string_to_int(value)
|
|
24
29
|
when "string->number"
|
|
@@ -36,6 +41,23 @@ module AnyVali
|
|
|
36
41
|
end
|
|
37
42
|
end
|
|
38
43
|
|
|
44
|
+
# Map the portable "string" source to the concrete string->kind coercion
|
|
45
|
+
# for the target schema kind. Integer family -> int; float family ->
|
|
46
|
+
# number; bool -> bool; everything else (string/unknown/...) is a no-op.
|
|
47
|
+
def coerce_from_string_to_kind(value, kind)
|
|
48
|
+
case kind
|
|
49
|
+
when "int", "int8", "int16", "int32", "int64",
|
|
50
|
+
"uint8", "uint16", "uint32", "uint64"
|
|
51
|
+
coerce_string_to_int(value)
|
|
52
|
+
when "number", "float32", "float64"
|
|
53
|
+
coerce_string_to_number(value)
|
|
54
|
+
when "bool"
|
|
55
|
+
coerce_string_to_bool(value)
|
|
56
|
+
else
|
|
57
|
+
{ success: true, value: value }
|
|
58
|
+
end
|
|
59
|
+
end
|
|
60
|
+
|
|
39
61
|
def coerce_string_to_int(value)
|
|
40
62
|
return { success: true, value: value } if value.is_a?(Integer)
|
|
41
63
|
return { success: false, value: value } unless value.is_a?(String)
|
data/lib/anyvali/schema.rb
CHANGED
|
@@ -66,7 +66,14 @@ module AnyVali
|
|
|
66
66
|
dup_with(default_value: value, has_default: true)
|
|
67
67
|
end
|
|
68
68
|
|
|
69
|
-
|
|
69
|
+
# Enable coercion on this schema.
|
|
70
|
+
#
|
|
71
|
+
# With no argument (`schema.coerce`) the only portable coercion source —
|
|
72
|
+
# "string" (spec 5.1) — is implied, and the coercion target is inferred
|
|
73
|
+
# from the schema kind (e.g. number().coerce coerces a string to a number).
|
|
74
|
+
# An explicit config string/array (e.g. "string->int", "trim",
|
|
75
|
+
# ["trim", "lower"]) is still accepted for backwards compatibility.
|
|
76
|
+
def coerce(config = "string")
|
|
70
77
|
dup_with(coerce_config: config)
|
|
71
78
|
end
|
|
72
79
|
|
|
@@ -51,6 +51,9 @@ module AnyVali
|
|
|
51
51
|
end
|
|
52
52
|
|
|
53
53
|
output = {}
|
|
54
|
+
mode = effective_unknown_keys(context)
|
|
55
|
+
previous_inherited_unknown_keys = context.inherited_unknown_keys
|
|
56
|
+
context.inherited_unknown_keys = mode if %w[strip reject].include?(mode)
|
|
54
57
|
|
|
55
58
|
# Check required fields
|
|
56
59
|
@required_keys.each do |key|
|
|
@@ -102,9 +105,11 @@ module AnyVali
|
|
|
102
105
|
end
|
|
103
106
|
end
|
|
104
107
|
|
|
108
|
+
context.inherited_unknown_keys = previous_inherited_unknown_keys
|
|
109
|
+
|
|
105
110
|
# Handle unknown keys
|
|
106
111
|
unknown = input.keys - @properties.keys
|
|
107
|
-
case
|
|
112
|
+
case mode
|
|
108
113
|
when "reject"
|
|
109
114
|
unknown.each do |key|
|
|
110
115
|
issues << ValidationIssue.new(
|
|
@@ -151,7 +156,8 @@ module AnyVali
|
|
|
151
156
|
|
|
152
157
|
private
|
|
153
158
|
|
|
154
|
-
def effective_unknown_keys
|
|
159
|
+
def effective_unknown_keys(context = nil)
|
|
160
|
+
return context.inherited_unknown_keys if context&.inherited_unknown_keys
|
|
155
161
|
@unknown_keys_explicit ? @unknown_keys : "strip"
|
|
156
162
|
end
|
|
157
163
|
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: anyvali
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.1.
|
|
4
|
+
version: 0.1.8
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- AnyVali Contributors
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2026-06-
|
|
11
|
+
date: 2026-06-20 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: minitest
|