scala-pet-store
scala-pet-store copied to clipboard
Enforce invariants in the models
Our entities / domain model have virtually no validations.
Add invariant checks so that no domain model can be created.
For example, for a user, the phone number must be a valid form, the email must be a valid form, first name and last name must only contain valid characters.
Can possibly add other invariants for Pets and Orders. The invariants to add can be arbitrary.
Also, looking for ideas on how to tie these into Circe serialization.
My current thinking is to have custom decoders that call out to the smart constructors, and somehow translate the ValidatedNels to an appropriate response.
Here is an example...
case class User(...)
object User {
def apply(userName: String, firstName: String ... ): ValidatedNel[String, User] = ...
sealed abstract case class can be helpful in situations like this. Refined may also be useful.
I'm not totally fluent with the project yet, but refined-circe could be really useful for validating input to the endpoints. It can do a lot of the heavy lifting to Refined types for you.
I looked at Refined recently, unclear on how everything comes together there yet, but it is definitely a good idea