anyvali 0.1.5 → 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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: adce766c576f4ff08e19cb157119086eb7853cc220ffcf0bc150ab695d0a850e
4
- data.tar.gz: 27b270066dd4e4ed0dfdf0928c298ea48652ac2424b7a2c0d6f34dd7e3a66d49
3
+ metadata.gz: 2a4ddd57b6576747bbb56bf5748fbad61c693374016883abcdcbca6fdf8b3562
4
+ data.tar.gz: 2694be8b631794c743b0c8cd0e4dc74e5d74814a2c0cce71c2d5156444dd2726
5
5
  SHA512:
6
- metadata.gz: 8c763bf7adb60b8575b41c06e633092133ab8d2f5b327dc8f377d1e993654afddba01de8234f11e5c2109296d0b3c972a7a30254c08092a41ed177854a05b53d
7
- data.tar.gz: 173f1434272b094c44a3b365ee4819b1a1b14bb01ec1f0196ac5b3f188d989548029d3d0719504c716de15447d309373e0a02c70eb9e51b2b4eba5b5aee20f50
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)
@@ -3,6 +3,7 @@
3
3
  module AnyVali
4
4
  module CoercionConfig
5
5
  PORTABLE_COERCIONS = %w[
6
+ string
6
7
  string->int
7
8
  string->number
8
9
  string->bool
@@ -66,7 +66,14 @@ module AnyVali
66
66
  dup_with(default_value: value, has_default: true)
67
67
  end
68
68
 
69
- def coerce(config)
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.5
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-14 00:00:00.000000000 Z
11
+ date: 2026-06-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: minitest