2017-04-11 148 views

回答

2

架構中添加正則表達式JSON模式 使用以下。

{ 
    "type": "string", 
    "pattern": "^\d{4}\-(0?[1-9]|1[012])\-(0?[1-9]|[12][0-9]|3[01])$" 
} 
+0

使用這個正則表達式將接受2月30日太..這就是我的問題 – user6543599

+0

哦好吧,你只是指出格式..但有效性,你可以參考以下鏈接。 http://stackoverflow.com/questions/51224/regular-expression-to-match-valid-dates/8768241#8768241 – Alekhya

+0

剛試過這個。獲取錯誤:ValueError:無效\ escape – GDB

0

JSON Schema已經爲日期,時間,日期時間,電子郵件,主機名,IP地址定義了格式。你可以更喜歡這個更簡單和推薦的方法,而不是寫自己的正則表達式。

"date": { 
    "type": "string", 
    "format": "date" 
} 

日期和時間格式名稱來源於RFC 3339的第5.6節[RFC3339]。

參考:http://json-schema.org/latest/json-schema-validation.html#rfc.section.7.3