Creating and validating ValueObjects #1122
Unanswered
speterson-zoll
asked this question in
Q&A
Replies: 1 comment
-
return (Validation<Error, DiscreteAlert>)(alertDefinition, alertLevel, myEvent, myEventDate)
.Apply((adt, al, ev, ed) =>
DiscreteAlert.Create(adt, al, ev, ed)); It looks like you've got a nested monad What you need to do is return (alertDefinition, alertLevel, myEvent, myEventDate)
.Apply((adt, al, ev, ed) => DiscreteAlert.Create(adt, al, ev, ed))
.Flatten(); This will follow the correct rules of the validation monad. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I've figured out a few simple validations, but when I tried to use newly created custom immutable types with another class, things went off track. Here is my attempt at an immutable ValueObject using
NewType
:And the validator is:
Where
AlertDefinitionType
is a standard Enum withint
values.This works fine on its own. However, I have a class that makes use of the
AlertDefinition
class:The
AlertLevel
and field is similar to theAlertDefinition
field in that it is a custom type with its own validation.I am using a method in a DTO, so that if the incoming DTO data is valid, I can successfully create a valid
DiscreteAlert
object:Note that the
DiscreteAlert
classCreate
method returnsValidation<Error, DiscreteAlert>
. I can create aDiscreteAlert
with no problem as long as all the data is valid. However, when testing failures in a unit test (I purposely provide an invalidAlertDefinition
value), I get an error:Validation is not in a Success state
. This code segment in theValidate
method:predictably is in a Fail state :
Fail([Not a valid Discrete alert type])
. However, when I hit the.Apply
call:I get the error mentioned above. I believe that this is happening because, while all the items in the Tuple in the
.Apply
have the same signature, there are Monads within that have different return types. When the.Apply
method is then called, I get a messageValidation is not in a Success state
. When debugging, I see that the code:specifically, the value of
alertDefinition
is:which is as expected since my test sends in an invalid value. As I said, running
.Apply
shows the error mentioned above.One other note: I am casting the
.Apply
call with(Validation<Error, DiscreteAlert>)
because I received an error in my IDE thatValidation<Error. Validation<Error, DiscreteAlert>>
cannot be converted toValidation<Error, DiscreteAlert>
. I'm not certain how the return typeValidation<Error. Validation<Error, DiscreteAlert>>
came to be. My goal is to collect all the validation errors at the.Apply
stage, but I think the ValueObject validation is causing an issue in the process.I feel like I'm close to figuring this out, but need help. Any advice on how to accomplish this better is most welcome.
Beta Was this translation helpful? Give feedback.
All reactions