Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fail when extra fields are in the payload #15

Merged
merged 3 commits into from
Jul 15, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 17 additions & 8 deletions lib/avro-patches/logical_types/schema_validator.rb
Original file line number Diff line number Diff line change
@@ -1,27 +1,29 @@
module AvroPatches
module LogicalTypes
module SchemaValidatorPatch
def validate!(expected_schema, logical_datum, options = { recursive: true, encoded: false })
def validate!(expected_schema, logical_datum, options = { recursive: true, encoded: false, fail_on_extra_fields: false})
options ||= {}
options[:recursive] = true unless options.key?(:recursive)

result = Avro::SchemaValidator::Result.new
if options[:recursive]
validate_recursive(expected_schema, logical_datum, Avro::SchemaValidator::ROOT_IDENTIFIER, result, options[:encoded])
validate_recursive(expected_schema, logical_datum,
Avro::SchemaValidator::ROOT_IDENTIFIER, result, options)
else
validate_simple(expected_schema, logical_datum, Avro::SchemaValidator::ROOT_IDENTIFIER, result, options[:encoded])
validate_simple(expected_schema, logical_datum,
Avro::SchemaValidator::ROOT_IDENTIFIER, result, options)
end
fail Avro::SchemaValidator::ValidationError, result if result.failure?
result
end

private

def validate_recursive(expected_schema, logical_datum, path, result, encoded = false)
datum = resolve_datum(expected_schema, logical_datum, encoded)
def validate_recursive(expected_schema, logical_datum, path, result, options={})
datum = resolve_datum(expected_schema, logical_datum, options[:encoded])

# The entire method is overridden so that encoded: true can be passed here
validate_simple(expected_schema, datum, path, result, true)
validate_simple(expected_schema, datum, path, result, encoded: true)

case expected_schema.type_sym
when :array
Expand All @@ -36,13 +38,20 @@ def validate_recursive(expected_schema, logical_datum, path, result, encoded = f
deeper_path = deeper_path_for_hash(field.name, path)
validate_recursive(field.type, datum[field.name], deeper_path, result)
end
if options[:fail_on_extra_fields]
datum_fields = datum.keys.map(&:to_s)
schema_fields = expected_schema.fields.map(&:name)
(datum_fields - schema_fields).each do |extra_field|
result.add_error(path, "extra field '#{extra_field}' - not in schema")
end
end
end
rescue Avro::SchemaValidator::TypeMismatchError
result.add_error(path, "expected type #{expected_schema.type_sym}, got #{actual_value_message(datum)}")
end

def validate_simple(expected_schema, logical_datum, path, result, encoded = false)
datum = resolve_datum(expected_schema, logical_datum, encoded)
def validate_simple(expected_schema, logical_datum, path, result, options = {})
datum = resolve_datum(expected_schema, logical_datum, options[:encoded])
super(expected_schema, datum, path, result)
end

Expand Down
24 changes: 22 additions & 2 deletions test/schema_validator/test_schema_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
require 'test_help'

class TestSchema < Test::Unit::TestCase
def validate!(schema, value)
Avro::SchemaValidator.validate!(schema, value)
def validate!(schema, value, options=nil)
Avro::SchemaValidator.validate!(schema, value, options)
end

def validate_simple!(schema, value)
Expand Down Expand Up @@ -469,4 +469,24 @@ def test_validate_multiple_errors
exception.to_s
)
end

def test_validate_extra_fields
schema = hash_to_schema(
type: 'record',
name: 'fruits',
fields: [
{
name: 'veggies',
type: 'string'
}
]
)
exception = assert_raise(Avro::SchemaValidator::ValidationError) do
validate!(schema, {'veggies' => 'tomato', 'bread' => 'rye'}, fail_on_extra_fields: true)
end
assert_equal(1, exception.result.errors.size)
assert_equal("at . extra field 'bread' - not in schema",
exception.to_s)
end

end