Esempio n. 1
0
    def _validate_collection(self, value, custom_formatters=None):
        if self.items is None:
            raise UndefinedItemsSchema(self.type)

        if self.min_items is not None:
            if self.min_items < 0:
                raise OpenAPISchemaError("Schema for collection invalid:"
                                         " minItems must be non-negative")
            if len(value) < self.min_items:
                raise InvalidSchemaValue(
                    "Value must contain at least {type} item(s),"
                    " {value} found", len(value), self.min_items)
        if self.max_items is not None:
            if self.max_items < 0:
                raise OpenAPISchemaError("Schema for collection invalid:"
                                         " maxItems must be non-negative")
            if len(value) > self.max_items:
                raise InvalidSchemaValue(
                    "Value must contain at most {value} item(s),"
                    " {type} found", len(value), self.max_items)
        if self.unique_items and len(set(value)) != len(value):
            raise OpenAPISchemaError("Value may not contain duplicate items")

        f = functools.partial(self.items.validate,
                              custom_formatters=custom_formatters)
        return list(map(f, value))
Esempio n. 2
0
    def _validate_object(self, value, custom_formatters=None):
        properties = value.__dict__

        if self.one_of:
            valid_one_of_schema = None
            for one_of_schema in self.one_of:
                try:
                    self._validate_properties(
                      properties, one_of_schema,
                      custom_formatters=custom_formatters)
                except OpenAPISchemaError:
                    pass
                else:
                    if valid_one_of_schema is not None:
                        raise MultipleOneOfSchema(self.type)
                    valid_one_of_schema = True

            if valid_one_of_schema is None:
                raise NoOneOfSchema(self.type)

        else:
            self._validate_properties(properties,
                                      custom_formatters=custom_formatters)

        if self.min_properties is not None:
            if self.min_properties < 0:
                raise OpenAPISchemaError(
                    "Schema for object invalid:"
                    " minProperties must be non-negative"
                )

            if len(properties) < self.min_properties:
                raise InvalidSchemaValue(
                    "Value must contain at least {type} properties,"
                    " {value} found", len(properties), self.min_properties
                )

        if self.max_properties is not None:
            if self.max_properties < 0:
                raise OpenAPISchemaError(
                    "Schema for object invalid:"
                    " maxProperties must be non-negative"
                )
            if len(properties) > self.max_properties:
                raise InvalidSchemaValue(
                    "Value must contain at most {type} properties,"
                    " {value} found", len(properties), self.max_properties
                )

        return True
Esempio n. 3
0
    def _validate_string(self, value, custom_formatters=None):
        try:
            schema_format = SchemaFormat(self.format)
        except ValueError:
            msg = "Unsupported {0} format validation".format(self.format)
            if custom_formatters is not None:
                formatstring = custom_formatters.get(self.format)
                if formatstring is None:
                    raise OpenAPISchemaError(msg)
            else:
                raise OpenAPISchemaError(msg)
        else:
            formatstring =\
                self.STRING_FORMAT_CALLABLE_GETTER[schema_format]

        if not formatstring.validate(value):
            raise InvalidSchemaValue(
                "Value {value} not valid format {type}", value, self.format)

        if self.min_length is not None:
            if self.min_length < 0:
                raise OpenAPISchemaError(
                    "Schema for string invalid:"
                    " minLength must be non-negative"
                )
            if len(value) < self.min_length:
                raise InvalidSchemaValue(
                    "Value is shorter ({value}) than the minimum length of {type}",
                    len(value), self.min_length
                )
        if self.max_length is not None:
            if self.max_length < 0:
                raise OpenAPISchemaError(
                    "Schema for string invalid:"
                    " maxLength must be non-negative"
                )
            if len(value) > self.max_length:
                raise InvalidSchemaValue(
                    "Value is longer ({value}) than the maximum length of {type}",
                    len(value), self.max_length
                )
        if self.pattern is not None and not self.pattern.search(value):
            raise InvalidSchemaValue(
                "Value {value} does not match the pattern {type}",
                    value, self.pattern.pattern
            )

        return True
Esempio n. 4
0
    def _unmarshal_string(self, value):
        try:
            schema_format = SchemaFormat(self.format)
        except ValueError:
            # @todo: implement custom format unmarshalling support
            raise OpenAPISchemaError(
                "Unsupported {0} format unmarshalling".format(self.format))
        else:
            formatter = self.STRING_FORMAT_CAST_CALLABLE_GETTER[schema_format]

        try:
            return formatter(value)
        except ValueError:
            raise InvalidSchemaValue(
                "Failed to format value of {0} to {1}".format(
                    value, self.format))
Esempio n. 5
0
    def _validate_string(self, value):
        try:
            schema_format = SchemaFormat(self.format)
        except ValueError:
            # @todo: implement custom format validation support
            raise OpenAPISchemaError(
                "Unsupported {0} format validation".format(self.format))
        else:
            format_validator_callable =\
                self.STRING_FORMAT_VALIDATOR_CALLABLE_GETTER[schema_format]

        if not format_validator_callable(value):
            raise InvalidSchemaValue(
                "Value of {0} not valid format of {1}".format(
                    value, self.format))

        return True
Esempio n. 6
0
    def _validate_collection(self, value):
        if self.items is None:
            raise OpenAPISchemaError("Schema for collection not defined")

        return list(map(self.items.validate, value))
Esempio n. 7
0
 def strict_to_str(x):
     if not isinstance(x, str):
         raise OpenAPISchemaError(f"Expected str but got {type(x)} -- {x}")
     return x
Esempio n. 8
0
 def strict_to_bool(x):
     if not isinstance(x, bool):
         raise OpenAPISchemaError(f"Expected bool but got {type(x)} -- {x}")
     return x