2

Is there a standard way to validate key/values (presence of a key, whether it's a valid value) in Ruby's Openstruct? In a pure Ruby, no Rails/Sinatra, but in a similar way to validations in ActiveRecord or ActiveModel::Validations is doing. For example, to check that Openstruct contains "id" key, check that a "name" key's value is not longer than X characters, check that "protocol" key's value contains only specific values such as "tcp", but not "abc", etc.

nacrafts
  • 51
  • 1
  • Look like you want to add behaviour to your "Struct" - maybe you just need to make it a class ? Strucs are made for simple structure without much behaviour... – Martin Dec 25 '14 at 12:16
  • The openstruct is created from json received from another server, or constructed directly by the user. There is a lot of dynamic content that should not (and could not) be validated at this point, but there is a certain number of attributes (3-5) that I'd like to validate for their presence and their content. Hence my question is regarding key/value validation - whether anything standard exists in relation to OpenStruct. – nacrafts Dec 28 '14 at 10:41

0 Answers0