问题
I need to define a JSON schema wherein the input can either be a date or empty string.
My current JSON schema is
{
"type": "object",
"required": [
"FirstName",
"DateOfBirth"
],
"properties": {
"FirstName": {
"type": "string"
},
"DateOfBirth": {
"type": "string",
"format": "date"
}
}
}
This allows
{
"FirstName": "Alex",
"DateOfBirth": "1980-10-31"
}
but not
{
"FirstName": "Alex",
"DateOfBirth": ""
}
How can I define the JSON schema so that DateOfBirth allows dates as well as empty string.
回答1:
Remove DateOfBirth
from required
list:
{
"type": "object",
"required": [ "FirstName" ],
"properties": {
"FirstName": {
"type": "string"
},
"DateOfBirth": {
"type": "string",
"format": "date"
}
}
}
Supported JSON:
DateOfBirth
is set:
{
"FirstName": "Alex",
"DateOfBirth": "1980-10-31"
}
DateOfBirth
isn't set:
{
"FirstName": "Alex",
}
Since you've added basic semantic validation for date
values, you need to make sure the provided value follows the given format. Empty string (""
) isn't a valid date format.
来源:https://stackoverflow.com/questions/61483435/json-schema-to-allow-date-or-empty-string