JSON记录应该无效,但它显示的值为True
import json
import jsonschema
dat='{"TID":"9020","CUR":"USD","DAT1":"t","DAT2":11}'
print type(dat)
def validJson(rowData):
jsonRec=json.loads(rowData)
jsonSc=json.loads('''{ "title": "NCAP", "type": "object", "properties": { "TID" : { "type ": [ "string" ] },"CUR" : { "type ": [ "string" ] , "maxLength" : 3 },"DAT1" : { "type ": [ "number","null"] , "maximum" : 99999999999 },"DAT2" : { "type ": [ "number" ,"null"] , "maximum" : 112 } }, "required": [ "TID", "CUR", "DAT1", "DAT2" ] }''')
val = jsonschema.Draft3Validator(jsonSc).is_valid(jsonRec)
print "is records valid ::"+str( val)
va=jsonschema.validate(jsonRec, jsonSc)
print "records validated "+str(va)
return json.dumps(jsonRec)
print validJson(dat)
它应该为False的原因是DAT1数据类型是数字,而其值是“t”
或者,如果有人可以建议某种方式,json记录的架构得到验证
答案 0 :(得分:0)
您的type
关键字后面有空格。 Json Schema并没有认识到这是一个关键词,而忽略了它。摆脱额外的空间,它将按预期工作。