DEV Community

Discussion on: Fast Type-Checked Serializers for Ruby Web APIs

 
jgaskins profile image
Jamie Gaskins

It does sound like you and I have different goals. Primalize is for converting domain objects into primitive data structures for the purpose serialization. It doesn’t have to be serialized, you can do whatever you like with it, but that’s the use case it’s optimized for.

The purpose of the type checking is only for ensuring that the structure I send matches what the client expects. The values of those types are beyond its scope because you’re probably already allowing your domain model to store that value. You can use Primalize’s attribute coercion or virtual attributes to massage those values, but I'm not sure it should be up to the serializer to validate the values.

Then again, maybe it’s not beyond the scope. You could argue that it’s a serializer’s responsibility to ensure it’s not emitting an unexpected value. For example, if part of the contract of the REST API is that a particular attribute returned would be a percentage represented as a float between 0-1, it might be worth arguing and I would totally entertain a discussion about it in the repo’s issues. :-) If it helps, it’d be really simple to implement.

Also, Dry::Types and Dry::Validation are more flexible, but they’re also quite a bit more verbose. I can give Primalize attributes declaration directly to a consumer of my REST API and it would be immediately obvious to them what to expect, even if they don’t know Ruby, because the attributes declaration is even in the same shape as the data they would receive. They might have to spend a little more time deciphering the Dry::Types DSL.