How to use the fastavro.validation.validate function in fastavro

To help you get started, we’ve selected a few fastavro examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github fastavro / fastavro / tests / test_validation.py View on Github external
np.float64,
    ]

    schema_ints = ['int', 'long']

    schema_floats = ['float', 'double']

    # all these should work
    for nptype, schema in zip(np_ints, schema_ints):
        validate(nptype(1), schema)

    for nptype, schema in zip(np_ints, schema_floats):
        validate(nptype(1), schema)

    for nptype, schema in zip(np_floats, schema_floats):
        validate(nptype(1), schema)

    # these shouldn't work
    for nptype, schema in zip(np_floats, schema_ints):
        with pytest.raises(ValidationError):
            validate(nptype(1), schema)
github fastavro / fastavro / tests / test_validate.py View on Github external
]

    np_floats = [
        np.float_,
        np.float16,
        np.float32,
        np.float64,
    ]

    schema_ints = ['int', 'long']

    schema_floats = ['float', 'double']

    # all these should work
    for nptype, schema in zip(np_ints, schema_ints):
        assert validate(nptype(1), schema)

    for nptype, schema in zip(np_ints, schema_floats):
        assert validate(nptype(1), schema)

    for nptype, schema in zip(np_floats, schema_floats):
        assert validate(nptype(1), schema)

    # these shouldn't work
    for nptype, schema in zip(np_floats, schema_ints):
        assert not validate(nptype(1), schema)
github fastavro / fastavro / tests / test_validation.py View on Github external
{
                "name": "map",
                "type": {
                    "type": "map",
                    "values": "string",
                },
            },
        ],
        "namespace": "namespace",
        "name": "test_validate_map",
        "type": "record"
    }

    datum = {"map": {"key": 1}}
    with pytest.raises(ValidationError) as exc:
        validate(datum, my_schema)

    for error in exc.value.errors:
        assert error.field == 'namespace.test_validate_map.map'
github fastavro / fastavro / tests / test_validation.py View on Github external
{
                "name": "array",
                "type": {
                    "type": "array",
                    "items": "string",
                },
            },
        ],
        "namespace": "namespace",
        "name": "test_validate_array",
        "type": "record"
    }

    datum = {"array": [1]}
    with pytest.raises(ValidationError) as exc:
        validate(datum, my_schema)

    for error in exc.value.errors:
        assert error.field == 'namespace.test_validate_array.array'
github fastavro / fastavro / tests / test_validate.py View on Github external
def test_validator_numeric():
    for datum, schema in [
        (1, 'int'),
        (1, 'long'),
        (1.0, 'float'),
        (1.0, 'double'),
        (1, 'float'),
        (1, 'double'),
    ]:
        assert validate(datum, schema)

    for datum, schema in [
        (1.0, 'int'),
        (1.0, 'long'),
        ("1.0", 'float'),
        ("1.0", 'double'),
        ("1", 'float'),
        ("1", 'double'),
    ]:
        assert not validate(datum, schema)
    # and plenty more to add I suppose
github fastavro / fastavro / tests / test_validation.py View on Github external
def test_validator_logical():
    """https://github.com/fastavro/fastavro/issues/365"""
    for datum, schema in [
        (1, {"type": "long", "logicalType": "timestamp-micros"}),
    ]:
        validate(datum, schema)

    for datum, schema in [
        ("foo", {"type": "long", "logicalType": "timestamp-micros"}),
    ]:
        with pytest.raises(ValidationError):
            validate(datum, schema)
            pytest.fail("{} should not validate as {}".format(datum, schema))
github fastavro / fastavro / tests / test_validation.py View on Github external
(1.0, 'long'),
        ("1.0", 'float'),
        ("1.0", 'double'),
        ("1", 'float'),
        ("1", 'double'),
        (True, 'int'),
        (True, 'long'),
        (True, 'float'),
        (True, 'double'),
        (False, 'int'),
        (False, 'long'),
        (False, 'float'),
        (False, 'double'),
    ]:
        with pytest.raises(ValidationError):
            validate(datum, schema)
            pytest.fail("{} should not validate as {}".format(datum, schema))
github fastavro / fastavro / fastavro / _write_py.py View on Github external
def __init__(self,
                 schema,
                 metadata=None,
                 validator=None):
        self.schema = parse_schema(schema)
        self.validate_fn = validate if validator is True else validator
        self.metadata = metadata or {}

        if isinstance(schema, dict):
            schema = {
                key: value
                for key, value in iteritems(schema)
                if key != "__fastavro_parsed"
            }

        self.metadata['avro.schema'] = json.dumps(schema)
github fastavro / fastavro / fastavro / json_writer.py View on Github external
)

    if record_type in passthrough_schemas:
        return datum

    elif record_type == 'bytes':
        return btou(datum, encoding='iso-8859-1')

    elif record_type == 'fixed':
        return btou(datum, encoding='iso-8859-1')

    elif record_type == 'union':
        best_match_index = -1
        most_fields = -1
        for index, candidate in enumerate(schema):
            if validate(datum, candidate, raise_errors=False):
                if extract_record_type(candidate) == 'record':
                    fields = len(candidate['fields'])
                    if fields > most_fields:
                        best_match_index = index
                        most_fields = fields
                else:
                    best_match_index = index
                    break
        if best_match_index < 0:
            pytype = type(datum)
            msg = '%r (type %s) do not match %s' % (datum, pytype, schema)
            raise ValueError(msg)

        best_match_schema = schema[best_match_index]

        if best_match_schema == 'null':
github fastavro / fastavro / fastavro / _write_py.py View on Github external
if extract_record_type(candidate) == 'record':
                schema_name = candidate['name']
            else:
                schema_name = candidate
            if name == schema_name:
                break
        else:
            msg = 'provided union type name %s not found in schema %s' \
              % (name, schema)
            raise ValueError(msg)
    else:
        pytype = type(datum)
        best_match_index = -1
        most_fields = -1
        for index, candidate in enumerate(schema):
            if validate(datum, candidate, raise_errors=False):
                if extract_record_type(candidate) == 'record':
                    candidate_fields = set(
                        f["name"] for f in candidate["fields"]
                    )
                    datum_fields = set(datum)
                    fields = len(candidate_fields.intersection(datum_fields))
                    if fields > most_fields:
                        best_match_index = index
                        most_fields = fields
                else:
                    best_match_index = index
                    break
        if best_match_index < 0:
            msg = '%r (type %s) do not match %s' % (datum, pytype, schema)
            raise ValueError(msg)
        index = best_match_index