Assert(assertion)
Last updated September 23, 2004
Version: 2 | Requires: ColdFusion 5 | 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 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:
<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>
Search CFLib.org
Latest Additions
Tayo Akinmade added
arrayTrim
4 day(s) ago
Will Belden added
longTime
9 day(s) ago
James Sleeman added
quickSort
19 day(s) ago
Ben Forta added
GetHostAddress
22 day(s) ago
Top Rated
EksporSQLData
Rated 5.0, 16 time(s)
backupDatabase
Rated 5.0, 13 time(s)
indentXml
Rated 5.0, 10 time(s)
generateSsccAsn
Rated 5.0, 4 time(s)