flutter_manifest.dart 22.4 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5 6
// @dart = 2.8

7
import 'package:meta/meta.dart';
8
import 'package:pub_semver/pub_semver.dart';
9 10
import 'package:yaml/yaml.dart';

11
import 'base/deferred_component.dart';
12
import 'base/file_system.dart';
13
import 'base/logger.dart';
14
import 'base/user_messages.dart';
15
import 'base/utils.dart';
16
import 'plugins.dart';
17

18 19 20 21
const Set<String> _kValidPluginPlatforms = <String>{
  'android', 'ios', 'web', 'windows', 'linux', 'macos'
};

22
/// A wrapper around the `flutter` section in the `pubspec.yaml` file.
23
class FlutterManifest {
24
  FlutterManifest._(this._logger);
25

26
  /// Returns an empty manifest.
27 28
  factory FlutterManifest.empty({ @required Logger logger }) {
    final FlutterManifest manifest = FlutterManifest._(logger);
29 30 31 32 33
    manifest._descriptor = const <String, dynamic>{};
    manifest._flutterDescriptor = const <String, dynamic>{};
    return manifest;
  }

34
  /// Returns null on invalid manifest. Returns empty manifest on missing file.
35 36 37 38 39 40
  static FlutterManifest createFromPath(String path, {
    @required FileSystem fileSystem,
    @required Logger logger,
  }) {
    if (path == null || !fileSystem.isFileSync(path)) {
      return _createFromYaml(null, logger);
41
    }
42 43
    final String manifest = fileSystem.file(path).readAsStringSync();
    return FlutterManifest.createFromString(manifest, logger: logger);
44
  }
45

46
  /// Returns null on missing or invalid manifest.
47
  @visibleForTesting
48
  static FlutterManifest createFromString(String manifest, { @required Logger logger }) {
49
    return _createFromYaml(manifest != null ? loadYaml(manifest) : null, logger);
50 51
  }

52
  static FlutterManifest _createFromYaml(dynamic yamlDocument, Logger logger) {
53
    if (yamlDocument != null && !_validate(yamlDocument, logger)) {
54
      return null;
55
    }
56

57 58
    final FlutterManifest pubspec = FlutterManifest._(logger);
    final Map<dynamic, dynamic> yamlMap = yamlDocument as YamlMap;
59 60 61 62 63 64
    if (yamlMap != null) {
      pubspec._descriptor = yamlMap.cast<String, dynamic>();
    } else {
      pubspec._descriptor = <String, dynamic>{};
    }

65
    final Map<dynamic, dynamic> flutterMap = pubspec._descriptor['flutter'] as Map<dynamic, dynamic>;
66 67 68 69 70 71
    if (flutterMap != null) {
      pubspec._flutterDescriptor = flutterMap.cast<String, dynamic>();
    } else {
      pubspec._flutterDescriptor = <String, dynamic>{};
    }

72 73 74
    return pubspec;
  }

75 76
  final Logger _logger;

77 78 79 80 81 82
  /// A map representation of the entire `pubspec.yaml` file.
  Map<String, dynamic> _descriptor;

  /// A map representation of the `flutter` section in the `pubspec.yaml` file.
  Map<String, dynamic> _flutterDescriptor;

83
  /// True if the `pubspec.yaml` file does not exist.
84 85
  bool get isEmpty => _descriptor.isEmpty;

86
  /// The string value of the top-level `name` property in the `pubspec.yaml` file.
87
  String get appName => _descriptor['name'] as String ?? '';
88

89 90 91 92 93 94 95
  /// Contains the name of the dependencies.
  /// These are the keys specified in the `dependency` map.
  Set<String> get dependencies {
    final YamlMap dependencies = _descriptor['dependencies'] as YamlMap;
    return dependencies != null ? <String>{...dependencies.keys.cast<String>()} : <String>{};
  }

96 97 98
  // Flag to avoid printing multiple invalid version messages.
  bool _hasShowInvalidVersionMsg = false;

99 100 101
  /// The version String from the `pubspec.yaml` file.
  /// Can be null if it isn't set or has a wrong format.
  String get appVersion {
102 103 104 105 106 107 108 109 110 111
    final String verStr = _descriptor['version']?.toString();
    if (verStr == null) {
      return null;
    }

    Version version;
    try {
      version = Version.parse(verStr);
    } on Exception {
      if (!_hasShowInvalidVersionMsg) {
112
        _logger.printStatus(userMessages.invalidVersionSettingHintMessage(verStr), emphasis: true);
113 114 115
        _hasShowInvalidVersionMsg = true;
      }
    }
116
    return version?.toString();
117 118 119 120 121
  }

  /// The build version name from the `pubspec.yaml` file.
  /// Can be null if version isn't set or has a wrong format.
  String get buildName {
122
    if (appVersion != null && appVersion.contains('+')) {
123
      return appVersion.split('+')?.elementAt(0);
124 125
    }
    return appVersion;
126 127 128 129
  }

  /// The build version number from the `pubspec.yaml` file.
  /// Can be null if version isn't set or has a wrong format.
130
  String get buildNumber {
131 132
    if (appVersion != null && appVersion.contains('+')) {
      final String value = appVersion.split('+')?.elementAt(1);
133
      return value;
134 135 136 137 138
    } else {
      return null;
    }
  }

139
  bool get usesMaterialDesign {
140
    return _flutterDescriptor['uses-material-design'] as bool ?? false;
141 142
  }

143 144 145 146
  /// True if this Flutter module should use AndroidX dependencies.
  ///
  /// If false the deprecated Android Support library will be used.
  bool get usesAndroidX {
147 148 149 150
    if (_flutterDescriptor.containsKey('module')) {
      return _flutterDescriptor['module']['androidX'] as bool;
    }
    return false;
151 152
  }

153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
  /// Any additional license files listed under the `flutter` key.
  ///
  /// This is expected to be a list of file paths that should be treated as
  /// relative to the pubspec in this directory.
  ///
  /// For example:
  ///
  /// ```yaml
  /// flutter:
  ///   licenses:
  ///     - assets/foo_license.txt
  /// ```
  List<String> get additionalLicenses => _flutterDescriptor.containsKey('licenses')
    ? (_flutterDescriptor['licenses'] as YamlList).map((dynamic element) => element.toString()).toList()
    : <String>[];

169
  /// True if this manifest declares a Flutter module project.
170
  ///
171 172 173
  /// A Flutter project is considered a module when it has a `module:`
  /// descriptor. A Flutter module project supports integration into an
  /// existing host app, and has managed platform host code.
174
  ///
175 176
  /// Such a project can be created using `flutter create -t module`.
  bool get isModule => _flutterDescriptor.containsKey('module');
177 178 179 180 181 182 183 184 185 186 187 188

  /// True if this manifest declares a Flutter plugin project.
  ///
  /// A Flutter project is considered a plugin when it has a `plugin:`
  /// descriptor. A Flutter plugin project wraps custom Android and/or
  /// iOS code in a Dart interface for consumption by other Flutter app
  /// projects.
  ///
  /// Such a project can be created using `flutter create -t plugin`.
  bool get isPlugin => _flutterDescriptor.containsKey('plugin');

  /// Returns the Android package declared by this manifest in its
189
  /// module or plugin descriptor. Returns null, if there is no
190 191
  /// such declaration.
  String get androidPackage {
192
    if (isModule) {
193
      return _flutterDescriptor['module']['androidPackage'] as String;
194
    }
195 196 197 198
    if (supportedPlatforms == null) {
      // Pre-multi-platform plugin format
      if (isPlugin) {
        final YamlMap plugin = _flutterDescriptor['plugin'] as YamlMap;
199
        return plugin['androidPackage'] as String;
200
      }
201 202 203 204
      return null;
    }
    if (supportedPlatforms.containsKey('android')) {
       return supportedPlatforms['android']['package'] as String;
205
    }
206 207
    return null;
  }
208

209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
  /// Returns the deferred components configuration if declared. Returns
  /// null if no deferred components are declared.
  List<DeferredComponent> get deferredComponents => _deferredComponents ??= computeDeferredComponents();
  List<DeferredComponent> _deferredComponents;
  List<DeferredComponent> computeDeferredComponents() {
    if (!_flutterDescriptor.containsKey('deferred-components')) {
      return null;
    }
    final List<DeferredComponent> components = <DeferredComponent>[];
    if (_flutterDescriptor['deferred-components'] == null) {
      return components;
    }
    for (final dynamic componentData in _flutterDescriptor['deferred-components']) {
      final YamlMap component = componentData as YamlMap;
      List<Uri> assetsUri = <Uri>[];
      final List<dynamic> assets = component['assets'] as List<dynamic>;
      if (assets == null) {
        assetsUri = const <Uri>[];
      } else {
        for (final Object asset in assets) {
          if (asset is! String || asset == null || asset == '') {
            _logger.printError('Deferred component asset manifest contains a null or empty uri.');
            continue;
          }
          final String stringAsset = asset as String;
          try {
            assetsUri.add(Uri.parse(stringAsset));
          } on FormatException {
            _logger.printError('Asset manifest contains invalid uri: $asset.');
          }
        }
      }
      components.add(
        DeferredComponent(
          name: component['name'] as String,
          libraries: component['libraries'] == null ?
              <String>[] : component['libraries'].cast<String>() as List<String>,
          assets: assetsUri,
        )
      );
    }
    return components;
  }

253
  /// Returns the iOS bundle identifier declared by this manifest in its
254
  /// module descriptor. Returns null if there is no such declaration.
255
  String get iosBundleIdentifier {
256
    if (isModule) {
257
      return _flutterDescriptor['module']['iosBundleIdentifier'] as String;
258
    }
259 260 261
    return null;
  }

262 263 264 265 266 267 268 269 270 271 272 273 274 275
  /// Gets the supported platforms. This only supports the new `platforms` format.
  ///
  /// If the plugin uses the legacy pubspec format, this method returns null.
  Map<String, dynamic> get supportedPlatforms {
    if (isPlugin) {
      final YamlMap plugin = _flutterDescriptor['plugin'] as YamlMap;
      if (plugin.containsKey('platforms')) {
        final YamlMap platformsMap = plugin['platforms'] as YamlMap;
        return platformsMap.value.cast<String, dynamic>();
      }
    }
    return null;
  }

276 277 278 279 280 281 282 283 284 285 286 287 288 289
  /// Like [supportedPlatforms], but only returns the valid platforms that are supported in flutter plugins.
  Map<String, dynamic> get validSupportedPlatforms {
    final Map<String, dynamic> allPlatforms = supportedPlatforms;
    if (allPlatforms == null) {
      return null;
    }
    final Map<String, dynamic> platforms = <String, dynamic>{}..addAll(supportedPlatforms);
    platforms.removeWhere((String key, dynamic _) => !_kValidPluginPlatforms.contains(key));
    if (platforms.isEmpty) {
      return null;
    }
    return platforms;
  }

290
  List<Map<String, dynamic>> get fontsDescriptor {
291 292 293 294
    return fonts.map((Font font) => font.descriptor).toList();
  }

  List<Map<String, dynamic>> get _rawFontsDescriptor {
295
    final List<dynamic> fontList = _flutterDescriptor['fonts'] as List<dynamic>;
296 297 298
    return fontList == null
        ? const <Map<String, dynamic>>[]
        : fontList.map<Map<String, dynamic>>(castStringKeyedMap).toList();
299 300
  }

301 302 303
  List<Uri> get assets => _assets ??= _computeAssets();
  List<Uri> _assets;
  List<Uri> _computeAssets() {
304
    final List<dynamic> assets = _flutterDescriptor['assets'] as List<dynamic>;
305 306 307
    if (assets == null) {
      return const <Uri>[];
    }
308
    final List<Uri> results = <Uri>[];
309
    for (final Object asset in assets) {
310
      if (asset is! String || asset == null || asset == '') {
311
        _logger.printError('Asset manifest contains a null or empty uri.');
312 313
        continue;
      }
314
      final String stringAsset = asset as String;
315
      try {
316
        results.add(Uri(pathSegments: stringAsset.split('/')));
317
      } on FormatException {
318
        _logger.printError('Asset manifest contains invalid uri: $asset.');
319 320 321
      }
    }
    return results;
322 323 324 325 326 327 328 329 330 331
  }

  List<Font> _fonts;

  List<Font> get fonts {
    _fonts ??= _extractFonts();
    return _fonts;
  }

  List<Font> _extractFonts() {
332
    if (!_flutterDescriptor.containsKey('fonts')) {
333
      return <Font>[];
334
    }
335 336

    final List<Font> fonts = <Font>[];
337
    for (final Map<String, dynamic> fontFamily in _rawFontsDescriptor) {
338 339
      final YamlList fontFiles = fontFamily['fonts'] as YamlList;
      final String familyName = fontFamily['family'] as String;
340
      if (familyName == null) {
341
        _logger.printError('Warning: Missing family name for font.', emphasis: true);
342 343 344
        continue;
      }
      if (fontFiles == null) {
345
        _logger.printError('Warning: No fonts specified for font $familyName', emphasis: true);
346 347 348 349
        continue;
      }

      final List<FontAsset> fontAssets = <FontAsset>[];
350
      for (final Map<dynamic, dynamic> fontFile in fontFiles.cast<Map<dynamic, dynamic>>()) {
351
        final String asset = fontFile['asset'] as String;
352
        if (asset == null) {
353
          _logger.printError('Warning: Missing asset in fonts for $familyName', emphasis: true);
354 355 356
          continue;
        }

357
        fontAssets.add(FontAsset(
358
          Uri.parse(asset),
359 360
          weight: fontFile['weight'] as int,
          style: fontFile['style'] as String,
361 362
        ));
      }
363
      if (fontAssets.isNotEmpty) {
364
        fonts.add(Font(fontFamily['family'] as String, fontAssets));
365
      }
366 367 368
    }
    return fonts;
  }
369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388

  /// Whether a synthetic flutter_gen package should be generated.
  ///
  /// This can be provided to the [Pub] interface to inject a new entry
  /// into the package_config.json file which points to `.dart_tool/flutter_gen`.
  ///
  /// This allows generated source code to be imported using a package
  /// alias.
  bool get generateSyntheticPackage => _generateSyntheticPackage ??= _computeGenerateSyntheticPackage();
  bool _generateSyntheticPackage;
  bool _computeGenerateSyntheticPackage() {
    if (!_flutterDescriptor.containsKey('generate')) {
      return false;
    }
    final Object value = _flutterDescriptor['generate'];
    if (value is! bool) {
      return false;
    }
    return value as bool;
  }
389 390 391
}

class Font {
392 393 394 395
  Font(this.familyName, this.fontAssets)
    : assert(familyName != null),
      assert(fontAssets != null),
      assert(fontAssets.isNotEmpty);
396 397 398 399 400 401 402

  final String familyName;
  final List<FontAsset> fontAssets;

  Map<String, dynamic> get descriptor {
    return <String, dynamic>{
      'family': familyName,
403
      'fonts': fontAssets.map<Map<String, dynamic>>((FontAsset a) => a.descriptor).toList(),
404 405 406 407 408 409 410 411
    };
  }

  @override
  String toString() => '$runtimeType(family: $familyName, assets: $fontAssets)';
}

class FontAsset {
412 413
  FontAsset(this.assetUri, {this.weight, this.style})
    : assert(assetUri != null);
414

415
  final Uri assetUri;
416 417 418 419 420
  final int weight;
  final String style;

  Map<String, dynamic> get descriptor {
    final Map<String, dynamic> descriptor = <String, dynamic>{};
421
    if (weight != null) {
422
      descriptor['weight'] = weight;
423
    }
424

425
    if (style != null) {
426
      descriptor['style'] = style;
427
    }
428

429
    descriptor['asset'] = assetUri.path;
430 431 432 433
    return descriptor;
  }

  @override
434
  String toString() => '$runtimeType(asset: ${assetUri.path}, weight; $weight, style: $style)';
435 436
}

437

438
bool _validate(dynamic manifest, Logger logger) {
439
  final List<String> errors = <String>[];
440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464
  if (manifest is! YamlMap) {
    errors.add('Expected YAML map');
  } else {
    for (final MapEntry<dynamic, dynamic> kvp in (manifest as YamlMap).entries) {
      if (kvp.key is! String) {
        errors.add('Expected YAML key to be a string, but got ${kvp.key}.');
        continue;
      }
      switch (kvp.key as String) {
        case 'name':
          if (kvp.value is! String) {
            errors.add('Expected "${kvp.key}" to be a string, but got ${kvp.value}.');
          }
          break;
        case 'flutter':
          if (kvp.value == null) {
            continue;
          }
          if (kvp.value is! YamlMap) {
            errors.add('Expected "${kvp.key}" section to be an object or null, but got ${kvp.value}.');
          } else {
            _validateFlutter(kvp.value as YamlMap, errors);
          }
          break;
        default:
465
        // additionalProperties are allowed.
466 467
          break;
      }
468 469
    }
  }
470

471
  if (errors.isNotEmpty) {
472 473
    logger.printStatus('Error detected in pubspec.yaml:', emphasis: true);
    logger.printError(errors.join('\n'));
474 475
    return false;
  }
476 477 478 479 480 481 482 483 484 485

  return true;
}

void _validateFlutter(YamlMap yaml, List<String> errors) {
  if (yaml == null || yaml.entries == null) {
    return;
  }
  for (final MapEntry<dynamic, dynamic> kvp in yaml.entries) {
    if (kvp.key is! String) {
486
      errors.add('Expected YAML key to be a string, but got ${kvp.key} (${kvp.value.runtimeType}).');
487 488
      continue;
    }
489
    switch (kvp.key as String) {
490 491 492 493 494 495 496 497 498 499 500 501 502
      case 'uses-material-design':
        if (kvp.value is! bool) {
          errors.add('Expected "${kvp.key}" to be a bool, but got ${kvp.value} (${kvp.value.runtimeType}).');
        }
        break;
      case 'assets':
        if (kvp.value is! YamlList || kvp.value[0] is! String) {
          errors.add('Expected "${kvp.key}" to be a list, but got ${kvp.value} (${kvp.value.runtimeType}).');
        }
        break;
      case 'fonts':
        if (kvp.value is! YamlList || kvp.value[0] is! YamlMap) {
          errors.add('Expected "${kvp.key}" to be a list, but got ${kvp.value} (${kvp.value.runtimeType}).');
503
        } else {
504
          _validateFonts(kvp.value as YamlList, errors);
505 506
        }
        break;
507 508 509
      case 'licenses':
        final dynamic value = kvp.value;
        if (value is YamlList) {
510
          _validateListType<String>(value, errors, '"${kvp.key}"', 'files');
511 512 513 514
        } else {
          errors.add('Expected "${kvp.key}" to be a list of files, but got $value (${value.runtimeType})');
        }
        break;
515 516 517 518 519
      case 'module':
        if (kvp.value is! YamlMap) {
          errors.add('Expected "${kvp.key}" to be an object, but got ${kvp.value} (${kvp.value.runtimeType}).');
        }

520 521 522
        if (kvp.value['androidX'] != null && kvp.value['androidX'] is! bool) {
          errors.add('The "androidX" value must be a bool if set.');
        }
523 524 525 526 527 528 529 530
        if (kvp.value['androidPackage'] != null && kvp.value['androidPackage'] is! String) {
          errors.add('The "androidPackage" value must be a string if set.');
        }
        if (kvp.value['iosBundleIdentifier'] != null && kvp.value['iosBundleIdentifier'] is! String) {
          errors.add('The "iosBundleIdentifier" section must be a string if set.');
        }
        break;
      case 'plugin':
531
        if (kvp.value is! YamlMap || kvp.value == null) {
532
          errors.add('Expected "${kvp.key}" to be an object, but got ${kvp.value} (${kvp.value.runtimeType}).');
533
          break;
534
        }
535
        final List<String> pluginErrors = Plugin.validatePluginYaml(kvp.value as YamlMap);
536
        errors.addAll(pluginErrors);
537
        break;
538 539
      case 'generate':
        break;
540 541 542
      case 'deferred-components':
        _validateDeferredComponents(kvp, errors);
        break;
543 544 545 546 547
      default:
        errors.add('Unexpected child "${kvp.key}" found under "flutter".');
        break;
    }
  }
548
}
549

550
void _validateListType<T>(YamlList yamlList, List<String> errors, String context, String typeAlias) {
551 552
  for (int i = 0; i < yamlList.length; i++) {
    if (yamlList[i] is! T) {
553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583
      errors.add('Expected $context to be a list of $typeAlias, but element $i was a ${yamlList[i].runtimeType}');
    }
  }
}

void _validateDeferredComponents(MapEntry<dynamic, dynamic> kvp, List<String> errors) {
  if (kvp.value != null && (kvp.value is! YamlList || kvp.value[0] is! YamlMap)) {
    errors.add('Expected "${kvp.key}" to be a list, but got ${kvp.value} (${kvp.value.runtimeType}).');
  } else if (kvp.value != null) {
    for (int i = 0; i < (kvp.value as YamlList).length; i++) {
      if (kvp.value[i] is! YamlMap) {
        errors.add('Expected the $i element in "${kvp.key}" to be a map, but got ${kvp.value[i]} (${kvp.value[i].runtimeType}).');
        continue;
      }
      if (!(kvp.value[i] as YamlMap).containsKey('name') || kvp.value[i]['name'] is! String) {
        errors.add('Expected the $i element in "${kvp.key}" to have required key "name" of type String');
      }
      if ((kvp.value[i] as YamlMap).containsKey('libraries')) {
        if (kvp.value[i]['libraries'] is! YamlList) {
          errors.add('Expected "libraries" key in the $i element of "${kvp.key}" to be a list, but got ${kvp.value[i]['libraries']} (${kvp.value[i]['libraries'].runtimeType}).');
        } else {
          _validateListType<String>(kvp.value[i]['libraries'] as YamlList, errors, '"libraries" key in the $i element of "${kvp.key}"', 'dart library Strings');
        }
      }
      if ((kvp.value[i] as YamlMap).containsKey('assets')) {
        if (kvp.value[i]['assets'] is! YamlList) {
          errors.add('Expected "assets" key in the $i element of "${kvp.key}" to be a list, but got ${kvp.value[i]['assets']} (${kvp.value[i]['assets'].runtimeType}).');
        } else {
          _validateListType<String>(kvp.value[i]['assets'] as YamlList, errors, '"assets" key in the $i element of "${kvp.key}"', 'file paths');
        }
      }
584 585 586 587
    }
  }
}

588 589 590 591
void _validateFonts(YamlList fonts, List<String> errors) {
  if (fonts == null) {
    return;
  }
592
  const Set<int> fontWeights = <int>{
593
    100, 200, 300, 400, 500, 600, 700, 800, 900,
594
  };
595 596 597 598 599
  for (final dynamic fontListEntry in fonts) {
    if (fontListEntry is! YamlMap) {
      errors.add('Unexpected child "$fontListEntry" found under "fonts". Expected a map.');
      continue;
    }
600
    final YamlMap fontMap = fontListEntry as YamlMap;
601
    for (final dynamic key in fontMap.keys.where((dynamic key) => key != 'family' && key != 'fonts')) {
602 603 604 605 606 607 608
      errors.add('Unexpected child "$key" found under "fonts".');
    }
    if (fontMap['family'] != null && fontMap['family'] is! String) {
      errors.add('Font family must either be null or a String.');
    }
    if (fontMap['fonts'] == null) {
      continue;
609 610 611
    } else if (fontMap['fonts'] is! YamlList) {
      errors.add('Expected "fonts" to either be null or a list.');
      continue;
612
    }
613 614 615 616 617
    for (final dynamic fontListItem in fontMap['fonts']) {
      if (fontListItem is! YamlMap) {
        errors.add('Expected "fonts" to be a list of maps.');
        continue;
      }
618
      final YamlMap fontMapList = fontListItem as YamlMap;
619
      for (final MapEntry<dynamic, dynamic> kvp in fontMapList.entries) {
620 621 622
        if (kvp.key is! String) {
          errors.add('Expected "${kvp.key}" under "fonts" to be a string.');
        }
623
        switch(kvp.key as String) {
624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645
          case 'asset':
            if (kvp.value is! String) {
              errors.add('Expected font asset ${kvp.value} ((${kvp.value.runtimeType})) to be a string.');
            }
            break;
          case 'weight':
            if (!fontWeights.contains(kvp.value)) {
              errors.add('Invalid value ${kvp.value} ((${kvp.value.runtimeType})) for font -> weight.');
            }
            break;
          case 'style':
            if (kvp.value != 'normal' && kvp.value != 'italic') {
              errors.add('Invalid value ${kvp.value} ((${kvp.value.runtimeType})) for font -> style.');
            }
            break;
          default:
            errors.add('Unexpected key ${kvp.key} ((${kvp.value.runtimeType})) under font.');
            break;
        }
      }
    }
  }
646
}