@@ -179,7 +179,7 @@ impl Event {
179
179
let schema_ref = Arc :: new ( schema) ;
180
180
// validate schema before processing the event
181
181
let Ok ( mut event) = self . get_record ( schema_ref. clone ( ) ) else {
182
- return Err ( EventError :: SchemaMismatch ( self . stream_name . clone ( ) ) ) ;
182
+ return Err ( EventError :: SchemaMismatch ) ;
183
183
} ;
184
184
185
185
if event
@@ -312,12 +312,39 @@ impl Event {
312
312
313
313
fn get_record ( & self , schema : Arc < Schema > ) -> Result < RecordBatch , EventError > {
314
314
let mut iter = std:: iter:: once ( Ok ( self . body . clone ( ) ) ) ;
315
+ if fields_mismatch ( & schema, & self . body ) {
316
+ return Err ( EventError :: SchemaMismatch ) ;
317
+ }
315
318
let record = Decoder :: new ( schema, DecoderOptions :: new ( ) ) . next_batch ( & mut iter) ?;
316
319
317
320
record. ok_or ( EventError :: MissingRecord )
318
321
}
319
322
}
320
323
324
+ fn fields_mismatch ( schema : & Schema , body : & Value ) -> bool {
325
+ for ( name, val) in body. as_object ( ) . expect ( "body is of object variant" ) {
326
+ let Ok ( field) = schema. field_with_name ( name) else { return true } ;
327
+
328
+ // datatype check only some basic cases
329
+ let valid_datatype = match field. data_type ( ) {
330
+ DataType :: Boolean => val. is_boolean ( ) ,
331
+ DataType :: Int8 | DataType :: Int16 | DataType :: Int32 | DataType :: Int64 => val. is_i64 ( ) ,
332
+ DataType :: UInt8 | DataType :: UInt16 | DataType :: UInt32 | DataType :: UInt64 => {
333
+ val. is_u64 ( )
334
+ }
335
+ DataType :: Float16 | DataType :: Float32 | DataType :: Float64 => val. is_f64 ( ) ,
336
+ DataType :: Utf8 => val. is_string ( ) ,
337
+ _ => false ,
338
+ } ;
339
+
340
+ if !valid_datatype {
341
+ return true ;
342
+ }
343
+ }
344
+
345
+ false
346
+ }
347
+
321
348
fn replace (
322
349
schema : Arc < Schema > ,
323
350
batch : RecordBatch ,
@@ -390,8 +417,8 @@ pub mod error {
390
417
Metadata ( #[ from] MetadataError ) ,
391
418
#[ error( "Stream Writer Failed: {0}" ) ]
392
419
Arrow ( #[ from] ArrowError ) ,
393
- #[ error( "Schema Mismatch: {0} " ) ]
394
- SchemaMismatch ( String ) ,
420
+ #[ error( "Schema Mismatch" ) ]
421
+ SchemaMismatch ,
395
422
#[ error( "Schema Mismatch: {0}" ) ]
396
423
ObjectStorage ( #[ from] ObjectStorageError ) ,
397
424
}
0 commit comments