JSON 当必填字段为空字符串时,架构未对其进行验证
JSON Schema not validating required field when it is an empty string
我正在使用 mule validate JSON 架构组件来验证我的传入 json 请求。
它验证类型但不验证必需的字段属性
{
"type": "object",
"$schema": "http://json-schema.org/draft-04/schema",
"properties": {
"Employees": {
"type": "array",
"minItems": 1,
"items": {
"type": "object",
"properties": {
"BirthDate": {
"type": "string",
"format": "date-time"
},
"EmpNum": {
"type": "number"
},
"FirstName": {
"type": "string"
},
"Gender": {
"type": "string"
},
"LastName": {
"type": "string"
},
"LicenseNumber": {
"type": "string"
},
"ZipCode": {
"type": "string"
}
},
"required": ["EmpNum", "LastName", "FirstName", "Street", "ZipCode", "BirthDate" ]
}
}
}
}
我有json如下图:
{
"Employees": [
{
"EmpNum": 3,
"FirstName": "Finder",
"LastName": "Path",
"Street": "392 CDI CDIJUW",
"ZipCode": "12345",
"BirthDate": "1943-05-19T04:00:00Z",
"Gender": "M"
},
{
"EmpNum": 3,
"FirstName": "",
"LastName": "Path",
"Street": "392 CDI CDIJUW",
"ZipCode": "12345",
"BirthDate": "1943-05-19T04:00:00Z",
"Gender": "M"
}
]
}
即使我将一个字段设置为空字符串,它仍然会被视为有效请求并继续进行。
如果您故意将 FirstName 设置为空字符串并希望使其无效,请尝试添加 minLength:
"FirstName": {
"type": "string",
"minLength": 1
},
我正在使用 mule validate JSON 架构组件来验证我的传入 json 请求。 它验证类型但不验证必需的字段属性
{
"type": "object",
"$schema": "http://json-schema.org/draft-04/schema",
"properties": {
"Employees": {
"type": "array",
"minItems": 1,
"items": {
"type": "object",
"properties": {
"BirthDate": {
"type": "string",
"format": "date-time"
},
"EmpNum": {
"type": "number"
},
"FirstName": {
"type": "string"
},
"Gender": {
"type": "string"
},
"LastName": {
"type": "string"
},
"LicenseNumber": {
"type": "string"
},
"ZipCode": {
"type": "string"
}
},
"required": ["EmpNum", "LastName", "FirstName", "Street", "ZipCode", "BirthDate" ]
}
}
}
}
我有json如下图:
{
"Employees": [
{
"EmpNum": 3,
"FirstName": "Finder",
"LastName": "Path",
"Street": "392 CDI CDIJUW",
"ZipCode": "12345",
"BirthDate": "1943-05-19T04:00:00Z",
"Gender": "M"
},
{
"EmpNum": 3,
"FirstName": "",
"LastName": "Path",
"Street": "392 CDI CDIJUW",
"ZipCode": "12345",
"BirthDate": "1943-05-19T04:00:00Z",
"Gender": "M"
}
]
}
即使我将一个字段设置为空字符串,它仍然会被视为有效请求并继续进行。
如果您故意将 FirstName 设置为空字符串并希望使其无效,请尝试添加 minLength:
"FirstName": {
"type": "string",
"minLength": 1
},