Reputation: 5510
I know, from reading online, that when you type
function foo(required integer ObjectID)
The CFML engine (Lucee for me) looks for integer.cfc, since CFCs don't support integer as a data type (strangely).
What should integer.cfc look like for CF to consider it a valid data type. Is there a certain place that it must be, or can it be in one of the mapped folders?
I've made a few investigatory attempts and not really getting any insight by trial and error.
Use-case is, as with most data-type checking, that I know the the expected data-type but it's an additional line of defense against client-tampering without having to be more verbose with param
or if
checking.
Also, if anyone has done this, can you say anything about any performance difference?
I know that I can do this as below, but that seems like it should be an unnecessary layer.
function foo(required numeric myID)
param name="arguments.myID" type="integer";
..use the parameter in queries..
Edit for clarity: At the moment, the below passes, but ideally I'd love to say required integer myID
, and thus it would fail.
<cfscript>
function TestNum(required numeric myID) {
return myID;
}
writeoutput(TestNum(8.3));
</cfscript>
Upvotes: 2
Views: 334
Reputation: 8123
Numeric
in CFML as close to an integer as CFML getsWhile you can (and should) define custom data types as classes, much like any OO language, you probably shouldn't do it for simple data types like specific types of numbers.
Types in ColdFusion are a gray area. It's a string. It's an integer. It can be a boolean. There are simple types that you can't mess with but will output directly to the screen/page/buffer/whatever, then there are complex types like array, struct, and query, as well as CFC types that you can visualize with <cfdump>
and writeDump
.
If you need to know if your incoming data type is a whole integer, you should probably come up with a different method.
Don't declare your own integer data type. Not in ColdFusion especially. Remember, this is a very high-level language, many times removed from the hardware layers. Often times, for performance reasons, type checking is turned off in production. This works because type checking in ColdFusion is largely used as a programming tool, not a user validation tool. It's all for the ceremony.
If you are looking to validate user input, there are better approaches. I didn't get a sense of quantity; how many of these are you going to have to write? How are you going to get messages back to the user? Think a little bigger, but not so big that you're redefining mathematical concepts.
What if your form submission (or REST API? Whatever the input...) populated a cfc for that type -- not an individual field, but a collection of fields. Say you're editing a user, make a User class with all the properties.
Knowing that type checking doesn't really do what you want frees you to let everything be a string (or any
) but you can decorate the properties of your class with their validating types.
component name="User" {
// notice that "validateType" is custom metadata, use anything you want
property name="ID" type="string" validateType="integer";
property name="Name" type="string" validateType="Name";
}
Then make a validator that reads the component's metadata and validates in whatever custom way you want. It may be as simple as this untested one:
function validate(component obj) {
var md = getMetaData(obj);
for (var prop in md.properties) {
if (structKeyExists(prop, "validateType") {
switch (prop.validateType) {
case "integer":
isValid("integer", evaluate("obj.get#prop.name#()"));
break;
// other custom validation types
}
}
}
}
Then put that validator in a reusable place, such as a validation class, or in the supertype of your own cfc so it can validate itself.
That's just an idea. There are plenty of ways to skin a cat and validate fields. Another good choice is to try the ValidateThis framework which will set you up with a good pattern and code that's already written for you.
Upvotes: 2