ghanshyam | f9ded35 | 2016-04-12 17:03:01 +0900 | [diff] [blame] | 1 | # Copyright 2016 NEC Corporation. |
| 2 | # All Rights Reserved. |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 5 | # not use this file except in compliance with the License. You may obtain |
| 6 | # a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | # License for the specific language governing permissions and limitations |
| 14 | # under the License. |
| 15 | |
| 16 | import jsonschema |
| 17 | from oslo_utils import timeutils |
| 18 | |
| 19 | # JSON Schema validator and format checker used for JSON Schema validation |
| 20 | JSONSCHEMA_VALIDATOR = jsonschema.Draft4Validator |
| 21 | FORMAT_CHECKER = jsonschema.draft4_format_checker |
| 22 | |
| 23 | |
| 24 | # NOTE(gmann): Add customized format checker for 'date-time' format because: |
| 25 | # 1. jsonschema needs strict_rfc3339 or isodate module to be installed |
| 26 | # for proper date-time checking as per rfc3339. |
| 27 | # 2. Nova or other OpenStack components handle the date time format as |
| 28 | # ISO 8601 which is defined in oslo_utils.timeutils |
| 29 | # so this checker will validate the date-time as defined in |
| 30 | # oslo_utils.timeutils |
| 31 | @FORMAT_CHECKER.checks('iso8601-date-time') |
| 32 | def _validate_datetime_format(instance): |
| 33 | try: |
| 34 | if isinstance(instance, jsonschema.compat.str_types): |
| 35 | timeutils.parse_isotime(instance) |
| 36 | except ValueError: |
| 37 | return False |
| 38 | else: |
| 39 | return True |