Marusyk / grok.net

.NET implementation of the grok 📝
MIT License
287 stars 55 forks source link

Grok Pattern Validation Enhancement #77

Closed MattFromRVA closed 9 months ago

MattFromRVA commented 10 months ago

Issue Description: The current implementation of Grok does not validate if the Grok patterns specified in an expression are defined in the loaded patterns. This can lead to runtime errors when there are typos or usage of undefined patterns.

Proposed Enhancement: I propose adding a method to validate Grok patterns against the set of loaded patterns. This method will ensure each pattern used in a Grok expression is defined. Here's the proposed implementation:

private void ValidateGrokPattern(string grokPattern)
{
    var grokPatternRegex = new Regex("%\\{(.*?)(?::\\w+)?\\}");
    MatchCollection matches = grokPatternRegex.Matches(grokPattern);

    foreach (Match match in matches.Cast<Match>())
    {
        var patternName = match.Groups[1].Value;
        if (!_patterns.ContainsKey(patternName))
        {
            throw new FormatException($"Invalid Grok pattern: Pattern '{patternName}' not found.");
        }
    }
}

If you agree on this enhancement I can be assigned this issue and will submit a PR. Thank you.

Marusyk commented 9 months ago

Hi @MattFromRVA Sounds like a plan. Please, go on :)

MattFromRVA commented 9 months ago

Thanks @Marusyk. PR has been submitted.