env_guard 1.3.1

SDKdartflutter
Platformandroidioswindowslinuxmacos

A simple, robust, and typed environment validation library for Dart/Flutter, designed to simplify and secure environment in applications

⚙️ Env Guard

Env Guard is a robust, typed validation library for Dart/Flutter, designed to simplify and secure the management of environment variables in your applications.

The library offers an elegant solution for validating and transforming environment data, ensuring that it complies with an expected format before being used.

icons technologies

🛠 Key features

FeatureDescription
✅ Type-Safe ValidationDefine schemas with a fluent API and ensure data integrity
🧱 Rich Set of ValidatorsStrings, numbers, booleans, enums
🔄 Data TransformationTransform values during validation
🚧 Null SafetyFull support for optional properties
📦 Extremely small sizePackage size < 8kb

✨ Simply to use

Consider the following example, where we define a schema for our application's environment variables.

HOST=127.0.0.1
PORT=8080
LOG_LEVEL=debug
URI={HOST}:{PORT}

We can validate these environment variables using the env_guard library in the following way.

enum LogLevel implements Enumerate<String>{
  info('info'),
  error('error'),
  debug('debug');
  
  final String value;
  const LogLevel(this.value);
}

void main() {
  env.define({
    'HOST': env.string(),
    'PORT': env.number(),
    'LOG_LEVEL': env.enumerable(LogLevel.values),
  });

  expect(env.get('HOST'), '127.0.0.1');
  expect(env.get('URI'), '127.0.0.1:8080');
}

💪 Enforce data recovery

You can define your validation constraints from an enumeration to make the names of your variables contractual in addition to your types.

final class Env implements DefineEnvironment {
  static final String host = 'HOST';
  static final String port = 'PORT';
  static final String uri = 'URI';

  @override
  final Map<String, EnvSchema> schema = {
    host: env.string().optional(),
    port: env.number().integer(),
    uri: env.string(),
  };
}
void main() {
  env.defineOf(Env.new);

  expect(env.get(Env.host), '127.0.0.1');
  expect(env.get(Env.port), 8080);
}

🚧 Error handling

When your application starts up and your environment does not meet the requirements defined by the validator, an EnvGuardException is thrown using the following format.

{
  "errors": [
    {
      "message": "The value must be an enum of [info, error, debug]",
      "rule": "enum",
      "key": "LOG_LEVEL"
    }
  ]
}