Skip to content

Commit

Permalink
#130: added more accurate error loging
Browse files Browse the repository at this point in the history
  • Loading branch information
jorre127 committed Sep 6, 2023
1 parent 8b50025 commit 249f595
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
12 changes: 10 additions & 2 deletions lib/config/yml_generator_config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ class YmlGeneratorConfig {
));
});

models.add(EnumModel(
final enumModel = EnumModel(
addJsonKeyToProperties: value['use_default_json_key'] ?? true,
name: key,
path: path,
Expand All @@ -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;
Expand Down
12 changes: 12 additions & 0 deletions lib/model/model/enum_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 249f595

Please sign in to comment.