You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Mar 27, 2026. It is now read-only.
Repository navigation
This repository was archived by the owner on Mar 27, 2026. It is now read-only.
ValueObject for enums: how to properly validate? #13
Continuing on implementing my map project, let’s say I have this enum representing a map layer’s "type"/kind: enum Type { raster, vector, geojson }.
If I wish to have some entity’s attribute be of that kind, as far as I understand (and based on trial/error), it cannot be simply typed as a Type but rather has to be wrapped in a value object — such as this one, although it seems "useless" (see inlined comments as to why):
import"package:freezed_annotation/freezed_annotation.dart";
part'map_layer_type.modddel.dart';
part'map_layer_type.freezed.dart';
enumType { raster, vector, geojson }
@Modddel(
validationSteps: [
// It seems like at least one validation step MUST be implemented.ValidationStep([Validation("allowed", FailureType<EnumFailure>())]),
],
)
classMapLayerTypeextendsSingleValueObject<InvalidMapLayerType, ValidMapLayerType>
with_$MapLayerType {
MapLayerType._();
// For value is typed as Type, the "allowed" validation is useless.// Code will *not* compile if trying to instanciate a ValueObject with anything// but a (valid by design) Type enum value.factoryMapLayerType(Type value) {
return_$MapLayerType._create(
value: value,
);
}
@overrideOption<EnumFailure> validateAllowed(mapLayerType) {
if (mapLayerType.value is!Type) { // Warning: "Unecessary type check; the result is always 'false'. Try correcting the type check, or removing the type check.returnsome(constEnumFailure.allowed());
}
// I guess I could get rid of the type checking then, and simply return none(), period.returnnone();
}
}
@freezedclassEnumFailureextendsValueFailurewith_$EnumFailure {
constfactoryEnumFailure.allowed() =_Allowed;
}
Is there a better way?
I feel like enums being sealed by design, having to come up with a dedicated value object is useless boilerplate, but maybe I’m missing something?
Activity
chikamichi commentedon Jun 4, 2023
Damn, I wanted to create a Discussion, not an Issue. Feel free to transform it!
chikamichi commentedon Jun 4, 2023
Well, I guess that’s that:
🙈