def _validate_json_data(json_data): # Here we are validating the followings: # 1. date values are actually date type data and the beginDate <= endDate # 2. 'url' is valid and live if the url value is not none or 'unknown' (case insensitive) # 3. 'sampleMedium' is not empty string # 4. 'variableName' is not empty string # 5. 'title' is not empty string # 6. 'abstract' is not empty string # 7. 'fileVersion' is not empty string # 8. 'symbol' is not empty string # 9. 'siteName' is not empty string # 10. 'methodLink' is not empty string # 11. 'methodDescription' is not empty string err_msg = "Invalid json file. {}" # validate title _check_for_empty_string(json_data['timeSeriesReferenceFile']['title'], 'title') # validate abstract _check_for_empty_string(json_data['timeSeriesReferenceFile']['abstract'], 'abstract') # validate fileVersion _check_for_empty_string(json_data['timeSeriesReferenceFile']['fileVersion'], 'fileVersion') # validate symbol _check_for_empty_string(json_data['timeSeriesReferenceFile']['symbol'], 'symbol') series_data = json_data['timeSeriesReferenceFile']['referencedTimeSeries'] urls = [] for series in series_data: try: start_date = parser.parse(series['beginDate']) end_date = parser.parse(series['endDate']) except Exception: raise Exception(err_msg.format("Invalid date values")) if timezone.is_aware(start_date): start_date = timezone.make_naive(start_date) if timezone.is_aware(end_date): end_date = timezone.make_naive(end_date) if start_date > end_date: raise Exception(err_msg.format("Invalid date values")) # validate requestInfo object request_info = series["requestInfo"] # validate refType if request_info['refType'] not in RefTimeseriesLogicalFile.get_allowed_ref_types(): raise ValidationError("Invalid value for refType") # validate returnType if request_info['returnType'] not in RefTimeseriesLogicalFile.get_allowed_return_types(): raise ValidationError("Invalid value for returnType") # validate serviceType if request_info['serviceType'] not in RefTimeseriesLogicalFile.get_allowed_service_types(): raise ValidationError("Invalid value for serviceType") # check valueCount is not a negative number if series['valueCount'] is not None and series['valueCount'] < 0: raise ValidationError("valueCount can't be a negative number") # check that sampleMedium _check_for_empty_string(series['sampleMedium'], 'sampleMedium') # validate siteName _check_for_empty_string(series['site']['siteName'], 'siteName') # validate variableName _check_for_empty_string(series['variable']['variableName'], 'variableName') url = Request(request_info['url']) if url not in urls: urls.append(url) try: urlopen(url) except URLError: raise Exception(err_msg.format("Invalid web service URL found")) # validate methodDescription for empty string _check_for_empty_string(series['method']['methodDescription'], 'methodDescription') # validate methodLink if series['method']['methodLink'] is not None: url = series['method']['methodLink'].strip() if not url: raise ValidationError("methodLink has a value of empty string") if url.lower() != 'unknown': url = Request(url) if url not in urls: urls.append(url) try: urlopen(url) except URLError: raise Exception(err_msg.format("Invalid method link found"))