Python json架构formatchecker未验证有效的datetime字符串

2024-03-29 06:45:44 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用有效的日期时间字符串使用JSON架构formatchecker在数据中进行验证,但它没有验证此有效的日期时间格式,因此失败,并出现以下错误:

jsonschema.exceptions.ValidationError: '2013-09-16T15:40:16.21211' is not a 'date-time'

我的代码:

import jsonschema
import json 

data = {"timestamp": "2013-09-16T15:40:16.21211"}

schema ="""{
    "type":"object",
    "$schema": "http://json-schema.org/draft-03/schema",
    "id": "http://jsonschema.net",
    "required":true,
    "properties":{
        "timestamp": { 
            "format" :"date-time",
            "type":"string",
            "required" :false
        }
     }}"""

jsonschema.validate(data,json.loads(schema),format_checker=jsonschema.FormatChecker())

Tags: 字符串importjsonformathttpdatadatetime
1条回答
网友
1楼 · 发布于 2024-03-29 06:45:44

date-time验证器requires the format(“日期和时间格式名称源自RFC 3339,第5.6节”)与RFC3339中指定的一致:

date-fullyear   = 4DIGIT
date-month      = 2DIGIT  ; 01-12
date-mday       = 2DIGIT  ; 01-28, 01-29, 01-30, 01-31 based on 
                          ; month/year
time-hour       = 2DIGIT  ; 00-23
time-minute     = 2DIGIT  ; 00-59
time-second     = 2DIGIT  ; 00-58, 00-59, 00-60 based on leap second
                          ; rules
time-secfrac    = "." 1*DIGIT
time-numoffset  = ("+" / "-") time-hour ":" time-minute
time-offset     = "Z" / time-numoffset

partial-time    = time-hour ":" time-minute ":" time-second
                 [time-secfrac]
full-date       = date-fullyear "-" date-month "-" date-mday
full-time       = partial-time time-offset

date-time       = full-date "T" full-time

规范的full-time部分要求指定time-offset,即必须在datetime之后添加Z,以表示时间是UTC,或者必须添加显式偏移量(即+0100等)。你知道吗

相关问题 更多 >