Beispiel #1
0
    def validate_request(cls,
                         req,
                         resource_type,
                         single_schema=None,
                         collection_schema=None):
        #首先需要是一个合法的json串
        json_req = cls.validate_json(req)

        use_schema = {
            "single": single_schema or cls.single_schema,
            "collection": collection_schema or cls.collection_schema
        }.get(resource_type)

        try:
            jsonschema.validate(json_req, use_schema)
        except exceptions.ValidationError as exc:
            if len(exc.path) > 0:
                raise errors.JsonValidationError(
                    # NOTE(ikutukov): here was a exc.path.pop(). It was buggy
                    # because JSONSchema error path could contain integers
                    # and joining integers as string is not a good idea in
                    # python. So some schema error messages were not working
                    # properly and give 500 error code except 400.
                    ": ".join([six.text_type(exc.path), exc.message]))
            raise errors.JsonValidationError(exc.message)
Beispiel #2
0
    def validate_attribute(cls, attr_name, attr):
        """Validates a single attribute from settings.yaml.

        Dict is of this form::

          description: <description>
          label: <label>
          restrictions:
            - <restriction>
            - <restriction>
            - ...
          type: <type>
          value: <value>
          weight: <weight>
          regex:
            error: <error message>
            source: <regexp source>

        We validate that 'value' corresponds to 'type' according to
        attribute_type_schemas mapping in json_schema/cluster.py.
        If regex is present, we additionally check that the provided string
        value matches the regexp.

        :param attr_name: Name of the attribute being checked
        :param attr: attribute value
        :return: attribute or raise InvalidData exception
        """

        if not isinstance(attr, dict):
            return attr

        if 'type' not in attr and 'value' not in attr:
            return attr

        schema = copy.deepcopy(base_types.ATTRIBUTE_SCHEMA)
        type_ = attr.get('type')
        if type_:
            value_schema = base_types.ATTRIBUTE_TYPE_SCHEMAS.get(type_)
            if value_schema:
                schema['properties'].update(value_schema)

        try:
            cls.validate_schema(attr, schema)
        except errors.JsonValidationError as e:
            raise errors.JsonValidationError('[{0}] {1}'.format(
                attr_name, e.message))

        # Validate regexp only if some value is present
        # Otherwise regexp might be invalid
        if attr['value']:
            regex_err = restrictions.AttributesRestriction.validate_regex(attr)
            if regex_err is not None:
                raise errors.JsonValidationError('[{0}] {1}'.format(
                    attr_name, regex_err))