def validate_ex(expected_schema,                  <EOL>datum,                            <EOL>identifiers=None,                 <EOL>strict=False,                     <EOL>foreign_properties=None,          <EOL>raise_ex=True,                    <EOL>strict_foreign_properties=False,  <EOL>logger=_logger,                   <EOL>skip_foreign_properties=False     <EOL>):<EOL>
if not identifiers:<EOL><INDENT>identifiers = []<EOL><DEDENT>if not foreign_properties:<EOL><INDENT>foreign_properties = set()<EOL><DEDENT>schema_type = expected_schema.type<EOL>if schema_type == '<STR_LIT:null>':<EOL><INDENT>if datum is None:<EOL><INDENT>return True<EOL><DEDENT>else:<EOL><INDENT>if raise_ex:<EOL><INDENT>raise ValidationException(u"<STR_LIT>")<EOL><DEDENT>else:<EOL><INDENT>return False<EOL><DEDENT><DEDENT><DEDENT>elif schema_type == '<STR_LIT>':<EOL><INDENT>if isinstance(datum, bool):<EOL><INDENT>return True<EOL><DEDENT>else:<EOL><INDENT>if raise_ex:<EOL><INDENT>raise ValidationException(u"<STR_LIT>")<EOL><DEDENT>else:<EOL><INDENT>return False<EOL><DEDENT><DEDENT><DEDENT>elif schema_type == '<STR_LIT:string>':<EOL><INDENT>if isinstance(datum, six.string_types):<EOL><INDENT>return True<EOL><DEDENT>elif isinstance(datum, bytes):<EOL><INDENT>datum = datum.decode(u"<STR_LIT:utf-8>")<EOL>return True<EOL><DEDENT>else:<EOL><INDENT>if raise_ex:<EOL><INDENT>raise ValidationException(u"<STR_LIT>")<EOL><DEDENT>else:<EOL><INDENT>return False<EOL><DEDENT><DEDENT><DEDENT>elif schema_type == '<STR_LIT:int>':<EOL><INDENT>if (isinstance(datum, six.integer_types)<EOL>and INT_MIN_VALUE <= datum <= INT_MAX_VALUE):<EOL><INDENT>return True<EOL><DEDENT>else:<EOL><INDENT>if raise_ex:<EOL><INDENT>raise ValidationException(u"<STR_LIT>" % vpformat(datum))<EOL><DEDENT>else:<EOL><INDENT>return False<EOL><DEDENT><DEDENT><DEDENT>elif schema_type == '<STR_LIT>':<EOL><INDENT>if ((isinstance(datum, six.integer_types))<EOL>and LONG_MIN_VALUE <= datum <= LONG_MAX_VALUE):<EOL><INDENT>return True<EOL><DEDENT>else:<EOL><INDENT>if raise_ex:<EOL><INDENT>raise ValidationException(<EOL>u"<STR_LIT>" % vpformat(datum))<EOL><DEDENT>else:<EOL><INDENT>return False<EOL><DEDENT><DEDENT><DEDENT>elif schema_type in ['<STR_LIT:float>', '<STR_LIT>']:<EOL><INDENT>if (isinstance(datum, six.integer_types)<EOL>or isinstance(datum, float)):<EOL><INDENT>return True<EOL><DEDENT>else:<EOL><INDENT>if raise_ex:<EOL><INDENT>raise ValidationException(<EOL>u"<STR_LIT>" % vpformat(datum))<EOL><DEDENT>else:<EOL><INDENT>return False<EOL><DEDENT><DEDENT><DEDENT>elif isinstance(expected_schema, avro.schema.EnumSchema):<EOL><INDENT>if expected_schema.name == "<STR_LIT>":<EOL><INDENT>if datum is not None:<EOL><INDENT>return True<EOL><DEDENT>else:<EOL><INDENT>if raise_ex:<EOL><INDENT>raise ValidationException(u"<STR_LIT>")<EOL><DEDENT>else:<EOL><INDENT>return False<EOL><DEDENT><DEDENT><DEDENT>if not isinstance(datum, six.string_types):<EOL><INDENT>if raise_ex:<EOL><INDENT>raise ValidationException(<EOL>u"<STR_LIT>" % (type(datum).__name__))<EOL><DEDENT>else:<EOL><INDENT>return False<EOL><DEDENT><DEDENT>if expected_schema.name == "<STR_LIT>":<EOL><INDENT>if "<STR_LIT>" in datum or "<STR_LIT>" in datum:<EOL><INDENT>return True<EOL><DEDENT>if raise_ex:<EOL><INDENT>raise ValidationException(u"<STR_LIT>" % datum)<EOL><DEDENT>else:<EOL><INDENT>return False<EOL><DEDENT><DEDENT>if datum in expected_schema.symbols:<EOL><INDENT>return True<EOL><DEDENT>else:<EOL><INDENT>if raise_ex:<EOL><INDENT>raise ValidationException(u"<STR_LIT>" % (vpformat(datum), expected_schema.name,<EOL>"<STR_LIT>" if len(<EOL>expected_schema.symbols) > <NUM_LIT:1> else "<STR_LIT>",<EOL>"<STR_LIT:'>" + "<STR_LIT>".join(expected_schema.symbols) + "<STR_LIT:'>"))<EOL><DEDENT>else:<EOL><INDENT>return False<EOL><DEDENT><DEDENT><DEDENT>elif isinstance(expected_schema, avro.schema.ArraySchema):<EOL><INDENT>if isinstance(datum, MutableSequence):<EOL><INDENT>for i, d in enumerate(datum):<EOL><INDENT>try:<EOL><INDENT>sl = SourceLine(datum, i, ValidationException)<EOL>if not validate_ex(expected_schema.items, d, identifiers,<EOL>strict=strict,<EOL>foreign_properties=foreign_properties,<EOL>raise_ex=raise_ex,<EOL>strict_foreign_properties=strict_foreign_properties,<EOL>logger=logger,<EOL>skip_foreign_properties=skip_foreign_properties):<EOL><INDENT>return False<EOL><DEDENT><DEDENT>except ValidationException as v:<EOL><INDENT>if raise_ex:<EOL><INDENT>raise sl.makeError(<EOL>six.text_type("<STR_LIT>" % (indent(str(v)))))<EOL><DEDENT>else:<EOL><INDENT>return False<EOL><DEDENT><DEDENT><DEDENT>return True<EOL><DEDENT>else:<EOL><INDENT>if raise_ex:<EOL><INDENT>raise ValidationException(u"<STR_LIT>" % (<EOL>vpformat(datum), friendly(expected_schema.items)))<EOL><DEDENT>else:<EOL><INDENT>return False<EOL><DEDENT><DEDENT><DEDENT>elif isinstance(expected_schema, avro.schema.UnionSchema):<EOL><INDENT>for s in expected_schema.schemas:<EOL><INDENT>if validate_ex(s, datum, identifiers, strict=strict, raise_ex=False,<EOL>strict_foreign_properties=strict_foreign_properties,<EOL>logger=logger, skip_foreign_properties=skip_foreign_properties):<EOL><INDENT>return True<EOL><DEDENT><DEDENT>if not raise_ex:<EOL><INDENT>return False<EOL><DEDENT>errors = []  <EOL>checked = []<EOL>for s in expected_schema.schemas:<EOL><INDENT>if isinstance(datum, MutableSequence) and not isinstance(s, avro.schema.ArraySchema):<EOL><INDENT>continue<EOL><DEDENT>elif isinstance(datum, MutableMapping) and not isinstance(s, avro.schema.RecordSchema):<EOL><INDENT>continue<EOL><DEDENT>elif (isinstance(  <EOL>datum, (bool, six.integer_types, float, six.string_types))<EOL>and isinstance(s, (avro.schema.ArraySchema,<EOL>avro.schema.RecordSchema))):<EOL><INDENT>continue<EOL><DEDENT>elif datum is not None and s.type == "<STR_LIT:null>":<EOL><INDENT>continue<EOL><DEDENT>checked.append(s)<EOL>try:<EOL><INDENT>validate_ex(s, datum, identifiers, strict=strict,<EOL>foreign_properties=foreign_properties,<EOL>raise_ex=True,<EOL>strict_foreign_properties=strict_foreign_properties,<EOL>logger=logger, skip_foreign_properties=skip_foreign_properties)<EOL><DEDENT>except ClassValidationException as e:<EOL><INDENT>raise<EOL><DEDENT>except ValidationException as e:<EOL><INDENT>errors.append(six.text_type(e))<EOL><DEDENT><DEDENT>if bool(errors):<EOL><INDENT>raise ValidationException(bullets(["<STR_LIT>" % (friendly(<EOL>checked[i]), indent(errors[i])) for i in range(<NUM_LIT:0>, len(errors))], "<STR_LIT>"))<EOL><DEDENT>else:<EOL><INDENT>raise ValidationException("<STR_LIT>" % (<EOL>type(datum).__name__, friendly(expected_schema)))<EOL><DEDENT><DEDENT>elif isinstance(expected_schema, avro.schema.RecordSchema):<EOL><INDENT>if not isinstance(datum, MutableMapping):<EOL><INDENT>if raise_ex:<EOL><INDENT>raise ValidationException(u"<STR_LIT>")<EOL><DEDENT>else:<EOL><INDENT>return False<EOL><DEDENT><DEDENT>classmatch = None<EOL>for f in expected_schema.fields:<EOL><INDENT>if f.name in ("<STR_LIT:class>",):<EOL><INDENT>d = datum.get(f.name)<EOL>if not d:<EOL><INDENT>if raise_ex:<EOL><INDENT>raise ValidationException(<EOL>u"<STR_LIT>" % (f.name))<EOL><DEDENT>else:<EOL><INDENT>return False<EOL><DEDENT><DEDENT>if expected_schema.name != d:<EOL><INDENT>if raise_ex:<EOL><INDENT>raise ValidationException(<EOL>u"<STR_LIT>" % (expected_schema.name, d))<EOL><DEDENT>else:<EOL><INDENT>return False<EOL><DEDENT><DEDENT>classmatch = d<EOL>break<EOL><DEDENT><DEDENT>errors = []<EOL>for f in expected_schema.fields:<EOL><INDENT>if f.name in ("<STR_LIT:class>",):<EOL><INDENT>continue<EOL><DEDENT>if f.name in datum:<EOL><INDENT>fieldval = datum[f.name]<EOL><DEDENT>else:<EOL><INDENT>try:<EOL><INDENT>fieldval = f.default<EOL><DEDENT>except KeyError:<EOL><INDENT>fieldval = None<EOL><DEDENT><DEDENT>try:<EOL><INDENT>sl = SourceLine(datum, f.name, six.text_type)<EOL>if not validate_ex(f.type, fieldval, identifiers, strict=strict,<EOL>foreign_properties=foreign_properties,<EOL>raise_ex=raise_ex,<EOL>strict_foreign_properties=strict_foreign_properties,<EOL>logger=logger, skip_foreign_properties=skip_foreign_properties):<EOL><INDENT>return False<EOL><DEDENT><DEDENT>except ValidationException as v:<EOL><INDENT>if f.name not in datum:<EOL><INDENT>errors.append(u"<STR_LIT>" % f.name)<EOL><DEDENT>else:<EOL><INDENT>errors.append(sl.makeError(u"<STR_LIT>" % (<EOL>f.name, indent(str(v)))))<EOL><DEDENT><DEDENT><DEDENT>for d in datum:<EOL><INDENT>found = False<EOL>for f in expected_schema.fields:<EOL><INDENT>if d == f.name:<EOL><INDENT>found = True<EOL><DEDENT><DEDENT>if not found:<EOL><INDENT>sl = SourceLine(datum, d, six.text_type)<EOL>if d not in identifiers and d not in foreign_properties and d[<NUM_LIT:0>] not in ("<STR_LIT:@>", "<STR_LIT:$>"):<EOL><INDENT>if (d not in identifiers and strict) and (<EOL>d not in foreign_properties and strict_foreign_properties and not skip_foreign_properties) and not raise_ex:<EOL><INDENT>return False<EOL><DEDENT>split = urllib.parse.urlsplit(d)<EOL>if split.scheme:<EOL><INDENT>if not skip_foreign_properties:<EOL><INDENT>err = sl.makeError(u"<STR_LIT>"<EOL>% (d,<EOL>"<STR_LIT>"<EOL>if strict_foreign_properties else "<STR_LIT>",<EOL>"<STR_LIT>" % "<STR_LIT>".join(sorted(foreign_properties))<EOL>if len(foreign_properties) > <NUM_LIT:0> else "<STR_LIT>"))<EOL>if strict_foreign_properties:<EOL><INDENT>errors.append(err)<EOL><DEDENT>elif len(foreign_properties) > <NUM_LIT:0>:<EOL><INDENT>logger.warning(strip_dup_lineno(err))<EOL><DEDENT><DEDENT><DEDENT>else:<EOL><INDENT>err = sl.makeError(u"<STR_LIT>" % (<EOL>d, "<STR_LIT:U+002CU+0020>".join("<STR_LIT>" % fn.name for fn in expected_schema.fields)))<EOL>if strict:<EOL><INDENT>errors.append(err)<EOL><DEDENT>else:<EOL><INDENT>logger.warning(err)<EOL><DEDENT><DEDENT><DEDENT><DEDENT><DEDENT>if bool(errors):<EOL><INDENT>if raise_ex:<EOL><INDENT>if classmatch:<EOL><INDENT>raise ClassValidationException(bullets(errors, "<STR_LIT>"))<EOL><DEDENT>else:<EOL><INDENT>raise ValidationException(bullets(errors, "<STR_LIT>"))<EOL><DEDENT><DEDENT>else:<EOL><INDENT>return False<EOL><DEDENT><DEDENT>else:<EOL><INDENT>return True<EOL><DEDENT><DEDENT>if raise_ex:<EOL><INDENT>raise ValidationException(u"<STR_LIT>" % schema_type)<EOL><DEDENT>else:<EOL><INDENT>return False<EOL><DEDENT>
Determine if a python datum is an instance of a schema.