Text field regex

Can we restrict the entry format to a text field in jsonforms using a regex. I have a text field for an email address in my form and I would like to restrict it to a particular email domain.

[original thread by Johnny]

Hi @syntiro, JSON Schema allows to define a string regex pattern and Ajv will honor it. Additionally you can specify your string to be of email format.

For example you could define a customerMail property like this:

{
  type: 'string',
  format: 'email',
  pattern: '.*example\\.com'
}

Alternatively you could of course only use the pattern for validation

[Johnny]

Excellent Stefan thanks for this

[Johnny]

Hi @sdirix Here is my schema:

[Johnny]

{
  "type": "object",
  "page0": {
    "emailaddress": {
      "type": "string",
      "format": "email",
      "pattern": ".*example\\.com"
    }
  }
}

[Johnny]

and my uischema:

{
  "type": "VerticalLayout",
  "elements": [
    {
      "type": "Control",
      "label": "Email Address",
      "scope": "#/page0/emailaddress"
    }
  ]
}

[Johnny]

and it’s not validating according to the pattern. Do I need a customized Ajv?

The object properties should be defined in a properties object, so either replace page0 with properties or you need to define nested objects, i.e.

{
  "type": "object",
  "properties": {
    "page0": {
      "type": "object",
      "properties": {
        "emailaddress": {
          "type": "string",
          "format": "email",
          "pattern": ".*example\\.com"
        }
      }
    }
  }
}

and

{
  "type": "VerticalLayout",
  "elements": [
    {
      "type": "Control",
      "label": "Email Address",
      "scope": "#/properties/page0/properties/emailaddress"
    }
  ]
}

I can recommend the Understanding JSON Schema object chapter for more information.