philiprehberger-env_validator 0.2.9 → 0.3.0

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: b7880d4b817b1eb742182c5967ba6881626978a31303900cb1e7428674beab40
4
- data.tar.gz: b20afee2c7ca8ebe026e1c10bf52126466e9d2bd2488ac2bd61c295c90e582d0
3
+ metadata.gz: 5f5eb2c42fbde35384ba3f7b6dc6c15ae3c73f7ef9a51da44f743f4f5a1a759c
4
+ data.tar.gz: f77bc75aa9b0eef7de0f5601fc294733812b9e478667a539126498d94e4546c1
5
5
  SHA512:
6
- metadata.gz: 44de6ab04e8c33df4a7692c96feef5da5073e72659bf1068129231e5707b60423779d180eab81c7993d7d0dcdc3e36f952ef05a4cd6ce9b5546d9c02b1a54fca
7
- data.tar.gz: db17df01d68d556079242e450ddb28cd3cfe96c737fb9cfbc1891fd2ddcd77bfc83de16da869c5cb683bf072f56adb10b8c20a05ea1da9e6b01daffa5d456bbd
6
+ metadata.gz: 3547c88cb78b081d3799044704cbc4dc7ee0269b9a7b700284b0e48868e21a6a132e7ff03f6e9c2baea4ca98124cc9682ebbc5d2c08285e2c502d455791e372c
7
+ data.tar.gz: ce9ab8674cdf6409519b73f4126b08e98c95f58f5830c9567a2c7c1c7488f4f59b39943e6185b1737bb59e0e5ec405ba5860978a2b4be1e59ce90736616508fd
data/CHANGELOG.md CHANGED
@@ -7,6 +7,18 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7
7
 
8
8
  ## [Unreleased]
9
9
 
10
+ ## [0.3.0] - 2026-04-10
11
+
12
+ ### Added
13
+ - `prefix:` option for `EnvValidator.define` to group related variables under a shared prefix
14
+
15
+ ### Changed
16
+ - Trim `.rubocop.yml` to minimal config per guide standards
17
+ - Update issue templates to match guide (add gem version field, alternatives field)
18
+
19
+ ### Fixed
20
+ - Merge duplicate `[0.2.2]` CHANGELOG entries into single entry
21
+
10
22
  ## [0.2.9] - 2026-04-08
11
23
 
12
24
  ### Changed
@@ -50,8 +62,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
50
62
  ### Changed
51
63
  - Update rubocop configuration for Windows compatibility
52
64
 
53
- ## [0.2.2] - 2026-03-21
54
-
55
65
  ### Fixed
56
66
  - Standardize Installation section in README
57
67
 
data/README.md CHANGED
@@ -82,6 +82,25 @@ config.key?(:PORT) # => true
82
82
  config.slice(:PORT) # => { "PORT" => 3000 }
83
83
  ```
84
84
 
85
+ ### Prefix
86
+
87
+ Group related variables with a shared prefix:
88
+
89
+ ```ruby
90
+ env = { "REDIS_HOST" => "localhost", "REDIS_PORT" => "6379", "REDIS_PASSWORD" => "secret" }
91
+
92
+ config = Philiprehberger::EnvValidator.define(env: env, prefix: "REDIS_") do
93
+ string :HOST, required: true
94
+ integer :PORT, default: 6379
95
+ string :PASSWORD
96
+ end
97
+
98
+ config[:HOST] # => "localhost"
99
+ config[:PORT] # => 6379
100
+ ```
101
+
102
+ The prefix is prepended when looking up each variable in the environment. Result keys use the short name (without prefix). Error messages include the full prefixed name.
103
+
85
104
  ### Validation Errors
86
105
 
87
106
  ```ruby
@@ -107,7 +126,7 @@ end
107
126
 
108
127
  | Method / Class | Description |
109
128
  |----------------|-------------|
110
- | `EnvValidator.define(env:, &block)` | Define schema and validate |
129
+ | `EnvValidator.define(env:, prefix:, &block)` | Define schema and validate |
111
130
  | `Result#fetch(name)` / `Result#[name]` | Get a validated value |
112
131
  | `Result#keys` | List all defined variable names |
113
132
  | `Result#key?(name)` | Check if a variable was defined |
@@ -9,9 +9,11 @@ module Philiprehberger
9
9
 
10
10
  # @param schema [Schema] the schema to validate against
11
11
  # @param env [Hash] the environment hash (default: ENV)
12
- def initialize(schema, env: ENV)
12
+ # @param prefix [String, nil] optional prefix prepended to variable names during lookup
13
+ def initialize(schema, env: ENV, prefix: nil)
13
14
  @schema = schema
14
15
  @env = env
16
+ @prefix = prefix
15
17
  end
16
18
 
17
19
  # Validate and return a Result.
@@ -36,20 +38,21 @@ module Philiprehberger
36
38
  private
37
39
 
38
40
  def resolve(name, definition)
39
- raw = @env[name]
41
+ env_key = @prefix ? "#{@prefix}#{name}" : name
42
+ raw = @env[env_key]
40
43
 
41
- return resolve_missing(name, definition) if raw.nil? || raw.empty?
44
+ return resolve_missing(env_key, definition) if raw.nil? || raw.empty?
42
45
 
43
- value = cast(raw, definition[:type], name)
44
- validate_choices(name, value, definition[:choices])
46
+ value = cast(raw, definition[:type], env_key)
47
+ validate_choices(env_key, value, definition[:choices])
45
48
  [value, nil]
46
49
  rescue CastError => e
47
50
  [nil, e.message]
48
51
  end
49
52
 
50
- def resolve_missing(name, definition)
53
+ def resolve_missing(env_key, definition)
51
54
  if definition[:required] && definition[:default].nil?
52
- [nil, "Missing required variable: #{name}"]
55
+ [nil, "Missing required variable: #{env_key}"]
53
56
  else
54
57
  [definition[:default], nil]
55
58
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Philiprehberger
4
4
  module EnvValidator
5
- VERSION = '0.2.9'
5
+ VERSION = '0.3.0'
6
6
  end
7
7
  end
@@ -18,14 +18,15 @@ module Philiprehberger
18
18
  # Define and validate environment variables.
19
19
  #
20
20
  # @param env [Hash] the environment hash (default: ENV)
21
+ # @param prefix [String, nil] optional prefix prepended to variable names during lookup
21
22
  # @yield [schema] configure the schema
22
23
  # @yieldparam schema [Schema]
23
24
  # @return [Result] validated values
24
25
  # @raise [ValidationError] if validation fails
25
- def self.define(env: ENV, &block)
26
+ def self.define(env: ENV, prefix: nil, &block)
26
27
  schema = Schema.new
27
28
  schema.instance_eval(&block)
28
- Validator.new(schema, env: env).validate!
29
+ Validator.new(schema, env: env, prefix: prefix).validate!
29
30
  end
30
31
  end
31
32
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: philiprehberger-env_validator
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.9
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Philip Rehberger
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2026-04-09 00:00:00.000000000 Z
11
+ date: 2026-04-11 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Define environment variable schemas with type casting, required/optional
14
14
  flags, and defaults. Validates at boot time and provides typed accessors.