Skip to content

Commit

Permalink
add jsonschema validation for ongoing attribute.
Browse files Browse the repository at this point in the history
  • Loading branch information
Johnetordoff committed Jul 26, 2018
1 parent 344f50a commit fe0ad65
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 7 deletions.
31 changes: 31 additions & 0 deletions api/users/education-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,37 @@
"dependencies": {
"endMonth": ["endYear"],
"startMonth": ["startYear"]
},
"anyOf": [
{
"not": {
"properties": {
"ongoing": {
"enum": [true]
}
},
"required": ["ongoing"]
}
},
{ "required": ["startYear"]}
],
"anyOf": [
{
"not": {
"properties": {
"ongoing": { "enum": [false] }
},
"required": ["ongoing"]
}
},
{ "required": ["endYear"] }
],
"not": {
"properties": {
"ongoing": {"enum": [true]}
},
"required": ["endYear"],
"message": "Ongoing positions cannot have end dates."
}
}
}
31 changes: 31 additions & 0 deletions api/users/employment-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,37 @@
"dependencies": {
"endMonth": ["endYear"],
"startMonth": ["startYear"]
},
"anyOf": [
{
"not": {
"properties": {
"ongoing": {
"enum": [true]
}
},
"required": ["ongoing"]
}
},
{ "required": ["startYear"]}
],
"anyOf": [
{
"not": {
"properties": {
"ongoing": { "enum": [false] }
},
"required": ["ongoing"]
}
},
{ "required": ["endYear"] }
],
"not": {
"properties": {
"ongoing": {"enum": [true]}
},
"required": ["endYear"],
"message": "Ongoing positions cannot have end dates."
}
}
}
5 changes: 2 additions & 3 deletions api/users/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,10 @@ def _validate_user_json(self, value, json_schema):
try:
jsonschema.validate(value, from_json(json_schema))
except jsonschema.ValidationError as e:
if type(e.validator_value) == dict and e.validator_value.get('message'):
raise InvalidModelValueError(e.validator_value.get('message'))
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)
Expand All @@ -158,8 +159,6 @@ def _validate_dates(self, info):
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.')
elif history.get('endYear') or history.get('endMonth'):
raise InvalidModelValueError(detail='Ongoing positions cannot have end dates.')

def validate_employment(self, value):
self._validate_user_json(value, 'employment-schema.json')
Expand Down
11 changes: 7 additions & 4 deletions api_tests/users/views/test_user_detail.py
Original file line number Diff line number Diff line change
Expand Up @@ -1138,16 +1138,19 @@ def test_user_put_profile_date_validate_start_month_dependency(self, app, user_o
assert res.status_code == 400
assert res.json['errors'][0]['detail'] == "u'startYear' is a dependency of u'startMonth'"

def test_user_put_profile_date_validate_start_date_no_end_date(self, app, user_one, user_attr, user_one_url, start_dates_no_end_dates_payload, request_key):
def test_user_put_profile_date_validate_start_date_no_end_date_not_ongoing(self, app, user_one, user_attr, user_one_url, start_dates_no_end_dates_payload, request_key):
# End date is greater then start date
res = app.put_json_api(user_one_url, start_dates_no_end_dates_payload, auth=user_one.auth, expect_errors=True)
user_one.reload()
assert res.status_code == 200
assert getattr(user_one, user_attr) == start_dates_no_end_dates_payload['data']['attributes'][request_key]
assert res.status_code == 400
print res.json['errors'][0]['detail']
assert res.json['errors'][0]['detail'] == "{{u'startYear': 1991, u'{}': u'', u'startMonth': 9, u'ongoing':" \
" False, u'department': u'', u'institution': u'Fake U'}}" \
' is not valid under any of the given schemas'.format('title' if user_attr == 'jobs' else 'degree')

def test_user_put_profile_date_validate_end_date_no_start_date(self, app, user_one, user_attr, user_one_url, end_dates_no_start_dates_payload, request_key):
# End dates, but no start dates
res = app.put_json_api(user_one_url, end_dates_no_start_dates_payload, auth=user_one.auth, expect_errors=True)
res = app.put_json_api(user_one_url, end_dates_no_start_dates_payload, auth=user_one.auth)
user_one.reload()
assert res.status_code == 200
assert getattr(user_one, user_attr) == end_dates_no_start_dates_payload['data']['attributes'][request_key]
Expand Down

0 comments on commit fe0ad65

Please sign in to comment.