Repository: frictionlessdata/frictionless-py

Issue:
Improve error message: schemas with duplicate field names are not supported (using sync-schema)
# Overview

Using frictionless cli to validate a CSV file containing a duplicated field, I got this error:

```bash
frictionless validate --schema-sync --schema schema.json duplicate-column.csv

# -------
# invalid: duplicate-column.csv
# -------
============  =========================================================================
code          message                                                                  
============  =========================================================================
schema-error  Schema is not valid: Schemas with duplicate field names are not supported
============ ==========================================================================
``` 

IMHO, the error lies in the CSV file and not in the schema. I was expecting a more explicit error message from `frictionless`. 

## Resources

- schema.json
```json
{
  "fields": [{ "name": "A" }, { "name": "B" }, { "name": "C" }]
}
```

- duplicate-column.csv
```csv
A,B,B,C
a,b,b,c
```

[duplicate-column.csv](https://github.com/frictionlessdata/frictionless-py/files/7194624/duplicate-column.csv)
[schema.json.txt](https://github.com/frictionlessdata/frictionless-py/files/7194625/schema.json.txt)

## Note

Encountered problem is relative to `--sync-schema` option usage. Removing this option, errors are way more explicit (faulty `B` column is mentioned):

```bash
frictionless validate --schema schema.json duplicate-column.csv

# -------
# invalid: duplicate-column.csv
# -------

===  =====  ===============  =================================================================================
row  field  code             message                                                                          
===  =====  ===============  =================================================================================
         4  extra-label      There is an extra label "" in header at position "4"                             
         3  duplicate-label  Label "B" in the header at position "3" is duplicated to a label: at position "2"
  2      4  extra-cell       Row at position "2" has an extra value in field at position "4"                  
===  =====  ===============  =================================================================================
```
---

Please preserve this line to notify @roll (lead of this repository)


Discussion:

Patch:
diff --git a/data/duplicate-column-schema.json b/data/duplicate-column-schema.json
new file mode 100644
index 00000000..c3118852
--- /dev/null
+++ b/data/duplicate-column-schema.json
@@ -0,0 +1,3 @@
+{
+  "fields": [{ "name": "A" }, { "name": "B" }, { "name": "C" }]
+}
diff --git a/data/duplicate-column.csv b/data/duplicate-column.csv
new file mode 100644
index 00000000..06a11d5f
--- /dev/null
+++ b/data/duplicate-column.csv
@@ -0,0 +1,2 @@
+A,B,B,C
+a,b,C,c
\ No newline at end of file
diff --git a/frictionless/detector.py b/frictionless/detector.py
index 6e109751..e06d14e7 100644
--- a/frictionless/detector.py
+++ b/frictionless/detector.py
@@ -494,6 +494,9 @@ class Detector:
         # Validate schema
         # NOTE: at some point we might need to remove it for transform needs
         if len(schema.field_names) != len(set(schema.field_names)):
+            if self.__schema_sync:
+                note = 'Duplicate labels in header is not supported with "schema_sync"'
+                raise FrictionlessException(errors.GeneralError(note=note))
             note = "Schemas with duplicate field names are not supported"
             raise FrictionlessException(errors.SchemaError(note=note))
 


Test patch:
diff --git a/tests/validate/test_resource.py b/tests/validate/test_resource.py
index ea66c1ef..ab1b25a0 100644
--- a/tests/validate/test_resource.py
+++ b/tests/validate/test_resource.py
@@ -1184,3 +1184,18 @@ def test_validate_resource_array_path_issue_991():
             'Multipart resource requires "multipart" scheme but "file" is set',
         ],
     ]
+
+
+def test_validate_resource_duplicate_labels_with_sync_schema_issue_910():
+    detector = Detector(schema_sync=True)
+    report = validate(
+        "data/duplicate-column.csv",
+        schema="data/duplicate-column-schema.json",
+        detector=detector,
+    )
+    assert report.flatten(["code", "note"]) == [
+        [
+            "general-error",
+            'Duplicate labels in header is not supported with "schema_sync"',
+        ],
+    ]