CFLib.org – Common Function Library Project

isArrayOf(object, validator)

Last updated December 19, 2012

author

Adam Cameron

Version: 1 | Requires: CF9 | Library: CFMLLib

Description:
Validates an array of [anything] using a callback to define what constitutes a valid [anything]. The callback should return a boolean: true if the passed-in array element is valid, otherwise false.

Return Values:
Returns a boolean

Example:

numbers = [
    {number=1, english="one", maori="tahi"},
    {number=2, english="two", maori="rua"},
    {number=3, english="three", maori="toru"},
    {number=4, english="four", maori="wha"}
];

function numbersValdiator(number){
    return structKeyExists(number, "number") && structKeyExists(number, "english") && structKeyExists(number, "maori");
}

writeOutput(isArrayOf(numbers, numbersValdiator));

Parameters:

Name Description Required
object The object to validate Yes
validator The callback to use to validate each element in the array Yes

Full UDF Source:

/**
 * Validates an array of [anything].
 * version 1.0 by Adam Cameron
 * 
 * @param object 	 The object to validate (Required)
 * @param validator 	 The callback to use to validate each element in the array (Required)
 * @return Returns a boolean 
 * @author Adam Cameron (adamcameroncoldfusion@gmail.com) 
 * @version 1.0, December 19, 2012 
 */
function isArrayOf(object, validator){
    if (!isArray(object)){
        return false;
    }

    for (var element in object){
        if (!validator(element)){
            return false;
        }
    }
    return true;
}
blog comments powered by Disqus

Search CFLib.org


Latest Additions

Kevin Cotton added
date2ExcelDate
May 5, 2016

Raymond Camden added
CapFirst
April 25, 2016

Chris Wigginton added
loremIpsum
January 18, 2016

Gary Stanton added
calculateArrival...
November 19, 2015

Sebastiaan Naafs - van Dijk added
getDaysInQuarter
November 13, 2015

Created by Raymond Camden / Design by Justin Johnson