Validations

This schema snippet the various validations available for text, numbers and arrays.

  • The simplestringwithmaxandmin property uses minLength and maxLength to constrain the length of the string. This property is also included in the properties listed as required, so a valid value must be entered in the property before it can be saved.

  • The dateWithPattern and urlslug properties demonstrate how to constrain the value entered using a regular expression defined by the pattern keyword. You can find out more about regular expressions in JSON Schema in the JSON Schema documentation.

  • The numbervalidationexample shows how to use the multipleOf keyword, for a number that must be a multiple of 10

  • The stringlistofenums demonstrates how to use an enum to constrain the values that can be added to a list. The uniqueItems keyword is also used to ensure that each value can only be chosen once. This property is also required, so a valid value must be entered before the the content item can be saved. Users have a choice of selecting "none" if the array uses an enum.

Pre-requisites

None. This is a self contained schema.

How to use

You can use the validations demonstrated in this schema for your own properties.

Validations example

{
    "$schema": "http://json-schema.org/draft-07/schema#",
    "$id": "https://schema-examples.com/validations",

    "title": "Title",
    "description": "Description",

    "allOf": [
        {
            "$ref": "http://bigcontent.io/cms/schema/v1/core#/definitions/content"
        }
    ],

    "type": "object",
    "properties": {

        "simplestringwithmaxandmin": {
            "title": "Simple string with maximum length",
            "description": "A string that can contain between 4 and 255 characters",
            "type": "string",
            "minLength": 4,
            "maxLength": 255
        },
        "datewithpatternvalidation": {
            "title": "Date validated with a pattern",
            "description": "Creation date (YYYY-MM-DD)",
            "type": "string",
            "maxLength": 10,
            "minLength": 10,
            "pattern": "^[0-9]{4}-((0[1-9])|(1[0-2]))-((0[1-9])|([1-2][0-9])|(3[0-1]))$"
        },
        "urlslugpatternvalidation": {
            "title": "URL slug",
            "description": "URL slug (a-z and 0-9)",
            "type": "string",
            "minLength": 1,
            "maxLength": 100,
            "pattern": "^[a-z0-9-]+$"
        },
        "numbervalidationexample": {
            "title": "Number validation example",
            "description": "Enter a rating between 10- 100. (must be a multiple of 10)",
            "type": "integer",
            "minimum": 10,
            "maximum": 100,
            "multipleOf": 10
        },
        "stringlistofenums": {
            "title": "Strings defined in enum",
            "description": "Choose up to 3 string values",
            "type": "array",
            "minItems": 0,
            "maxItems": 3,
            "uniqueItems": true,
            "items": {
                "type": "string",
                "enum": [
                    "Electrics",
                    "Home",
                    "Kitchen",
                    "Lighting",
                    "Computers"
                ]
            }
        }

    },
    "propertyOrder": [],
    "required": [
        "simplestringwithmaxandmin",
        "stringlistofenums"
    ]
}

Content form preview

An example of creating a content item using a content type registered from the validations example schema is shown in the image below.

A content item created from the validations content type
A content item created from the validations content type

Validations

results matching ""

    No results matching ""