[ 
https://issues.apache.org/jira/browse/AVRO-3483?focusedWorklogId=755058&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-755058
 ]

ASF GitHub Bot logged work on AVRO-3483:
----------------------------------------

                Author: ASF GitHub Bot
            Created on: 10/Apr/22 23:48
            Start Date: 10/Apr/22 23:48
    Worklog Time Spent: 10m 
      Work Description: martin-g commented on code in PR #1636:
URL: https://github.com/apache/avro/pull/1636#discussion_r846866563


##########
lang/rust/avro/src/types.rs:
##########
@@ -365,50 +378,129 @@ impl Value {
             (&Value::Bytes(_), &Schema::Decimal { .. }) => true,
             (&Value::String(_), &Schema::String) => true,
             (&Value::String(_), &Schema::Uuid) => true,
-            (&Value::Fixed(n, _), &Schema::Fixed { size, .. }) => n == size,
-            (&Value::Bytes(ref b), &Schema::Fixed { size, .. }) => b.len() == 
size,
-            (&Value::Fixed(n, _), &Schema::Duration) => n == 12,
+            (&Value::Fixed(n, _), &Schema::Fixed { size, .. }) => {
+                let res = n == size;
+                if !res {
+                    reason = format!(
+                        "The value's size ({}) is different than the schema's 
size ({})",
+                        n, size
+                    );
+                }
+                res
+            }
+            (&Value::Bytes(ref b), &Schema::Fixed { size, .. }) => {
+                let res = b.len() == size;
+                if !res {
+                    reason = format!(
+                        "The bytes' length ({}) is different than the schema's 
size ({})",
+                        b.len(),
+                        size
+                    );
+                }
+                res
+            }
+            (&Value::Fixed(n, _), &Schema::Duration) => {
+                let res = n == 12;
+                if !res {
+                    reason = format!(
+                        "The value's size ('{}') must be exactly 12 to be a 
Duration",
+                        n
+                    );
+                }
+                res
+            }
             // TODO: check precision against n
             (&Value::Fixed(_n, _), &Schema::Decimal { .. }) => true,
-            (&Value::String(ref s), &Schema::Enum { ref symbols, .. }) => 
symbols.contains(s),
+            (&Value::String(ref s), &Schema::Enum { ref symbols, .. }) => {
+                let res = symbols.contains(s);
+                if !res {
+                    reason = format!("'{}' is not a member of the possible 
symbols", s);
+                }
+                res
+            }
             (&Value::Enum(i, ref s), &Schema::Enum { ref symbols, .. }) => 
symbols
                 .get(i as usize)
-                .map(|ref symbol| symbol == &s)
-                .unwrap_or(false),
+                .map(|ref symbol| {
+                    let res = symbol == &s;
+                    if !res {
+                        reason = format!("Symbol '{}' is not at position 
'{}'", s, i);
+                    }
+                    res
+                })
+                .unwrap_or_else(|| {
+                    reason = format!("No symbol at position '{}'", i);
+                    false
+                }),
             // (&Value::Union(None), &Schema::Union(_)) => true,
             (&Value::Union(i, ref value), &Schema::Union(ref inner)) => inner
                 .variants()
                 .get(i as usize)
-                .map(|schema| value.validate_internal(schema, names))
+                .map(|schema| {
+                    parent_or_leaf_check = false;
+                    value.validate_internal(schema, names)
+                })
                 .unwrap_or(false),

Review Comment:
   Thanks!
   I've also added a test case for it!





Issue Time Tracking
-------------------

    Worklog Id:     (was: 755058)
    Time Spent: 1h 10m  (was: 1h)

> [Rust] Log error messages with a reason when the validation fails
> -----------------------------------------------------------------
>
>                 Key: AVRO-3483
>                 URL: https://issues.apache.org/jira/browse/AVRO-3483
>             Project: Apache Avro
>          Issue Type: Improvement
>          Components: rust
>            Reporter: Martin Tzvetanov Grigorov
>            Assignee: Martin Tzvetanov Grigorov
>            Priority: Major
>              Labels: pull-request-available
>          Time Spent: 1h 10m
>  Remaining Estimate: 0h
>
> Currently types::Value::validate(Schema) just returns a boolean with the 
> result of the validation.
> It would be good if the Rust SDK logs the reason for a failed validation.



--
This message was sent by Atlassian Jira
(v8.20.1#820001)

Reply via email to