Validation Configuration
The configuration file for validations is a subset of JSON Table Schema.
Full support of the standard is planned in future.
I will refer to the differences or limitations to the defined standard in this wiki page.
Types
Read all about the possible types in the JSON Table Schema specification.
supported types
- string
- integer
- number
- date
- datetime
- time
not supported types
- object
- array
- duration
- geopoint
- geojson
Available Validations
Read all about the possible constraints in the JSON Table Schema specification.
required
"required" : true
The cell could not be empty. Otherwise a cell can be empty or has to be valid in combination of the other validations. If an empty cell is not allowed, this validations has to be set!
minLength
"minLength" : 4
The cell must have at least the given number of characters, in the above example a number of 4. (Empty cells are not checked, see not empty validation)
maxLength
"maxLength" : 4
The cell should not have more than the given number of characters, in the above example a number of 4. (Empty cells are not checked, see not empty validation)
enum
"enum" : ["0", "1", "TWO", "SOME OTHER VALUE"]
The value must be one of the values defined in a list of strings. (Empty cells are not checked, see not empty validation)
date, datetime, time
"date" : "yyyyMMdd"
"datetime" : "yyyyMMdd hh:mm"
"time" : "hh:mm"
The value has to be a date in the given format. If the format is given and not prefixed with fmt: the format is defined for java.text.SimpleDateFormat, check the official documentation of this java class for further information. If prefixed with fmt: it is defined as standard Python/C strptime more ... (Empty cells are not checked, see not empty validation)
unique
"unique" : true
The value can only exist once in this column.
pattern
"pattern" : "[0-9a-zA-Z]*"
To support more complex validations, the cell can be matched against a regular expression. (Empty cells are not checked, see not empty validation)
special groovy constraint
"groovy" : "value.contains('9') ? true : 'SOME ERROR TEXT'"
The groovy constraint is not part of the official constraints. The standard does not provide the possibility to define custom constraints, but custom attributes on the field directly. So the groovy If the regexp is not enough, the groovy validation should fit. The groovy expressions can use the variable value which is the String in the cell. It should return a boolean true or the String "true" to be valid, in all other cases the toString representation of the result will be the error message. That also means, that there is no i18n support for the message! (Empty cells are not checked, see not empty validation)
Full Example
{
"fields": [
{
"name": "COLUMN WITH EXACTLY 4 DIGITS",
"type": "integer",
"constraints": {
"required": true,
"minLength": 4,
"maxLength": 4
}
},
{
"name": "COLUMN MAYBE EMPTY OR 1 OR 0",
"type": "string",
"constraints": {
"enum": [
"0","1"
]
}
},
{
"name": "COLUMN MAYBE EMPTY OR DOUBLE",
"type": "number",
},
{
"name": "UNCHECKED COLUMN"
},
{
"name": "NOT EMPTY AND ONE OF THE COLORS",
"type": "string",
"constraints": {
"required": true,
"enum": [
"red","green","blue"
]
}
},
{
"name": "AN INTEGER AND NOT EMPTY",
"type": "integer",
"constraints": {
"required": true
}
},
{
"name": "SOME MANDATORY DATE",
"type": "date",
"format": "yyyyMMdd",
"constraints": {
"required": true
}
},
{
"name": "OPTIONAL DATE TIME",
"type": "datetime",
"format": "yyyyMMdd hh:mm",
},
{
"name": "EMPTY OR STRING STARTS WIDTH ID MINUS",
"type": "string",
"groovy": "value.startsWith('ID-') ? true : 'NOT A CORRECT ID'"
}
]
}