Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 1 addition & 11 deletions benchmark/benchmark.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,7 @@
from fastavro import writer, reader
from fastavro._timezone import utc

try:
from fastavro.validate import validate, validate_many
except ImportError:
try:
from fastavro._write import validate
except ImportError:
from fastavro._write_py import validate


def validate_many(records, schema):
return all([validate(record, schema) for record in records])
from fastavro.validate import validate, validate_many

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be updated as well for the name change of validate to validation



def write(schema, records, runs=1):
Expand Down
4 changes: 2 additions & 2 deletions fastavro/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
import fastavro.read
import fastavro.write
import fastavro.schema
import fastavro.validate
import fastavro.validation


def _acquaint_schema(schema):
Expand All @@ -71,7 +71,7 @@ def _acquaint_schema(schema):
acquaint_schema = _acquaint_schema
fastavro.schema.acquaint_schema = _acquaint_schema
is_avro = fastavro.read.is_avro
validator = fastavro.validate.validate
validate = fastavro.validation.validate

__all__ = [
n for n in locals().keys() if not n.startswith('_')
Expand Down
2 changes: 1 addition & 1 deletion fastavro/_schema.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ cpdef inline str extract_logical_type(schema):
return None


def schema_name(object schema, parent_ns):
def schema_name(schema, parent_ns):
name = schema.get('name')
if not name:
return parent_ns, None
Expand Down
4 changes: 2 additions & 2 deletions fastavro/_validate_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ def __str__(self):
self.field = ''

if self.datum is None:
return 'Field({field}) is null' \
return 'Field({field}) is None' \
' expected {schema}'.format(field=self.field,
schema=self.schema)
return '{field} is {datum} of type ' \
return '{field} is <{datum}> of type ' \
'{given_type} expected {schema}'. \
format(datum=self.datum, given_type=type(self.datum),
schema=self.schema, field=self.field)
Expand Down
95 changes: 29 additions & 66 deletions fastavro/_validate.pyx → fastavro/_validation.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ cdef int32 INT_MAX_VALUE = const.INT_MAX_VALUE
cdef long64 LONG_MIN_VALUE = const.LONG_MIN_VALUE
cdef long64 LONG_MAX_VALUE = const.LONG_MAX_VALUE

cpdef bint validate_null(datum, schema=None,
str parent_ns='', bint raise_errors=False):
cdef inline bint validate_null(datum, schema=None,
str parent_ns='', bint raise_errors=False):
"""
Checks that the data value is None.

Expand All @@ -32,8 +32,8 @@ cpdef bint validate_null(datum, schema=None,
"""
return datum is None

cpdef bint validate_boolean(datum, schema=None,
str parent_ns='', bint raise_errors=False):
cdef inline bint validate_boolean(datum, schema=None,
str parent_ns='', bint raise_errors=False):
"""Check that the data value is bool instance

:param datum: data to validate as boolean
Expand All @@ -44,8 +44,8 @@ cpdef bint validate_boolean(datum, schema=None,
"""
return isinstance(datum, bool)

cpdef bint validate_string(datum, schema=None,
str parent_ns='', bint raise_errors=False):
cdef inline bint validate_string(datum, schema=None,
str parent_ns='', bint raise_errors=False):
"""Check that the data value is string type, uses
six for Python version compatibility.

Expand All @@ -57,8 +57,8 @@ cpdef bint validate_string(datum, schema=None,
"""
return is_str(datum)

cpdef bint validate_bytes(datum, schema=None,
str parent_ns='', bint raise_errors=False):
cdef inline bint validate_bytes(datum, schema=None,
str parent_ns='', bint raise_errors=False):
"""
Check that the data value is
(bytes or decimal.Decimal)
Expand All @@ -71,8 +71,8 @@ cpdef bint validate_bytes(datum, schema=None,
"""
return isinstance(datum, (bytes, decimal.Decimal))

cpdef bint validate_int(datum, schema=None,
str parent_ns='', bint raise_errors=False):
cdef inline bint validate_int(datum, schema=None,
str parent_ns='', bint raise_errors=False):
"""
Check that the data value is a non floating
point number with size less that Int32.
Expand All @@ -95,8 +95,8 @@ cpdef bint validate_int(datum, schema=None,
datetime.time, datetime.datetime, datetime.date))
)

cpdef bint validate_long(datum, schema=None,
str parent_ns='', bint raise_errors=False):
cdef inline bint validate_long(datum, schema=None,
str parent_ns='', bint raise_errors=False):
"""
Check that the data value is a non floating
point number with size less that Int32.
Expand All @@ -118,8 +118,8 @@ cpdef bint validate_long(datum, schema=None,
datetime.time, datetime.datetime, datetime.date))
)

cpdef bint validate_float(datum, schema=None,
str parent_ns='', bint raise_errors=False):
cdef inline bint validate_float(datum, schema=None,
str parent_ns='', bint raise_errors=False):
"""
Check that the data value is a floating
point number or double precision.
Expand All @@ -132,8 +132,8 @@ cpdef bint validate_float(datum, schema=None,
"""
return isinstance(datum, (int, long, float, numbers.Real))

cpdef bint validate_fixed(datum, dict schema,
str parent_ns='', bint raise_errors=False):
cdef inline bint validate_fixed(datum, dict schema,
str parent_ns='', bint raise_errors=False):
"""
Check that the data value is fixed width bytes,
matching the schema['size'] exactly!
Expand All @@ -148,8 +148,8 @@ cpdef bint validate_fixed(datum, dict schema,
len(datum) == schema['size']) or \
(isinstance(datum, decimal.Decimal))

cpdef bint validate_enum(datum, dict schema,
str parent_ns='', bint raise_errors=False):
cdef inline bint validate_enum(datum, dict schema,
str parent_ns='', bint raise_errors=False):
"""
Check that the data value matches one of the enum symbols.

Expand All @@ -163,8 +163,8 @@ cpdef bint validate_enum(datum, dict schema,
"""
return datum in schema['symbols']

cpdef bint validate_array(datum, dict schema,
str parent_ns='', bint raise_errors=False):
cdef inline bint validate_array(datum, dict schema,
str parent_ns='', bint raise_errors=False):
"""
Check that the data list values all match schema['items'].

Expand All @@ -189,8 +189,8 @@ cpdef bint validate_array(datum, dict schema,
return False
return True

cpdef bint validate_map(object datum, dict schema, str parent_ns='',
bint raise_errors=False):
cdef inline bint validate_map(object datum, dict schema, str parent_ns='',
bint raise_errors=False):
"""
Check that the data is a Map(k,v)
matching values to schema['values'] type.
Expand Down Expand Up @@ -220,8 +220,8 @@ cpdef bint validate_map(object datum, dict schema, str parent_ns='',
return False
return True

cpdef bint validate_record(object datum, dict schema, str parent_ns='',
bint raise_errors=False):
cdef inline bint validate_record(object datum, dict schema, str parent_ns='',
bint raise_errors=False):
"""
Check that the data is a Mapping type with all schema defined fields
validated as True.
Expand All @@ -247,8 +247,8 @@ cpdef bint validate_record(object datum, dict schema, str parent_ns='',
return False
return True

cpdef bint validate_union(object datum, list schema, str parent_ns=None,
bint raise_errors=False):
cdef inline bint validate_union(object datum, list schema, str parent_ns=None,
bint raise_errors=False):
"""
Check that the data is a list type with possible options to
validate as True.
Expand Down Expand Up @@ -287,36 +287,6 @@ cpdef bint validate_union(object datum, list schema, str parent_ns=None,
raise ValidationError(*errors)
return False

cpdef BASE_VALIDATORS = {
'null': validate_null,
'boolean': validate_boolean,
'string': validate_string,
'int': validate_int,
'long': validate_long,
'float': validate_float,
'double': validate_float,
'bytes': validate_bytes,
'fixed': validate_fixed,
'enum': validate_enum,
'array': validate_array,
'map': validate_map,
'union': validate_union,
'error_union': validate_union,
'record': validate_record,
'error': validate_record,
'request': validate_record
}

cpdef VALIDATORS = BASE_VALIDATORS.copy()

cpdef void register_validator(record_type, validator):
if record_type in BASE_VALIDATORS:
raise ValueError("Not allowed to override Base Validators.")
VALIDATORS[record_type] = validator

cpdef get_validator(record_type):
return VALIDATORS.get(record_type)

cpdef validate(object datum, object schema, str field='',
bint raise_errors=False):
"""Determine if a python datum is an instance of a schema."""
Expand Down Expand Up @@ -369,24 +339,17 @@ cpdef validate(object datum, object schema, str field='',
elif record_type in ('record', 'error', 'request'):
result = validate_record(datum, schema=schema, parent_ns=ns_field,
raise_errors=raise_errors)
else:
validator = get_validator(record_type)
if validator:
result = validator(datum, schema=schema, parent_ns=ns_field,
raise_errors=raise_errors)

if record_type in SCHEMA_DEFS and result is None:
elif record_type in SCHEMA_DEFS:
result = validate(datum,
schema=SCHEMA_DEFS[record_type],
field=ns_field,
raise_errors=raise_errors)
else:
raise UnknownType(record_type)

if raise_errors and result is False:
raise ValidationError(ValidationErrorData(datum, schema, ns_field))

if result is None:
raise UnknownType(record_type)

return bool(result)


Expand Down
25 changes: 7 additions & 18 deletions fastavro/_validate_py.py → fastavro/_validation_py.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
import numbers
from collections import Iterable, Mapping

from fastavro.const import INT_MAX_VALUE, INT_MIN_VALUE, \
LONG_MAX_VALUE, LONG_MIN_VALUE
from fastavro.const import (
INT_MAX_VALUE, INT_MIN_VALUE, LONG_MAX_VALUE, LONG_MIN_VALUE
)
from ._validate_common import ValidationError, ValidationErrorData
from .schema import extract_record_type, schema_name, UnknownType
from .six import long, is_str, iterkeys, itervalues
Expand Down Expand Up @@ -161,7 +162,7 @@ def validate_array(datum, schema, parent_ns=None, raise_errors=False):
:except: ValidationError
"""
if raise_errors:
namespace, name = schema_name(schema, parent_ns)
_, name = schema_name(schema, parent_ns)
else:
name = parent_ns
return (
Expand All @@ -186,7 +187,7 @@ def validate_map(datum, schema, parent_ns=None, raise_errors=False):
:except: ValidationError
"""
if raise_errors:
namespace, name = schema_name(schema, parent_ns)
_, name = schema_name(schema, parent_ns)
else:
name = parent_ns
return (
Expand Down Expand Up @@ -265,7 +266,7 @@ def validate_union(datum, schema, parent_ns=None, raise_errors=False):
return False


BASE_VALIDATORS = {
VALIDATORS = {
'null': validate_null,
'boolean': validate_boolean,
'string': validate_string,
Expand All @@ -285,18 +286,6 @@ def validate_union(datum, schema, parent_ns=None, raise_errors=False):
'request': validate_record
}

VALIDATORS = BASE_VALIDATORS.copy()


def register_validator(record_type, validator):
if record_type in BASE_VALIDATORS:
raise ValueError("Not allowed to override Base Validators.")
VALIDATORS[record_type] = validator


def get_validator(record_type):
return VALIDATORS.get(record_type)


def validate(datum, schema, field=None, raise_errors=False):
"""Determine if a python datum is an instance of a schema."""
Expand All @@ -309,7 +298,7 @@ def validate(datum, schema, field=None, raise_errors=False):
elif field:
ns_field = field

validator = get_validator(record_type)
validator = VALIDATORS.get(record_type)
if validator:
result = validator(datum, schema=schema,
parent_ns=ns_field,
Expand Down
2 changes: 1 addition & 1 deletion fastavro/_write.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ from os import urandom
from zlib import compress

from fastavro import const
from ._validate import validate
from ._validation import validate
from ._six import utob, long, iteritems, mk_bits
from ._read import HEADER_SCHEMA, SYNC_SIZE, MAGIC
from ._schema import (
Expand Down
2 changes: 1 addition & 1 deletion fastavro/_write_py.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from struct import pack
from zlib import compress

from .validate import validate
from .validation import validate
from .const import (
MCS_PER_HOUR, MCS_PER_MINUTE, MCS_PER_SECOND, MLS_PER_HOUR, MLS_PER_MINUTE,
MLS_PER_SECOND, DAYS_SHIFT
Expand Down
42 changes: 0 additions & 42 deletions fastavro/validate.py

This file was deleted.

Loading