Beispiel #1
0
def parse_geometry_obj(obj) -> Geometry:
    """
    `obj` is an object that is supposed to represent a GeoJSON geometry. This method returns the
    reads the `"type"` field and returns the correct pydantic Geometry model.
    """
    if "type" not in obj:
        raise ValidationError([
            ErrorWrapper(ValueError("Missing 'type' field in geometry"),
                         "type"),
            "Geometry",
        ])
    if obj["type"] == "Point":
        return Point.parse_obj(obj)
    elif obj["type"] == "MultiPoint":
        return MultiPoint.parse_obj(obj)
    elif obj["type"] == "LineString":
        return LineString.parse_obj(obj)
    elif obj["type"] == "MultiLineString":
        return MultiLineString.parse_obj(obj)
    elif obj["type"] == "Polygon":
        return Polygon.parse_obj(obj)
    elif obj["type"] == "MultiPolygon":
        return MultiPolygon.parse_obj(obj)
    raise ValidationError([ErrorWrapper(ValueError("Unknown type"), "type")],
                          "Geometry")
Beispiel #2
0
async def test_exception_handler_pydantic_validationerror_model():
    async def test_receive():
        return {
            "type": "http.request",
            "body": json.dumps({"id": "str", "name": []}).encode("utf-8"),
        }

    request = Request(
        {"type": "http", "method": "GET", "path": "/"}, receive=test_receive
    )
    exc = Exception()
    exc.raw_errors = [ErrorWrapper(loc=("hello", "world"), exc=Exception())]
    error = ValidationError(
        [ErrorWrapper(loc=("hello", "world"), exc=exc)]
    )
    raw_response = await validation_exception_handler(request, error)
    response = json.loads(raw_response.body.decode("utf-8"))

    assert response["code"] == 400
    assert response["detail"] == "Validation error"
    assert response["fields"] == [{"name": "hello", "message": "World: "}]

    exc = Exception()
    error = ValidationError([ErrorWrapper(loc=("hello", "world"), exc=exc)])
    raw_response = await validation_exception_handler(request, error)
    response = json.loads(raw_response.body.decode("utf-8"))

    assert response["code"] == 400
    assert response["detail"] == "Validation error"
    assert response["fields"] == [{"name": "hello", "message": "World: "}]
Beispiel #3
0
def validate_groundtruth_uri(manifest: dict):
    """
    Validate groundtruth_uri
    Returns entries count if succeeded
    """
    request_type = manifest.get('request_type', '')
    uri_key = "groundtruth_uri"
    uri = manifest.get(uri_key)
    if uri is None:
        return
    try:
        response = requests.get(uri, timeout=1)
        response.raise_for_status()

        entries_count = 0
        data = response.json()
        if isinstance(data, dict):
            for k, v in data.items():
                entries_count += 1
                validate_groundtruth_entry(k, v, request_type)
        else:
            for v in data:
                entries_count += 1
                validate_groundtruth_entry("", v, request_type)

    except (ValidationError, RequestException) as e:
        raise ValidationError(f"{uri_key} validation failed: {e}", Manifest) from e

    if entries_count == 0:
        raise ValidationError(f"fetched {uri_key} is empty", Manifest)
 def hash_password(cls, value):
     from auth import get_password_hash
     if value == "":
         raise ValidationError("password cannot be empty", model=UserIn)
     if len(value) <= 2:
         raise ValidationError("password must have at least 2 characters",
                               model=UserIn)
     return get_password_hash(value)
Beispiel #5
0
    def validate_summary(cls, summary: str) -> str:
        """Check if summary is a valid one line string."""
        summary_re = re.compile(r"^.+$")
        if len(summary) > 512:
            raise ValidationError("Summary should be less than 512 chars.")

        if not summary_re.match(summary):
            raise ValidationError("Summary should contain one line only.")
        return summary
 def check_num_items(cls, materials):
     "Ensure length of material is at least 1 and not more than 10."
     if len(materials) == 0:
         raise ValidationError(
             'Opaque construction should at least have one material.')
     elif len(materials) > 10:
         raise ValidationError(
             'Opaque construction cannot have more than 10 materials.')
     else:
         return materials
Beispiel #7
0
 def number_and_code(cls, v, values):  # noqa
     # Parse and validate the format based on the input provided
     kind = values.get("kind", None)
     if kind == TimeSlotKind.nctu:
         # example: 3EF2G, 1A3CD
         res = re.match(r"^(\d[A-Za-z]+)+$", v)
         if not res:
             raise ValidationError(v)
         return v
     else:
         raise ValidationError(f"Not recognized encoding type: {kind}")
Beispiel #8
0
    def validate_metadata(cls, value):
        if value is None:
            return value

        if len(value) > 10:
            raise ValidationError("10 key max. in metadata")

        if len(str(value)) > 1024:
            raise ValidationError("metadata should be < 1024")

        return value
    def check_num_items(cls, layers):
        "Ensure length of material is at least 1 and not more than 8."
        if len(layers) == 0:
            raise ValidationError(
                'Window construction should at least have one material.')

        elif len(layers) > 8:
            raise ValidationError(
                'Window construction cannot have more than 8 materials.')
        else:
            return layers
Beispiel #10
0
    def validate_login_credentials(cls, values):
        v = values.copy()
        v.pop('password')
        if len(v) > 1:
            raise ValidationError(
                "Only one of the following fields is allowed: email, username")
        if len(v) < 1:
            raise ValidationError(
                "At least one of the following fields must be passed: email, username"  # noqa
            )

        return values
Beispiel #11
0
    def check_image(cls, values):
        image = values.get('image')
        build_context = values.get('build_context')

        if build_context and not os.path.exists(build_context):
            raise ValidationError(
                f'Docker build context {build_context} does not exists', )

        if (not image and not build_context) or (image and build_context):
            raise ValidationError(
                'Docker config should contains one of `image` or `build_context`',
            )
        return values
Beispiel #12
0
    def validate_requester_question_example(cls, value, values, **kwargs):
        # validation runs before other params, so need to handle missing case
        if not ("request_type" in values):
            raise ValidationError("request_type missing")

        # based on https://github.com/hCaptcha/hmt-basemodels/issues/27#issuecomment-590706643
        supports_lists = [
            BaseJobTypesEnum.image_label_area_select, BaseJobTypesEnum.image_label_binary
        ]

        if isinstance(value, list) and not values['request_type'] in supports_lists:
            raise ValidationError("Lists are not allowed in this challenge type")
        return value
Beispiel #13
0
 def pos_correct_size(cls, v):
     if 'kind' not in v:
         raise ValidationError('"kind" property must exist')
     kind = v['kind']
     if 'pos' not in v:
         raise ValidationError('"pos" integer array must be present')
     pos_length = len(v['pos'])
     if kind == Kind.point and pos_length != 3:
         raise ValidationError('Point must have 3 elements in pos')
     if kind == Kind.lineseg and pos_length != 6:
         raise ValidationError('Line segment must have 6 elements in pos')
     if kind == Kind.sphere and pos_length != 6:
         raise ValidationError('Sphere must have 6 elements in pos')
     return v
Beispiel #14
0
 def validate(self, cls, value, values):
     """
     validate request types for all types of challenges
     multi_challenge should always have multi_challenge_manifests
     """
     if value == BaseJobTypesEnum.multi_challenge:
         if not self.multi_challenge:
             raise ValidationError("multi_challenge request is not allowed here.")
         if "multi_challenge_manifests" not in values:
             raise ValidationError("multi_challenge requires multi_challenge_manifests.")
     elif value in [BaseJobTypesEnum.image_label_multiple_choice, BaseJobTypesEnum.image_label_area_select]:
         if values.get('multiple_choice_min_choices', 1) > values.get('multiple_choice_max_choices', 1):
             raise ValidationError(
                 "multiple_choice_min_choices cannot be greater than multiple_choice_max_choices")
     return value
Beispiel #15
0
    def validate(cls, v, field: ModelField):
        """Pydantic validator."""
        from pydantic.utils import sequence_like

        if not sequence_like(v):
            raise TypeError(
                trans._(
                    'Value is not a valid sequence: {value}',
                    deferred=True,
                    value=v,
                )
            )
        if not field.sub_fields:
            return cls(v)

        type_field = field.sub_fields[0]
        errors = []
        for i, v_ in enumerate(v):
            _valid_value, error = type_field.validate(v_, {}, loc=f'[{i}]')
            if error:
                errors.append(error)
        if errors:
            from pydantic import ValidationError

            raise ValidationError(errors, cls)  # type: ignore
        return cls(v)
Beispiel #16
0
class MockException:
    raw_errors = [
        ValidationError(
            errors=[ErrorWrapper(AnyStrMaxLengthError(limit_value=10), ('name',))],
            model=MockModel
        )
    ]
Beispiel #17
0
    def test_pydantic_validation_error(self):
        self.add_route_raises_exception(
            ExceptionHandlingSpec(
                Exception,
                broad_exception_handler,
                ValidationError(
                    [
                        ErrorWrapper(NoneIsNotAllowedError(), "foo"),
                        ErrorWrapper(BoolError(), "bar"),
                    ],
                    BaseModel,
                ),
            ))

        resp = self.request()

        self.assertEqual(400, resp.status_code)
        self.assertDictEqual(
            {
                "error": [
                    {
                        "loc": ["foo"],
                        "msg": "none is not an allowed value",
                        "type": "type_error.none.not_allowed",
                    },
                    {
                        "loc": ["bar"],
                        "msg": "value could not be parsed to a boolean",
                        "type": "type_error.bool",
                    },
                ]
            },
            resp.json,
        )
Beispiel #18
0
 def load_creds_file(cls, v, values):
     try:
         if v.is_file():
             dotenv.load_dotenv(v)
     except Exception:
         raise ValidationError(f"Failed to load CREDS_FILE '{v}'")
     return v
Beispiel #19
0
def correct_length_mapping(cls, mapping):
    """Validates that each mapping contains only 2 values (original & changed) and at most 1000 values"""
    # if no mapping is provided (i.e. when updating only name), do nothing
    if not mapping:
        return mapping

    invalid_value_idx = []
    for idx, elem in enumerate(mapping):
        if len(elem) != 2:
            invalid_value_idx.append(idx)

    if invalid_value_idx:
        raise ValidationError(
            [
                ErrorWrapper(
                    ValueError(
                        f'This mapping contains {len(mapping[idx])} values instead of 2'
                    ), str(idx + 1)) for idx in invalid_value_idx
            ],
            cls,
        )

    if len(mapping) > _MAX_MAPPING_CNT:
        raise ValueError(
            f'There is {len(mapping)} mappings, but Husky only supports {_MAX_MAPPING_CNT} mappings.'
        )

    return mapping
Beispiel #20
0
def serialize_response(
    *,
    response_content: Any,
    field: Optional[ModelField] = None,
    include: Optional[Set[str]] = None,
    exclude: Optional[Set[str]] = None,
    by_alias: bool = True,
) -> Any:
    if field:
        errors = []
        response_content = _prepare_response_content(
            content=response_content, )
        value, errors_ = field.validate(response_content, {},
                                        loc=("response", ))

        if isinstance(errors_, ErrorWrapper):
            errors.append(errors_)
        elif isinstance(errors_, list):
            errors.extend(errors_)

        if errors:
            raise ValidationError(errors, field.type_)
        result = value.dict(by_alias=by_alias,
                            include=include,
                            exclude=exclude)

        if "__root__" in result:
            result = result["__root__"]

        return result

    else:
        return response_content
Beispiel #21
0
def create_vps_server(db: Session = Depends(get_db), *, vps_profile: VpsCreateSchema):
    # validate
    vps_isp_obj = crud_isp.get(db_session=db, id=vps_profile.isp_id)
    if not vps_isp_obj:
        raise ValidationError(
            [ErrorWrapper(Exception('provider_name is not matched'), loc="isp_id")],
            model=VpsCreateSchema,
        )

    # create base vps data
    rp = RedisPool()
    vps_spec_data = rp.get_vps_spec_value(
        db_session=db,
        isp_id=vps_profile.isp_id,
        os_code=vps_profile.os_code,
        plan_code=vps_profile.plan_code,
        region_code=vps_profile.region_code
    )
    vps_config = dict(
        hostname=vps_profile.hostname,
        isp_id=vps_profile.isp_id,
        ssh_keys=vps_profile.ssh_keys,
        remark=vps_profile.remark,
        status=vps_profile.status,
        **vps_spec_data
    )

    vps_obj = crud_vps.create(db_session=db, obj_in=vps_config, serializer=None)
    task = celery_app.send_task(
        "create_vps", args=[vps_profile.dict(), vps_obj.id]
    )
    return dict(result=task)
Beispiel #22
0
    def _validate_feature_server_config(cls, values):
        # Having no feature server is the default.
        if "feature_server" not in values:
            return values

        # Skip if we aren't creating the configuration from a dict
        if not isinstance(values["feature_server"], Dict):
            return values

        # Make sure that the provider configuration is set. We need it to set the defaults
        if "provider" not in values:
            raise FeastProviderNotSetError()

        feature_server_type = FEATURE_SERVER_TYPE_FOR_PROVIDER.get(
            values["provider"])
        defined_type = values["feature_server"].get("type")
        # Make sure that the type is either not set, or set correctly, since it's defined by the provider
        if defined_type not in (None, feature_server_type):
            raise FeastFeatureServerTypeSetError(defined_type)
        values["feature_server"]["type"] = feature_server_type

        # Validate the dict to ensure one of the union types match
        try:
            feature_server_config_class = get_feature_server_config_from_type(
                feature_server_type)
            feature_server_config_class(**values["feature_server"])
        except ValidationError as e:
            raise ValidationError(
                [ErrorWrapper(e, loc="feature_server")],
                model=RepoConfig,
            )

        return values
Beispiel #23
0
def with_errs_locations(
    model: Type[BaseModel],
    validation_err: ValidationError,
    values_source: AnySourceLocProvider,
) -> ValidationError:
    def process_err_wrapper(
        err_wrapper: ErrorWrapper, loc_override: JsonLocation
    ) -> ErrorWrapper:
        try:
            location = values_source.get_location(loc_override)
        except KeyError:
            if isinstance(err_wrapper, ExtendedErrorWrapper):
                return ExtendedErrorWrapper(
                    err_wrapper.exc, loc_override, err_wrapper.source_loc
                )
            else:
                return ErrorWrapper(err_wrapper.exc, tuple(loc_override))

        return ExtendedErrorWrapper(
            err_wrapper.exc, loc_override, source_loc=location
        )

    return ValidationError(
        [
            process_err_wrapper(raw_err, model_loc)
            for model_loc, raw_err in _flatten_errors_wrappers(
                validation_err.raw_errors
            )
        ],
        model,
    )
Beispiel #24
0
    def test_should_raise_httpexception_403_when_validationerror_is_raised(
        self,
        mocker,
        faker,
    ):
        db = mocker.MagicMock()
        token = faker.sha1()
        secret_key = faker.sha1()
        access_token_algorithm = faker.word()

        decode = mocker.patch(
            f"{MODULE}.decode",
            side_effect=ValidationError(
                errors=["errors-stub"],
                model="model-stub",
            ),
        )
        settings = mocker.patch(f"{MODULE}.core.settings")
        settings.SECRET_KEY = secret_key
        settings.ACCESS_TOKEN_ALGORITHM = access_token_algorithm

        with pytest.raises(HTTPException) as ex:
            get_authenticated_user(db=db, token=token)

        assert ex.value.status_code == 403
        assert ex.value.detail == "Could not validate the user credentials."

        decode.assert_called_once_with(
            jwt=token,
            key=secret_key,
            algorithms=[access_token_algorithm],
        )
Beispiel #25
0
 def validate_protocol_version(cls, protocol_version: int) -> int:
     """Check if protocol_version is 1."""
     if not protocol_version:
         return protocol_version
     if protocol_version != 1:
         raise ValidationError("Protocol version 1 only supported.")
     return protocol_version
Beispiel #26
0
    def image_file_validate(cls, image_path):
        # try:
        #     image_path = sup.save_file(v)
        # except:
        #     raise ValueError("field required")

        file_type = filetype.guess(image_path)
        image_file_size = os.path.getsize(image_path)

        if file_type is not None:
            file_extension = file_type.extension
            if file_extension not in [
                    "jpg", "jpeg", "png", "tiff", "tif", "webp"
            ]:
                raise ValidationError("file format is not supported",
                                      "image_file")
        else:
            raise ValueError("file is not supported")
        if chunk_size < image_file_size:
            raise ValueError("file size must be less than 5 MB")

        image = Image.open(image_path)
        width, height = image.size
        if width > 5000:
            raise ValueError("image width must be less than 5000 pixel")
        if height > 5000:
            raise ValueError("image height must be less than 5000 pixel")

        np_img = np.array(image)

        logger.debug(f"file extension: {file_extension}")

        os.remove(image_path)
        return np_img
Beispiel #27
0
    def _validate_offline_store_config(cls, values):
        # Set empty offline_store config if it isn't set explicitly
        if "offline_store" not in values:
            values["offline_store"] = dict()

        # Skip if we aren't creating the configuration from a dict
        if not isinstance(values["offline_store"], Dict):
            return values

        # Make sure that the provider configuration is set. We need it to set the defaults
        assert "provider" in values

        # Set the default type
        if "type" not in values["offline_store"]:
            if values["provider"] == "local":
                values["offline_store"]["type"] = "file"
            elif values["provider"] == "gcp":
                values["offline_store"]["type"] = "bigquery"
            elif values["provider"] == "aws":
                values["offline_store"]["type"] = "redshift"

        offline_store_type = values["offline_store"]["type"]

        # Validate the dict to ensure one of the union types match
        try:
            offline_config_class = get_offline_config_from_type(
                offline_store_type)
            offline_config_class(**values["offline_store"])
        except ValidationError as e:
            raise ValidationError(
                [ErrorWrapper(e, loc="offline_store")],
                model=RepoConfig,
            )

        return values
Beispiel #28
0
 def validate_version(cls, version: str) -> str:
     """Check version does not contains whitespaces."""
     version_re = re.compile(r"^(?!\s).*(?<!\s)$")
     if not version_re.match(version):
         raise ValidationError(
             "Version Can't have leading or trailing whitespace.")
     return version
Beispiel #29
0
    def validate(cls: Type["Range"], value: Any) -> "Range":
        """ Validate the passed in value """
        if isinstance(value, str):
            match = RANGE_REGEX.fullmatch(value)
            if not match:
                raise ValidationError(
                    [
                        ErrorWrapper(ValueError(f"Unable to parse Range!"),
                                     loc=cls.__name__)
                    ],
                    cls,
                )

            match_groups = match.groupdict()
            return cls(
                unit=match_groups["unit"],
                ranges=[
                    Subrange(**{
                        k: int(v) if v else None
                        for k, v in m.groupdict().items()
                    }) for m in SUBRANGE_REGEX.finditer(match_groups["ranges"])
                ],
            )

        return super().validate(value)
Beispiel #30
0
 def __setattr__(self, name, value):
     if (self.__config__.extra is not Extra.allow
             and name not in self.__fields__):
         raise ValueError(f'"{self.__class__.__name__}" object has no '
                          f'field "{name}"')
     elif not self.__config__.allow_mutation:
         raise TypeError(f'"{self.__class__.__name__}" is immutable and '
                         f'does not support item assignment')
     elif (self.__config__.validate_assignment
           and name not in self.__config__.validate_assignment_exclude):
         if self.__config__.validate_assignment == 'limited':
             kw = {'include': {}}
         else:
             kw = {'exclude': {name}}
         value_, error_ = self.fields[name].validate(value,
                                                     self.dict(**kw),
                                                     loc=name)
         if error_:
             raise ValidationError([error_])
         else:
             self.__values__[name] = value_
             self.__fields_set__.add(name)
     else:
         self.__values__[name] = value
         self.__fields_set__.add(name)