39 public static function getErrors( AvroSchema $schema, $datum ) {
40 switch ( $schema->type ) {
41 case AvroSchema::NULL_TYPE:
42 if ( !is_null( $datum ) ) {
43 return self::wrongType(
'null', $datum );
46 case AvroSchema::BOOLEAN_TYPE:
47 if ( !is_bool( $datum ) ) {
48 return self::wrongType(
'boolean', $datum );
51 case AvroSchema::STRING_TYPE:
52 case AvroSchema::BYTES_TYPE:
53 if ( !is_string( $datum ) ) {
54 return self::wrongType(
'string', $datum );
57 case AvroSchema::INT_TYPE:
58 if ( !is_int( $datum ) ) {
59 return self::wrongType(
'integer', $datum );
61 if ( AvroSchema::INT_MIN_VALUE > $datum
62 || $datum > AvroSchema::INT_MAX_VALUE
64 return self::outOfRange(
65 AvroSchema::INT_MIN_VALUE,
66 AvroSchema::INT_MAX_VALUE,
71 case AvroSchema::LONG_TYPE:
72 if ( !is_int( $datum ) ) {
73 return self::wrongType(
'integer', $datum );
75 if ( AvroSchema::LONG_MIN_VALUE > $datum
76 || $datum > AvroSchema::LONG_MAX_VALUE
78 return self::outOfRange(
79 AvroSchema::LONG_MIN_VALUE,
80 AvroSchema::LONG_MAX_VALUE,
85 case AvroSchema::FLOAT_TYPE:
86 case AvroSchema::DOUBLE_TYPE:
87 if ( !is_float( $datum ) && !is_int( $datum ) ) {
88 return self::wrongType(
'float or integer', $datum );
91 case AvroSchema::ARRAY_SCHEMA:
92 if ( !is_array( $datum ) ) {
93 return self::wrongType(
'array', $datum );
96 foreach ( $datum as $d ) {
97 $result = self::getErrors( $schema->items(), $d );
103 case AvroSchema::MAP_SCHEMA:
104 if ( !is_array( $datum ) ) {
105 return self::wrongType(
'array', $datum );
108 foreach ( $datum as $k => $v ) {
109 if ( !is_string( $k ) ) {
110 $errors[] = self::wrongType(
'string key', $k );
112 $result = self::getErrors( $schema->values(), $v );
114 $errors[$k] = $result;
118 case AvroSchema::UNION_SCHEMA:
120 foreach ( $schema->schemas() as $schema ) {
121 $result = self::getErrors( $schema, $datum );
129 return [
"Expected any one of these to be true", $errors ];
131 return "No schemas provided to union";
132 case AvroSchema::ENUM_SCHEMA:
133 if ( !in_array( $datum, $schema->symbols() ) ) {
134 $symbols = implode(
', ', $schema->symbols );
135 return "Expected one of $symbols but recieved $datum";
138 case AvroSchema::FIXED_SCHEMA:
139 if ( !is_string( $datum ) ) {
140 return self::wrongType(
'string', $datum );
142 $len = strlen( $datum );
143 if ( $len !== $schema->size() ) {
144 return "Expected string of length {$schema->size()}, "
145 .
"but recieved one of length $len";
148 case AvroSchema::RECORD_SCHEMA:
149 case AvroSchema::ERROR_SCHEMA:
150 case AvroSchema::REQUEST_SCHEMA:
151 if ( !is_array( $datum ) ) {
152 return self::wrongType(
'array', $datum );
155 foreach ( $schema->fields() as $field ) {
156 $name = $field->name();
157 if ( !array_key_exists( $name, $datum ) ) {
158 $errors[$name] =
'Missing expected field';
161 $result = self::getErrors( $field->type(), $datum[$name] );
163 $errors[$name] = $result;
168 return "Unknown avro schema type: {$schema->type}";