def validate_object(obj, field_validators=None, non_field_validators=None, schema=None, context=None): """ Takes a mapping and applies a mapping of validator functions to it collecting and reraising any validation errors that occur. """ if schema is None: schema = {} if context is None: context = {} if field_validators is None: field_validators = ValidationDict() if non_field_validators is None: non_field_validators = ValidationList() from flex.validation.schema import ( construct_schema_validators, ) schema_validators = construct_schema_validators(schema, context) if '$ref' in schema_validators and hasattr(schema_validators['$ref'], 'validators'): ref_ = field_validators.pop('$ref') for k, v in ref_.validators.items(): if k not in schema_validators: schema_validators.add_validator(k, v) if 'discriminator' in schema: add_polymorphism_requirements(obj, schema, context, schema_validators) schema_validators.update(field_validators) schema_validators.validate_object(obj, context=context) non_field_validators.validate_object(obj, context=context) return obj
def validate_object(obj, field_validators=None, non_field_validators=None, schema=None, context=None): """ Takes a mapping and applies a mapping of validator functions to it collecting and reraising any validation errors that occur. """ if schema is None: schema = {} if context is None: context = {} if field_validators is None: field_validators = ValidationDict() if non_field_validators is None: non_field_validators = ValidationList() from flex.validation.schema import ( construct_schema_validators, ) schema_validators = construct_schema_validators(schema, context) if '$ref' in schema_validators and hasattr(schema_validators['$ref'], 'validators'): ref_ = field_validators.pop('$ref') for k, v in ref_.validators.items(): if k not in schema_validators: schema_validators.add_validator(k, v) schema_validators.update(field_validators) schema_validators.validate_object(obj, context=context) non_field_validators.validate_object(obj, context=context) return obj
'properties', functools.partial(apply_validator_to_object, validator=schema_validator), ) properties_validator = generate_object_validator( schema=common_properties_schema, non_field_validators=properties_non_field_validators, ) # Now put the properties validator onto the schema validator. schema_field_validators.add_property_validator('properties', properties_validator) # # Items # items_non_field_validators = ValidationList() items_non_field_validators.add_validator( skip_if_empty(skip_if_not_of_type(OBJECT)(schema_validator)) ) items_non_field_validators.add_validator( skip_if_empty(skip_if_not_of_type(ARRAY)( functools.partial(apply_validator_to_array, validator=schema_validator), )), ) items_validator = generate_object_validator( schema=common_items_schema, non_field_validators=items_non_field_validators, ) # Now put the items validator onto the schema validator
responses_schema = { 'type': OBJECT, } field_validators = ValidationDict() field_validators.add_property_validator( 'default', generate_any_validator( referenceObject=reference_object_validator, responseObject=single_response_validator, ), ) non_field_validators = ValidationList() non_field_validators.add_validator( functools.partial( apply_validator_to_object, validator=generate_any_validator( referenceObject=reference_object_validator, responseObject=single_response_validator, ), )) responses_validator = generate_object_validator( schema=responses_schema, field_validators=field_validators, non_field_validators=non_field_validators, )
if not all(allowed.match(x) for x in hostname.split(".")): errors.add_error( 'invalid', MESSAGES['host']['invalid'].format(value), ) if path: errors.add_error( 'path', MESSAGES['host']['may_not_include_path'].format(value), ) if scheme: errors.add_error( 'scheme', MESSAGES['host']['may_not_include_scheme'].format(value), ) host_schema = { 'type': STRING, } non_field_validators = ValidationList() non_field_validators.add_validator(host_validator) host_validator = generate_object_validator( schema=host_schema, non_field_validators=non_field_validators, )
import functools from flex.datastructures import ( ValidationList, ) from flex.constants import ( OBJECT, ) from flex.validation.common import ( generate_object_validator, apply_validator_to_object, ) from .single import ( single_parameter_validator, ) parameters_schema = { 'type': OBJECT, } parameters_non_field_validators = ValidationList() parameters_non_field_validators.add_validator( functools.partial(apply_validator_to_object, validator=single_parameter_validator), ) parameters_validator = generate_object_validator( schema=parameters_schema, non_field_validators=parameters_non_field_validators, )
generate_object_validator, ) ALLOWED_TYPES = [STRING, NUMBER, INTEGER, BOOLEAN, ARRAY] type_schema = { 'type': [STRING, ARRAY], 'required': True, 'items': { 'type': STRING, 'enum': ALLOWED_TYPES, }, } @skip_if_not_of_type(STRING) @skip_if_empty def validate_type(_type, **kwargs): if _type not in ALLOWED_TYPES: raise ValidationError(MESSAGES['enum']['invalid'].format(_type, ALLOWED_TYPES)) non_field_validators = ValidationList() non_field_validators.add_validator(validate_type) type_validator = generate_object_validator( schema=type_schema, non_field_validators=non_field_validators, )
from flex.validation.common import ( generate_object_validator, ) from flex.decorators import ( skip_if_not_of_type, skip_if_empty, ) @skip_if_empty @skip_if_not_of_type(STRING) def validate_reference(reference, context, **kwargs): try: parts = urlparse.urlparse(reference) jsonpointer.resolve_pointer(context, parts.fragment) except jsonpointer.JsonPointerException: raise ValidationError(MESSAGES['reference']['undefined'].format(reference)) ref_schema = { 'type': STRING, } non_field_validators = ValidationList() non_field_validators.add_validator(validate_reference) ref_validator = generate_object_validator( schema=ref_schema, non_field_validators=non_field_validators, )
import functools from flex.datastructures import ValidationList from flex.constants import OBJECT from flex.validation.common import generate_object_validator, apply_validator_to_object from .schema import schema_validator schema_definitions_schema = {"type": OBJECT} non_field_validators = ValidationList() non_field_validators.add_validator(functools.partial(apply_validator_to_object, validator=schema_validator)) schema_definitions_validator = generate_object_validator( schema=schema_definitions_schema, non_field_validators=non_field_validators )
import functools from flex.datastructures import ( ValidationList, ) from flex.constants import ( OBJECT, ) from flex.validation.common import ( generate_object_validator, apply_validator_to_object, ) from .single import ( single_response_validator, ) responses_schema = { 'type': OBJECT, } non_field_validators = ValidationList() non_field_validators.add_validator( functools.partial(apply_validator_to_object, validator=single_response_validator), ) responses_validator = generate_object_validator( schema=responses_schema, non_field_validators=non_field_validators, )
from flex.validation.utils import ( generate_any_validator, ) from flex.loading.common.reference import ( reference_object_validator, ) from .single import ( single_parameter_validator, ) parameters_schema = { 'type': ARRAY, } parameters_non_field_validators = ValidationList() parameters_non_field_validators.add_validator( functools.partial( apply_validator_to_object, validator=generate_any_validator( referenceObject=reference_object_validator, parameterObject=single_parameter_validator, ), ), ) parameters_validator = generate_object_validator( schema=parameters_schema, non_field_validators=parameters_non_field_validators, )
from flex.decorators import ( skip_if_empty, skip_if_not_of_type, ) string_type_validator = generate_type_validator(STRING) @skip_if_empty @skip_if_not_of_type(STRING) def base_path_validator(value, **kwargs): if not value.startswith('/'): raise ValidationError(MESSAGES['path']['must_start_with_slash']) parts = urlparse.urlparse(value) if value != parts.path: raise ValidationError(MESSAGES['path']['invalid']) base_path_schema = { 'type': STRING, } non_field_validators = ValidationList() non_field_validators.add_validator(base_path_validator) base_path_validator = generate_object_validator( schema=base_path_schema, non_field_validators=non_field_validators, )
) from flex.decorators import ( skip_if_empty, skip_if_not_of_type, ) string_type_validator = generate_type_validator(STRING) @skip_if_empty @skip_if_not_of_type(STRING) def base_path_validator(value, **kwargs): if not value.startswith('/'): raise ValidationError(MESSAGES['path']['must_start_with_slash']) parts = urlparse.urlparse(value) if value != parts.path: raise ValidationError(MESSAGES['path']['invalid']) base_path_schema = { 'type': STRING, } non_field_validators = ValidationList() non_field_validators.add_validator(base_path_validator) base_path_validator = generate_object_validator( schema=base_path_schema, non_field_validators=non_field_validators, )
from flex.validation.common import ( generate_object_validator, ) from flex.decorators import ( skip_if_empty, skip_if_not_of_type, ) @skip_if_empty @skip_if_not_of_type(ARRAY) def validate_schemes(schemes, **kwargs): for value in schemes: if value not in SCHEMES: raise ValidationError( MESSAGES['schemes']['invalid'].format(value), ) scheme_schema = { 'type': ARRAY, } non_field_validators = ValidationList() non_field_validators.add_validator(validate_schemes) schemes_validator = generate_object_validator( schema=scheme_schema, non_field_validators=non_field_validators, )
from .path_item import ( path_item_validator, ) @skip_if_empty @skip_if_not_of_type(OBJECT) def validate_path_items(paths, **kwargs): with ErrorDict() as errors: for path, path_definition in paths.items(): # TODO: move this to its own validation function that validates the keys. if not path.startswith('/'): errors.add_error(path, MESSAGES['path']['must_start_with_slash']) try: path_item_validator(path_definition, **kwargs) except ValidationError as err: errors.add_error(path, err.detail) paths_schema = { 'type': OBJECT, } non_field_validators = ValidationList() non_field_validators.add_validator(validate_path_items) paths_validator = generate_object_validator( schema=paths_schema, non_field_validators=non_field_validators, )
generate_object_validator, ) from flex.decorators import ( skip_if_not_of_type, skip_if_empty, ) @skip_if_empty @skip_if_not_of_type(STRING) def validate_reference(reference, context, **kwargs): try: definitions = context['definitions'] except KeyError: raise KeyError(MESSAGES['unknown_reference']['no_definitions']) if reference not in definitions: raise ValidationError( MESSAGES['unknown_reference']['definition'].format(reference)) ref_schema = { 'type': STRING, } non_field_validators = ValidationList() non_field_validators.add_validator(validate_reference) ref_validator = generate_object_validator( schema=ref_schema, non_field_validators=non_field_validators, )
from flex.datastructures import ValidationList from flex.constants import ( OBJECT, ) from flex.validation.common import ( generate_object_validator, apply_validator_to_object, ) from .single import ( single_header_validator, ) headers_schema = { 'type': OBJECT, } non_field_validators = ValidationList() non_field_validators.add_validator( functools.partial(apply_validator_to_object, validator=single_header_validator), ) headers_validator = generate_object_validator( schema=headers_schema, non_field_validators=non_field_validators, )
from flex.validation.common import ( generate_object_validator, ) ALLOWED_TYPES = [STRING, NUMBER, INTEGER, BOOLEAN, ARRAY] type_schema = { 'type': [STRING, ARRAY], 'items': { 'type': STRING, 'enum': ALLOWED_TYPES, }, } @skip_if_not_of_type(STRING) @skip_if_empty def validate_type(_type, **kwargs): if _type not in ALLOWED_TYPES: raise ValidationError(MESSAGES['enum']['invalid'].format(_type, ALLOWED_TYPES)) non_field_validators = ValidationList() non_field_validators.add_validator(validate_type) type_validator = generate_object_validator( schema=type_schema, non_field_validators=non_field_validators, )
ARRAY, ) from flex.validation.common import ( generate_object_validator, apply_validator_to_object, ) from flex.validation.utils import ( generate_any_validator, ) from flex.loading.common.reference import ( reference_object_validator, ) from .single import ( single_parameter_validator, ) parameters_schema = { 'type': ARRAY, } parameters_non_field_validators = ValidationList() parameters_non_field_validators.add_validator( functools.partial( apply_validator_to_object, validator=generate_any_validator( referenceObject=reference_object_validator, parameterObject=single_parameter_validator, ), ), ) parameters_validator = generate_object_validator( schema=parameters_schema, non_field_validators=parameters_non_field_validators, )
import functools from flex.datastructures import ( ValidationList, ) from flex.constants import OBJECT from flex.validation.common import ( generate_object_validator, apply_validator_to_object, ) from .schema import ( schema_validator, ) schema_definitions_schema = { 'type': OBJECT, } non_field_validators = ValidationList() non_field_validators.add_validator( functools.partial(apply_validator_to_object, validator=schema_validator), ) schema_definitions_validator = generate_object_validator( schema=schema_definitions_schema, non_field_validators=non_field_validators, )
'type': OBJECT, } field_validators = ValidationDict() field_validators.add_property_validator( 'default', generate_any_validator( referenceObject=reference_object_validator, responseObject=single_response_validator, ), ) non_field_validators = ValidationList() non_field_validators.add_validator( functools.partial( apply_validator_to_object, validator=generate_any_validator( referenceObject=reference_object_validator, responseObject=single_response_validator, ), ) ) responses_validator = generate_object_validator( schema=responses_schema, field_validators=field_validators, non_field_validators=non_field_validators,
skip_if_not_of_type, ) from flex.validation.common import ( generate_object_validator, ) from .schema import ( schema_validator, ) @skip_if_empty @skip_if_not_of_type(OBJECT) def validate_schema_definitions(definitions, **kwargs): with ErrorDict() as errors: for name, schema in definitions.items(): try: schema_validator(schema, **kwargs) except ValidationError as err: errors.add_error(name, err.detail) schema_definitions_schema = { 'type': OBJECT, } non_field_validators = ValidationList() non_field_validators.add_validator(validate_schema_definitions) schema_definitions_validator = generate_object_validator( schema=schema_definitions_schema, non_field_validators=non_field_validators, )
from .path_item import ( path_item_validator, ) @skip_if_empty @skip_if_not_of_type(OBJECT) def validate_path_items(paths, **kwargs): with ErrorCollection() as errors: for path, path_definition in paths.items(): # TODO: move this to its own validation function that validates the keys. if not path.startswith('/'): errors.add_error(path, MESSAGES['path']['must_start_with_slash']) try: path_item_validator(path_definition, **kwargs) except ValidationError as err: errors.add_error(path, err.detail) paths_schema = { 'type': OBJECT, } non_field_validators = ValidationList() non_field_validators.add_validator(validate_path_items) paths_validator = generate_object_validator( schema=paths_schema, non_field_validators=non_field_validators, )
functools.partial(apply_validator_to_object, validator=schema_validator), ) properties_validator = generate_object_validator( schema=common_properties_schema, non_field_validators=properties_non_field_validators, ) # Now put the properties validator onto the schema validator. schema_field_validators.add_property_validator('properties', properties_validator) # # Items # items_non_field_validators = ValidationList() items_non_field_validators.add_validator( skip_if_empty(skip_if_not_of_type(OBJECT)(schema_validator))) items_non_field_validators.add_validator( skip_if_empty( skip_if_not_of_type(ARRAY)(functools.partial( apply_validator_to_array, validator=schema_validator), )), ) items_validator = generate_object_validator( schema=common_items_schema, non_field_validators=items_non_field_validators, ) # Now put the items validator onto the schema validator schema_field_validators.add_property_validator('items', items_validator)
from flex.validation.common import ( generate_object_validator, ) from flex.decorators import ( skip_if_not_of_type, skip_if_empty, ) @skip_if_empty @skip_if_not_of_type(STRING) def defer_reference(reference, context, **kwargs): try: deferred_references = context['deferred_references'] except KeyError: raise KeyError("`deferred_references` not found in validation context") deferred_references.add(reference) ref_schema = { 'type': STRING, } non_field_validators = ValidationList() non_field_validators.add_validator(defer_reference) ref_validator = generate_object_validator( schema=ref_schema, non_field_validators=non_field_validators, )