Exemplo n.º 1
0
    def _validate_properties(self, value, one_of_schema=None):
        all_props = self.get_all_properties()
        all_props_names = self.get_all_properties_names()
        all_req_props_names = self.get_all_required_properties_names()

        if one_of_schema is not None:
            all_props.update(one_of_schema.get_all_properties())
            all_props_names |= one_of_schema.\
                get_all_properties_names()
            all_req_props_names |= one_of_schema.\
                get_all_required_properties_names()

        value_props_names = value.keys()
        extra_props = set(value_props_names) - set(all_props_names)
        if extra_props and self.additional_properties is None:
            raise UndefinedSchemaProperty(
                "Undefined properties in schema: {0}".format(extra_props))

        for prop_name in extra_props:
            prop_value = value[prop_name]
            self.additional_properties.validate(prop_value)

        for prop_name, prop in iteritems(all_props):
            try:
                prop_value = value[prop_name]
            except KeyError:
                if prop_name in all_req_props_names:
                    raise MissingSchemaProperty(
                        "Missing schema property {0}".format(prop_name))
                if not prop.nullable and not prop.default:
                    continue
                prop_value = prop.default
            prop.validate(prop_value)

        return True
Exemplo n.º 2
0
    def _unmarshal_properties(self,
                              value,
                              one_of_schema=None,
                              custom_formatters=None,
                              strict=True,
                              require_all_props=False):
        all_props = self.get_all_properties()
        all_props_names = self.get_all_properties_names()
        all_req_props_names = self.get_all_required_properties_names()

        if one_of_schema is not None:
            all_props.update(one_of_schema.get_all_properties())
            all_props_names |= one_of_schema.\
                get_all_properties_names()
            all_req_props_names |= one_of_schema.\
                get_all_required_properties_names()

        value_props_names = value.keys()
        extra_props = set(value_props_names) - set(all_props_names)
        extra_props_allowed = self.are_additional_properties_allowed(
            one_of_schema)
        if extra_props and not extra_props_allowed:
            raise UndefinedSchemaProperty(extra_props)

        properties = {}
        if self.additional_properties is not True:
            for prop_name in extra_props:
                prop_value = value[prop_name]
                properties[prop_name] = self.additional_properties.unmarshal(
                    prop_value,
                    custom_formatters=custom_formatters,
                    require_all_props=require_all_props)

        for prop_name, prop in iteritems(all_props):
            try:
                prop_value = value[prop_name]
            except KeyError:
                if prop_name in all_req_props_names:
                    raise MissingSchemaProperty(prop_name)
                if not prop.nullable and not prop.default:
                    continue
                prop_value = prop.default
            try:
                properties[prop_name] = prop.unmarshal(
                    prop_value,
                    custom_formatters=custom_formatters,
                    require_all_props=require_all_props)
            except OpenAPISchemaError as exc:
                raise InvalidSchemaProperty(prop_name, exc)

        self._validate_properties(properties,
                                  one_of_schema=one_of_schema,
                                  custom_formatters=custom_formatters,
                                  require_all_props=require_all_props)

        return properties
Exemplo n.º 3
0
    def _validate_properties(self, value, one_of_schema=None,
                             custom_formatters=None):
        all_props = self.get_all_properties()
        all_props_names = self.get_all_properties_names()
        all_req_props_names = self.get_all_required_properties_names()

        if one_of_schema is not None:
            all_props.update(one_of_schema.get_all_properties())
            all_props_names |= one_of_schema.\
                get_all_properties_names()
            all_req_props_names |= one_of_schema.\
                get_all_required_properties_names()

        value_props_names = value.keys()
        extra_props = set(value_props_names) - set(all_props_names)
        extra_props_allowed = self.are_additional_properties_allowed(
            one_of_schema)
        if extra_props and not extra_props_allowed:
            raise UndefinedSchemaProperty(extra_props)

        for prop_name in extra_props:
            prop_value = value[prop_name]
            if self.additional_properties is True:
                continue
            else:
                # Schema for extra props
                self.additional_properties.validate(
                    prop_value, custom_formatters=custom_formatters)

        for prop_name, prop in iteritems(all_props):
            try:
                prop_value = value[prop_name]
            except KeyError:
                if prop_name in all_req_props_names:
                    raise MissingSchemaProperty(prop_name)
                if not prop.nullable and not prop.default:
                    continue
                prop_value = prop.default
            try:
                prop.validate(prop_value, custom_formatters=custom_formatters)
            except OpenAPISchemaError as exc:
                raise InvalidSchemaProperty(prop_name, original_exception=exc)

        return True
Exemplo n.º 4
0
    def _validate_properties(self,
                             value,
                             one_of_schema=None,
                             custom_formatters=None,
                             read=False,
                             write=False):
        all_props = self.get_all_properties()
        all_props_names = self.get_all_properties_names()
        all_req_props_names = self.get_all_required_properties_names()

        if one_of_schema is not None:
            all_props.update(one_of_schema.get_all_properties())
            all_props_names |= one_of_schema.\
                get_all_properties_names()
            all_req_props_names |= one_of_schema.\
                get_all_required_properties_names()

        value_props_names = value.keys()
        extra_props = set(value_props_names) - set(all_props_names)
        extra_props_allowed = self.are_additional_properties_allowed(
            one_of_schema)
        if extra_props and not extra_props_allowed:
            raise UndefinedSchemaProperty(extra_props)

        if self.additional_properties is not True:
            for prop_name in extra_props:
                prop_value = value[prop_name]
                self.additional_properties.validate(
                    prop_value,
                    custom_formatters=custom_formatters,
                    read=read,
                    write=write)

        for prop_name, prop in iteritems(all_props):
            should_skip = (write and prop.read_only) or (read
                                                         and prop.write_only)
            try:
                prop_value = value[prop_name]
                if read and prop.write_only:
                    message = "WriteOnly property {prop} defined on read.".format(
                        prop=prop_name)
                    raise UndefinedSchemaProperty(message)

                if write and prop.read_only:
                    message = "ReadOnly property {prop} defined on write.".format(
                        prop=prop_name)
                    raise UndefinedSchemaProperty(message)

            except KeyError:
                if prop_name in all_req_props_names and not should_skip:
                    raise MissingSchemaProperty(prop_name)
                if (not prop.nullable and not prop.default) or should_skip:
                    continue
                prop_value = prop.default
            try:
                prop.validate(prop_value,
                              custom_formatters=custom_formatters,
                              read=read,
                              write=write)
            except OpenAPISchemaError as exc:
                raise InvalidSchemaProperty(prop_name, original_exception=exc)

        return True