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