GORM standalone: get all Domain's constraints

30 Views Asked by At

I'm using GORM-Stanalone in my app with:

dependencies {
  api 'org.grails:grails-datastore-gorm-hibernate5:8.0.1'
  // api 'org.grails:grails-validation:3.3.16' // <- tried that with no effect
}

I have a domain class like:

@Entity
class User implements GormEntity<User> {
  
  // yada yadda

  static constraints = {
    name blank:false, matches:/(\p{L}+\s?)+/
    password validator:{ passwordValidator.validate it }
    email unique:true
    permissionMask min:1
    birthDate validator:{
      Date now = new Date()
      now - 130 * 365 < it && it < now - 10 * 365
    }
    address nullable:true
  }
}

Now, I want to get list of constrained properties.

I tried following the other answer, but the code

User.getConstraints()
//or
User.constraints

returns a closure

{"delegate":null,"owner":"vx.demo.domain2.User","thisObject":"vx.demo.domain2.User",
"resolveStrategy":3,"directive":0,"parameterTypes":["java.lang.Object"],"maximumNumberOfParameters":1}

and the calls like

User.getConstraints().containsKey 'name'

also fail:

groovy.lang.MissingMethodException: No signature of method: vx.demo.domain2.User$__clinit__closure5.containsKey() is applicable for argument types: (String) values: [name]

The call

User.constraints.name

results in

java.lang.NullPointerException: Cannot get property 'name' on null object

What am I doing wrong? How to get the list of all constrained props' names?

0

There are 0 best solutions below