I am working on some code and I keep refactoring trying to improve upon the design. I am working on a project of mine (hyrule) that does data validation. A user can set some properties like so.
this.constraints = [
{property="firstname",blank=false},
{property="lastname",blank=false},
{property="email",email=true},
{property="username",min=6,max=20,context="create"},
{property="password",min=6,max=20,context="create"}
]
Property is the name of the property we are validating
blank/email/min/max are constraints that we want to validate
context is used to validate that data in certain contexts (so you don't validate all of the data all of the time
so lets say we have a validator class that validates the data the user entered and in that class we have a validate method
public boolean function validate(){
// lots of stuff happening
// we read the type based on that struct coming in..say it was blank
var obj = getConstraintFactory().getConstraint(key);
}
This hands the responsibility off to the factory which looks like this
My question is what the heck do you do in the instance someone passes an incorrect type to getConstraint() ? Do you just return a null, throw an error? Is it common for a constraint factory to have a list of valid constraints that you can check against before trying to get a component?
I ask this because in my case I have a ton of properties i am searching against
property
email
blank
context
and not all of them have a constraint nor should they be validated against. My idea was to do something like
if( structKeyExists(getConstraintFactory().getValidConstriants(),"blank"){
getConstraintFactory().getConstraint(blank)
}
I really hope through all that rambling any of that made sense :( Thanks for listening guys (and girls)