简体   繁体   English

“ jsonschema”未在Python中验证架构

[英]“jsonschema” not validating schema in Python

The JSON records should be invalid but its showing the value to be True 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)

The reason why it should be False is that DAT1 datatype is number while its value is "t" 应该为False的原因是DAT1数据类型是数字,而其值是“ t”

Or if anyone can suggest some a way where it the json records gets validated with respect to its Schema 或者如果有人可以建议某种方式来验证JSON记录是否符合其架构

You have a space after your type keyword. 您的type关键字后有一个空格。 Json Schema doesn't recognize that as a keyword and ignores it. Json Schema无法将其识别为关键字,因此会忽略它。 Get rid of that extra space and it will work as expected. 摆脱多余的空间,它将按预期工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM