Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ValueObject for enums: how to properly validate? #13

Closed
chikamichi opened this issue Jun 4, 2023 · 2 comments
Closed

ValueObject for enums: how to properly validate? #13

chikamichi opened this issue Jun 4, 2023 · 2 comments
Labels
discussion Open-ended topics or questions, likely to be converted to a Github Discussion

Comments

@chikamichi
Copy link

Hi 👋,

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';

enum Type { raster, vector, geojson }

@Modddel(
  validationSteps: [
    // It seems like at least one validation step MUST be implemented.
    ValidationStep([Validation("allowed", FailureType<EnumFailure>())]),
  ],
)
class MapLayerType
    extends SingleValueObject<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.
  factory MapLayerType(Type value) {
    return _$MapLayerType._create(
      value: value,
    );
  }

  @override
  Option<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.
      return some(const EnumFailure.allowed());
    }
    // I guess I could get rid of the type checking then, and simply return none(), period.
    return none();
  }
}

@freezed
class EnumFailure extends ValueFailure with _$EnumFailure {
  const factory EnumFailure.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?

@chikamichi
Copy link
Author

Damn, I wanted to create a Discussion, not an Issue. Feel free to transform it!

@chikamichi
Copy link
Author

Well, I guess that’s that:

@validParam required Type type, // where Type is an enum

🙈

@CodingSoot CodingSoot added the discussion Open-ended topics or questions, likely to be converted to a Github Discussion label Jun 4, 2023
Repository owner locked and limited conversation to collaborators Jun 4, 2023
@CodingSoot CodingSoot converted this issue into discussion #14 Jun 4, 2023

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
discussion Open-ended topics or questions, likely to be converted to a Github Discussion
Projects
None yet
Development

No branches or pull requests

2 participants