These are chat archives for dry-rb/chat

31st
Jul 2018
Jonah
@jonahx
Jul 31 2018 15:03

at a high level, why do we need both types and validations? i.e., considering something like:

UserSchema = Dry::Validation.Schema do
  required(:name).filled

  required(:email).filled(format?: EMAIL_REGEX)
  # etc..

it seems to me that a schema like this would be implied by a User type like, e.g.:

class User < Dry::Struct
  attribute :name, Types::Strict::String
  attribute :email, Types::Email # would be defined in Types module with regex
  # etc...
end

i.e., even though the schema might be used a bit differently in code, in the context of a form validation for example, the schema could be mechanically derived from the User type? and so it would be redundant to have both. what am i missing?

Grant Shangreaux
@gcentauri
Jul 31 2018 16:08
as i understand it, the purpose of the typed struct is to absolutely prevent a broken data object from ever being created. violating constraints when trying to instantiate the structs will raise errors. on the other hand, a schema won't raise anything if constraints are broken, but instead returns a result with helpful messages which may allow you to do something about it. the schemas can also process and coerce incoming data (though i suppose the struct constructors can do that too right?)
but you are right, it seems like there should be a way to derive the validation schema from the struct you're wanting to fill in some cases
Nikita Shilnikov
@flash-gordon
Jul 31 2018 17:05
I recently started an experiment of having diffent kinds of types in production vs test/development
I use strict versions in tests and nominal/checkless types in production
Jonah
@jonahx
Jul 31 2018 17:06
@gcentauri yes, what you said is more or less what i meant by "even though the schema might be used a bit differently in code, in the context of a form validation for example.” rather than “why do we need both?”, my original question is probably better phrased as “types and validations seem to share a lot of “information". won’t we repeating that information (and hence failing to be DRY) if there is no way to derive schemas, either in whole or in part, from types?"
@flash-gordon iirc, the sanctuary-js project has options to do that as well. Also, eg, in purescript all the checks are done at compile time, and the runtime code doesn’t have any, because in theory it’s impossible those errors would exist at that point.
Nikita Shilnikov
@flash-gordon
Jul 31 2018 17:08
yeah but we don't have compile time here
Jonah
@jonahx
Jul 31 2018 17:08
@flash-gordon yeah i know, it was just an FYI :) just made me think of it
Nikita Shilnikov
@flash-gordon
Jul 31 2018 17:08
I would prefer having a compiler, though, but it's not about ruby
Jonah
@jonahx
Jul 31 2018 17:09
+1