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: c865ca84583452f4064bf2789277cd979c33e32f718aa3ed3653e58de25e0158
4
- data.tar.gz: 25de1308d962585be84d11b97830acbb5d399d2e401411e98fdd54ea7375a28d
3
+ metadata.gz: 3059151c336de14c296db20a2e44782184b88ae4210d34fc4b5b6316d7be8d15
4
+ data.tar.gz: 6905cd9c596343340335eff8bd3875fe1989caefa861e8e34952e9e194ce3b28
5
5
  SHA512:
6
- metadata.gz: b89bc74437da5378dc0d7417a3a50165796f179d3aeb501ed3b73db55e39fb13146fcc3a7f6d7efb0041cffda6818009c5ba0b552768a930d9b9ed5ea7be0ccf
7
- data.tar.gz: ada9ceee7bcb4c9782378168814b14f1e7767e387d54247a24877ef73e107cb55072b411097d7841afd0e9cda5154cd11eaedc92c3730e4b68d45e296598fcb1
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)
@@ -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
 
@@ -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 effective_unknown_keys
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
 
@@ -3,9 +3,11 @@
3
3
  module AnyVali
4
4
  class ValidationContext
5
5
  attr_reader :definitions
6
+ attr_accessor :inherited_unknown_keys
6
7
 
7
8
  def initialize(definitions: {})
8
9
  @definitions = definitions
10
+ @inherited_unknown_keys = nil
9
11
  end
10
12
  end
11
13
  end
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.6
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-15 00:00:00.000000000 Z
11
+ date: 2026-06-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: minitest