Core
Guides v2.x
2

Data Validation #

One common requirement when you are managing data is to perform data validation. In real world applications you may need to check that any newly created or updated document meet a certain criteria.

A common example is email validation. Let's say you have developed a registration page where you request a user's name and email and you want to ensure that the email they provide is valid.

With Kuzzle, instead of programming the validation logic yourself, you can pick from a set of predefined validation types. Validations are defined in a validation schema which determines what pattern is linked to what field, every time Kuzzle receives input data, it checks this validation schema and returns an error if a validation pattern fails. The validation schema can be configured in the validation field of the configuration file.

For a detailed look at the available properties and types, please refer to the Validation Reference at the end of this page.

You can bypass data validation by using bulk:write or bulk:mWrite actions.


Basic Validation #

A validation schema is defined using a hierarchical structure that contains a set of rules within an index, collection, and document field.

For example, below is a validation schema for the onlineshop index and products collection that defines the validation pattern for field price and field productDescription:

{
  "validation": {
    "onlineshop": {
      "products": {
        "fields": {
          "price": {
            "type": "numeric",
            "mandatory": true
          },
          "productDescription": {
            "type": "string",
            "defaultValue": "Sorry, no description available for this product."
          }
        }
      }
    }
  }
}

Let's take a look at what this validation schema does:

  • It defines a set of rules for documents in the products collection of the onlineshop index.
  • It ensures that price exists and is a number.
  • It ensures that productDescription is a String and has a value when none is provided.

For a complete list of validation types please refer to the Validation Types reference.


Type Options #

Type Options can be used to provide advanced validation of certain fields. These are only available for some field types.

Below is an example of how the range type option is used to ensure that the field price is greater than zero:

{
  "validation": {
    "onlineshop": {
      "products": {
        "fields": {
          "price": {
            "type": "numeric",
            "mandatory": true,
            "typeOptions": {
              "range": {
                "min": 0
              }
            }
          },
          "productDescription": {
            "type": "string",
            "defaultValue": "Sorry, no description available for this product."
          }
        }
      }
    }
  }
}

For more information regarding Type Options, please refer to the Validation Types reference.


Advanced Validation #

If the basic validation functionality doesn't meet your requirements, you can take advantage of Koncorde to create complex validation specifications.

Koncorde is the same component used to create real-time subscriptions.

The idea is simple: use Koncorde to specify a filter that can be used to validate documents. For example, here we ensure that at least one of the fields price or vatPrice exists by placing a filter in the validators field of the validation schema:

{
  "validation": {
    "onlineshop": {
      "products": {
        "fields": {
          "price": {
            "type": "numeric",
            "typeOptions": {
              "range": {
                "min": 0
              }
            }
          },
          "vatPrice": {
            "type": "numeric",
            "typeOptions": {
              "range": {
                "min": 0
              }
            }
          },
          "productDescription": {
            "type": "string",
            "defaultValue": "Sorry, no description available for this product."
          }
        },
        "validators": [
          // Here goes the filters
          {
            "or": [
              {
                "exists": {
                  "field": "price"
                }
              },
              {
                "exists": {
                  "field": "vatPrice"
                }
              }
            ]
          }
        ]
      }
    }
  }
}

In the example above, we used both the exists operator and the or operator to build our validation rule. For more information take a look at our Koncorde Reference.


Validation Reference #

Collection properties #

The validation schema of a collection, under validation.<index>.<collection>, accepts these properties:

PropertyDescription
fieldsThe validation rules of each document field (see below). Nested fields are declared with their path, using / as separator (e.g. address/city)
strictIf true, documents cannot contain fields that are not declared in fields (default: false)
validatorsAn array of Koncorde filters that documents must match (see Advanced Validation)

Field properties #

PropertyDescription
typeRequired. The validation type of the field
mandatoryIf true, the field must be present (default: false)
defaultValueValue given to the field when the document does not provide one
descriptionFree text describing the field
multivalued{ "value": true, "minCount": <n>, "maxCount": <n> } to accept an array of values (minCount and maxCount are optional)
typeOptionsOptions specific to the field type (see below)

Validation types #

TypeAcceptstypeOptions
anythingAny valuenone
booleanA booleannone
dateA dateformats: list of accepted date formats (default: ["epoch_millis"]); range: { "min": <date>, "max": <date> } ("NOW" is accepted as a bound)
emailAn email addressnotEmpty: if true, the empty string is refused
enumOne of a list of stringsvalues: the list of accepted strings (required)
geo_pointA geographical pointnone
geo_shapeA geographical shapeshapeTypes: list of accepted shape types (point, linestring, polygon, multipoint, multilinestring, multipolygon, geometrycollection, envelope, circle)
integerAn integerrange: { "min": <number>, "max": <number> }
ip_addressAn IPv4 or IPv6 addressnotEmpty: if true, the empty string is refused
numericA numberrange: { "min": <number>, "max": <number> }
objectAn objectstrict: if true, the object cannot contain sub-fields that are not declared (default: the strictness of the parent)
stringA stringlength: { "min": <number>, "max": <number> }
urlA URLnotEmpty: if true, the empty string is refused

Plugins can add their own types with DataValidation.addType, available as context.accessors.validation.addType.