Field Validation
Rules and constraints on profile and relationship definition fields.
Clinia's Profiles and Relationship Definitions enable specifying validation rules for their properties. These rules are tested in addition to the standard field type validation (See Data Types).
Validation Rules
The rules
field on all properties allows to specify a list of validation criteria:
Validation Rule | Format | Data Types |
---|---|---|
required | boolean (true or false ) | All (Including Array and custom Object) |
min & max ( on Value) | integer or decimal | Numerical Data Type |
min & max (on Length) | integer | String-Based Data Type or Array |
enum | Must be a valid format for the property | Numerical Data Type or String-Based Data Type |
pattern | Regex | String-Based Data Type |
Those validation rules are inserted in the Profiles and Relationship definitions as such:
{
"properties": {
"<propertyKey>": {
"type": "...",
"rules": [
{
// All field rules
}
]
}
}
}
All Fields
All fields support out-of-the-box the required
validation rule. This can be specified on a field as such:
{
"properties": {
"propertyName": {
"type": "symbol",
"rules": [
{
"required": true
// ... Other validation rules
}
]
}
}
}
Numerical Data Types
On top of the existing rule format for Integer and Decimal (See the pattern section), those numerical fields allow for the following validation rules to be specified:
min
: Value cannot be less than the specified value, but can be equal.max
: Value cannot be more than the specified value, but can be equal.enum
: Value must be one of the enumerated values.required
: Value is required. Missing key or null values will trigger an error.
Example for min
& max
:
{
"properties": {
"age": {
"type": "integer",
"rules": [
{
"required": true,
"min": 18,
"max": 120
// ... Other validation rules
}
]
}
}
}
Example for enum
{
"properties": {
"taxRate": {
"type": "decimal",
"rules": [
{
"enum": [
10.5,
12.2,
15.05
]
// ... Other validation rules
}
]
}
}
}
String-Based Data Types
On top of the existing rule format for Code, Symbol, Markdown, URI, URL and XHTML (See the pattern section), those string-based fields allow for the following validation rules to be specified:
min
: Value cannot be on length less than the specified value, but can be equal. This value must be an integer and be positivemax
: Value cannot be of length greater than the specified value, but can be equal. This value must be an integer and bigger or equal to themin
value (If specified)enum
: Value must be one of the enumerated values.required
: Value is required. Missing key or null values will trigger an error.
Example for min
& max
:
{
"properties": {
"comment": {
"type": "symbol",
"rules": [
{
"min": 5, // Comment must be between 5 and 120 character long
"max": 120
// ... Other validation rules
}
]
}
}
}
Example for enum
:
{
"properties": {
"gender": {
"type": "code",
"rules": [
{
"enum": [
"male",
"female",
"non-binary",
"..."
]
// ... Other validation rules
}
]
}
}
}
Example for pattern
:
{
"properties": {
"email": {
"type": "symbol",
"rules": [
{
"pattern": "^[\\w\\-\\.]+@([\\w-]+\\.)+[\\w-]{2,4}$"
// ... Other validation rules
}
]
}
}
}
Pattern Building and Validation
Clinia uses the ECMA (JavaScript) Regular Expression flavor with string literals.
This means a pattern such as:
^[\w-\.]+@([\w\-]+\.)+[\w-]{2,4}$
Needs to be escaped like:
^[\\w\\-\\.]+@([\\w-]+\\.)+[\\w-]{2,4}$
Array Data Type
Array Data types allow for validation of size and sub-element.
min
: Array size cannot be less than the specified value but can be equal. This value must be an integer and be positivemax
: Array size cannot be greater than the specified value but can be equal. This value must be an integer and bigger or equal to themin
value (If specified)required
: Array must be provided, but can be empty ([]
). A null value will trigger an error.
Example for a list of contacts (contactpoint
) data type:
{
"phones": {
"type": "array",
"rules": [
{
"required": true,
"min": 2,
"max": 10
}
],
"items": {
"type": "contactpoint",
"properties": {
"value": {
"rules": [
{
"required": true
// ... Other validation rules
}
]
}
}
}
}
}
If you use an array of basic types (Such as string-based types), you can also use field validation on items:
{
"countries": {
"type": "array",
"items": {
"type": "code",
"rules": [
{
"enum": ["CA","US"],
"pattern": "^[A-Z]*$"
// ... Other validation rules
}
]
}
}
}
Object Data Type
Other object data types such as Address, ContactDetail, ContactPoint, ... allow for sub-field validation rules to be specified. Those can be specified as such:
required
: Object must be provided but can be empty. A null value will trigger an error
Example for the city
field (symbol
type) in an address
data type:
{
"properties": {
"address": {
"type": "address",
"rules": [
{
"required": true
}
],
"properties": {
"city": {
"rules": [
{
"required": true
// ... Other validation rules
}
]
}
}
}
}
}
Note: Only a single rule without Conditional Validation (
when
clause) can be specified on each property.If multiple rules are specified with no "when" clauses, the profile creation will return an error.
Updated 19 days ago