People make mistakes – that’s an essential part of being a human. Intelligent software can identify a mistake, elegantly inform about it and help resolve it. This is especially true for a CMS, which should always assist editors in their work.
Software designers came up with validations as a way of preventing mistakes from happening. Validations enforce specific rules on the editable content, making it impossible to type more than 140 characters in a tweet field or to upload a 50-megabyte TIFF image as a feature photograph.
Validations are helpful, as they ensure consistency of the content. When the editorial team consists of a single editor, setting up validations will make little sense and would not be worth the effort, but when the team is growing, it's much easier to control the content on software level rather than hoping everyone will stick to the content guide rules.
We've reworked the validation editor in the Contentful web application, and in this post we're presenting the highlights of the improvements.
Our goal was to simplify adding specific validation rules to the content. First, validation rules now depend on the field types: some fields have multiple validation options, while others have none. For instance, media assets can be checked against file size and file type limits, whereas there are no validation rules for locations.
Second, we wanted to prevent users from building room for struggle by creating rules that are impossible to satisfy: imagine an event that begins on October 10 and ends on March 5 same year. Likewise, setting a limit of -100 characters would make little sense. To avoid that, we’ve implemented something clever: validations on validations. How meta. The validation builder will strictly say no, refusing to save any nonsense like negative file sizes.
Third, we wanted to add more human touch to error responses, so that they can guide the users, explaining what went wrong and giving directions on how to fix it. Error messages often tend to be more on the technical side. Most certainly, “Error 131. File size limitation exceeded” reads less pleasantly than “Please upload a file no larger than 5 MB”.
We’re aiming for the latter – friendly, human, respectful and clear speech. We’ve rewritten all error messages, and also enabled writing custom messages. The overall goal is to make Contentful communicate with the editorial team like a good friend.
Next, we wanted to empower you to control the content in some new ways. Thus, the following values can now be validated as well:
Number of items in an array. This is great for indicating a specific number of items in multiple entries, symbols or assets fields.
Date range. Specify earliest and/or latest date.
File size. This had been requested quite often to prevent photo editors from uploading 30MB files. Just limit the upload file size.
Image dimensions. You can now explicitly set preferred image dimensions so that they fit into layout as designed.
The validations would not be complete without the possibility of using regular expressions, which enable selecting specific strings of characters which follow certain patterns. Regular expressions can help identify incomplete phone numbers, mistyped ZIP codes or URLs containing emojis, to name but a few popular applications.
We included a number of popular presets with regex patterns, so that you can enforce the need to provide a valid email, URL, date and more. When setting up such a rule, take into account that regions like US, Europe and Asia may differ in how they display such information: it’s 01.04.2015 in Berlin, but 04/01/2015 in US. Naturally, everybody is welcome to write custom regular expressions.
To try the new validations, open the Contentful web app and navigate to the Content model section. Open any content type and click Validate inside some field. Do consider playing with different types of fields, as they are quite diverse. In case you need further documentation, consult our guide to Contentful validations.
Note that the validation rules only become effective once a content type is published or updated, so new rules will not apply to existing content. Entries are only validated the moment they are published.
Please note that it’s necessary to be careful when setting up validations, so they’ll be helping editors, not irritate them. The validation limits should be chosen wisely: if some content can't be uploaded because of restriction which violates common sense, people will get disappointed. There's no real reason in setting the file size limit to 1 MB, as the servers will certainly not break if someone uploads a 1.3 MB photo.