From 249f59517249ecebbdf5349b2667541f1d97fff5 Mon Sep 17 00:00:00 2001 From: Jordy de Jonghe Date: Wed, 6 Sep 2023 10:05:25 +0200 Subject: [PATCH] #130: added more accurate error loging --- lib/config/yml_generator_config.dart | 12 ++++++++++-- lib/model/model/enum_model.dart | 12 ++++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/lib/config/yml_generator_config.dart b/lib/config/yml_generator_config.dart index 4a1867c..12c7eb7 100644 --- a/lib/config/yml_generator_config.dart +++ b/lib/config/yml_generator_config.dart @@ -157,7 +157,7 @@ class YmlGeneratorConfig { )); }); - models.add(EnumModel( + final enumModel = EnumModel( addJsonKeyToProperties: value['use_default_json_key'] ?? true, name: key, path: path, @@ -167,7 +167,15 @@ class YmlGeneratorConfig { extraImports: extraImports, extraAnnotations: extraAnnotations, description: description, - )); + ); + + final error = enumModel.validate(); + + if (error != null) { + throw Exception(error); + } + + models.add(enumModel); } else { final staticCreate = (value['static_create'] ?? false) == true; final disallowNullForDefaults = value.containsKey('disallow_null_for_defaults') ? (value['disallow_null_for_defaults'] == true) : pubspecConfig.disallowNullForDefaults; diff --git a/lib/model/model/enum_model.dart b/lib/model/model/enum_model.dart index 7e8be09..60f3f6e 100644 --- a/lib/model/model/enum_model.dart +++ b/lib/model/model/enum_model.dart @@ -24,6 +24,18 @@ class EnumModel extends Model { extraAnnotations: extraAnnotations, description: description, ); + + String? validate() { + for (final property in properties) { + for (final field in fields) { + final containsProperty = field.values.map((value) => value.propertyName).contains(property.name); + if (!containsProperty && !property.isOptional && property.defaultValue == null) { + return 'There is no value defined for property ${property.name} for the enum value ${field.name} in model $name. Either make this property optional or give it a value'; + } + } + } + return null; + } } class EnumField {