CFLib.org – Common Function Library Project

Assert(assertion)

Last updated September 23, 2004

Version: 2 | Requires: ColdFusion 5 | Library: DataManipulationLib

 
Rated 1 time(s). Average Rating: 4.0

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:

view plain print about
<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:

view plain print about
<cfscript>
/**
 * 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 (mikey@mikeycentral.comdchalk99@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;
}
</cfscript>
blog comments powered by Disqus

Search CFLib.org


Latest Additions

Tayo Akinmade Tayo Akinmade added
arrayTrim
4 day(s) ago

Will Belden Will Belden added
longTime
9 day(s) ago

James Sleeman James Sleeman added
quickSort
19 day(s) ago

Ben Forta Ben Forta added
GetHostAddress
22 day(s) ago

Top Rated

Darwan Leonardo Sitepu EksporSQLData
Rated 5.0, 16 time(s)

Darwan Leonardo Sitepu backupDatabase
Rated 5.0, 13 time(s)

Barney Boisvert indentXml
Rated 5.0, 10 time(s)

Kevin Pepperman generateSsccAsn
Rated 5.0, 4 time(s)

Created by Raymond Camden / Design by Justin Johnson