codeconsole opened a new pull request, #15289:
URL: https://github.com/apache/grails-core/pull/15289
add-field CLI Command
Overview
The add-field command adds a new field to an existing Grails domain class,
optionally with validation
constraints using either Grails constraints or Jakarta Validation
annotations.
Usage
grails add-field <DOMAIN-CLASS> <FIELD:TYPE> [OPTIONS]
Arguments
| Argument | Description | Example
|
|--------------|----------------------------------------|-----------------------------|
| DOMAIN-CLASS | The name of the domain class | Book,
com.example.Author |
| FIELD:TYPE | Field name and type separated by colon | title:String,
pages:Integer |
Supported Field Types
- String
- Integer
- Long
- Boolean
- Date
- BigDecimal
- Double
- Float
- Short
- Byte
- Character
Options
| Option | Description
|
|----------------------------|------------------------------------------------------------------------|
| --nullable | Mark the field as nullable
|
| --not-nullable | Mark the field as NOT nullable (generates
`@NotNull` or nullable: false) |
| --blank | Allow blank values (String fields only)
|
| --not-blank | Disallow blank values (generates
`@NotBlank` or blank: false) |
| --max-size <N> | Maximum size constraint (String fields
only) |
| --min-size <N> | Minimum size constraint (String fields
only) |
| --constraint-style <STYLE> | Constraint style: grails (default),
jakarta, or both |
Constraint Styles
| Style | Description
|
|---------|------------------------------------------------------------------|
| grails | Uses Grails static constraints block (default)
|
| jakarta | Uses Jakarta Validation annotations (`@NotNul`l, `@NotBlank`,
`@Size`) |
| both | Uses both Grails constraints AND Jakarta annotations
|
Examples
Basic field (no constraints)
add-field Book title:String
Result:
```groovy
class Book {
String title
}
```
With Grails constraints (default)
add-field Book title:String --not-nullable --max-size 255
Result:
```groovy
class Book {
String title
static constraints = {
title nullable: false, maxSize: 255
}
}
```
With Jakarta Validation annotations
add-field Book title:String --not-nullable --not-blank --max-size 255
--constraint-style jakarta
Result:
```groovy
import jakarta.validation.constraints.NotNull
import jakarta.validation.constraints.NotBlank
import jakarta.validation.constraints.Size
class Book {
@NotNull
@NotBlank
@Size(max = 255)
String title
}
```
With both constraint styles
add-field Book title:String --not-nullable --max-size 255
--constraint-style both
Result:
```groovy
import jakarta.validation.constraints.NotNull
import jakarta.validation.constraints.Size
class Book {
@NotNull
@Size(max = 255)
String title
static constraints = {
title nullable: false, maxSize: 255
}
}
```
Constraint Mapping
| Option | Grails Constraint | Jakarta Annotation |
|----------------|-------------------|--------------------|
| --not-nullable | nullable: false | @NotNull |
| --nullable | nullable: true | (none) |
| --not-blank | blank: false | @NotBlank |
| --blank | blank: true | (none) |
| --max-size N | maxSize: N | @Size(max = N) |
| --min-size N | minSize: N | @Size(min = N) |
Error Handling
- Domain class not found: Displays error message suggesting to run
create-domain-class first
- Field already exists: Displays error message indicating the field
already exists
- Invalid field type: Displays error with list of supported types
- Invalid constraint options: Displays specific validation error (e.g.,
--blank only valid for String)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]