How to check null string using Hypi Directive(@notEmpty)

In the previous post, we started using Hypi Directives and we have seen how to check string length using @length directive.

In this post, we will see how to check null string using Hypi directive.

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 @notEmpty directive!
str: String @notEmpty

  • Use the @notEmpty directive to check if the string or an array is empty or null.
  • It returns an error if the string/array is empty or null
  • It also returns an error if the string only has whitespaces.

We will work with the below schema.

type DirectiveObj {
    str: String @notEmpty
}

Sample Query

Let’s add the whitespace to the str field in the DirectiveObj and check how @notEmpty works.

mutation {
  upsert(values: { DirectiveObj: { str: "  " } }) {
    id
  }
}
#result
{
  "data": {
    "upsert": [
      {
        "id": "01FN8EC4SA79GEPMMZHEHJ5NRT"
      }
    ]
  },
  "errors": [
    {
      "message": "'DirectiveObj.str' is required, it cannot be null, empty or contain only spaces.",
      "extensions": {}
    }
  ]
}

Check the POSTMAN collection for the @notEmpty 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