我正在try 验证JSON模式.为released
Cerberus指定正确的数据类型date
时,会抛出一个错误.
def test_validate_books_schema():
schema = {
"url" : {'type': 'string'},
"name" : {'type': 'string'},
"isbn" : {'type': 'string'},
"authors" : {'type': ['string','list']},
"numberOfPages" : {'type': 'integer'},
"publisher" : {'type': 'string'},
"country" : {'type': 'string'},
"mediaType" : {'type': 'string'},
"released" : {'type': 'date'},
"characters" : {'type': ['string','list']},
"povCharacters" : {'type': ['string','list']}
}
response = requests.get("https://www.anapioficeandfire.com/api/books/1")
v = Validator(schema)
validate_response = v.validate(response.json())
assert_that(validate_response, description=v.errors).is_true()
./tests/books/test_books.py::test_validate_books_schema Failed: [undefined]AssertionError: [{'released': ['must be of date type']}] Expected <True>, but was not.
def test_validate_books_schema():
schema = {
"url" : {'type': 'string'},
"name" : {'type': 'string'},
"isbn" : {'type': 'string'},
"authors" : {'type': ['string','list']},
"numberOfPages" : {'type': 'integer'},
"publisher" : {'type': 'string'},
"country" : {'type': 'string'},
"mediaType" : {'type': 'string'},
"released" : {'type': 'date'},
"characters" : {'type': ['string','list']},
"povCharacters" : {'type': ['string','list']}
}
response = requests.get("https://www.anapioficeandfire.com/api/books/1")
v = Validator(schema)
validate_response = v.validate(response.json())
> assert_that(validate_response, description=v.errors).is_true()
E AssertionError: [{'released': ['must be of date type']}] Expected <True>, but was not.
tests\books\test_books.py:42: AssertionError
documentation表示released
的数据类型是Date
.当我为released
指定string
时,它是有效的.