CFLib.org – Common Function Library Project

Assert(assertion)

Last updated September 23, 2004

author

Dean Chalk

Version: 2 | Requires: CF5 | Library: DataManipulationLib

Description:
This function when passed an assertion rule string will validate assertions given, which enables the user to parse multiple variables against multiple rule sets.

Return Values:
Returns a Boolean value.

Example:

<cfset myage = 35>
<cfset myname = "dean">
<CFOUTPUT>
Given Assertion="myage as a: isnumeric(|a|); |a| gt 0; |a| lt 100 ! myname as b: isdefined('|b|'); len(|b|) GT 3":<BR>
<cfif assert("myage as a: isnumeric(|a|); |a| gt 0; |a| lt 100 ! myname as b: isdefined('|b|'); len(|b|) GT 3")>

Assertion  is correct
<cfelse>
Assertion is incorrect
</cfif>
</CFOUTPUT>

Parameters:

Name Description Required
assertion Assertion rule string you want to validate against. Variable names should be delimited by the pipe (|). Yes

Full UDF Source:

/**
 * Complex variable checking with a single function call.
 * Version 2 by Michael Wolfe, mikey@mikeycentral.com. Returns false earlier.
 * 
 * @param assertion      Assertion rule string you want to validate against.  Variable names should be delimited by the pipe (|). (Required)
 * @return Returns a Boolean value. 
 * @author Dean Chalk (dchalk99@hotmail.com) 
 * @version 2, September 23, 2004 
 */
function assert(assertion) {
    var result = 1;
    var loopvar1 = 0;
    var loopvar2 = 0;
    var variableassertion = "";
    var varsection = "";
    var varname = "";
    var varalias = "";
    var assertionsection = "";
    var anassertion = "";
    var assertnow = "";
    var doBreak = false;
    
    for(loopvar1 = 1; loopvar1 LTE listlen(assertion, "!"); loopvar1 = incrementvalue(loopvar1)) {
        variableassertion = listgetat(assertion, loopvar1, "!");
        varsection = trim(gettoken(variableassertion, 1, ":"));
        varname = trim(listfirst(varsection, " "));
        varalias = trim(listlast(varsection, " "));
        assertionsection = trim(gettoken(variableassertion, 2, ":"));
        
        for(loopvar2 = 1; loopvar2 LTE listlen(assertionsection, ";"); loopvar2 = incrementvalue(loopvar2)) {
            anassertion = listgetat(assertionsection, loopvar2, ";");
            
            if(not isdefined(varname)){
                result = 0;
                doBreak = true;
                break;
            } else {
                assertnow = replacenocase(anassertion, "|#varalias#|", varname, "ALL");
                
                if(not(evaluate(trim(assertnow)))){
                    result = 0;
                    doBreak = true;
                    break;
                }
            }
        }
        
        if(doBreak){
            break;
        }
    }
    
    return result;
}

Search CFLib.org


Latest Additions

Raymond Camden added
QueryDeleteRows
November 04, 2017

Leigh added
nullPad
May 11, 2016

Raymond Camden added
stripHTML
May 10, 2016

Kevin Cotton added
date2ExcelDate
May 05, 2016

Raymond Camden added
CapFirst
April 25, 2016

Created by Raymond Camden / Design by Justin Johnson