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;
}

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