-
Notifications
You must be signed in to change notification settings - Fork 336
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
move schemas and make utility functions.
- Loading branch information
1 parent
53ab147
commit 70eaad7
Showing
6 changed files
with
44 additions
and
38 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
Empty file.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import os | ||
import json | ||
import datetime | ||
import jsonschema | ||
|
||
from api.base.exceptions import InvalidModelValueError | ||
|
||
here = os.path.split(os.path.abspath(__file__))[0] | ||
|
||
def from_json(fname): | ||
with open(os.path.join(here, fname)) as f: | ||
return json.load(f) | ||
|
||
|
||
def validate_user_json(value, json_schema): | ||
try: | ||
jsonschema.validate(value, from_json(json_schema)) | ||
except jsonschema.ValidationError as e: | ||
if len(e.path) > 1: | ||
raise InvalidModelValueError("For '{}' the field value {}".format(e.path[-1], e.message)) | ||
raise InvalidModelValueError(e.message) | ||
except jsonschema.SchemaError as e: | ||
raise InvalidModelValueError(e.message) | ||
|
||
validate_dates(value) | ||
|
||
|
||
def validate_dates(info): | ||
for history in info: | ||
|
||
if history.get('startYear'): | ||
startDate = datetime.date(history['startYear'], history.get('startMonth', 1), 1) | ||
|
||
if not history['ongoing']: | ||
if history.get('endYear'): | ||
endDate = datetime.date(history['endYear'], history.get('endMonth', 1), 1) | ||
|
||
if history.get('startYear') and history.get('endYear'): | ||
if (endDate - startDate).days <= 0: | ||
raise InvalidModelValueError(detail='End date must be greater than or equal to the start date.') | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters