RedactionConfig.fromJson constructor

RedactionConfig.fromJson(
  1. Map? json
)

Restores a configuration written by toJson; tolerates null values and unknown/invalid keys by ignoring them and falling back to defaults.

Implementation

factory RedactionConfig.fromJson(Map<dynamic, dynamic>? json) {
  if (json == null) return const RedactionConfig();
  final toggles = <RedactionLayer, bool>{};
  final rawLayers = json['layers'];
  if (rawLayers is Map) {
    rawLayers.forEach((key, value) {
      if (key is! String || value is! bool) return;
      for (final layer in RedactionLayer.values) {
        if (layer.name == key) toggles[layer] = value;
      }
    });
  }
  final allowlist = <RegExp>[];
  final rawAllowlist = json['allowlist'];
  if (rawAllowlist is List) {
    for (final entry in rawAllowlist) {
      if (entry is String) allowlist.add(RegExp(entry));
    }
  }
  return RedactionConfig(
    enabled: json['enabled'] is bool ? json['enabled'] as bool : true,
    blockMode: json['blockMode'] is bool ? json['blockMode'] as bool : false,
    layerToggles: toggles,
    allowlistRegexes: allowlist,
    minEntropy: json['minEntropy'] is num
        ? (json['minEntropy'] as num).toDouble()
        : 4.5,
    minLength: json['minLength'] is num
        ? (json['minLength'] as num).toInt()
        : 32,
    toolAllow: _stringSet(json['toolAllow']),
    toolDeny: _stringSet(json['toolDeny']),
  );
}