Practical: Validates a configuration file format by enforcing the number of times specific keys must appear.

ID: 1342

				
					using uCalcSoftware;

var uc = new uCalc();
using (var validator = new uCalc.Transformer()) {
   
   // Rule 1: Must contain exactly one 'Host' setting.
   var hostRule = validator.Pattern("Host: {@Alpha}").SetMinimum(1).SetMaximum(1);

   // Rule 2: Must contain at least one 'Port' setting.
   var portRule = validator.Pattern("Port: {@Number}").SetMinimum(1);

   var configFile_OK = """

Host: server1
Port: 80
Port: 443

""";
   var configFile_FAIL = "Port: 80"; // Missing Host

   Console.WriteLine("--- Testing Valid Config ---");
   validator.SetText(configFile_OK).Find();
   if (hostRule.Matches.Count() > 0 && portRule.Matches.Count() > 0) {
      Console.WriteLine("Config file is valid.");
   } else {
      Console.WriteLine("Config file is invalid.");
   }
   Console.WriteLine();
   Console.WriteLine("--- Testing Invalid Config ---");
   validator.SetText(configFile_FAIL).Find();
   // The Minimum(1) on hostRule causes it to have 0 matches if none are found.
   if (hostRule.Matches.Count() > 0 && portRule.Matches.Count() > 0) {
      Console.WriteLine("Config file is valid.");
   } else {
      Console.WriteLine("Config file is invalid.");
      if (hostRule.Matches.Count() == 0) {
         Console.WriteLine($"- Reason: Host rule failed (found {hostRule.Matches.Count()}, expected 1).");
      }
   }
}
				
			
--- Testing Valid Config ---
Config file is valid.

--- Testing Invalid Config ---
Config file is invalid.
- Reason: Host rule failed (found 0, expected 1).
				
					#include <iostream>
#include "uCalc.h"

using namespace std;
using namespace uCalcSoftware;

int main() {
   uCalc uc;
   {
      uCalc::Transformer validator;
      validator.Owned(); // Causes validator to be released when it goes out of scope

      // Rule 1: Must contain exactly one 'Host' setting.
      auto hostRule = validator.Pattern("Host: {@Alpha}").SetMinimum(1).SetMaximum(1);

      // Rule 2: Must contain at least one 'Port' setting.
      auto portRule = validator.Pattern("Port: {@Number}").SetMinimum(1);

      auto configFile_OK = R"(
Host: server1
Port: 80
Port: 443
)";
      auto configFile_FAIL = "Port: 80"; // Missing Host

      cout << "--- Testing Valid Config ---" << endl;
      validator.SetText(configFile_OK).Find();
      if (hostRule.Matches().Count() > 0 && portRule.Matches().Count() > 0) {
         cout << "Config file is valid." << endl;
      } else {
         cout << "Config file is invalid." << endl;
      }
      cout << endl;
      cout << "--- Testing Invalid Config ---" << endl;
      validator.SetText(configFile_FAIL).Find();
      // The Minimum(1) on hostRule causes it to have 0 matches if none are found.
      if (hostRule.Matches().Count() > 0 && portRule.Matches().Count() > 0) {
         cout << "Config file is valid." << endl;
      } else {
         cout << "Config file is invalid." << endl;
         if (hostRule.Matches().Count() == 0) {
            cout << "- Reason: Host rule failed (found " << hostRule.Matches().Count() << ", expected 1)." << endl;
         }
      }
   }
}
				
			
--- Testing Valid Config ---
Config file is valid.

--- Testing Invalid Config ---
Config file is invalid.
- Reason: Host rule failed (found 0, expected 1).
				
					Imports System
Imports uCalcSoftware
Public Module Program
   Public Sub Main()
      Dim uc As New uCalc()
      Using validator As New uCalc.Transformer()
         
         '// Rule 1: Must contain exactly one 'Host' setting.
         Dim hostRule = validator.Pattern("Host: {@Alpha}").SetMinimum(1).SetMaximum(1)
         
         '// Rule 2: Must contain at least one 'Port' setting.
         Dim portRule = validator.Pattern("Port: {@Number}").SetMinimum(1)
         
         Dim configFile_OK = "
Host: server1
Port: 80
Port: 443
"
         Dim configFile_FAIL = "Port: 80" '// Missing Host
         
         Console.WriteLine("--- Testing Valid Config ---")
         validator.SetText(configFile_OK).Find()
         If hostRule.Matches.Count() > 0 And portRule.Matches.Count() > 0 Then
            Console.WriteLine("Config file is valid.")
         Else
            Console.WriteLine("Config file is invalid.")
         End If
         Console.WriteLine()
         Console.WriteLine("--- Testing Invalid Config ---")
         validator.SetText(configFile_FAIL).Find()
         '// The Minimum(1) on hostRule causes it to have 0 matches if none are found.
         If hostRule.Matches.Count() > 0 And portRule.Matches.Count() > 0 Then
            Console.WriteLine("Config file is valid.")
         Else
            Console.WriteLine("Config file is invalid.")
            If hostRule.Matches.Count() = 0 Then
               Console.WriteLine($"- Reason: Host rule failed (found {hostRule.Matches.Count()}, expected 1).")
            End If
         End If
      End Using
   End Sub
End Module
				
			
--- Testing Valid Config ---
Config file is valid.

--- Testing Invalid Config ---
Config file is invalid.
- Reason: Host rule failed (found 0, expected 1).