A complete data sanitization pipeline that processes multiple key-value pairs, using a native callback to perform custom email validation.

ID: 1371

				
					using uCalcSoftware;

var uc = new uCalc();

static void IsValidEmail(uCalc.Callback cb) {
   var email = cb.ArgStr(1);
   var uc = cb.uCalc;
   // Simple validation: check for '@' and '.'
   var isValid = uc.EvalStr("Contains('" + email + "', '@') And Contains('" + email + "', '@')");
   if (isValid == "true") {
      cb.ReturnBool(true);
   } else {
      cb.ReturnBool(false);
   }
}


// 1. Define the custom validation function in the uCalc engine
uc.DefineFunction("IsValidEmail(email As String) As Bool", IsValidEmail);

// 2. Create and configure the transformer
using (var t = new uCalc.Transformer(uc)) {
   // 3. Define the sanitization and validation rules
   t.FromTo("user = {val};", "User: {val},");
   t.FromTo("age = {val};", "Age: {val},");
   t.FromTo("status = {val}", "Status: {@Eval: UCase(val)}"); // Last rule, no trailing comma

   // The email rule uses the custom function for validation
   t.FromTo("email = {val};",
   "Email: {val} {@Eval: IIf(IsValidEmail(val), '(Valid)', '(INVALID)')},");

   // 4. Define the messy input strings
   var input1 = "user= Alice  ; age  =30 ; email= alice@ucalc.com ; status=active";
   var input2 = "user= Bob; age= 45; email= bob-at-ucalc ; status=inactive";

   // 5. Run the transformations
   Console.WriteLine(t.Transform(input1));
   Console.WriteLine(t.Transform(input2));
};
				
			
User: Alice, Age: 30, Email: alice@ucalc.com (Valid), Status: ACTIVE
User: Bob, Age: 45, Email: bob-at-ucalc (INVALID), Status: INACTIVE
				
					#include <iostream>
#include "uCalc.h"

using namespace std;
using namespace uCalcSoftware;

void ucalc_call IsValidEmail(uCalcBase::Callback cb) {
   auto email = cb.ArgStr(1);
   auto uc = cb.uCalc();
   // Simple validation: check for '@' and '.'
   auto isValid = uc.EvalStr("Contains('" + email + "', '@') And Contains('" + email + "', '@')");
   if (isValid == "true") {
      cb.ReturnBool(true);
   } else {
      cb.ReturnBool(false);
   }
}

int main() {
   uCalc uc;
   // 1. Define the custom validation function in the uCalc engine
   uc.DefineFunction("IsValidEmail(email As String) As Bool", IsValidEmail);

   // 2. Create and configure the transformer
   {
      uCalc::Transformer t(uc);
      t.Owned(); // Causes t to be released when it goes out of scope
      // 3. Define the sanitization and validation rules
      t.FromTo("user = {val};", "User: {val},");
      t.FromTo("age = {val};", "Age: {val},");
      t.FromTo("status = {val}", "Status: {@Eval: UCase(val)}"); // Last rule, no trailing comma

      // The email rule uses the custom function for validation
      t.FromTo("email = {val};",
      "Email: {val} {@Eval: IIf(IsValidEmail(val), '(Valid)', '(INVALID)')},");

      // 4. Define the messy input strings
      auto input1 = "user= Alice  ; age  =30 ; email= alice@ucalc.com ; status=active";
      auto input2 = "user= Bob; age= 45; email= bob-at-ucalc ; status=inactive";

      // 5. Run the transformations
      cout << t.Transform(input1) << endl;
      cout << t.Transform(input2) << endl;
   };
}
				
			
User: Alice, Age: 30, Email: alice@ucalc.com (Valid), Status: ACTIVE
User: Bob, Age: 45, Email: bob-at-ucalc (INVALID), Status: INACTIVE
				
					Imports System
Imports uCalcSoftware
Public Module Program
   
   Public Sub IsValidEmail(ByVal cb As uCalc.Callback)
      Dim email = cb.ArgStr(1)
      Dim uc = cb.uCalc
      '// Simple validation: check for '@' and '.'
      Dim isValid = uc.EvalStr("Contains('" + email + "', '@') And Contains('" + email + "', '@')")
      If isValid = "true" Then
         cb.ReturnBool(true)
      Else
         cb.ReturnBool(false)
      End If
   End Sub
   
   Public Sub Main()
      Dim uc As New uCalc()
      '// 1. Define the custom validation function in the uCalc engine
      uc.DefineFunction("IsValidEmail(email As String) As Bool", AddressOf IsValidEmail)
      
      '// 2. Create and configure the transformer
      Using t As New uCalc.Transformer(uc)
         '// 3. Define the sanitization and validation rules
         t.FromTo("user = {val};", "User: {val},")
         t.FromTo("age = {val};", "Age: {val},")
         t.FromTo("status = {val}", "Status: {@Eval: UCase(val)}") '// Last rule, no trailing comma
         
         '// The email rule uses the custom function for validation
         t.FromTo("email = {val};",
         "Email: {val} {@Eval: IIf(IsValidEmail(val), '(Valid)', '(INVALID)')},")
         
         '// 4. Define the messy input strings
         Dim input1 = "user= Alice  ; age  =30 ; email= alice@ucalc.com ; status=active"
         Dim input2 = "user= Bob; age= 45; email= bob-at-ucalc ; status=inactive"
         
         '// 5. Run the transformations
         Console.WriteLine(t.Transform(input1))
         Console.WriteLine(t.Transform(input2))
      End Using
   End Sub
End Module
				
			
User: Alice, Age: 30, Email: alice@ucalc.com (Valid), Status: ACTIVE
User: Bob, Age: 45, Email: bob-at-ucalc (INVALID), Status: INACTIVE