TheWizardOfTN
TheWizardOfTN

Reputation: 186

JSON Schema with a minimum length, unless empty/null

I'm creating a JSON Schema, and I'm using a definition for social security number, and I'm wondering if I can have both a minimum length AND allow it to be null in the same definition.

"socialSecurityField":{
"type": "string",
"minLength":9,
"maxLength": 11,
"pattern":"(^\\d{3}([ -])?)\\d{2}([ -])?)\\d{4})?"
}

So we have a SS field that will allow just numbers, or numbers and dashes, and no letters, but not nulls unless I get rid of the minLength.

How do I allow nulls AND minLength?

Upvotes: 3

Views: 2338

Answers (1)

gregsdennis
gregsdennis

Reputation: 8428

The type keyword can take an array of values. This allows you to specify several types that your schema will accept.

{
  "type": [ "string", "null" ],
  ...
}

Keep the rest of the schema as is. The other keywords you have will only apply if the value is a string.

Upvotes: 6

Related Questions