# -*- coding: utf-8 -*- import ispformat.schema as _schema from jsonschema import Draft4Validator, RefResolver, draft4_format_checker from jsonschema.exceptions import RefResolutionError, ValidationError try: from urllib.parse import urlsplit #py3 except ImportError as e: from urlparse import urlsplit #py2 class MyRefResolver(RefResolver): def resolve_remote(self, uri): # Prevent remote resolving raise RefResolutionError("LOL NOPE") geojson_allowed_types=('Polygon', 'MultiPolygon') def validate_geojson_type(d): """ Make sure a geojson dict only contains allowed geometry types """ type_=d.get('type') if type_ not in geojson_allowed_types: return False return True def validate_geojson(geodict): """ Convenience function to validate a geojson dict """ _version = 0.1 schema = _schema.load_schema(_version, 'geojson/geojson') v = Draft4Validator( schema, resolver=MyRefResolver.from_schema(schema, store=_schema.deps_for_version(_version)), format_checker=draft4_format_checker, ) for err in v.iter_errors(geodict): return False if not validate_geojson_type(geodict): return False return True def validate_isp(jdict): """ Validate a json-object against the isp json-schema """ if not 'version' in jdict: raise ValidationError('version is a required property') try: schema=_schema.versions[jdict['version']] except (AttributeError, TypeError, KeyError): raise ValidationError('version %r unsupported'%jdict['version']) v=Draft4Validator( schema, resolver=MyRefResolver.from_schema(schema, store=_schema.deps_for_version(jdict['version'])), format_checker=draft4_format_checker, ) for err in v.iter_errors(jdict): yield err def is_valid_url(u): try: pu=urlsplit(u) except: return False if pu.scheme not in ('', 'http', 'https'): return False if not pu.netloc: return False return True if 'website' in jdict and not is_valid_url(jdict['website']): yield ValidationError('%r must be an absolute HTTP URL'%'website', instance=jdict['website'], schema=schema['properties']['website'], path=['website'], schema_path=['properties', 'website', 'description'], validator='validate_url', validator_value=jdict['website']) if 'logoURL' in jdict and not is_valid_url(jdict['logoURL']): yield ValidationError('%r must be an absolute HTTP URL'%'logoURL', instance=jdict['logoURL'], schema=schema['properties']['logoURL'], path=['logoURL'], schema_path=['properties', 'logoURL', 'description'], validator='validate_url', validator_value=jdict['logoURL']) sch=schema['properties']['otherWebsites']['patternProperties']['^.+$'] for name, url in list(jdict.get('otherWebsites', {}).items()): if is_valid_url(url): continue yield ValidationError('%r must be an absolute HTTP URL'%name, instance=url, schema=sch, path=['otherWebsite', name], schema_path=['properties', 'otherWebsites', 'patternProperties', '^.+$', 'description'], validator='validate_url', validator_value=url) for i, ca in enumerate(jdict.get('coveredAreas', [])): area=ca.get('area') if area and validate_geojson_type(area): continue elif not area: continue yield ValidationError( 'GeoJSON can only contain the following types: %s'%repr(geojson_allowed_types), instance=ca, schema=schema['definitions']['coveredArea']['properties']['area'], path=['coveredAreas', i, 'area'], schema_path=['properties', 'coveredAreas', 'items', 'properties', 'area'], validator='validate_geojson_type', validator_value=ca )