Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 907
TheWizardOfTN Avatar asked Apr 28 '26 11:04

TheWizardOfTN


1 Answers

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.

like image 140
gregsdennis Avatar answered May 01 '26 00:05

gregsdennis



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!