Skip to content

Commit 81aebb1

Browse files
committed
feat: release version 1.3.0 with environment key exception removal and enhanced documentation
1 parent af5280f commit 81aebb1

File tree

3 files changed

+84
-3
lines changed

3 files changed

+84
-3
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
## 1.3.0
2+
- Remove existing environment key exception for `DART_ENV`
3+
14
## 1.2.0
2-
- Remove existing environment key exception
35
- Add `toJson()` method to convert environment variables to a Map
46

57
## 1.1.0

lib/src/env.dart

Lines changed: 80 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,32 @@ final class Env {
6767
return _envParser.parse(content);
6868
}
6969

70+
/// Parses environment variables from a string content into a Map.
71+
///
72+
/// Takes a [content] string containing environment variable definitions in the format:
73+
/// ```dotenv
74+
/// KEY1=value1
75+
/// KEY2=value2
76+
/// ```
77+
///
78+
/// Returns a [Map<String, dynamic>] where each key is the environment variable name
79+
/// and the value is the parsed string value.
80+
///
81+
/// Example:
82+
/// ```dart
83+
/// final content = '''
84+
/// PORT=8080
85+
/// HOST=localhost
86+
/// ''';
87+
///
88+
/// final schema = {
89+
/// 'PORT': env.number().integer(),
90+
/// 'HOST': env.string(),
91+
/// };
92+
///
93+
/// final validatedEnv = env.validate(schema, envVars);
94+
/// // Returns: {'PORT': 8080, 'HOST': 'localhost'}
95+
/// ```
7096
Map<String, dynamic> validate(
7197
Map<String, EnvSchema> schema,
7298
Map<String, dynamic> data,
@@ -94,6 +120,33 @@ final class Env {
94120
return resultMap;
95121
}
96122

123+
/// Validates environment variables against a schema.
124+
///
125+
/// Takes a [schema] defining the expected structure and validation rules for environment variables,
126+
/// and [data] containing the actual environment variable values to validate.
127+
///
128+
/// Returns a [Map] containing the validated environment variables. The returned map will have
129+
/// the same keys as the schema, with values converted and validated according to the schema rules.
130+
///
131+
/// Throws an [EnvGuardException] if any validation errors occur. The exception will contain details
132+
/// about which validations failed.
133+
///
134+
/// Example:
135+
/// ```dart
136+
/// final schema = {
137+
/// 'PORT': env.number().integer(),
138+
/// 'HOST': env.string(),
139+
/// };
140+
///
141+
/// final data = {
142+
/// 'PORT': '8080',
143+
/// 'HOST': 'localhost'
144+
/// };
145+
///
146+
/// final validated = env.validate(schema, data);
147+
/// // Returns: {'PORT': 8080, 'HOST': 'localhost'}
148+
/// ```
149+
97150
void define(
98151
Map<String, EnvSchema> schema, {
99152
Directory? root,
@@ -123,7 +176,8 @@ final class Env {
123176
if (current != null) {
124177
final values = _envParser.parse(current.content);
125178
for (final element in values.entries) {
126-
if (_environments.containsKey(element.key)) {
179+
if (element.key != 'DART_ENV' &&
180+
_environments.containsKey(element.key)) {
127181
throw Exception('Environment variable ${element.key} already exists');
128182
}
129183
}
@@ -138,6 +192,31 @@ final class Env {
138192
}
139193
}
140194

195+
/// Defines environment variables using a class that implements [DefineEnvironment].
196+
///
197+
/// This method provides a more structured way to define environment variables using a class-based approach.
198+
/// The class must implement [DefineEnvironment] interface which requires a [schema] property.
199+
///
200+
/// Parameters:
201+
/// - [source]: A function that returns an instance of type [T] which implements [DefineEnvironment]
202+
/// - [root]: Optional directory where the .env files are located. Defaults to current directory if not specified
203+
/// - [includeDartEnv]: Whether to include Dart's environment variables. Defaults to true
204+
///
205+
/// Example:
206+
/// ```dart
207+
/// class AppEnv implements DefineEnvironment {
208+
/// static final String host = 'HOST';
209+
/// static final String port = 'PORT';
210+
///
211+
/// @override
212+
/// final Map<String, EnvSchema> schema = {
213+
/// host: env.string(),
214+
/// port: env.number().integer(),
215+
/// };
216+
/// }
217+
///
218+
/// env.defineOf(AppEnv.new);
219+
/// ```
141220
void defineOf<T extends DefineEnvironment>(
142221
T Function() source, {
143222
Directory? root,

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: env_guard
22
description: A simple, robust, and typed environment validation library for Dart/Flutter, designed to simplify and secure environment in applications
3-
version: 1.2.0
3+
version: 1.3.0
44
repository: https://github.com/LeadcodeDev/env_guard
55

66
environment:

0 commit comments

Comments
 (0)