schemavalidator.py 4.0 KB

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