0
votes

Below are the jsonSchema fields I need to validate

"field1": 
      {
        "type": "integer",
        "javaType": "long",
        "pattern": "^[0-9]{6}"
      },
      "field2": {
        "type": "integer",
        "javaType": "long",
        "pattern": "^[0-9]{10}"
      },

Field 1 should be 6 exactly digits and field 2 exactly 10 digits. But when I upload data less or greater than the given limit it still works. Not sure where the issue is

1

1 Answers

0
votes

The patterns are incomplete, they're equivalent to startsWith(6 digits) and startsWith(10 digits). You need to add $ at the end to verify the fields are exactly 6 and 10 digits long.

^[0-9]{6}$ ^[0-9]{10}$

After some testing, I discovered that because of some black magic I don't understand, patterns will always match if you use it with integer fields. If you want to check the length of an integer, you should use minimum and maximum.

"field1": {
    "type": "integer",
    "javaType": "long",
    "minimum": 100000,
    "maximum": 999999
},
"field2": {
    "type": "integer",
    "javaType": "long",
    "minimum": 1000000000,
    "maximum": 9999999999
}

I made the tests with Postman, I don't know if it's the best for that, but that's the only tool I had running.