No commit activity in last 3 years
No release in over 3 years
Recursive thrift struct validator
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.7
~> 5.11
~> 10.0

Runtime

~> 0.9
 Project Readme

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 or required struct types
  • optional or required list<struct> items
  • optional or required set<struct> items
  • optional or required map types with struct 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

  1. Fork it ( https://github.com/saltside/thrift-validator-ruby/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request