Thrift::Validator
Recursive thrift struct validator. The thrift library out of the box does not validated nested structs; This library fixes that problem. Rather than monkey-patching Thrift, this library includes a class to validate objects recursively.
Here's an example from this library's tests. Given the following schema:
struct SimpleStruct {
1: required string required_string,
2: optional string optional_string,
}
struct NestedExample {
1: required SimpleStruct required_struct,
2: optional SimpleStruct optional_struct,
}
This library provides:
struct = SimpleStruct.new
nested = NestedStruct.new(required_struct: struct)
# Method defined by the Thrift library
struct.validate # => Thrift::ProtocolException
# Thrift only validates fields set as required on this instance. Since
# `required_struct` is non-nil, validation succeeds. Also note that Thrift
# does not validate semantics of the assigned objects, so assigning an
# invalid struct will pass its validation method.
nested.validate # => true
# With the validator
Thrift::Validator.new.validate(nested) # => Thrift::ProtocolException
Semantics enforced
- all original Thrift validation semantics
-
optional
orrequired
struct
types -
optional
orrequired
list<struct>
items -
optional
orrequired
set<struct>
items -
optional
orrequired
map
types withstruct
keys and/or values
Exception handling
Due to the recursive nature of thrift-validator
, the validation
errors raised by Thrift become less than helpful. In order
to provide more information, the resulting Thrift::ProtocolException
message is prefixed with the type where the error occurred. For
example:
# Without thrift-validator
> struct.validate
Thrift::ProtocolException: Required field required_string is unset!
# With thrift-validator
> Thrift::Validator.new.validate(struct)
Thrift::ProtocolException: SimpleStruct: Required field required_string is unset!
This feature becomes especially useful with nested structs, where validation may fail at any depth.
Installation
Add this line to your application's Gemfile:
gem 'thrift-validator'
And then execute:
$ bundle
Or install it yourself as:
$ gem install thrift-validator
Testing
First, install a thrift
compiler on your platform (e.g., brew install thrift
, sudo apt install thrift
). Then run:
$ rake test
Contributing
- Fork it ( https://github.com/saltside/thrift-validator-ruby/fork )
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create a new Pull Request