validate method

Map<String, dynamic> validate(
  1. Map<String, EnvSchema<ErrorReporter>> schema,
  2. Map<String, dynamic> data
)

Parses environment variables from a string content into a Map.

Takes a content string containing environment variable definitions in the format:

KEY1=value1
KEY2=value2

Returns a Map<String, dynamic> where each key is the environment variable name and the value is the parsed string value.

Example:

final content = '''
  PORT=8080
  HOST=localhost
''';

final schema = {
  'PORT': env.number().integer(),
  'HOST': env.string(),
};

final validatedEnv = env.validate(schema, envVars);
// Returns: {'PORT': 8080, 'HOST': 'localhost'}

Implementation

Map<String, dynamic> validate(
  Map<String, EnvSchema> schema,
  Map<String, dynamic> data,
) {
  final Map<String, dynamic> resultMap = {};
  final reporter = errorReporter();
  final validatorContext = ValidatorContext(reporter, data);
  final property = Property('', data);

  for (final element in schema.entries) {
    property.name = element.key;
    property.value =
        data.containsKey(element.key) ? data[element.key] : MissingValue();

    element.value.parse(validatorContext, property);
    resultMap[property.name] = property.value;
  }

  if (reporter.hasError) {
    throw reporter.createError({'errors': reporter.errors});
  }

  reporter.clear();

  return resultMap;
}