Some thoughts on Constraints

28 views
Skip to first unread message

Dan Vega

unread,
Mar 14, 2011, 11:06:58 PM3/14/11
to hyrule-v...@googlegroups.com
Since the idea to set constraints in the model came from grails I have been looking at a lot of their constraints and I wanted to pick your brains on a few things. In this release you are going to have the ability to define constraints like so

component persistent="true" table="Users" extends="BaseEntity" {

property name="id" column="user_id";
property name="firstName";
property name="lastName";
property name="email";
property name="age";
property name="username";
property name="password";
property name="passwordConfirm" persistent="false";
property name="type";
property name="roles" fieldtype="many-to-many" cfc="Role" singularname="role" fkcolumn="user_id" inversejoincolumn="role_id" linktable="users_roles";   

// model constraints
this.constraints = [
{property="firstName",blank=false},
{property="lastName",blank=false},
{property="email",blank=false,email=true,unique=true,context="create,foo,bar"},
{property="age",blank=false,size="18..30"},
{property="passwordConfirm",blank=false,validator="passwordsMatch"},
{property="type",blank=false,inList="employee,manager"}
];
public User function init(){
return this;
}
// custom validators
public boolean function passwordsMatch(){
if( getPassword() == getPasswordConfirm() ){
return true;
}
return false;
}

}

Some real questions came up while I started writing the constraints. 

1.) With a restraint like blank=false you are telling hyrule that you do not want to allow a blank value. 
     a) should this be a string only or check for a blank on any value (empty collection/empty array)
     b) would a user ever say blank=true.. ie should we ever test for that?

2.) Min / Max 
     a.) should min and max only test numbers or all simple values (strings/dates/numbers)
     b.) if so grails has a minSize/maxSize we could do the same to test collection sizes

3.) Same questions with creditcard and email
     a) would a user ever say creditcard=false... why would you ever test to see if something is not a credit card.

4.) I have some implementation questions on unique but I will save that for another thread.

Tony Nelson

unread,
Mar 16, 2011, 2:39:16 PM3/16/11
to Hyrule Validation Framework
Here's a random thought completely unrelated to your questions...

If you're defining your constraints through this.constraints = [], you
might run into issues with inheritance, since you can't really inherit
the this scope from a parent class.

While using this.constraints might be more concise, I think I would
prefer to use metadata annotations since then inheritance isn't really
an issue. Plus your objects should serialize a lot cleaner too.

-Tony

Dan Vega

unread,
Mar 16, 2011, 4:37:22 PM3/16/11
to hyrule-v...@googlegroups.com
You will still be able to use meta data as I planned on supporting both.. 

I totally forgot that this won't inherit and didn't think about out because I wanted to getting working before I worried about inheritance.. 

I am in a bummed out mood now..what to do

Dan Vega

unread,
Mar 16, 2011, 10:04:14 PM3/16/11
to hyrule-v...@googlegroups.com
I know this is a cop out but if 

Employee extends user

your object instantiation is done at the employee level

+User 
  -first
  -last
+Employee
 -social

Employee cfc

this.constraints = [
 {property="first",blank=false}.
 {property="last",blank=false}.
 {property="social",blank=false,ssn=true}
];

Dan Vega

unread,
Mar 17, 2011, 9:01:47 AM3/17/11
to hyrule-v...@googlegroups.com
disregard ... I don't like that approach. I seriously don't know how I will be able support this now.. sigh
Message has been deleted

Dan Vega

unread,
Mar 17, 2011, 11:09:52 AM3/17/11
to hyrule-v...@googlegroups.com
OK.. How about this, again I want to support 2 ways of defining constraints.. Annotations / Setting Property Constraints via code

this.constraints is not going to work because we have issues with inheritance. What about this approach? 

Person
component persistent="true" {

property name="id" column="person_id" fieldtype="id" generator="increment";
property name="firstName";
property name="lastName";
property name="constraints" persistent="false";

public Person function init(){
return this;
}
public void function setConstraints(){
variables.constraints = [
{property="firstName",blank=false},
{property="lastName",blank=false}
];
public string function getName(){
return getFirstName() & " " & getLastName();
}
}

Employee
component persistent="true" extends="Person" {

property name="employeeid" column="employee_id" fieldtype="id"; 
property name="salary" ormtype="double";
property name="constraints" persistent="false";
this.constraints = [
{property="salary",blank=false}
]; 
 
public Employee function init(){
return this;
public void function setConstraints(){
variables.constraints = [
{property="salary",blank=false}
];
}

Dan Vega

unread,
Mar 18, 2011, 8:48:59 AM3/18/11
to hyrule-v...@googlegroups.com
That won't work either because you end up with the same issues, no way to get the properties of a parent w/out instantiating an object. I think I am just going to support this 

this.constraints = []

but w/out inheritance, if you need inheritance you will have to use annotations. smh

blog post coming soon
Reply all
Reply to author
Forward
0 new messages