How to define pattern for a string using the @pattern directive

We have seen the following Hypi directives till now.

Now we will check the @pattern directive to define a pattern for a string.

Again a brief info on Hypi Directives!

  • Hypi directives help to customise the behaviour of the data fields.
  • @ character depicts the Hypi directive. It is followed by a series of characters.
  • A directive may have a list of optional named arguments.
  • Disable directive by adding # in front of it.

Let’s check the @pattern directive!

pattern: String @pattern(regex: ["^test.*$"])

  • Use the @pattern directive to define a pattern for a string field.
  • Provide the pattern in terms of a regular expression (regex)
  • @pattern checks if the field value matches the regex
  • Provide regex values as inputs within brackets
  • allMustMatch argument of the @pattern directive accepts a boolean value
  • If allMustMatch is true, the field value must match all the regex values inside the bracket. (AND condition)
  • If allMustMatch is false, the field value must match any of the regex values inside the bracket. (OR condition)

Check this comprehensive guide to form a regex input.

We will work with the below schema.

type RegExPattern {
    pattern: String @pattern(regex: ["[a-zA-Z0-9]{6}"])
    pattern1: String @pattern(regex: ["[amn]?","\\D","\\d","[789][0-9]{9}"], 
              allMustMatch: true)
    pattern2: String @pattern(regex: ["[789][0-9]{9}", "^hypi.*$"], 
              allMustMatch: false)
}

Sample Query

Let’s upsert data to String fields and check how the @pattern directive works.

# Query1
mutation {
    upsert(values: {
            RegExPattern: {
              pattern:"aw",
              pattern1:"d",
              pattern2:"4234242498"
            }
        }
    ) {
        id
    }
}
#result
{
  "data": {
    "upsert": [
      {
        "id": "01FPA3RNZW3FJK0D9659RP5CPA"
      }
    ]
  },
  "errors": [
    {
      "message": "Invalid value 'aw'. Does not match patterns '[a-zA-Z0-9]{6}' from the list of required patterns [a-zA-Z0-9]{6}. All of which must match",
      "extensions": {}
    },
    {
      "message": "Invalid value 'd'. Does not match patterns '[amn]?' from the list of required patterns [amn]? AND \\D. All of which must match",
      "extensions": {}
    },
    {
      "message": "Invalid value '4234242498'. Does not match patterns '[789][0-9]{9} OR ^hypi.*
``` from the list of possible patterns [789][0-9]{9} OR ^hypi.*$. ",
      "extensions": {}
    }
  ]
}
# Query2
mutation {
    upsert(values: {
            RegExPattern: {
              pattern:"aSveh3",
              pattern1:"n",
              pattern2:"8234242498"
            }
        }
    ) {
        id
    }
}
#result
{
  "data": {
    "upsert": [
      {
        "id": "01FPA3QN87XRVJX0C0304ERXMQ"
      }
    ]
  }
}

Check the POSTMAN collection for the @pattern directive in different programming languages! Click </> and choose the programming language of your choice.
Don’t forget to insert your own Authorization key and Hypi Domain under Headers to test the results!
Run in Postman