JSON Schema Regular Expressions

One of the best parts about the JSON Schema evaluation is the simple use of Regular Expressions to validate your data.  JSON Schema lets you embed any regular expression into the properties that you create using relatively simple syntax.

In this example, I have a property (called record-id) that uses a regular expression to only allow alphabet characters, spaces, underscores and dashes.

              "record-id": {

                "$id": "#/record/record-id",

                "type": "string",

                "title": "A unique identifier, used by the institution to identify the complaint in their source platform.",

                "default": "",

                "maxLength": 100,

                "examples": [

                  "RECORD-12_345 Y"

                ],

                "pattern": "^(?=.*[a-zA-Z])(?=.*[0-9])[a-zA-Z0-9 _-]+$"

              }

What’s great about this is that I can supply defaults and examples to what is being asked, simplifying the task and documentation for the user.

I love this because I’m not an expert at Regular Expression design and if I were to implement this in my own code outside of JSON-Schema – you’re looking at 4 – 5 lines of code on it’s own making my life a lot simpler.

Post A Reply

%d bloggers like this: