philiprehberger-env_validator 0.2.7 → 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 +4 -4
- data/CHANGELOG.md +22 -2
- data/README.md +20 -1
- data/lib/philiprehberger/env_validator/validator.rb +10 -7
- data/lib/philiprehberger/env_validator/version.rb +1 -1
- data/lib/philiprehberger/env_validator.rb +3 -2
- metadata +5 -5
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 5f5eb2c42fbde35384ba3f7b6dc6c15ae3c73f7ef9a51da44f743f4f5a1a759c
|
|
4
|
+
data.tar.gz: f77bc75aa9b0eef7de0f5601fc294733812b9e478667a539126498d94e4546c1
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 3547c88cb78b081d3799044704cbc4dc7ee0269b9a7b700284b0e48868e21a6a132e7ff03f6e9c2baea4ca98124cc9682ebbc5d2c08285e2c502d455791e372c
|
|
7
|
+
data.tar.gz: ce9ab8674cdf6409519b73f4126b08e98c95f58f5830c9567a2c7c1c7488f4f59b39943e6185b1737bb59e0e5ec405ba5860978a2b4be1e59ce90736616508fd
|
data/CHANGELOG.md
CHANGED
|
@@ -7,6 +7,28 @@ 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
|
+
|
|
22
|
+
## [0.2.9] - 2026-04-08
|
|
23
|
+
|
|
24
|
+
### Changed
|
|
25
|
+
- Align gemspec summary with README description.
|
|
26
|
+
|
|
27
|
+
## [0.2.8] - 2026-03-31
|
|
28
|
+
|
|
29
|
+
### Added
|
|
30
|
+
- Add GitHub issue templates, dependabot config, and PR template
|
|
31
|
+
|
|
10
32
|
## [0.2.7] - 2026-03-31
|
|
11
33
|
|
|
12
34
|
### Changed
|
|
@@ -40,8 +62,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
40
62
|
### Changed
|
|
41
63
|
- Update rubocop configuration for Windows compatibility
|
|
42
64
|
|
|
43
|
-
## [0.2.2] - 2026-03-21
|
|
44
|
-
|
|
45
65
|
### Fixed
|
|
46
66
|
- Standardize Installation section in README
|
|
47
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
|
-
|
|
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
|
-
|
|
41
|
+
env_key = @prefix ? "#{@prefix}#{name}" : name
|
|
42
|
+
raw = @env[env_key]
|
|
40
43
|
|
|
41
|
-
return resolve_missing(
|
|
44
|
+
return resolve_missing(env_key, definition) if raw.nil? || raw.empty?
|
|
42
45
|
|
|
43
|
-
value = cast(raw, definition[:type],
|
|
44
|
-
validate_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(
|
|
53
|
+
def resolve_missing(env_key, definition)
|
|
51
54
|
if definition[:required] && definition[:default].nil?
|
|
52
|
-
[nil, "Missing required variable: #{
|
|
55
|
+
[nil, "Missing required variable: #{env_key}"]
|
|
53
56
|
else
|
|
54
57
|
[definition[:default], nil]
|
|
55
58
|
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.
|
|
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-
|
|
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.
|
|
@@ -26,11 +26,11 @@ files:
|
|
|
26
26
|
- lib/philiprehberger/env_validator/schema.rb
|
|
27
27
|
- lib/philiprehberger/env_validator/validator.rb
|
|
28
28
|
- lib/philiprehberger/env_validator/version.rb
|
|
29
|
-
homepage: https://
|
|
29
|
+
homepage: https://philiprehberger.com/open-source-packages/ruby/philiprehberger-env_validator
|
|
30
30
|
licenses:
|
|
31
31
|
- MIT
|
|
32
32
|
metadata:
|
|
33
|
-
homepage_uri: https://
|
|
33
|
+
homepage_uri: https://philiprehberger.com/open-source-packages/ruby/philiprehberger-env_validator
|
|
34
34
|
source_code_uri: https://github.com/philiprehberger/rb-env-validator
|
|
35
35
|
changelog_uri: https://github.com/philiprehberger/rb-env-validator/blob/main/CHANGELOG.md
|
|
36
36
|
bug_tracker_uri: https://github.com/philiprehberger/rb-env-validator/issues
|
|
@@ -53,5 +53,5 @@ requirements: []
|
|
|
53
53
|
rubygems_version: 3.5.22
|
|
54
54
|
signing_key:
|
|
55
55
|
specification_version: 4
|
|
56
|
-
summary: Schema-based environment variable validation with typed accessors
|
|
56
|
+
summary: Schema-based environment variable validation with typed accessors
|
|
57
57
|
test_files: []
|