@@ -94,6 +94,8 @@ def friendly(v: Any) -> Any:
94
94
return avro_shortname (v .name )
95
95
if isinstance (v , avro .schema .ArraySchema ):
96
96
return f"array of <{ friendly (v .items )} >"
97
+ if isinstance (v , (avro .schema .MapSchema , avro .schema .NamedMapSchema )):
98
+ return f"map of <{ friendly (v .values )} >"
97
99
if isinstance (v , avro .schema .PrimitiveSchema ):
98
100
return v .type
99
101
if isinstance (v , (avro .schema .UnionSchema , avro .schema .NamedUnionSchema )):
@@ -258,10 +260,18 @@ def validate_ex(
258
260
for s in expected_schema .schemas :
259
261
if isinstance (datum , MutableSequence ) and not isinstance (s , avro .schema .ArraySchema ):
260
262
continue
261
- if isinstance (datum , MutableMapping ) and not isinstance (s , avro .schema .RecordSchema ):
263
+ if isinstance (datum , MutableMapping ) and not isinstance (
264
+ s , (avro .schema .RecordSchema , avro .schema .MapSchema , avro .schema .NamedMapSchema )
265
+ ):
262
266
continue
263
267
if isinstance (datum , (bool , int , float , str )) and isinstance (
264
- s , (avro .schema .ArraySchema , avro .schema .RecordSchema )
268
+ s ,
269
+ (
270
+ avro .schema .ArraySchema ,
271
+ avro .schema .RecordSchema ,
272
+ avro .schema .MapSchema ,
273
+ avro .schema .NamedMapSchema ,
274
+ ),
265
275
):
266
276
continue
267
277
if datum is not None and s .type == "null" :
@@ -437,6 +447,40 @@ def validate_ex(
437
447
raise ValidationException ("" , None , errors , "*" )
438
448
return False
439
449
return True
450
+
451
+ if isinstance (expected_schema , (avro .schema .MapSchema , avro .schema .NamedMapSchema )):
452
+ if isinstance (datum , MutableMapping ):
453
+ for key , val in datum .items ():
454
+ if not isinstance (key , str ):
455
+ pass
456
+ try :
457
+ sl = SourceLine (datum , key , ValidationException )
458
+ if not validate_ex (
459
+ expected_schema .values ,
460
+ val ,
461
+ identifiers ,
462
+ strict = strict ,
463
+ foreign_properties = foreign_properties ,
464
+ raise_ex = raise_ex ,
465
+ strict_foreign_properties = strict_foreign_properties ,
466
+ logger = logger ,
467
+ skip_foreign_properties = skip_foreign_properties ,
468
+ vocab = vocab ,
469
+ ):
470
+ return False
471
+ except ValidationException as v :
472
+ if raise_ex :
473
+ source = v if debug else None
474
+ raise ValidationException ("item is invalid because" , sl , [v ]) from source
475
+ return False
476
+ return True
477
+ if raise_ex :
478
+ raise ValidationException (
479
+ f"the value { vpformat (datum )} is not an object, "
480
+ f"expected object of { friendly (expected_schema .values )} "
481
+ )
482
+ return False
483
+
440
484
if raise_ex :
441
485
raise ValidationException (f"Unrecognized schema_type { schema_type } " )
442
486
return False
0 commit comments