Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean yaml validator false positive output #682

Merged
merged 3 commits into from
Jun 28, 2024
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
petrochuk marked this conversation as resolved.
Show resolved Hide resolved
{
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();
}
}
Loading