There are several things you can do.
If you have a domain class like this:
// grails-app/domain/com/demo/Person.groovy
class Person {
String firstName
String lastName
// notice that only firstName is constrained, not lastName
static constraints = {
firstName matches: /[A-Z].*/
}
}
You can interrogate the persistentProperties property to get a list of all persistent properties:
def person = new Person()
def personDomainClass = person.domainClass
// this will not include id and version...
def persistentPropertyNames = personDomainClass.persistentProperties*.name
assert persistentPropertyNames.size() == 2
assert 'firstName' in persistentPropertyNames
assert 'lastName' in persistentPropertyNames
If you wanted to do the same sort of thing but don't have an instance of the Person class to interrogate you can do something like this:
def personDomainClass = grailsApplication.getDomainClass('com.demo.Person')
// this will not include id and version...
def persistentPropertyNames = personDomainClass.persistentProperties*.name
assert persistentPropertyNames.size() == 2
assert 'firstName' in persistentPropertyNames
assert 'lastName' in persistentPropertyNames
You could also get the keys out of the constraints Map:
// this will include both firstName and lastName,
// even though lastName is not listed in the constraints
// closure. GORM has added lastName to make it non
// nullable by default.
// this will not include id and version...
def constrainedPropertyNames = Person.constraints.keySet()
assert constrainedPropertyNames.size() == 2
assert 'firstName' in constrainedPropertyNames
assert 'lastName' in constrainedPropertyNames
I hope that helps.