frictionlessdata / forum

🗣 Frictionless Data Forum esp for "How do I" type questions
https://frictionlessdata.io/
10 stars 0 forks source link

Include row data in validation report errors #10

Closed didiez closed 4 years ago

didiez commented 4 years ago

I couldn't find a way to include the actual row values in validation errors.

In order to show the report using goodtables-ui, the errors must include the 'row' field to show the actual cell values.

The generated report without error.row fields, shows only the row-number, with empty cell values (using goodtables-ui as pluggable script from CDN).

Here's a self-explanatory, runnable script:

# !/usr/bin/python
# -*- coding: utf-8 -*-

from goodtables import validate
from goodtables import Error
import json

# example resource and schema
DESCRIPTOR = {
    "resources": [
        {
            "name": "people",
            "data": [
                ["id","name","surname","dob"],
                [1,"Stephen","Hawking","1942-01-08"],
                [2,"Marie","Curie","07/11/1867"],
                [3,"Ada","Lovelace","1815-12-10"],
                [4,None,"Darwin","1809-02-12"],
                [5,"Isaac","Newton","1642-12-25"],
                [5,"Galileo","Galilei","1564-02-15"],
                [7,"Leonardo","da Vinci","1452-04-15"]
            ],
        "schema": {
            "fields": [{
                    "name": "id",
                    "type": "number",
                    "constraints": {
                        "required": True,
                        "unique": True
                    }
                },
                {
                    "name": "name",
                    "type": "string",
                    "constraints": {
                        "required": True
                    }
                },
                {
                    "name": "surname",
                    "type": "string"
                },
                {
                    "name": "dob",
                    "type": "date",
                    "constraints": {
                        "required": True
                    }
                }
            ]
        }
    }]
}

# default report (without tables.errors.row)
report = validate(DESCRIPTOR)
print("error rows: ", [error["row"] for error in report["tables"][0]["errors"] if "row" in error])
print("report: ", json.dumps(report))

print("----------------------------------------------------------")

# Error._to_dict own implementation, adding 'row' field
def to_dict_include_row(self):
    return {
        'code': self.code,
        'row-number': self.row_number,
        'row': self.row,  # field required to show the row data using goodtables-ui
        'column-number': self.column_number,
        'message': self.message,
        'message-data': {k: v.strip('"') if isinstance(v, str) else v for k, v
                         in self._message_substitutions.items()},
    }

Error._to_dict = to_dict_include_row  # override the default _to_dict method

# 'fixed' report (includes tables.errors.row, required by goodtables-ui to show the cell values)
report = validate(DESCRIPTOR)
print("error rows: ", [error["row"] for error in report["tables"][0]["errors"] if "row" in error])
print("report: ", json.dumps(report))
roll commented 4 years ago

Hi @didiez,

It must've been a bug in the goodtables@2 update. I'm releasing goodtables@2.4.7 that should fix it.

Please re-open if it didn't help