def type_validator(swagger_spec, validator, types, instance, schema): """Skip the `type` validator when a Swagger parameter value is None. Otherwise it will fail with a "None is not a valid type" failure instead of letting the downstream `required_validator` do its job. Also skip when a Swagger property value is None and the schema contains the extension field `x-nullable` set to True. In all other cases, delegate to the existing Draft4 `type` validator. :param swagger_spec: needed for access to deref() :type swagger_spec: :class:`bravado_core.spec.Spec` :param validator: Validator class used to validate the object :type validator: :class:`Swagger20Validator` or :class:`jsonschema.validators.Draft4Validator` :param types: validate types :type types: string or list :param instance: object instance value :param schema: swagger spec for the object :type schema: dict """ if (is_param_spec(swagger_spec, schema) or is_prop_nullable(swagger_spec, schema)) and instance is None: return for error in _validators.type_draft4(validator, types, instance, schema): yield error
def test_true(minimal_swagger_spec): param_spec = { 'in': 'path', 'name': 'petId', 'type': 'integer', } assert is_param_spec(minimal_swagger_spec, param_spec)
def enum_validator(swagger_spec, validator, enums, instance, schema): """Swagger 2.0 allows enums to be validated against objects of type arrays, like query parameter (collectionFormat: multi) :param swagger_spec: needed for access to deref() :type swagger_spec: :class:`bravado_core.spec.Spec` :param validator: Validator class used to validate the object :type validator: :class: `Swagger20Validator` or :class: `jsonschema.validators.Draft4Validator` :param enums: allowed enum values :type enums: list :param instance: enum instance value :param schema: swagger spec for the object :type schema: dict """ if schema.get('type') == 'array': for element in instance: for error in _validators.enum(validator, enums, element, schema): yield error return # Handle optional enum params with no value if is_param_spec(swagger_spec, schema): if not is_required(swagger_spec, schema) and instance is None: return for error in _validators.enum(validator, enums, instance, schema): yield error
def required_validator( swagger_spec, # type: Spec validator, # type: Draft4Validator required, # type: typing.Any instance, # type: typing.Any schema, # type: JSONDict ): # type: (...) -> typing.Generator[ValidationError, None, None] """Swagger 2.0 expects `required` to be a bool in the Parameter object, but a list of properties everywhere else. :param swagger_spec: needed for access to deref() :type swagger_spec: :class:`bravado_core.spec.Spec` :param validator: Validator class used to validate the object :type validator: :class:`Swagger20Validator` or :class:`jsonschema.validators.Draft4Validator` :param required: value of `required` field :type required: boolean or list or None :param instance: object instance value :param schema: swagger spec for the object :type schema: dict """ if is_param_spec(swagger_spec, schema): if required and instance is None: yield ValidationError('{0} is a required parameter.'.format(schema['name'])) else: for error in _DRAFT4_REQUIRED_VALIDATOR(validator, required, instance, schema): yield error
def test_ref_false(minimal_swagger_dict): minimal_swagger_dict['responses'] = { 'InvalidInput': { 'description': 'Invalid input', }, } response_spec = {'$ref': '#/responses/InvalidInput'} swagger_spec = Spec.from_dict(minimal_swagger_dict) assert not is_param_spec(swagger_spec, response_spec)
def test_ref_false(minimal_swagger_dict): minimal_swagger_dict['responses'] = { 'InvalidInput': { 'description': 'Invalid input' } } response_spec = {'$ref': '#/responses/InvalidInput'} swagger_spec = Spec.from_dict(minimal_swagger_dict) assert not is_param_spec(swagger_spec, response_spec)
def test_ref_true(minimal_swagger_dict): minimal_swagger_dict['parameters'] = { 'PetId': { 'in': 'path', 'name': 'petId', 'type': 'integer', } } param_spec = {'$ref': '#/parameters/PetId'} swagger_spec = Spec.from_dict(minimal_swagger_dict) assert is_param_spec(swagger_spec, param_spec)
def format_validator(swagger_spec, validator, format, instance, schema): """Skip the `format` validator when a Swagger parameter value is None. Otherwise it will fail with a "Failed validating u'format' in schema" failure instead of letting the downstream `required_validator` do its job. Also skip when a Swagger property value is None and the schema contains the extension field `x-nullable` set to True. In all other cases, delegate to the existing Draft4 `format` validator. """ if (is_param_spec(swagger_spec, schema) or is_prop_nullable(swagger_spec, schema)) and instance is None: return for error in _DRAFT4_FORMAT_VALIDATOR(validator, format, instance, schema): yield error
def format_validator(swagger_spec, validator, format, instance, schema): """Skip the `format` validator when a Swagger parameter value is None. Otherwise it will fail with a "Failed validating u'format' in schema" failure instead of letting the downstream `required_validator` do its job. Also skip when a Swagger property value is None and the schema contains the extension field `x-nullable` set to True. In all other cases, delegate to the existing Draft4 `format` validator. """ if (is_param_spec(swagger_spec, schema) or is_prop_nullable(swagger_spec, schema)) and instance is None: return for error in _validators.format(validator, format, instance, schema): yield error
def required_validator(validator, required, instance, schema): """Swagger 2.0 expects `required` to be a bool in the Parameter object, but a list of properties everywhere else. :param validator: Validator class used to validate the object :type validator: :class:`Swagger20Validator` or :class:`jsonschema.validators.Draft4Validator` :param required: value of `required` field :type required: boolean or list or None :param instance: object instance value :param schema: swagger spec for the object :type schema: dict """ if is_param_spec(schema): if required and instance is None: return [ValidationError("%s is required" % schema['name'])] else: return _validators.required_draft4(validator, required, instance, schema)
def type_validator(validator, types, instance, schema): """Skip the `type` validator when a Swagger parameter value is None. Otherwise it will fail with a "None is not a valid type" failure instead of letting the downstream `required_validator` do its job. In all other cases, delegate to the existing Draft4 `type` validator. :param validator: Validator class used to validate the object :type validator: :class:`Swagger20Validator` or :class:`jsonschema.validators.Draft4Validator` :param types: validate types :type types: string or list :param instance: object instance value :param schema: swagger spec for the object :type schema: dict """ if is_param_spec(schema) and instance is None: return return _validators.type_draft4(validator, types, instance, schema)
def required_validator(validator, required, instance, schema): """Swagger 2.0 expects `required` to be a bool in the Parameter object, but a list of properties everywhere else. :param validator: Validator class used to validate the object :type validator: :class:`Swagger20Validator` or :class:`jsonschema.validators.Draft4Validator` :param required: value of `required` field :type required: boolean or list or None :param instance: object instance value :param schema: swagger spec for the object :type schema: dict """ if is_param_spec(schema): if required and instance is None: return [ValidationError("%s is required" % schema['name'])] else: return _validators.required_draft4( validator, required, instance, schema)
def enum_validator( swagger_spec, # type: Spec validator, # type: Draft4Validator enums, # type: typing.Any instance, # type: typing.Any schema, # type: JSONDict ): # type: (...) -> typing.Generator[ValidationError, None, None] """Swagger 2.0 allows enums to be validated against objects of type arrays, like query parameter (collectionFormat: multi) :param swagger_spec: needed for access to deref() :type swagger_spec: :class:`bravado_core.spec.Spec` :param validator: Validator class used to validate the object :type validator: :class: `Swagger20Validator` or :class: `jsonschema.validators.Draft4Validator` :param enums: allowed enum values :type enums: list :param instance: enum instance value :param schema: swagger spec for the object :type schema: dict """ if instance is None and is_prop_nullable(swagger_spec, schema): return if schema.get('type') == 'array': for element in instance: for error in _DRAFT4_ENUM_VALIDATOR(validator, enums, element, schema): yield error return # Handle optional enum params with no value if is_param_spec(swagger_spec, schema): if instance is None and not is_required(swagger_spec, schema): return for error in _DRAFT4_ENUM_VALIDATOR(validator, enums, instance, schema): yield error
def format_validator( swagger_spec, # type: Spec validator, # type: Draft4Validator format, # type: typing.Any instance, # type: typing.Any schema, # type: JSONDict ): # type: (...) -> typing.Generator[ValidationError, None, None] """Skip the `format` validator when a Swagger parameter value is None. Otherwise it will fail with a "Failed validating u'format' in schema" failure instead of letting the downstream `required_validator` do its job. Also skip when a Swagger property value is None and the schema contains the extension field `x-nullable` set to True. In all other cases, delegate to the existing Draft4 `format` validator. """ if instance is None and (is_param_spec(swagger_spec, schema) or is_prop_nullable(swagger_spec, schema)): return for error in _DRAFT4_FORMAT_VALIDATOR(validator, format, instance, schema): yield error
def required_validator(swagger_spec, validator, required, instance, schema): """Swagger 2.0 expects `required` to be a bool in the Parameter object, but a list of properties everywhere else. :param swagger_spec: needed for access to deref() :type swagger_spec: :class:`bravado_core.spec.Spec` :param validator: Validator class used to validate the object :type validator: :class:`Swagger20Validator` or :class:`jsonschema.validators.Draft4Validator` :param required: value of `required` field :type required: boolean or list or None :param instance: object instance value :param schema: swagger spec for the object :type schema: dict """ if is_param_spec(swagger_spec, schema): if required and instance is None: yield ValidationError('{0} is a required parameter.'.format( schema['name'])) else: for error in _validators.required_draft4(validator, required, instance, schema): yield error
def test_false(minimal_swagger_spec): response_spec = {'description': 'Invalid input'} assert not is_param_spec(minimal_swagger_spec, response_spec)