Fork me on GitHub

LD.

Music, software, life… and stuff.

[ Twitter ] [ GitHub ] [ Linked In ]

Gldapo Validation

Dec 3rd, 2008 @ 8:52 pm

I am about to commit the new GORM inspired validation mechanism to Gldapo. Here is what it is going to look like…

Declaring your constraints

Instead of a DSL like GORM, we are using annotations…

import gldapo.schema.annotations.

class Person {

    @GldapoNamingAttribute 
    String uid

    @Required
    String sn

    @Required
    @Matches(/[A-Z]./)
    String givenName
}

Plugging in your own constraints

You can register your own constraints quite easily. There are two parts, the constraint annotation and the validator.

The constraint annotation (has to be in Java since you can’t define annotations in Groovy 1.5.x)…

import java.lang.annotation.*;
import gldapo.schema.annotation.Validator;
import gldapo.schema.attribute.validator.EqualsValidator;

@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Validator(EqualsValidator) // 
public @interface Equals
{
    Object value();
}

As you can see it’s a stock standard annotation, that is itself annotated with @Validator to tell Gldapo how to validate this type of constraint.

The validator looks like this…

package gldapo.schema.attribute.validator

class EqualsValidator extends AbstractFieldValidator {

    def validate(obj) {
        if (obj != this.constraint.value()) {
            return ['notequals', "notequals.${this.constraint.value()}"]
        }
    }

}

Validators have to extend gldapo.schema.attribute.validator.AbstractFieldValidator and implement validate(obj). This method takes the value to validate and returns either a single error code, or a list of error codes. If the value is valid it should return null.

Gldapo will register several error codes if validation fails. If a class called Person has it’s name field annotated with @Equals('Luke') and it fails validation, the following error codes will be registered…

notequals
notequals.Luke
person.notequals
person.notequals.Luke
person.name.notequals
person.name.notequals.Luke

If the usage of error codes seem a bit weird here, then you should check out 7.4 Validation and Internationalization in the Grails user guide as the concept is exactly the same.

Finally, you need to register your constraint with Gldapo. You do this by listing your custom constraint types as a list under the contraintTypes key in the config…

Gldapo.initialize(
    contraintTypes: [Equals]
)

Performing validation

This side of things works exactly like GORM…

def p = new Person(uid: "ld", givenName: "Luke" sn: "Daley")
    if (!p.save()) {
        p.errors.allErrors.each {
            println it
        }
    }

There is also the validate() method if you just want to perform validation without trying to write the object back.

If you haven’t used validation in GORM, check out this chapter in the user guide, because it will all hold true (except how constraints are defined) with Gldapo.

Tags: #gldapo  #software  

Comments

Archive · RSS · Theme by Autumn