Merge pull request #85 from p1c2u/feature/collection-validation

Collection validation
This commit is contained in:
A 2018-08-22 12:58:18 +01:00 committed by GitHub
commit 101a11ebe9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 2 deletions

View file

@ -243,6 +243,7 @@ class Schema(object):
def get_validator_mapping(self):
mapping = {
SchemaType.ARRAY: self._validate_collection,
SchemaType.OBJECT: self._validate_object,
}
@ -270,6 +271,12 @@ class Schema(object):
return value
def _validate_collection(self, value):
if self.items is None:
raise OpenAPISchemaError("Schema for collection not defined")
return list(map(self.items.validate, value))
def _validate_object(self, value):
properties = value.__dict__

View file

@ -5,7 +5,7 @@ import pytest
from openapi_core.extensions.models.models import Model
from openapi_core.schema.schemas.exceptions import (
InvalidSchemaValue, MultipleOneOfSchema, NoOneOfSchema,
InvalidSchemaValue, MultipleOneOfSchema, NoOneOfSchema, OpenAPISchemaError,
)
from openapi_core.schema.schemas.models import Schema
@ -199,9 +199,16 @@ class TestSchemaValidate(object):
schema.validate(value)
@pytest.mark.parametrize('value', [[1, 2], (3, 4)])
def test_array(self, value):
def test_array_no_schema(self, value):
schema = Schema('array')
with pytest.raises(OpenAPISchemaError):
schema.validate(value)
@pytest.mark.parametrize('value', [[1, 2], (3, 4)])
def test_array(self, value):
schema = Schema('array', items=Schema('integer'))
result = schema.validate(value)
assert result == value