Pathfinder
Pathfinder

Reputation: 994

JSON Schema not validating required field when it is an empty string

I'm using mule validate JSON schema component to validate my incoming json request. It validates the type but not the required field attributes

{
  "type": "object",
  "$schema": "http://json-schema.org/draft-04/schema",
  "properties": {
    "Employees": {
      "type": "array",
      "minItems": 1,
      "items": {
        "type": "object",
        "properties": {
          "BirthDate": {
            "type": "string",
            "format": "date-time"
          },
          "EmpNum": {
            "type": "number"
          },
          "FirstName": {
            "type": "string"
          },
          "Gender": {
            "type": "string"
          },
          "LastName": {
            "type": "string"
          },
          "LicenseNumber": {
            "type": "string"
          },
          "ZipCode": {
            "type": "string"
          }
        },
        "required": ["EmpNum", "LastName", "FirstName", "Street", "ZipCode", "BirthDate" ]
      }
    }
  }
}

I have json as shown below:

{
  "Employees": [
    {
      "EmpNum": 3,
      "FirstName": "Finder",
      "LastName": "Path",
      "Street": "392 CDI CDIJUW",
      "ZipCode": "12345",
      "BirthDate": "1943-05-19T04:00:00Z",
      "Gender": "M"
    },
    {
       "EmpNum": 3,
      "FirstName": "",
      "LastName": "Path",
      "Street": "392 CDI CDIJUW",
      "ZipCode": "12345",
      "BirthDate": "1943-05-19T04:00:00Z",
      "Gender": "M"
    }
  ]
}

Even though I have set a field to an empty string, it still takes as a valid request and proceeds further.

Upvotes: 1

Views: 6293

Answers (1)

Angelo Sandaga
Angelo Sandaga

Reputation: 126

If you intentionally set FirstName to be an empty string and want to invalidate it, try adding minLength:

      "FirstName": {
        "type": "string",
        "minLength": 1
      },

Upvotes: 6

Related Questions