Creating a custom type definition
Shared type definition
It is possible to create a custom field type inside your document definition, so other fields in the same document definition can reference it.
A custom type definition is almost exactly like a regular field definition, with the exception of defining a base-type
key instead of a type
key.
The id of a custom type can be used by other fields as the value of field type
.
document-definition:
name: person
types:
- id: country (1)
base-type: string
possible-values:
- BE
- NL
- id: address (1)
base-type: fieldset
fields:
- id: street-name
type: string
- id: street-nr
type: number
- id: city
type: string
- id: country
type: country (2)
content:
- id: firstname
type: string
- id: lastname
type: string
- id: home-address
type: address (2)
- id: additional-addresses
type: address[] (3)
1 | Custom type definitions. |
2 | Using custom type as a single field type. |
3 | Using the custom type as a collection field type.
This creates a collection of the fieldset represented by the address type. |
In our example the address
custom type in turn defines a field using the country
custom type.
A custom type behaves as a sort of template definition for a field. The options defined on the type definition can often be extended by the same options on the implementing field.
The following rules apply:
-
Any
possible-values
defined on a field, will replace thepossible-values
from the custom type. -
Any validators defined on the field will be added to the ones attached to the custom type; for that particular field.
-
Any messages defined on the field will replace the same message codes from the custom type. All other messages defined both on the field or the custom type will be available.
-
When dealing with fieldset: any
fields
defined on the field will be added after the ones defined on the custom type.-
When dealing with a collection of fieldsets, these rules also apply to the
member
fieldset, which could be extended using this approach.
-
These rules cascade: if you have a custom field type that uses another custom type as base-type
, all definitions will be merged according to the rules described above.
Globally available base type
You can also define a new globally available custom base type from Java code.
To define a new base type you must provide a DynamicTypeDefinitionBuilder
component (@Bean
or @Component
).
Defining a new base type is rarely necessary and only useful if you want to provide advanced customizations.
Example code can be found in the default implementations:
-
SimpleDynamicTypeDefinitionBuilder
-
FieldsetDynamicTypeDefinitionBuilder
-
EntityDynamicTypeDefinitionBuilder
-
EnumDynamicTypeDefinitionBuilder