safe_config 3.0.0-b2

  • Readme
  • Changelog
  • Installing
  • 88

safe_config #

A library to add type and name safety to YAML configuration files.

Basic Usage #

safe_config is simple - it maps YAML files to Dart objects using the keys as property names. This mapping ensures that the types of your YAML values are checked at runtime and that you haven't typo'ed any YAML key names.

Consider a case where you want to configure the port and the Server header of your application. You define a subclass of Configuration with those properties:

class ApplicationConfiguration extends Configuration {
 	ApplicationConfiguration(String fileName) : 
	int port;
	String serverHeader;

Your YAML file should contain those two, case-sensitive keys:

port: 8000
serverHeader: booyah/1

To read your configuration file:

var config = new ApplicationConfiguration("config.yaml");
print("${config.port}"); // -> 8000
print("${config.serverHeader}"); // -> "booyah/1"

If port is not an int or is missing, you will get an exception. If serverHeader is not a String or missing, you will get an exception.

Useful Usage #

You may mark properties in Configurations as optional.

class ApplicationConfiguration extends Configuration {
 	ApplicationConfiguration(String fileName) : 
	int port;
	String serverHeader;

If serverHeader is omitted from your YAML when read, its value will be null and no exception is thrown.

There are two built-in Configuration, DatabaseConfiguration and APIConfiguration. These contain typical properties for common configuration values.

You may nest Configuration as deeply as you wish:

class ApplicationConfiguration extends Configuration {
 	ApplicationConfiguration(String fileName) : 
	int port;
	DatabaseConfiguration userDatabase;

For which the YAML may be:

port: 8000
  databaseName: dartstuff
  port: 5432

You may also use arrays and maps, for which the values can be primitive types or Configuration subclasses.

class ApplicationConfiguration extends Configuration {
 	ApplicationConfiguration(String fileName) : 
	Map<String, DatabaseConfiguration> databases;

The YAML here may be:

    databaseName: dartstuff
    port: 5432
    databaseName: otherstuff
    port: 5432

Then, you may access it as such:

var config = new ApplicationConfig("config.yaml");

var databaseOne = config.databases["db1"];
await database.connect(, 

A configuration may have multiple YAML representations. For example, a DatabaseConfiguration can be represented as a Map<String, dynamic> of each component (username, host, etc.). It may also be represented as a connection string, e.g. "postgres://user:password@host:port/database". You may allow this behavior by overriding decode in a subclass of Configuration:

class AuthorityConfiguration extends Configuration {
  String username;
  String password;

  void decode(dynamic anyValue) {
    if (anyValue is! String) {
      throw new ConfigurationException("Expected a String for AuthorityConfiguration.";

    username = anyValue.split(":").first;
    password = anyValue.split(":").last;

This configuration could be read in either of these two scenarios:

    username: "Bob"
    password: "Fred"

// or

authority: "Bob:Fred"

Configurations may also be redirected to use environment variables. To reference an environment variable in a configuration file, use the '$VARIABLE' syntax as a value:

port: $PORT

When read, this configuration file would replace '$PORT' with the environment variable named 'PORT'.

See the tests for more examples.

Features and bugs #

Please file feature requests and bugs at the issue tracker.

Changelog #

3.0.0-b1 #

  • Improves error messaging at runtime to better identify issues with a configuration file
  • Adds 'package:runtime' support to enable AOT compilation using this package.

2.0.2 #

  • Fixes issue when a database connection Uri has percent encoded values for username/password segments.

2.0.1 #

  • Better error messaging for incorrect types
  • Allow 'bool' data type, fix regression in 2.0.0
  • Allow default values, fix regression in 2.0.0

2.0.0 #

  • Dart 2.0 compatability.
  • Rename ConfigurationItem -> Configuration.
  • Rename DatabaseConnectionConfiguration -> DatabaseConfiguration.

1.2.2 #

  • Fixes issue where environment variables could not be decoded as ConfigurationItems

1.2.1 #

  • Throws exception when parsing if environment variable does not exist and is required.

1.2.0 #

  • Allow ConfigurationItem to validate their values by overriding validate (Thanks to Denis Albuquerque, @zidenis)

1.1.3 #

  • Ignore private variables declared in ConfigurationItem subclasses

1.1.2 #

  • Ignore static variables declared in ConfigurationItem subclasses

1.1.1 #

  • Throw exception if an unexpected key is found when reading configuration.

1.1.0 #

  • Enable support for reading environment variables.
  • Allow decoders for ConfigurationItem subclasses that may have multiple representations, e.g. a DatabaseConnectionConfiguration from a database connection string.

1.0.4 #

  • Fix issue where nested ConfigurationItems marked as optional would fail to parse.

1.0.3 #

  • Add ConfigurationItem.fromMap to pass a Map as the source for a ConfigurationItem.

1.0.2 #

  • Add library level documentation.

1.0.1 #

  • Add documentation generation.
  • Update readme.

1.0.0 #

  • Initial version.

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:

  safe_config: ^3.0.0-b2

2. Install it

You can install packages from the command line:

with pub:

$ pub get

Alternatively, your editor might support pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:

import 'package:safe_config/safe_config.dart';
Describes how popular the package is relative to other packages. [more]
Code health derived from static analysis. [more]
Reflects how tidy and up-to-date the package is. [more]
Weighted score of the above. [more]
Learn more about scoring.

We analyzed this package on May 14, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.8.1
  • pana: 0.13.8-dev

Health suggestions

Fix lib/src/configuration.dart. (-0.50 points)

Analysis of lib/src/configuration.dart reported 1 hint:

line 93 col 32: 'CastError' is deprecated and shouldn't be used. Use TypeError instead.

Format lib/safe_config.dart.

Run dartfmt to format lib/safe_config.dart.

Format lib/src/intermediate_exception.dart.

Run dartfmt to format lib/src/intermediate_exception.dart.

Format lib/src/mirror_property.dart.

Run dartfmt to format lib/src/mirror_property.dart.

Format lib/src/runtime.dart.

Run dartfmt to format lib/src/runtime.dart.

Maintenance suggestions

Maintain an example. (-10 points)

Create a short demo in the example/ directory to show how to use this package.

Common filename patterns include main.dart, example.dart, and safe_config.dart. Packages with multiple examples should provide example/

For more information see the pub package layout conventions.

Package is pre-release. (-5 points)

Pre-release versions should be used with caution; their API can change in breaking ways.

Homepage URL is insecure. (-5 points)

Update the homepage field and use a secure (https) URL.

The package description is too short. (-1 points)

Add more detail to the description field of pubspec.yaml. Use 60 to 180 characters to describe the package, what it does, and its target use case.


Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0 <3.0.0
runtime ^1.0.0-5 1.0.0-9
yaml >=2.1.8 <3.0.0 2.2.1
Transitive dependencies
_fe_analyzer_shared 3.0.0
analyzer 0.39.8
args 1.6.0
async 2.4.1
charcode 1.1.3
checked_yaml 1.0.2
collection 1.14.12
convert 2.1.1
crypto 2.1.4
csslib 0.16.1
glob 1.2.0
html 0.14.0+3
isolate_executor 2.0.2+3
js 0.6.1+1
json_annotation 3.0.1
meta 1.1.8
node_interop 1.1.1
node_io 1.1.1
package_config 1.9.3
path 1.7.0
pedantic 1.9.0
pub_semver 1.4.4
pubspec_parse 0.1.5
source_span 1.7.0
string_scanner 1.0.5
term_glyph 1.1.0
typed_data 1.1.6
watcher 0.9.7+15
Dev dependencies
test ^1.3.0