diff --git a/src/Reveal.Sdk.Dom.Tests/Core/Utilities/RdashDocumentValidatorFixture.cs b/src/Reveal.Sdk.Dom.Tests/Core/Utilities/RdashDocumentValidatorFixture.cs index b3d9c923..ea46dce8 100644 --- a/src/Reveal.Sdk.Dom.Tests/Core/Utilities/RdashDocumentValidatorFixture.cs +++ b/src/Reveal.Sdk.Dom.Tests/Core/Utilities/RdashDocumentValidatorFixture.cs @@ -80,18 +80,44 @@ public void Validate_Adds_Fields() var document = new RdashDocument(); document.Visualizations.Add(new GridVisualization(dataSourceItem)); - Assert.Empty(document.Visualizations[0].DataDefinition.AsTabular().Fields); - RdashDocumentValidator.Validate(document); Assert.Single(document.Visualizations[0].DataDefinition.AsTabular().Fields); } + [Fact] + public void Validate_ThrowsException_When_Fields_Are_Null() + { + var dataSourceItem = new DataSourceItem("Test", new DataSource()); + dataSourceItem.Fields = null; + + var document = new RdashDocument(); + document.Visualizations.Add(new GridVisualization(dataSourceItem)); + + var exception = Assert.Throws(() => RdashDocumentValidator.Validate(document)); + + Assert.Equal("Fields for DataSourceItem Test is null or empty.", exception.Message); + } + + [Fact] + public void Validate_ThrowsException_When_Fields_Are_Empty() + { + var dataSourceItem = new DataSourceItem("Test", new DataSource()).SetFields(new List()); + + var document = new RdashDocument(); + document.Visualizations.Add(new GridVisualization(dataSourceItem)); + + var exception = Assert.Throws(() => RdashDocumentValidator.Validate(document)); + + Assert.Equal("Fields for DataSourceItem Test is null or empty.", exception.Message); + } + [Fact] public void Validate_PreventsDuplicate_Fields() { var dataSourceItem = new DataSourceItem("Test", new DataSource()).SetFields(new List { + new TextField("Test"), new TextField("Test"), new TextField("Test") }); @@ -99,8 +125,6 @@ public void Validate_PreventsDuplicate_Fields() var document = new RdashDocument(); document.Visualizations.Add(new GridVisualization(dataSourceItem)); - Assert.Empty(document.Visualizations[0].DataDefinition.AsTabular().Fields); - RdashDocumentValidator.Validate(document); Assert.Single(document.Visualizations[0].DataDefinition.AsTabular().Fields); diff --git a/src/Reveal.Sdk.Dom/Core/Utilities/RdashDocumentValidator.cs b/src/Reveal.Sdk.Dom/Core/Utilities/RdashDocumentValidator.cs index be8acaa1..99dd10d1 100644 --- a/src/Reveal.Sdk.Dom/Core/Utilities/RdashDocumentValidator.cs +++ b/src/Reveal.Sdk.Dom/Core/Utilities/RdashDocumentValidator.cs @@ -36,27 +36,15 @@ static void FixVisualizations(RdashDocument document) private static void FixFields(TabularDataDefinition tdd) { - if (tdd.DataSourceItem.Fields?.Count != 0) - { - // Create a HashSet to track added field names - HashSet fieldNames = new HashSet(tdd.Fields.Select(f => f.FieldName)); - - foreach (var field in tdd.DataSourceItem.Fields.Clone()) - { - if (field == null) - throw new Exception($"Field for DataSourceItem {tdd.DataSourceItem.Title} is null."); + if (tdd.Fields?.Count == 0) + throw new Exception($"Fields for DataSourceItem {tdd.DataSourceItem.Title} is null or empty."); - //prevent adding duplicate fields - if (!fieldNames.Contains(field.FieldName)) - { - tdd.Fields.Add(field); - fieldNames.Add(field.FieldName); - } - } + // Check if there are duplicates + var hasDuplicates = tdd.Fields.GroupBy(f => f.FieldName).Any(g => g.Count() > 1); + if (hasDuplicates) + { + tdd.Fields = tdd.Fields.GroupBy(f => f.FieldName).Select(g => g.First()).ToList(); } - - if (tdd.Fields?.Count == 0) - throw new Exception($"Fields for DataSourceItem {tdd.DataSourceItem.Title} is null."); } static void FixJoinedTables(TabularDataDefinition tdd)