Skip to content
frederic-reis edited this page Mar 2, 2021 · 2 revisions

Example

JSON schema example for documents

{
  "$schema": "http://json-schema.org/draft-07/schema",
  "$id": "//schema/document/payload.json",
  "type": "object",
  "title": "JSON schema for JSONField:data",
  "description": "Basic schema for a data field",
  "required": [
    "type",
    "title"
  ],
  "properties": {
    "title": {
      "$id": "#/properties/title",
      "type": "object",
      "format": "translate",
      "title": "Title",
      "required": [
        "de_DE",
        "fr_FR"
      ],
      "properties": {
        "de_DE": {
          "$id": "#/properties/title/properties/de_DE",
          "type": "string",
          "title": "Title in German",
          "description": "Example of multilingual required field with a max length and examples as placeholder",
          "minLength": 1,
          "maxLength": 256,
          "default": "",
          "examples": [
            "Finaler Bericht von Gaston Bohnenberger und Guy Modert zur Restrukturierung von EUTELSAT."
          ]
        },
        "fr_FR": {
          "$id": "#/properties/title/properties/fr_FR",
          "type": "string",
          "title": "Title in French",
          "description": "Example of multilingual required field with a max length and examples as placeholder",
          "minLength": 1,
          "maxLength": 256,
          "default": "",
          "examples": [
            "EUTELSAT - Rapport final de Guy Modert et Gaston Bohnenberger sur la restructuration"
          ]
        }
      }
    },
    "description": {
      "$id": "#/properties/description",
      "type": "object",
      "format": "translate",
      "title": "Description",
      "properties": {
        "de_DE": {
          "$id": "#/properties/description/properties/de_DE",
          "type": "string",
          "title": "Description in German",
          "description": "Example of multilingual field"
        },
        "fr_FR": {
          "$id": "#/properties/description/properties/fr_FR",
          "type": "string",
          "title": "Description in French",
          "description": "Example of multilingual field"
        }
      }
    },
    "type": {
      "$id": "#/properties/type",
      "type": "string",
      "title": "Document type",
      "description": "Example of select field with a default value",
      "enum": [
        "image",
        "pdf",
        "video",
        "audio",
        "event",
        "place"
      ],
      "default": "image"
    },
    "reference": {
      "$id": "#/properties/reference",
      "type": "string",
      "title": "Reference",
      "description": "Example of text field which is only used for images and videos types",
      "maxLength": 127,
      "groups": ["image", "video"]
    },
    "year": {
      "$id": "#/properties/year",
      "type": "integer",
      "title": "Year",
      "description": "Example of numeric field which is only used for image type and which can have a value between 1900 and 2100 (or undefined) and an example as placeholder",
      "minimum": 1900,
      "maximum": 2100,
      "examples": [2001],
      "groups": ["image"]
    },
    "start_date": {
      "$id": "#/properties/start_date",
      "type": "string",
      "format": "date",
      "title": "Start date",
      "description": "Example of date field in format yyyy-mm-dd",
      "pattern": "^[0-9]{4}-((0[13578]|1[02])-(0[1-9]|[12][0-9]|3[01])|(0[469]|11)-(0[1-9]|[12][0-9]|30)|(02)-(0[1-9]|[12][0-9]))(T(0[0-9]|1[0-9]|2[0-3]):(0[0-9]|[1-5][0-9]):(0[0-9]|[1-5][0-9])(\\.[0-9]{3}Z)?)?$",
      "maxLength": 10,
      "examples": ["2021-03-02"]
    },
    "existing": {
      "$id": "#/properties/existing",
      "type": "boolean",
      "title": "Does the place currently exist?",
      "description": "Example of checkbox field checked by default",
      "default": true
    },
    "coordinates": {
      "$id": "#/properties/coordinates",
      "type": "object",
      "title": "Coordinates of the place",
      "description": "Example of fields grouped in a collapsable box",
      "properties": {
        "type": {
          "$id": "#/properties/coordinates/properties/type",
          "type": "string",
          "title": "Type",
          "enum": ["Feature"]
        },
        "geometry": {
          "$id": "#/properties/coordinates/properties/geometry",
          "type": "object",
          "title": "Geometry",
          "description": "Example of grouped fields",
          "properties": {
            "type": {
              "$id": "#/properties/coordinates/properties/geometry/properties/type",
              "type": "string",
              "title": "Type",
              "enum": ["Point"]
            },
            "coordinates": {
              "$id": "#/properties/coordinates/properties/geometry/properties/coordinates",
              "type": "array",
              "title": "Coordinates",
              "description": "Example of array field which must have 2 items as value separated by a comma",
              "minItems": 2,
              "maxItems": 2,
              "examples": ["49.5953069,6.1350661"],
              "items": {
                "type": "string"
              }
            }
          }
        }
      }
    }
  }
}

Validation

There is a validation at 2 levels:

  • On the server: the JSON schema is loaded by python into Miller. There is a validation process which checks that the content of the data JSON field matches the JSON schema when the document form is submitted. If there is an error, the document is not saved and the browser go back to the edition of the form.
  • On the client:
    • The JSON schema is loaded into the client by javascript. If there is a validation error from the server, each properties making up the JSON content is checked to display the error message directly on the form above the matching field.
    • There is also a validation on the onChange event of each field. This is only valid for custom fields added by javascript and can be disabled by setup the validateOnChange configuration property on false in the initialization of the module.

Required fields

Properties: required and minLength

With the required properties, the JSON validation process on the server checks that the property exists in the JSON content but doesn't check if the property contains an empty string. To fix that, the required property must be combined with the _minLength _property defined on 1.

Pattern

Property: pattern which contains a regular expression (for type string only)

The validation process checks that the content matches the pattern

If the pattern doesn't allow an empty string, the field is automatically required

The pattern validation can be used for date field. By example, for a date in the format yyyy-mm-dd from 1900 to 2099, the pattern is ^(19|20)\\d\\d-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])$

Option list

Property: enum contains an array of values (for type string only)

The validation process checks that the content matches one of the values defined in the enum property.

Maximum length

Property: maxLength (for type string only)

The JSON validation process on the server checks that the size of the content does not exceed the maxLength property

On the client, the size is limited in the input tag by the maxlength attribute.

Minimum and maximum value

Properties: minimum and maximum (for type integer only)

The JSON validation process on the server checks that the value is between a minimum and a maximum

Number of items

Properties: minItems and maxItems (for type array only)

The JSON validation process on the server checks that the number of items in the field is between a minimum and a maximum

Type of fields

String

In the form, the field is represented by

  • an input tag if the maxLength property is defined
  • a textarea tag if the maxLength property is not defined
  • a select tag if the enum property is defined

Integer

In the form, the field is represented by an input field

The validation process checks that the value matches an integer and possibly is between a minimum and a maximum

###Boolean In the form, the field is represented by a select tag with false and true as options (or checkbox in Visual Editor)

The validation process checks that the value matches false or true

The field is required

Array

In the form, the field is represented by a text input. The value is a list of items separated by a comma.

The validation process checks that the number of items is between minItems and maxItems.

Object

Used to group child fields Used for multilingual field. The format property must be defined on translate