Пример #1
0
    def _validate(self, value):
        if self._type is not None and not isinstance(value, self._type):
            raise WrongType(value, self._type, self.__name__)

        if not self.constraint(value):
            raise ConstraintNotSatisfied(value, self.__name__)
        for validator in self._validators or []:
            validator(self, value)
Пример #2
0
    def _validate(self, value):
        if self.allowed_mime_types\
                and value.mimeType not in self.allowed_mime_types:
            raise WrongType(value, self.allowed_mime_types)

        if self.max_length is not None and len(value.raw) > self.max_length:
            raise Invalid(
                _('msg_text_too_long',
                  default=u'Text is too long. (Maximum ${max} characters.)',
                  mapping={'max': self.max_length}))

        if not self.constraint(value):
            raise ConstraintNotSatisfied(value)
Пример #3
0
 def _validate(self, value):
     # Pass all validations during initialization
     if self._init_field:
         return
     super(Choice, self)._validate(value)
     vocabulary = self.vocabulary
     if vocabulary is None:
         vr = getVocabularyRegistry()
         try:
             vocabulary = vr.get(None, self.vocabularyName)
         except VocabularyRegistryError:
             raise ValueError("Can't validate value without vocabulary")
     if value not in vocabulary:
         raise ConstraintNotSatisfied(value, self.__name__)
Пример #4
0
    def _validate(self, value):
        """ """
        super(FhirField, self)._validate(value)

        if self.resource_interface:
            try:
                verifyObject(
                    self._resource_interface_class, value.foreground_origin(), False
                )

            except (
                BrokenImplementation,
                BrokenMethodImplementation,
                DoesNotImplement,
            ) as exc:

                return reraise(Invalid, str(exc))

        if self.resource_type and value.resource_type != self.resource_type:
            msg = (
                "Resource type must be `{0}` but we got {1} " "which is not allowed!"
            ).format(self.resource_type, value.resource_type)
            raise ConstraintNotSatisfied(msg, field_name=self.getName())

        if self.resource_class:
            klass = self._resource_class

            if value.foreground_origin() is not None and not isinstance(
                value.foreground_origin(), klass
            ):
                msg = (
                    "Wrong fhir resource value is provided! "
                    "Value should be object of {0!r} but got {1!r}".format(
                        klass, value.foreground_origin().__class__
                    )
                )

                raise WrongContainedType(msg, field_name=self.getName())

        if value.foreground_origin() is not None:
            try:
                value.foreground_origin().as_json()
            except (FHIRValidationError, TypeError) as exc:
                msg = (
                    "There is invalid element inside " "fhir model object.\n{0!s}"
                ).format(exc)

                return reraise(Invalid, msg)
Пример #5
0
    def _validate(self, value):
        """ """
        super(FhirField, self)._validate(value)

        if self.resource_interface:
            try:
                verifyObject(self._resource_interface_class,
                             value.foreground_origin(), False)

            except (BrokenImplementation, BrokenMethodImplementation,
                    DoesNotImplement):

                t, v, tb = sys.exc_info()
                try:
                    reraise(Invalid(str(v)), None, tb)
                finally:
                    del t, v, tb

        if self.resource_type and value.resource_type != self.resource_type:
            msg = "Resource type must be `{0}` but we got {1} which is not allowed!".format(
                self.resource_type, value.resource_type)
            raise ConstraintNotSatisfied(msg)

        if self.resource_class:
            klass = self._resource_class

            if value.foreground_origin() is not None and not isinstance(
                    value.foreground_origin(), klass):
                msg = "Wrong fhir resource value is provided! "\
                      "Value should be object of {0!r} but got {1!r}".\
                    format(klass, value.foreground_origin().__class__)

                raise WrongContainedType(msg)

        if value.foreground_origin() is not None:
            try:
                value.foreground_origin().as_json()
            except (FHIRValidationError, TypeError) as exc:
                msg = "There is invalid element inside fhir model object.\n{0!s}".format(
                    exc)
                t, v, tb = sys.exc_info()
                try:
                    reraise(Invalid(msg), None, tb)
                finally:
                    del t, v, tb
Пример #6
0
    def _from_dict(self, dict_value):
        """ """
        self._pre_value_validate(dict_value)
        klass = self._resource_class

        if klass is None:
            # relay on json value for resource type
            klass = resource_type_to_resource_cls(dict_value["resourceType"])

        # check constraint
        if klass.resource_type != dict_value.get("resourceType"):
            raise ConstraintNotSatisfied(
                "Fhir Resource mismatched with provided resource type!\n"
                "`{0}` resource type is permitted but got `{1}`".format(
                    klass.resource_type, dict_value.get("resourceType")))

        value = FhirFieldValue(obj=klass(dict_value))

        return value