anyvali 0.1.6 → 0.1.7
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/anyvali/parse/coercion.rb +22 -0
- data/lib/anyvali/parse/coercion_config.rb +1 -0
- data/lib/anyvali/schema.rb +8 -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: 2a4ddd57b6576747bbb56bf5748fbad61c693374016883abcdcbca6fdf8b3562
|
|
4
|
+
data.tar.gz: 2694be8b631794c743b0c8cd0e4dc74e5d74814a2c0cce71c2d5156444dd2726
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 9f968aab5a5130792d94d85bcfc43be7ce3e489382ff1addc348fc08e8dc5f2f89acd38c193d2610743d079421c822e445870c9b9b299ae5d448562d7760b37e
|
|
7
|
+
data.tar.gz: c4424331f7289485c18cd5de177c9ea3af0e316e5f102f1a28eb68da1973bf282b412de4d50a749bc8fa6ad50a38d63f20dc2b40f9bbc6548b1b6554ffaa1832
|
|
@@ -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
|
|
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.7
|
|
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-17 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: minitest
|