lib/avro/schema.rb in avro-salsify-fork-1.9.0.3 vs lib/avro/schema.rb in avro-salsify-fork-1.9.0.4
- old
+ new
@@ -93,48 +93,13 @@
end
end
# Determine if a ruby datum is an instance of a schema
def self.validate(expected_schema, logical_datum, encoded = false)
- datum = if encoded
- logical_datum
- else
- expected_schema.type_adapter.encode(logical_datum) rescue nil
- end
-
- case expected_schema.type_sym
- when :null
- datum.nil?
- when :boolean
- datum == true || datum == false
- when :string, :bytes
- datum.is_a? String
- when :int
- (datum.is_a?(Fixnum) || datum.is_a?(Bignum)) &&
- (INT_MIN_VALUE <= datum) && (datum <= INT_MAX_VALUE)
- when :long
- (datum.is_a?(Fixnum) || datum.is_a?(Bignum)) &&
- (LONG_MIN_VALUE <= datum) && (datum <= LONG_MAX_VALUE)
- when :float, :double
- datum.is_a?(Float) || datum.is_a?(Fixnum) || datum.is_a?(Bignum)
- when :fixed
- datum.is_a?(String) && datum.bytesize == expected_schema.size
- when :enum
- expected_schema.symbols.include? datum
- when :array
- datum.is_a?(Array) &&
- datum.all?{|d| validate(expected_schema.items, d) }
- when :map
- datum.keys.all?{|k| k.is_a? String } &&
- datum.values.all?{|v| validate(expected_schema.values, v) }
- when :union
- expected_schema.schemas.any?{|s| validate(s, datum) }
- when :record, :error, :request
- datum.is_a?(Hash) &&
- expected_schema.fields.all?{|f| validate(f.type, datum[f.name]) }
- else
- raise "you suck #{expected_schema.inspect} is not allowed."
- end
+ SchemaValidator.validate!(expected_schema, logical_datum, encoded)
+ true
+ rescue SchemaValidator::ValidationError
+ false
end
def initialize(type, logical_type=nil)
@type_sym = type.is_a?(Symbol) ? type : type.to_sym
@logical_type = logical_type