Schema-Woven Validation

Schema-Woven Validation (SWV) is the next generation of web form validation mechanism employed by upcoming versions of Contact Form 7.

What problems does it solve?

In the early days of the World Wide Web, the roles of both the web form deliverer and form submission receiver were played by the same entity—the web server. The server naturally knew how it should treat the form input because the form was created by the server itself.

There was no problem until HTML5 introduced client-side form validation. Client-side validation allows a field value to validate promptly after the user inputs it. Although it can provide a good user experience, client-side validation doesn’t replace server-side validation. Form validation still must be run on the server side, but now there are two different entities who engage in form validation: web server and client (browser).

The spread of headless CMS or other types of decoupled architecture has further complicated the situation. In such architectures, it is not rare that there are multiple entities involved in web forms. And since there is no standardized method of form validation, the entities may not know the correct way to validate the form input. Schema-Woven Validation (SWV) solves this problem.

SWV provides a JSON-formatted schema language that defines the expected form of user input. With SWV, you can safely implement web forms without sacrificing the benefit gained from modern web architectures.

Example

This is an example SWV schema created for the default contact form.

{
    "version": "Contact Form 7 SWV Schema 2022-03",
    "locale": "en_US",
    "rules": [
        {
            "rule": "required",
            "field": "your-name",
            "error": "Please fill out this field."
        },
        {
            "rule": "required",
            "field": "your-email",
            "error": "Please fill out this field."
        },
        {
            "rule": "email",
            "field": "your-email",
            "error": "Please enter an email address."
        },
        {
            "rule": "required",
            "field": "your-subject",
            "error": "Please fill out this field."
        }
    ]
}

Supported rule types

Contact Form 7 5.6 (Contact Form 7 SWV Schema 2022-03) supports the following 15 rule types.

required

A required rule verifies that the field specified by the field property has a value that is not a zero-length string.

You cannot use this rule type for file uploading fields. For file uploading fields, use the requiredfile rule type instead.

requiredfile

A requiredfile rule verifies that the field specified by the field property has a file object value.

email

An email rule verifies that the field specified by the field property is empty or has a valid email address value.

If no format property is specified, a valid email address is an email address that passes the is_email() function’s test.

url

A url rule verifies that the field specified by the field property is empty or has a valid URL value.

tel

A tel rule verifies that the field specified by the field property is empty or has a telephone number value.

number

A number rule verifies that the field specified by the field property is empty or has a string value representing a floating-point number.

The definition of a valid floating-point number is according to the HTML5 specification.

date

A date rule verifies that the field specified by the field property is empty or has a date value in the YYYY-MM-DD format.

file

A file rule verifies that the field specified by the field property is empty or has a file object value.

If an accept property is specified, the file type must match one of file types that the property defines.

minlength

A minlength rule verifies that the total number of characters (as UTF-16 code units) in value of the fields specified by the field property is more than or equal to the number specified by the threshold property.

maxlength

A maxlength rule verifies that the total number of characters (as UTF-16 code units) in value of the fields specified by the field property is less than or equal to the number specified by the threshold property.

minnumber

A minnumber rule verifies that the numerical value of the field specified by the field property is equal to or greater than the number specified by the threshold property.

maxnumber

A maxnumber rule verifies that the numerical value of the field specified by the field property is equal to or lower than the number specified by the threshold property.

mindate

A mindate rule verifies that the date value of the field specified by the field property is not earlier than the date specified by the threshold property.

maxdate

A maxdate rule verifies that the date value of the field specified by the field property is not later than the date specified by the threshold property.

maxfilesize

A maxfilesize rule verifies that the size of the file object value of the field specified by the field property is equal to or smaller than the size specified by the threshold property.

Meta schema

This is the meta schema for Contact Form 7 SWV schemas created based on the JSON Schema specification.

{
    "$schema": "https://json-schema.org/draft/2020-12/schema",
    "title": "Contact Form 7 SWV",
    "description": "Contact Form 7 SWV meta-schema",
    "type": "object",
    "properties": {
        "version": {
            "type": "string"
        },
        "locale": {
            "type": "string"
        },
        "rules": {
            "type": "array",
            "items": {
                "type": "object",
                "properties": {
                    "rule": {
                        "type": "string",
                        "enum": [
                            "required",
                            "requiredfile",
                            "email",
                            "url",
                            "tel",
                            "number",
                            "date",
                            "file",
                            "minlength",
                            "maxlength",
                            "minnumber",
                            "maxnumber",
                            "mindate",
                            "maxdate",
                            "maxfilesize"
                        ]
                    },
                    "field": {
                        "type": "string",
                        "pattern": "^[A-Za-z][-A-Za-z0-9_:]*$"
                    },
                    "error": {
                        "type": "string"
                    },
                    "accept": {
                        "type": "array",
                        "items": {
                            "type": "string"
                        }
                    },
                    "threshold": {
                        "type": "string"
                    }
                },
                "required": [
                    "rule"
                ]
            }
        }
    }
}

Just another contact form plugin for WordPress. Simple but flexible.

%d bloggers like this: