예제 #1
0
def validate_das_path(validator, value, instance, schema):
    search = DASSearch().exact_search(instance)

    if search.count() == 0:
        yield ValidationError("{} not found in DAS.".format(instance))
예제 #2
0
def maxProperties(validator, mP, instance, schema):
    if not validator.is_type(instance, "object"):
        return
    if validator.is_type(instance, "object") and len(instance) > mP:
        yield ValidationError("%r has too many properties" % (instance, ))
def _config_get_validators_fail_bad_params(temp_dir, key_):
    # calling str() on ValidationErrors returns more detailed into about the error
    _config_get_validators_fail('', temp_dir,
                                ValidationError("'' is not of type 'object'"))
    _config_get_validators_fail(
        ['foo', 'bar'], temp_dir,
        ValidationError("['foo', 'bar'] is not of type 'object'"))
    _config_get_validators_fail(
        {'key': 'y'}, temp_dir,
        ValidationError(
            "Additional properties are not allowed ('key' was unexpected)"))
    _config_get_validators_fail({key_: 'y'}, temp_dir,
                                ValidationError("'y' is not of type 'object'"))
    _config_get_validators_fail({key_: {
        'y': ['foo']
    }}, temp_dir, ValidationError("['foo'] is not of type 'object'"))
    _config_get_validators_fail({key_: {
        'y': {
            'key_metadata': {
                'a': 'b'
            }
        }
    }}, temp_dir, ValidationError("'validators' is a required property"))
    _config_get_validators_fail({key_: {
        'y': {
            'randomkey': {
                'a': 'b'
            }
        }
    }}, temp_dir, ValidationError("'validators' is a required property"))
    _config_get_validators_fail({key_: {
        'key': {
            'validators': {}
        }
    }}, temp_dir, ValidationError("{} is not of type 'array'"))
    _config_get_validators_fail({key_: {
        'key': {
            'validators': ['foo']
        }
    }}, temp_dir, ValidationError("'foo' is not of type 'object'"))
    _config_get_validators_fail(
        {
            key_: {
                'key': {
                    'validators': [{
                        'module': 'foo',
                        'callable_builder': 'bar'
                    }],
                    'key_metadata': []
                }
            }
        }, temp_dir, ValidationError("[] is not of type 'object'"))
    _config_get_validators_fail(
        {
            key_: {
                'key': {
                    'validators': [{
                        'module': 'foo',
                        'callable_builder': 'bar'
                    }],
                    'key_metadata': {
                        'a': {}
                    }
                }
            }
        }, temp_dir,
        ValidationError(
            "{} is not of type 'number', 'boolean', 'string', 'null'"))
    _config_get_validators_fail({key_: {
        'key': {
            'validators': [{}]
        }
    }}, temp_dir, ValidationError("'module' is a required property"))
    _config_get_validators_fail(
        {key_: {
            'key': {
                'validators': [{
                    'module': 'foo'
                }]
            }
        }}, temp_dir,
        ValidationError("'callable_builder' is a required property"))
    _config_get_validators_fail(
        {
            key_: {
                'key': {
                    'validators': [{
                        'module': 'foo',
                        'callable-builder': 'bar'
                    }]
                }
            }
        }, temp_dir,
        ValidationError(
            "Additional properties are not allowed ('callable-builder' was unexpected)"
        ))
    _config_get_validators_fail(
        {
            key_: {
                'key': {
                    'validators': [{
                        'module': 'foo',
                        'callable_builder': 'bar',
                        'prefix': 1
                    }]
                }
            }
        }, temp_dir,
        ValidationError(
            "Additional properties are not allowed ('prefix' was unexpected)"))
    _config_get_validators_fail(
        {
            key_: {
                'key': {
                    'validators': [{
                        'module': ['foo'],
                        'callable_builder': 'bar'
                    }]
                }
            }
        }, temp_dir, ValidationError("['foo'] is not of type 'string'"))
    _config_get_validators_fail(
        {
            key_: {
                'key': {
                    'validators': [{
                        'module': 'foo',
                        'callable_builder': ['bar']
                    }]
                }
            }
        }, temp_dir, ValidationError("['bar'] is not of type 'string'"))
    _config_get_validators_fail(
        {
            key_: {
                'key': {
                    'validators': [{
                        'module': 'foo',
                        'callable_builder': 'bar',
                        'parameters': 'foo'
                    }]
                }
            }
        }, temp_dir, ValidationError("'foo' is not of type 'object'"))
예제 #4
0
def disallow_draft3(validator, disallow, instance, schema):
    for disallowed in _utils.ensure_list(disallow):
        if validator.is_valid(instance, {"type": [disallowed]}):
            yield ValidationError("%r is disallowed for %r" %
                                  (disallowed, instance))
예제 #5
0
def required(validator, required, instance, schema):
    if not validator.is_type(instance, "object"):
        return
    for property in required:
        if property not in instance:
            yield ValidationError("%r is a required property" % property)
예제 #6
0
def format(validator, format, instance, schema):
    if validator.format_checker is not None:
        try:
            validator.format_checker.check(instance, format)
        except FormatError as error:
            yield ValidationError(error.message, cause=error.cause)
예제 #7
0
def maxLength(validator, mL, instance, schema):
    if validator.is_type(instance, "string") and len(instance) > mL:
        yield ValidationError("%r is too long" % (instance, ))
def discriminator_validator(swagger_spec, validator, discriminator_attribute,
                            instance, schema):
    """
    Validates instance against the schema defined by the discriminator attribute.

    [Swagger 2.0 Schema Object](http://swagger.io/specification/#schemaObject) allows discriminator field to be defined.
    discriminator field defines the attribute that will be used to discriminate the object type.

    NOTE: discriminator_validator assumes that discriminator_attribute is not None or empty

    :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 discriminator_attribute: name of the discriminator attribute
    :type discriminator_attribute: str
    :param instance: object instance value
    :type instance: dict
    :param schema: swagger spec for the object
    :type schema: dict
    """

    discriminator_value = instance[discriminator_attribute]
    if discriminator_value not in swagger_spec.definitions:
        raise ValidationError(message='\'{}\' is not a recognized schema'.
                              format(discriminator_value))

    if discriminator_value == schema[MODEL_MARKER]:
        return

    discriminated_schema = swagger_spec.definitions[
        discriminator_value]._model_spec
    if 'allOf' not in discriminated_schema:
        raise ValidationError(
            message='discriminated schema \'{}\' must inherit from \'{}\''.
            format(discriminator_value, schema[MODEL_MARKER]))

    schemas_to_remove = [
        s for s in discriminated_schema['allOf']
        if swagger_spec.deref(s) == schema
    ]
    if not schemas_to_remove:
        # Not checking against len(schemas_to_remove) > 1 because it should be prevented by swagger spec validation
        raise ValidationError(
            message='discriminated schema \'{}\' must inherit from \'{}\''.
            format(discriminator_value, schema[MODEL_MARKER]))

    # Remove the current schema from the allOf list in order to avoid unbounded recursion
    # (the current object is already validated against schema)
    # WARNING: This is especially important if internally_dereference_refs is set to true
    #   as we're modifying new_schema and new_schema is a dict (so mutable) we need to copy
    #   it in order to have a brand new dictionary that we can modify
    new_schema = discriminated_schema.copy()
    new_schema['allOf'] = [
        all_of_schema if all_of_schema not in schemas_to_remove else {}
        for all_of_schema in new_schema['allOf']
    ]

    from bravado_core.validate import validate_object  # Local import due to circular dependency
    validate_object(swagger_spec=swagger_spec,
                    object_spec=new_schema,
                    value=instance)
예제 #9
0
 def validate(self):
     super(SignUpSerializer, self).validate()
     if self._data['password'] != self._data['password2']:
         raise ValidationError('Passwords must be equal')
예제 #10
0
파일: main.py 프로젝트: stonezhong/pyschema
def is_datetime_string(validator, value, instance, schema):
    try:
        ts = dateutil.parser.parse(instance)
    except:
        raise ValidationError("{} is not an ISO 8601 format".format(instance))
    def test_update_add_no_extras(self, SwaggerClient):
        SwaggerClient.return_value.Character.get_characters_character_id.return_value.result.return_value = {
            'corporation_id': 2
        }
        SwaggerClient.return_value.Corporation.get_corporations_corporation_id_membertracking.return_value.result.return_value = [
            {
                'character_id': 2,
                'ship_type_id': None,
                'logon_date': now(),
                'logoff_date': now(),
                'start_date': now()
            }
        ]
        SwaggerClient.return_value.Universe.get_universe_types_type_id.return_value.result.side_effect = ValidationError(
            "Test Failure")
        SwaggerClient.return_value.Universe.post_universe_names.return_value.result.return_value = [
            {
                'id': 2,
                'name': 'test character none',
                'category': 'character'
            }
        ]

        self.corpstat.update()
        self.assertTrue(
            CorpMember.objects.filter(character_id=2,
                                      character_name='test character none',
                                      corpstats=self.corpstat).exists())
def unique_publisher_validator(validator, value, instance, schema):
    """ Checks the object name being validated does not already exist """
    existing = mongo.db.publishers.find_one({'name': instance})
    if existing:
        return [ValidationError("'name' is already in use", instance=instance, validator=validator)]
    return None
예제 #13
0
def _mock_validate_fail(self):
    """Simulate a validation fail."""
    raise ValidationError("")
예제 #14
0
def validate_cadi_id(validator, value, instance, schema):
    from .utils.cadi import get_from_cadi_by_id
    if not get_from_cadi_by_id(instance, from_validator=True):
        yield ValidationError("{} not found in CADI.".format(instance))
예제 #15
0
def uniqueItems(validator, uI, instance, schema):
    if (uI and validator.is_type(instance, "array")
            and not _utils.uniq(instance)):
        yield ValidationError("%r has non-unique elements" % (instance, ))
    def diagnoseWhichOneOf(self, error_path, IIIFJsonPath):
        """
            Given a schema error that ends in oneOf the current json schema library 
            will check all possibilities in oneOf and return validation error messages for each one
            This method will identify the real oneOf that causes the error by checking the type of 
            each oneOf possibility and returning only the one that matches. 

            Arguments:
                error_path: list of strings and ints which are the path to the error in the schema
                            generated from validation_error.absolute_schema_path

                IIIF Json Path: list of strings and ints which gives the path to the failing part of the 
                                IIIF data. Generated from validation_error.absolute_path
        """

        # convert the IIIF path from a list to an actual JSON object containing only
        # the JSON which has failed.
        path = parse(self.pathToJsonPath(IIIFJsonPath))
        iiifJsonPart = path.find(self.iiif_asset)[0].value

        # Extract only the part of the schema that has failed and in practice will
        # be a list of the oneOf possibilities. Also add all references in the schema
        # so these resolve
        schema_part = self.addReferences(self.getSchemaPortion(error_path))
        valid_errors = []
        oneOfIndex = 0
        # For each of the oneOf possibilities in the current part of the schema
        for possibility in schema_part:
            try:
                # run through a validator passing the IIIF data snippet
                # and the json schema snippet
                validator = Draft7Validator(possibility)
                results = validator.iter_errors(iiifJsonPart)
            except SchemaError as err:
                print('Problem with the supplied schema:\n')
                print(err)
                raise

            # One of the oneOf possibilities is a reference to anouther part of the schema
            # this won't bother the validator but will complicate the diagnoise so replace
            # it with the actual schema json (and copy all references)
            if isinstance(possibility, dict) and "$ref" in possibility:
                tmpClas = possibility['classes']
                tmpType = possibility['types']
                possibility = self.resolver.resolve(possibility['$ref'])[1]
                possibility['classes'] = tmpClas
                possibility['types'] = tmpType

            # This oneOf possiblity failed validation
            if results:
                addErrors = True
                store_errs = []
                for err in results:
                    # For each of the reported errors check the types with the IIIF data to see if its relevant
                    # if one error in this oneOf possibility group is not relevant then none a relevant so discard
                    if not self.parse(list(err.absolute_schema_path),
                                      possibility, iiifJsonPart,
                                      list(err.absolute_path)):
                        addErrors = False
                    else:
                        # if this oneOf possiblity is still relevant add it to the list and check
                        # its not another oneOf error
                        if addErrors:
                            # if error is also a oneOf then diagnoise again
                            if err.absolute_schema_path[
                                    -1] == 'oneOf' and err.absolute_schema_path != error_path:
                                error_path.append(
                                    oneOfIndex
                                )  # this is is related to one of the original oneOfs at index oneOfIndex
                                error_path.extend(
                                    err.absolute_schema_path
                                )  # but we found another oneOf test at this location
                                store_errs.append(
                                    self.diagnoseWhichOneOf(
                                        error_path, IIIFJsonPath)
                                )  # so recursivly discovery real error
                        #print ('would add: {} by addErrors is {}'.format(err.message, addErrors))
                        store_errs.append(err)
                # if All errors are relevant to the current type add them to the list
                if addErrors:
                    valid_errors += store_errs
            oneOfIndex += 1

        if valid_errors:
            # this may hide errors as we are only selecting the first one but better to get one to work on and then can re-run
            # Also need to convert back the error paths to the full path
            error_path.reverse()
            valid_errors[0].absolute_schema_path.extendleft(error_path)
            IIIFJsonPath.reverse()
            valid_errors[0].absolute_path.extendleft(IIIFJsonPath)
            return valid_errors[0]
        else:
            # Failed to find the source of the error so most likely its a problem with the type
            # and it didn't match any of the possible oneOf types

            path = parse(self.pathToJsonPath(IIIFJsonPath))
            instance = path.find(self.iiif_asset)[0].value
            IIIFJsonPath.append('type')
            #print (IIIFJsonPath)
            return ValidationError(
                message=
                'Failed to find out which oneOf test matched your data. This is likely due to an issue with the type and it not being valid value at this level. SchemaPath: {}'
                .format(self.pathToJsonPath(error_path)),
                path=[],
                schema_path=error_path,
                schema=self.getSchemaPortion(error_path),
                instance=instance)
예제 #17
0
def pattern(validator, patrn, instance, schema):
    if (validator.is_type(instance, "string")
            and not re.search(patrn, instance)):
        yield ValidationError("%r does not match %r" % (instance, patrn))
예제 #18
0
 def wrapper(self, *args, **kwargs):
     for field, error in fields.items():
         if field not in self:
             raise ValidationError(error)
     return f(self, *args, **kwargs)
예제 #19
0
def minLength(validator, mL, instance, schema):
    if validator.is_type(instance, "string") and len(instance) < mL:
        yield ValidationError("%r is too short" % (instance, ))
예제 #20
0
def in_subnet(validator, value, instance, schema):
    if not ipaddress.ip_address(instance) in ipaddress.ip_network(value):
        yield ValidationError("{0} not in subnet {1}".format(instance, value))
예제 #21
0
def enum(validator, enums, instance, schema):
    if instance not in enums:
        yield ValidationError("%r is not one of %r" % (instance, enums))
예제 #22
0
def const(validator, const, instance, schema):
    if instance != const:
        yield ValidationError("%r was expected" % (const, ))
예제 #23
0
def type(validator, types, instance, schema):
    types = _utils.ensure_list(types)

    if not any(validator.is_type(instance, type) for type in types):
        yield ValidationError(_utils.types_msg(instance, types))
예제 #24
0
def contains(validator, contains, instance, schema):
    if not validator.is_type(instance, "array"):
        return

    if not any(validator.is_valid(element, contains) for element in instance):
        yield ValidationError("XXX")
예제 #25
0
def minProperties(validator, mP, instance, schema):
    if validator.is_type(instance, "object") and len(instance) < mP:
        yield ValidationError("%r does not have enough properties" %
                              (instance, ))
예제 #26
0
def minItems(validator, mI, instance, schema):
    if validator.is_type(instance, "array") and len(instance) < mI:
        yield ValidationError("%r is too short" % (instance, ))
예제 #27
0
def not_(validator, not_schema, instance, schema):
    if validator.is_valid(instance, not_schema):
        yield ValidationError("%r is not allowed for %r" %
                              (not_schema, instance))
예제 #28
0
def maxItems(validator, mI, instance, schema):
    if validator.is_type(instance, "array") and len(instance) > mI:
        yield ValidationError("%r is too long" % (instance, ))
예제 #29
0
def validator_form(validator, form, instance, schema, _from_items=False):
    """
    - Silk form validators
      - Every "form" property must be either absent or present
      - If present, the form must have exactly that value
      - Or, the "form" property must be a list, and have one of the values in the list
        Or, if numeric, it must have one of the values between any two adjacent ascending list items
      - The above applies a bit differently for shape, as it is already a list:
        - Or, the "form" property must be a list of lists. The property must have the same length.
          For each item in the lists-of-lists, the property must have one of the values, or
          be between any two adjacent ascending list items.
      - Validation on "strides" works through exact match of the strides value.
        Note that "strides" is only present if "contiguous" is absent (and vice versa)
    """
    def _allowed_value(schema_value, instance_value):
        if isinstance(schema_value, (list, tuple)):
            if instance_value in schema_value:
                return True
            for n in range(len(schema_value)-1):
                left, right = schema_value[n:n+2]
                if instance_value > left and instance_value < right:
                    return True
            return False
        else:
            return schema_value == instance_value

    if isinstance(instance, FormWrapper):
        instance_storage, instance_form = instance._storage, instance._form
    else:
        #TODO:BAD
        instance_storage, instance_form = get_form(instance)
    form_str = indent(pprint.pformat(instance_form, width=72))
    if instance_form is not None and "storage" in instance_form:
        storage_form = instance_form["storage"]
        for error in _validator_storage(storage_form, instance_storage, form_str):
            yield error
        if instance_storage is None:
            instance_storage = storage_form.get("form")
    if instance_storage is None:
        return

    if _from_items:
        form_str += "\n(on items)"
    binary_form_props = ("unsigned", "shape", "bytesize", "strides", "ndim")
    for key, value in sorted(form.items(),key=lambda item:item[0]):
        if key in binary_form_props and not instance_storage.endswith("binary"):
            continue
        missing_key = None
        if key == "ndim":
            if "shape" not in instance_form:
                missing_key = "'shape' (needed by ndim)"
        else:
            if key not in instance_form:
                missing_key = "'" + key  + "'"
        if missing_key:
            msg = textwrap.dedent("""

                No form property '%s'

                On form:
                %s
                """.rstrip()
            ) % (missing_key, form_str)
            yield ValidationError(msg)
        if key != "ndim":
            instance_value = instance_form[key]
        ok = True
        if key == "ndim":
            instance_value = len(instance_form["shape"])
            if instance_value != value:
                ok = False
        elif key == "strides":
            if value != instance_value:
                ok = False
        elif key == "shape":
            assert len(value) == len(instance_value) #TODO: check before for inconsistent shape/ndim requirement
            for schema_dim, instance_dim in zip(value, instance_value):
                if schema_dim == -1:
                    continue
                if not _allowed_value(schema_dim, instance_dim):
                    ok = False
        elif isinstance(instance_value, _types["number"]):
            if isinstance(value, _types["number"]):
                if not _allowed_value(value, instance_value):
                    ok = False
        else:
            if value != instance_value:
                ok = False
        if not ok:
            msg = textwrap.dedent("""

                Form property '%s' has value %r, not %r

                On form:
                %s
                """.rstrip()
            ) % (key, instance_value, value, form_str)
            yield ValidationError(msg)

    if not _from_items and is_numpy_structure_schema(schema):
        assert instance_storage is not None, schema
        if "items" not in instance_form:
            msg = textwrap.dedent("""

                No form property 'items'

                On form:
                %s
                """.rstrip()
            ) % (form_str,)
            yield ValidationError(msg)
            return
        form_wrapper =  FormWrapper(None, instance_form["items"], instance_storage)
        items_form = schema.get("items", {}).get("form" ,  {})
        for error in validator_form(
            validator,
            items_form, form_wrapper,
            schema, _from_items=True
          ):
            yield error
def _default_validator(validator, default, instance, schema):
    # We will also accept a "See values.yaml" default
    if default != instance and default != "See values.yaml":
        yield ValidationError(f"{instance} is not equal to the default of {default}")