Validates a configuration file format by enforcing the number of times specific keys must appear.
ID: 1450
using uCalcSoftware;
var uc = new uCalc();
using (var validator = new uCalc.Transformer()) {
// 1. Configure the transformer
validator.DefaultRuleSet.StatementSensitive = false;
validator.SkipOver(";{line}"); // Ignore comments
// 2. Define rules with validation constraints
var serverRule = validator.Pattern("'['Server']' {body}").SetMinimum(1).SetMaximum(1);
serverRule.Description = "Server Section";
var local_t = serverRule.LocalTransformer;
var hostRule = local_t.Pattern("Host = {@Alpha}").SetMinimum(1).SetMaximum(1);
hostRule.Description = "Host Key";
var portRule = local_t.Pattern("Port = {@Number}").SetMinimum(1);
portRule.Description = "Port Key";
// --- Test Data ---
var validConfig = "[Server]\nHost = db1\nPort = 1433";
var invalidConfig = "Host = web1\nPort = 80"; // Missing [Server] section
// --- Validate validConfig ---
Console.WriteLine("--- Validating valid_config.ini ---");
validator.SetText(validConfig).Find();
Console.WriteLine($" Server section check passed: {serverRule.Matches.Count() == 1}");
Console.WriteLine($" Host key check passed: {hostRule.Matches.Count() == 1}");
Console.WriteLine($" Port key check passed: {portRule.Matches.Count() >= 1}");
Console.WriteLine("");
// --- Validate invalidConfig ---
Console.WriteLine("--- Validating invalid_config.ini ---");
validator.SetText(invalidConfig).Find();
Console.WriteLine($" Server section check passed: {serverRule.Matches.Count() == 1}");
// The host and port rules will have 0 matches because their parent rule (serverRule) failed.
Console.WriteLine($" Host key check passed: {hostRule.Matches.Count() == 1}");
Console.WriteLine($" Port key check passed: {portRule.Matches.Count() >= 1}");
}
--- Validating valid_config.ini ---
Server section check passed: True
Host key check passed: True
Port key check passed: True
--- Validating invalid_config.ini ---
Server section check passed: False
Host key check passed: False
Port key check passed: False using uCalcSoftware; var uc = new uCalc(); using (var validator = new uCalc.Transformer()) { // 1. Configure the transformer validator.DefaultRuleSet.StatementSensitive = false; validator.SkipOver(";{line}"); // Ignore comments // 2. Define rules with validation constraints var serverRule = validator.Pattern("'['Server']' {body}").SetMinimum(1).SetMaximum(1); serverRule.Description = "Server Section"; var local_t = serverRule.LocalTransformer; var hostRule = local_t.Pattern("Host = {@Alpha}").SetMinimum(1).SetMaximum(1); hostRule.Description = "Host Key"; var portRule = local_t.Pattern("Port = {@Number}").SetMinimum(1); portRule.Description = "Port Key"; // --- Test Data --- var validConfig = "[Server]\nHost = db1\nPort = 1433"; var invalidConfig = "Host = web1\nPort = 80"; // Missing [Server] section // --- Validate validConfig --- Console.WriteLine("--- Validating valid_config.ini ---"); validator.SetText(validConfig).Find(); Console.WriteLine($" Server section check passed: {serverRule.Matches.Count() == 1}"); Console.WriteLine($" Host key check passed: {hostRule.Matches.Count() == 1}"); Console.WriteLine($" Port key check passed: {portRule.Matches.Count() >= 1}"); Console.WriteLine(""); // --- Validate invalidConfig --- Console.WriteLine("--- Validating invalid_config.ini ---"); validator.SetText(invalidConfig).Find(); Console.WriteLine($" Server section check passed: {serverRule.Matches.Count() == 1}"); // The host and port rules will have 0 matches because their parent rule (serverRule) failed. Console.WriteLine($" Host key check passed: {hostRule.Matches.Count() == 1}"); Console.WriteLine($" Port key check passed: {portRule.Matches.Count() >= 1}"); }
#include
#include "uCalc.h"
using namespace std;
using namespace uCalcSoftware;
#define tf(IsTrue) ((IsTrue) ? "True" : "False")
int main() {
uCalc uc;
{
uCalc::Transformer validator;
validator.Owned(); // Causes validator to be released when it goes out of scope
// 1. Configure the transformer
validator.DefaultRuleSet().StatementSensitive(false);
validator.SkipOver(";{line}"); // Ignore comments
// 2. Define rules with validation constraints
auto serverRule = validator.Pattern("'['Server']' {body}").SetMinimum(1).SetMaximum(1);
serverRule.Description("Server Section");
auto local_t = serverRule.LocalTransformer();
auto hostRule = local_t.Pattern("Host = {@Alpha}").SetMinimum(1).SetMaximum(1);
hostRule.Description("Host Key");
auto portRule = local_t.Pattern("Port = {@Number}").SetMinimum(1);
portRule.Description("Port Key");
// --- Test Data ---
auto validConfig = "[Server]\nHost = db1\nPort = 1433";
auto invalidConfig = "Host = web1\nPort = 80"; // Missing [Server] section
// --- Validate validConfig ---
cout << "--- Validating valid_config.ini ---" << endl;
validator.SetText(validConfig).Find();
cout << " Server section check passed: " << tf(serverRule.Matches().Count() == 1) << endl;
cout << " Host key check passed: " << tf(hostRule.Matches().Count() == 1) << endl;
cout << " Port key check passed: " << tf(portRule.Matches().Count() >= 1) << endl;
cout << "" << endl;
// --- Validate invalidConfig ---
cout << "--- Validating invalid_config.ini ---" << endl;
validator.SetText(invalidConfig).Find();
cout << " Server section check passed: " << tf(serverRule.Matches().Count() == 1) << endl;
// The host and port rules will have 0 matches because their parent rule (serverRule) failed.
cout << " Host key check passed: " << tf(hostRule.Matches().Count() == 1) << endl;
cout << " Port key check passed: " << tf(portRule.Matches().Count() >= 1) << endl;
}
}
--- Validating valid_config.ini ---
Server section check passed: True
Host key check passed: True
Port key check passed: True
--- Validating invalid_config.ini ---
Server section check passed: False
Host key check passed: False
Port key check passed: False #include <iostream> #include "uCalc.h" using namespace std; using namespace uCalcSoftware; #define tf(IsTrue) ((IsTrue) ? "True" : "False") int main() { uCalc uc; { uCalc::Transformer validator; validator.Owned(); // Causes validator to be released when it goes out of scope // 1. Configure the transformer validator.DefaultRuleSet().StatementSensitive(false); validator.SkipOver(";{line}"); // Ignore comments // 2. Define rules with validation constraints auto serverRule = validator.Pattern("'['Server']' {body}").SetMinimum(1).SetMaximum(1); serverRule.Description("Server Section"); auto local_t = serverRule.LocalTransformer(); auto hostRule = local_t.Pattern("Host = {@Alpha}").SetMinimum(1).SetMaximum(1); hostRule.Description("Host Key"); auto portRule = local_t.Pattern("Port = {@Number}").SetMinimum(1); portRule.Description("Port Key"); // --- Test Data --- auto validConfig = "[Server]\nHost = db1\nPort = 1433"; auto invalidConfig = "Host = web1\nPort = 80"; // Missing [Server] section // --- Validate validConfig --- cout << "--- Validating valid_config.ini ---" << endl; validator.SetText(validConfig).Find(); cout << " Server section check passed: " << tf(serverRule.Matches().Count() == 1) << endl; cout << " Host key check passed: " << tf(hostRule.Matches().Count() == 1) << endl; cout << " Port key check passed: " << tf(portRule.Matches().Count() >= 1) << endl; cout << "" << endl; // --- Validate invalidConfig --- cout << "--- Validating invalid_config.ini ---" << endl; validator.SetText(invalidConfig).Find(); cout << " Server section check passed: " << tf(serverRule.Matches().Count() == 1) << endl; // The host and port rules will have 0 matches because their parent rule (serverRule) failed. cout << " Host key check passed: " << tf(hostRule.Matches().Count() == 1) << endl; cout << " Port key check passed: " << tf(portRule.Matches().Count() >= 1) << endl; } }
Imports System
Imports uCalcSoftware
Public Module Program
Public Sub Main()
Dim uc As New uCalc()
Using validator As New uCalc.Transformer()
'// 1. Configure the transformer
validator.DefaultRuleSet.StatementSensitive = false
validator.SkipOver(";{line}") '// Ignore comments
'// 2. Define rules with validation constraints
Dim serverRule = validator.Pattern("'['Server']' {body}").SetMinimum(1).SetMaximum(1)
serverRule.Description = "Server Section"
Dim local_t = serverRule.LocalTransformer
Dim hostRule = local_t.Pattern("Host = {@Alpha}").SetMinimum(1).SetMaximum(1)
hostRule.Description = "Host Key"
Dim portRule = local_t.Pattern("Port = {@Number}").SetMinimum(1)
portRule.Description = "Port Key"
'// --- Test Data ---
Dim validConfig = "[Server]" & vbNewLine & "Host = db1" & vbNewLine & "Port = 1433"
Dim invalidConfig = "Host = web1" & vbNewLine & "Port = 80" '// Missing [Server] section
'// --- Validate validConfig ---
Console.WriteLine("--- Validating valid_config.ini ---")
validator.SetText(validConfig).Find()
Console.WriteLine($" Server section check passed: {serverRule.Matches.Count() = 1}")
Console.WriteLine($" Host key check passed: {hostRule.Matches.Count() = 1}")
Console.WriteLine($" Port key check passed: {portRule.Matches.Count() >= 1}")
Console.WriteLine("")
'// --- Validate invalidConfig ---
Console.WriteLine("--- Validating invalid_config.ini ---")
validator.SetText(invalidConfig).Find()
Console.WriteLine($" Server section check passed: {serverRule.Matches.Count() = 1}")
'// The host and port rules will have 0 matches because their parent rule (serverRule) failed.
Console.WriteLine($" Host key check passed: {hostRule.Matches.Count() = 1}")
Console.WriteLine($" Port key check passed: {portRule.Matches.Count() >= 1}")
End Using
End Sub
End Module
--- Validating valid_config.ini ---
Server section check passed: True
Host key check passed: True
Port key check passed: True
--- Validating invalid_config.ini ---
Server section check passed: False
Host key check passed: False
Port key check passed: False Imports System Imports uCalcSoftware Public Module Program Public Sub Main() Dim uc As New uCalc() Using validator As New uCalc.Transformer() '// 1. Configure the transformer validator.DefaultRuleSet.StatementSensitive = false validator.SkipOver(";{line}") '// Ignore comments '// 2. Define rules with validation constraints Dim serverRule = validator.Pattern("'['Server']' {body}").SetMinimum(1).SetMaximum(1) serverRule.Description = "Server Section" Dim local_t = serverRule.LocalTransformer Dim hostRule = local_t.Pattern("Host = {@Alpha}").SetMinimum(1).SetMaximum(1) hostRule.Description = "Host Key" Dim portRule = local_t.Pattern("Port = {@Number}").SetMinimum(1) portRule.Description = "Port Key" '// --- Test Data --- Dim validConfig = "[Server]" & vbNewLine & "Host = db1" & vbNewLine & "Port = 1433" Dim invalidConfig = "Host = web1" & vbNewLine & "Port = 80" '// Missing [Server] section '// --- Validate validConfig --- Console.WriteLine("--- Validating valid_config.ini ---") validator.SetText(validConfig).Find() Console.WriteLine($" Server section check passed: {serverRule.Matches.Count() = 1}") Console.WriteLine($" Host key check passed: {hostRule.Matches.Count() = 1}") Console.WriteLine($" Port key check passed: {portRule.Matches.Count() >= 1}") Console.WriteLine("") '// --- Validate invalidConfig --- Console.WriteLine("--- Validating invalid_config.ini ---") validator.SetText(invalidConfig).Find() Console.WriteLine($" Server section check passed: {serverRule.Matches.Count() = 1}") '// The host and port rules will have 0 matches because their parent rule (serverRule) failed. Console.WriteLine($" Host key check passed: {hostRule.Matches.Count() = 1}") Console.WriteLine($" Port key check passed: {portRule.Matches.Count() >= 1}") End Using End Sub End Module