Skip to content

Commit

Permalink
Clean yaml validator false positive output (#682)
Browse files Browse the repository at this point in the history
  • Loading branch information
abaskk-msft authored Jun 28, 2024
1 parent 0bffc34 commit 96f0efa
Showing 1 changed file with 60 additions and 1 deletion.
61 changes: 60 additions & 1 deletion src/YamlValidator/ValidatorResults.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using System.Collections.ObjectModel;

namespace Microsoft.PowerPlatform.PowerApps.Persistence.YamlValidator;
public class ValidatorResults
{
Expand All @@ -10,6 +12,63 @@ public class ValidatorResults
public ValidatorResults(bool schemaValid, IReadOnlyList<ValidatorError> traversalResults)
{
SchemaValid = schemaValid;
TraversalResults = traversalResults;
TraversalResults = FilterErrors(traversalResults);
}

// This will filter out the false positives that are not relevant to the error output, when the validation is false
private ReadOnlyCollection<ValidatorError> FilterErrors(IReadOnlyList<ValidatorError> traversalResults)
{
var maxSchemaArraySuffixSize = 0;
var maxSchemaObjectSuffixSize = 0;
var arrayTypeSchemaPath = "/oneOf/1";
var objectTypeSchemaPath = "/oneOf/0";
foreach (var err in traversalResults)
{
var errSchemaPath = err.SchemaPath;
if (!errSchemaPath.StartsWith(arrayTypeSchemaPath, StringComparison.Ordinal) &&
!err.SchemaPath.StartsWith(objectTypeSchemaPath, StringComparison.Ordinal))
{
continue;
}

var suffixLength = errSchemaPath.Length - arrayTypeSchemaPath.Length;
if (errSchemaPath.StartsWith(arrayTypeSchemaPath, StringComparison.Ordinal))
{
maxSchemaArraySuffixSize = Math.Max(maxSchemaArraySuffixSize, suffixLength);
}
else
{
maxSchemaObjectSuffixSize = Math.Max(maxSchemaObjectSuffixSize, suffixLength);
}
}
var filteredErrors = new List<ValidatorError>();
foreach (var err in traversalResults)
{
var errSchemaPath = err.SchemaPath;
if (!errSchemaPath.StartsWith(arrayTypeSchemaPath, StringComparison.Ordinal) &&
!err.SchemaPath.StartsWith(objectTypeSchemaPath, StringComparison.Ordinal))
{
filteredErrors.Add(err);
continue;
}

if (errSchemaPath.StartsWith(arrayTypeSchemaPath, StringComparison.Ordinal))
{
if (maxSchemaArraySuffixSize >= maxSchemaObjectSuffixSize)
{
filteredErrors.Add(err);
}
}
else
{
if (maxSchemaObjectSuffixSize >= maxSchemaArraySuffixSize)
{
filteredErrors.Add(err);
}
}

}

return filteredErrors.AsReadOnly();
}
}

0 comments on commit 96f0efa

Please sign in to comment.