schemavalidator.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. # -*- coding: utf-8 -*-
  2. import ispformat.schema as _schema
  3. from jsonschema import Draft4Validator, RefResolver, draft4_format_checker
  4. from jsonschema.exceptions import RefResolutionError, ValidationError
  5. try:
  6. from urllib.parse import urlsplit #py3
  7. except ImportError as e:
  8. from urlparse import urlsplit #py2
  9. class MyRefResolver(RefResolver):
  10. def resolve_remote(self, uri):
  11. # Prevent remote resolving
  12. raise RefResolutionError("LOL NOPE")
  13. geojson_allowed_types=('Polygon', 'MultiPolygon')
  14. def validate_geojson_type(d):
  15. """
  16. Make sure a geojson dict only contains allowed geometry types
  17. """
  18. type_=d.get('type')
  19. if type_ not in geojson_allowed_types:
  20. return False
  21. return True
  22. def validate_geojson(geodict):
  23. """
  24. Convenience function to validate a geojson dict
  25. """
  26. _version = 0.1
  27. schema = _schema.load_schema(_version, 'geojson/geojson')
  28. v = Draft4Validator(
  29. schema,
  30. resolver=MyRefResolver.from_schema(schema, store=_schema.deps_for_version(_version)),
  31. format_checker=draft4_format_checker,
  32. )
  33. for err in v.iter_errors(geodict):
  34. return False
  35. if not validate_geojson_type(geodict):
  36. return False
  37. return True
  38. def validate_isp(jdict):
  39. """
  40. Validate a json-object against the isp json-schema
  41. """
  42. if not 'version' in jdict:
  43. raise ValidationError('version is a required property')
  44. try:
  45. schema=_schema.versions[jdict['version']]
  46. except (AttributeError, TypeError, KeyError):
  47. raise ValidationError('version %r unsupported'%jdict['version'])
  48. v=Draft4Validator(
  49. schema,
  50. resolver=MyRefResolver.from_schema(schema, store=_schema.deps_for_version(jdict['version'])),
  51. format_checker=draft4_format_checker,
  52. )
  53. for err in v.iter_errors(jdict):
  54. yield err
  55. def is_valid_url(u):
  56. try:
  57. pu=urlsplit(u)
  58. except:
  59. return False
  60. if pu.scheme not in ('', 'http', 'https'):
  61. return False
  62. if not pu.netloc:
  63. return False
  64. return True
  65. if 'website' in jdict and not is_valid_url(jdict['website']):
  66. yield ValidationError('%r must be an absolute HTTP URL'%'website',
  67. instance=jdict['website'], schema=schema['properties']['website'],
  68. path=['website'], schema_path=['properties', 'website', 'description'],
  69. validator='validate_url', validator_value=jdict['website'])
  70. if 'logoURL' in jdict and not is_valid_url(jdict['logoURL']):
  71. yield ValidationError('%r must be an absolute HTTP URL'%'logoURL',
  72. instance=jdict['logoURL'], schema=schema['properties']['logoURL'],
  73. path=['logoURL'], schema_path=['properties', 'logoURL', 'description'],
  74. validator='validate_url', validator_value=jdict['logoURL'])
  75. sch=schema['properties']['otherWebsites']['patternProperties']['^.+$']
  76. for name, url in list(jdict.get('otherWebsites', {}).items()):
  77. if is_valid_url(url):
  78. continue
  79. yield ValidationError('%r must be an absolute HTTP URL'%name,
  80. instance=url, schema=sch, path=['otherWebsite', name],
  81. schema_path=['properties', 'otherWebsites', 'patternProperties', '^.+$', 'description'],
  82. validator='validate_url', validator_value=url)
  83. for i, ca in enumerate(jdict.get('coveredAreas', [])):
  84. area=ca.get('area')
  85. if area and validate_geojson_type(area):
  86. continue
  87. elif not area:
  88. continue
  89. yield ValidationError(
  90. 'GeoJSON can only contain the following types: %s'%repr(geojson_allowed_types),
  91. instance=ca, schema=schema['definitions']['coveredArea']['properties']['area'],
  92. path=['coveredAreas', i, 'area'],
  93. schema_path=['properties', 'coveredAreas', 'items', 'properties', 'area'],
  94. validator='validate_geojson_type', validator_value=ca
  95. )