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 RuleFormatData Types
requiredboolean (true or false)All (Including Array and custom Object)
min & max ( on Value)integer or decimalNumerical Data Type
min & max (on Length)integerString-Based Data Type or Array
enumMust be a valid format for the propertyNumerical Data Type or String-Based Data Type
patternRegexString-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 positive
  • max: 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 the min 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

To build valid patterns, we recomment using https://regex101.com/ to build and test your patterns and then use the Tools->Code Generator->Javascript to export valid patterns to use in your json profile payload.

This will change a pattern such as:

^[\w-\.]+@([\w\-]+\.)+[\w-]{2,4}$

To escaped pattern:

^[\\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 positive
  • max: Array size cannot be greater than the specified value but can be equal. This value must be an integer and bigger or equal to the min 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
                        }
                    ]
                }
            }
        }
    }
}