Reputation: 10888
Say you have this simple schema
{
"type": "string",
"minLength": 2,
}
The value "ab"
would be valid, but the value "a "
or " "
would also be valid. Is there a way to ignore spaces when validating the length, so only "ab"
is valid in this example?
Upvotes: 3
Views: 5151
Reputation: 7694
Something like this should work:
{
"pattern": "^(\\s*\\w\\s*){2,}$"
}
"at least 2 groups where each group has exactly one non-space character (optionally surrounded by space characters)".
"" -> false
" " -> false
" " -> false
" a" -> false
"a " -> false
" a " -> false
" a b " -> true
"ab" -> true
Upvotes: 4
Reputation: 1757
I do not know enough about JSON schema to definitively say whether there is a way to do what you are asking with the length attribute. However, if you have an idea of what possible non-whitespace characters are allowed, you can do something like the following that allows any number of white-space characters along with two required characters (in this case - alphabetic characters).
NOTE: I made the declaration inside an array for easy testing
{
"type": "array"
, "items": {
"type":"string"
, "pattern": "[A-Za-z]\\s*[A-Za-z]"
}
}
Some tests:
[
"ab"
, "a " // fails
, " " // fails
, "cz"
, "a b"
, " a b"
, " b" // fails
]
Upvotes: 1