class Parameters(object): """ Class that holds all parameter schemas """ api_key = Field( 'api-key', location='query', required=True, schema=String(description='API KEY for access healthsites api.'), ) page = Field( 'page', location='query', required=True, schema=Integer( description='A page number within the paginated result set.'), ) extent = Field( 'extent', location='query', required=False, schema=String( description='Extent of map that is used for filtering data. ' '(format: minLng, minLat, maxLng, maxLat)'), ) timestamp_from = Field( 'from', location='query', required=False, schema=Integer( description='Get latest modified data from this timestamp.'), ) timestamp_to = Field( 'to', location='query', required=False, schema=Integer( description='Get latest modified data from this timestamp.'), ) country = Field( 'country', location='query', required=False, schema=String(description='Filter by country'), ) output = Field( 'output', location='query', required=False, schema=String( description= 'Output format for the request. (json/xml/geojson, default: json)' ), )
def test_array_additional_items_disallowed(): schema = Array(items=[String(), Integer()]) assert schema.validate(['a', 123, True]) == [] schema = Array(items=[String(), Integer()], additional_items=False) assert schema.validate(['a', 123, True]) == [('Must have no more than 2 items.', [])] schema = Array(items=[String(), Integer()], additional_items=Integer()) assert schema.validate(['a', 123, 'c']) == [('Must be an integer.', [2])]
def test_union_of_unions(): schema = (Integer() | String()) | Boolean() assert schema.validate('abc') == [] assert schema.validate(123) == [] assert schema.validate(True) == [] assert schema.validate({}) == [('Must match one of the options.', [])] schema = Integer() | (String() | Boolean()) assert schema.validate('abc') == [] assert schema.validate(123) == [] assert schema.validate(True) == [] assert schema.validate({}) == [('Must match one of the options.', [])]
def test_intersection_of_intersections(): # validate will never succeed in this case. schema = (Integer() & String()) & Boolean() assert schema.validate('abc') == [('Must be an integer.', []), ('Must be a boolean.', [])] assert schema.validate(123) == [('Must be a string.', []), ('Must be a boolean.', [])]
def test_pattern(): schema = String(pattern='^a[0-9]$') assert schema.validate('a3') == [] assert schema.validate('ab') == [('Must match the pattern /^a[0-9]$/.', [])] schema = String(pattern='a[0-9]') assert schema.validate('z a3 z') == [] assert schema.validate('z ab z ') == [('Must match the pattern /a[0-9]/.', [])]
def doc(): return Document(url='http://example.org/', title='Example', content={ 'integer': 123, 'dict': { 'key': 'value' }, 'list': [1, 2, 3], 'link': Link(url='http://example.org/', fields=[ Field(name='noschema'), Field(name='string_example', schema=String()), Field(name='enum_example', schema=Enum(['a', 'b', 'c'])), ]), 'nested': { 'child': Link(url='http://example.org/123') }, '_type': 'needs escaping' })
def test_format_uri(): schema = String(format='uri') assert schema.validate('http://example.com') == [] assert schema.validate('example.com') == [('Must be a valid uri.', [])]
def test_xor_of_types(): schema = Integer() ^ String() assert schema.validate('abc') == [] assert schema.validate(123) == [] assert schema.validate(True) == [('Must match one of the options.', [])]
def test_array_items_as_list(): schema = Array(items=[String(), Integer()]) assert schema.validate([]) == [] assert schema.validate(['a', 123]) == [] assert schema.validate(['a', 'b']) == [('Must be an integer.', [1])]
from coreschema import Object, Array, String, Anything, Boolean media_type_pattern = '^[^/]+/[^/]' # TODO Reference SecurityScheme = Anything() # TODO XMLObject = Anything() # TODO Header = Anything() # TODO Schema = Anything() # TODO Items = Anything() # TODO Definitions = Object(additional_properties=Schema) ExternalDocumentation = Object(properties={ 'description': String(), 'url': String(format='uri') }, pattern_properties={'^x-': Anything()}, required=['url'], additional_properties=False) Tag = Object(properties={ 'name': String(), 'description': String(), 'externalDocs': ExternalDocumentation }, pattern_properties={'^x-': Anything()}, required=['name'], additional_properties=False)
def test_object_additional_properties_as_schema(): schema = Object(properties={'a': String()}, additional_properties=String()) assert schema.validate({'a': ''}) == [] assert schema.validate({'b': 1}) == [('Must be a string.', ['b'])]
def test_object_additional_properties_as_boolean(): schema = Object(properties={'a': String()}, additional_properties=False) assert schema.validate({'a': ''}) == [] assert schema.validate({'b': ''}) == [('Invalid property.', ['b'])]
def test_object_pattern_properties(): schema = Object(pattern_properties={'^x-': String(max_length=3)}) assert schema.validate({'foox-a': 1}) == [] assert schema.validate({'x-foo': 1}) == [('Must be a string.', ['x-foo'])]
def test_object_properties(): schema = Object(properties={'name': String()}) assert schema.validate({}) == [] assert schema.validate({'name': ''}) == [] assert schema.validate({'name': 1}) == [('Must be a string.', ['name'])]
def test_array_items(): schema = Array(items=String()) assert schema.validate([]) == [] assert schema.validate(['a', 'b', 'c']) == [] assert schema.validate(['a', 'b', 123]) == [('Must be a string.', [2])]
def test_format_email(): schema = String(format='email') assert schema.validate('a@b') == [] assert schema.validate('ab@') == [('Must be a valid email.', [])]
def test_string_type(): schema = String() assert schema.validate('') == [] assert schema.validate('abc') == [] assert schema.validate(123) == [('Must be a string.', [])]
def test_blank(): schema = String(min_length=1) assert schema.validate('abc') == [] assert schema.validate('') == [('Must not be blank.', [])]
def test_min_length(): schema = String(min_length=3) assert schema.validate('abc') == [] assert schema.validate('ab') == [('Must have at least 3 characters.', [])]
def test_max_length(): schema = String(max_length=3) assert schema.validate('abc') == [] assert schema.validate('abcd') == [('Must have no more than 3 characters.', [])]