Skip to content

Conversation

@gfs
Copy link
Contributor

@gfs gfs commented Sep 10, 2025

Introduces JSON schema validation for Application Inspector rules using JsonSchema.Net. Adds RuleSchemaProvider and rule-schema.json, updates RulesVerifier and RuleStatus to support schema validation, and extends RulesVerifierOptions for configuration. Includes unit tests for schema validation in SchemaValidationTests.

Introduces JSON schema validation for Application Inspector rules using JsonSchema.Net. Adds RuleSchemaProvider and rule-schema.json, updates RulesVerifier and RuleStatus to support schema validation, and extends RulesVerifierOptions for configuration. Includes unit tests for schema validation in SchemaValidationTests.
@gfs gfs requested a review from Copilot September 10, 2025 21:17
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR introduces JSON schema validation for Application Inspector rules using JsonSchema.Net library. It adds comprehensive schema validation capabilities to ensure rules conform to a defined JSON schema, helping catch structural issues early in the development process.

  • Adds RuleSchemaProvider class for validating rules against embedded JSON schema
  • Updates RulesVerifier and RuleStatus to support schema validation reporting
  • Extends RulesVerifierOptions with new configuration options for schema validation

Reviewed Changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
AppInspector.Tests/RuleProcessor/SchemaValidationTests.cs Comprehensive unit tests for schema validation functionality
AppInspector.RulesEngine/Schema/rule-schema.json JSON schema definition for Application Inspector rules
AppInspector.RulesEngine/Schema/RuleSchemaProvider.cs Provider class for loading and validating rules against schema
AppInspector.RulesEngine/RulesVerifierOptions.cs Configuration options for enabling schema validation
AppInspector.RulesEngine/RulesVerifier.cs Integration of schema validation into rule verification process
AppInspector.RulesEngine/RuleStatus.cs Schema validation result tracking in rule status
AppInspector.RulesEngine/AppInspector.RulesEngine.csproj Package reference for JsonSchema.Net library

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@gfs gfs requested a review from Copilot September 10, 2025 21:25
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

Copilot reviewed 7 out of 7 changed files in this pull request and generated 4 comments.


Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

Comment on lines 49 to 62
// Debug: Check what JSON is generated
var json = System.Text.Json.JsonSerializer.Serialize(new[] { rule }, new System.Text.Json.JsonSerializerOptions
{
WriteIndented = true
});

var result = provider.ValidateRule(rule);

// Debug: Show what errors we're getting
if (!result.IsValid)
{
var errorMessages = string.Join("; ", result.Errors.Select(e => $"'{e.Message}' at '{e.Path}' (Type: {e.ErrorType})"));
throw new Exception($"Schema validation failed. JSON: {json}\n\nErrors: {errorMessages}");
}
Copy link

Copilot AI Sep 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Debug code should be removed from unit tests. The JSON serialization and debug exception are not needed for the test and add unnecessary complexity.

Suggested change
// Debug: Check what JSON is generated
var json = System.Text.Json.JsonSerializer.Serialize(new[] { rule }, new System.Text.Json.JsonSerializerOptions
{
WriteIndented = true
});
var result = provider.ValidateRule(rule);
// Debug: Show what errors we're getting
if (!result.IsValid)
{
var errorMessages = string.Join("; ", result.Errors.Select(e => $"'{e.Message}' at '{e.Path}' (Type: {e.ErrorType})"));
throw new Exception($"Schema validation failed. JSON: {json}\n\nErrors: {errorMessages}");
}
var result = provider.ValidateRule(rule);

Copilot uses AI. Check for mistakes.
var provider = new RuleSchemaProvider();

// Load one of the actual default rule files
var ruleFilePath = Path.Combine("AppInspector", "rules", "default", "components", "load_dll.json");
Copy link

Copilot AI Sep 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hard-coded file path makes the test brittle and dependent on specific project structure. Consider using a relative path or test resource that's guaranteed to exist.

Copilot uses AI. Check for mistakes.
{
var json = JsonSerializer.Serialize(rules, new JsonSerializerOptions
{
WriteIndented = true
Copy link

Copilot AI Sep 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using WriteIndented = true adds unnecessary formatting overhead for validation purposes. Consider removing this option since the JSON is only used for validation, not display.

Suggested change
WriteIndented = true

Copilot uses AI. Check for mistakes.
{
foreach (var error in validationResult.Errors)
{
var errorMessage = $"Schema validation error at {error.Path}: {error.Message} (Line: {error.LineNumber}, Position: {error.LinePosition})";
Copy link

Copilot AI Sep 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The LineNumber and LinePosition properties are always 0 based on the SchemaValidationError class definition, making this error message misleading. These properties aren't populated by the validation logic.

Copilot uses AI. Check for mistakes.
gfs added 2 commits September 10, 2025 14:38
Changed 'must-match' and 'must-not-match' in rule-schema.json to arrays of strings with updated descriptions. Enhanced SchemaValidationTests to validate all embedded default rules using RuleSetUtils and provide detailed error reporting for failed schema validations.
Adds a SchemaValidationResult property to Rule and updates TypedRuleSet to validate and store schema results when loading rules from JSON. RulesVerifier now uses the cached schema validation result if available, avoiding redundant validations. Also updates constructors to pass the schema provider and simplifies JSON serialization in RuleSchemaProvider.
@gfs gfs requested a review from Copilot September 10, 2025 22:05
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

Copilot reviewed 10 out of 10 changed files in this pull request and generated 3 comments.


Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

gfs added 3 commits September 10, 2025 15:09
Eliminated the inclusion of line number and position details from schema validation error and warning messages in RulesVerifier. Also removed the corresponding properties from the RuleSchemaProvider error class, simplifying error reporting.
Eliminated commented-out and unused debug code related to JSON serialization and error output in SchemaValidationTests. This cleanup improves test readability and removes unnecessary code.
Introduces multiple unit tests to verify that invalid rule definitions fail schema validation as expected. Tests cover invalid pattern types, confidence levels, severities, scopes, empty arrays, missing required fields, and schema validation error handling at both error and warning levels.
@gfs gfs requested a review from Copilot September 10, 2025 22:19
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

Copilot reviewed 10 out of 10 changed files in this pull request and generated 1 comment.


Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

gfs and others added 3 commits September 10, 2025 15:23
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Moved rule-schema.json to the project root and updated its $id. Added rule-schema-v1.json as a new versioned schema file. Updated project and code references to use the new rule-schema-v1.json as the embedded resource.
@gfs gfs requested a review from Copilot September 10, 2025 22:34
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

Copilot reviewed 11 out of 11 changed files in this pull request and generated 5 comments.


Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

Comment on lines 124 to 142
// Check if there are any details with errors
if (result.Details != null)
{
foreach (var detail in result.Details)
{
if (!detail.IsValid)
{
var path = detail.InstanceLocation?.ToString() ?? currentPath;
var schemaPath = detail.SchemaLocation?.ToString() ?? "";

errors.Add(new SchemaValidationError
{
Message = $"Validation failed at schema location '{schemaPath}'",
Path = path,
ErrorType = "SchemaViolation"
});

// Recursively collect errors from nested details
CollectErrorsFromResult(detail, errors, path);
Copy link

Copilot AI Sep 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The recursive error collection method could potentially cause stack overflow with deeply nested schema violations. Consider implementing an iterative approach using a stack or queue to avoid deep recursion.

Suggested change
// Check if there are any details with errors
if (result.Details != null)
{
foreach (var detail in result.Details)
{
if (!detail.IsValid)
{
var path = detail.InstanceLocation?.ToString() ?? currentPath;
var schemaPath = detail.SchemaLocation?.ToString() ?? "";
errors.Add(new SchemaValidationError
{
Message = $"Validation failed at schema location '{schemaPath}'",
Path = path,
ErrorType = "SchemaViolation"
});
// Recursively collect errors from nested details
CollectErrorsFromResult(detail, errors, path);
// Use an explicit stack to avoid recursion and potential stack overflow
var stack = new Stack<(EvaluationResults node, string path)>();
stack.Push((result, currentPath));
while (stack.Count > 0)
{
var (currentResult, pathSoFar) = stack.Pop();
if (currentResult.Details != null)
{
foreach (var detail in currentResult.Details)
{
if (!detail.IsValid)
{
var path = detail.InstanceLocation?.ToString() ?? pathSoFar;
var schemaPath = detail.SchemaLocation?.ToString() ?? "";
errors.Add(new SchemaValidationError
{
Message = $"Validation failed at schema location '{schemaPath}'",
Path = path,
ErrorType = "SchemaViolation"
});
// Push nested details onto the stack for further processing
stack.Push((detail, path));
}

Copilot uses AI. Check for mistakes.
gfs added 2 commits September 10, 2025 15:40
Replaces deprecated Assert.Empty with Assert.Fail in SchemaValidationTests and simplifies type references for schema validation result and error classes. Also removes the rule-schema.json file, possibly indicating a migration to a different schema management approach.
Replaces recursive calls in CollectErrorsFromResult with an explicit stack to avoid potential stack overflow on deeply nested validation results. This change improves robustness when processing large or deeply nested schemas.
@gfs gfs merged commit 44d7fd4 into main Sep 24, 2025
14 checks passed
@gfs gfs deleted the gfs/SchemaValidation branch September 24, 2025 17:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants