allow validator option in json_writer
MODIFY
fastavro/json_write.py
fastavro/json_write.py
@@ -2,7 +2,7 @@ from ._write_py import writer
from .io.json_encoder import AvroJSONEncoder
-def json_writer(fo, schema, records, *, write_union_type=True):
+def json_writer(fo, schema, records, *, write_union_type=True, validator=None):
"""Write records to fo (stream) according to schema
Parameters
@@ -18,6 +18,11 @@ def json_writer(fo, schema, records, *, write_union_type=True):
Determine whether to write the union type in the json message.
If this is set to False the output will be clear json.
It may however not be decodable back to avro record by `json_read`.
+ validator: None, True or a function
+ Validator function. If None (the default) - no validation. If True then
+ then fastavro.validation.validate will be used. If it's a function, it
+ should have the same signature as fastavro.writer.validate and raise an
+ exeption on error.
Example::
@@ -48,5 +53,8 @@ def json_writer(fo, schema, records, *, write_union_type=True):
json_writer(out, parsed_schema, records)
"""
return writer(
- AvroJSONEncoder(fo, write_union_type=write_union_type), schema, records
+ AvroJSONEncoder(fo, write_union_type=write_union_type),
+ schema,
+ records,
+ validator=validator,
)

MODIFY
tests/test_json.py
tests/test_json.py
@@ -6,6 +6,7 @@ import pytest
from fastavro import json_writer, json_reader
from fastavro.schema import parse_schema
+from fastavro.validation import ValidationError
def roundtrip(schema, records):
@@ -661,3 +662,29 @@ def test_map_of_union_of_array_and_map():
new_records = roundtrip(schema, records)
assert records == new_records
+
+
+def test_json_writer_with_validation():
+ """https://github.com/fastavro/fastavro/issues/580"""
+ schema = {
+ "doc": "A weather reading.",
+ "name": "Weather",
+ "namespace": "test",
+ "type": "record",
+ "fields": [
+ {"name": "station", "type": "string"},
+ {"name": "time", "type": "long"},
+ {"name": "temp", "type": "int"},
+ ],
+ }
+
+ records = [
+ {"station": "011990-99999", "temp": 0, "time": 1433269388},
+ {"station": "011990-99999", "temp": 22, "time": "last day"},
+ {"station": "011990-99999", "temp": -11, "time": 1433273379},
+ {"station": "012650-99999", "temp": 111.9, "time": 1433275478},
+ ]
+
+ new_file = StringIO()
+ with pytest.raises(ValidationError):
+ json_writer(new_file, schema, records, validator=True)

allow validator option in json_writer