Python pydantic.error_wrappers.ErrorWrapper() Examples

The following are 8 code examples of pydantic.error_wrappers.ErrorWrapper(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module pydantic.error_wrappers , or try the search function .
Example #1
Source File: domains.py    From LuWu with Apache License 2.0 6 votes vote down vote up
def purchase_domain(
    db: Session = Depends(get_db),
    *,
    domain_profile: PurchaseDomainSchema
):
    domain_isp = crud_isp.get(
        db_session=db,
        id=domain_profile.isp_id,
    )
    if domain_isp and domain_isp.provider_name != domain_profile.provider_name:
        raise ValidationError(
            [ErrorWrapper(Exception('provider_name is not matched'), loc="provider_name")],
            model=PurchaseDomainSchema,
        )

    purchase_task = celery_app.send_task(
        "purchase_domain", kwargs=domain_profile.dict()
    )
    purchase_result = purchase_task.get()
    return dict(result=purchase_result) 
Example #2
Source File: utils.py    From fastapi with MIT License 5 votes vote down vote up
def get_missing_field_error(loc: Tuple[str, ...]) -> ErrorWrapper:
    if PYDANTIC_1:
        missing_field_error = ErrorWrapper(MissingError(), loc=loc)
    else:  # pragma: no cover
        missing_field_error = ErrorWrapper(  # type: ignore
            MissingError(), loc=loc, config=BaseConfig,
        )
    return missing_field_error 
Example #3
Source File: vps.py    From LuWu with Apache License 2.0 5 votes vote down vote up
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) 
Example #4
Source File: domains.py    From LuWu with Apache License 2.0 5 votes vote down vote up
def create_domain(
    db: Session = Depends(get_db),
    *,
    domain_config: DomainCreate
):
    if crud_domain.exists(db_session=db, domain=domain_config.domain):
        raise ValidationError(
            [ErrorWrapper(Exception('domain already exists'), loc="domain")],
            model=DomainCreate,
        )

    domain_data = crud_domain.create(db, obj_in=domain_config, serializer=None)
    celery_app.send_task("load_domain_extra_data", args=[domain_data.id])
    return dict(result=domain_data) 
Example #5
Source File: domains.py    From LuWu with Apache License 2.0 5 votes vote down vote up
def create_domain_monitor_task(
    db: Session = Depends(get_db),
    *,
    monitor_profile: DomainMonitorSchema
):
    if crud_domain_task.exists(db_session=db, domain_id=monitor_profile.domain_id):
        raise ValidationError(
            [ErrorWrapper(Exception('domain is already under monitor'), loc="domain_id")],
            model=DomainMonitorSchema,
        )

    monitor_task_obj = crud_domain_task.create(db_session=db, obj_in=monitor_profile, serializer=None)
    return dict(result=monitor_task_obj) 
Example #6
Source File: model_parser.py    From CumulusCI with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def _add_filenames(e: ValidationError, filename):
    def _recursively_add_filenames(l):
        processed = False
        if isinstance(l, Sequence):
            for e in l:
                _recursively_add_filenames(e)
            processed = True
        elif isinstance(l, ErrorWrapper):
            l._loc = (filename, l._loc)

            processed = True
        assert processed, f"Should have processed by now {l}, {repr(l)}"

    _recursively_add_filenames(e.raw_errors) 
Example #7
Source File: routing.py    From fastapi with MIT License 4 votes vote down vote up
def serialize_response(
    *,
    field: ModelField = None,
    response_content: Any,
    include: Union[SetIntStr, DictIntStrAny] = None,
    exclude: Union[SetIntStr, DictIntStrAny] = set(),
    by_alias: bool = True,
    exclude_unset: bool = False,
    exclude_defaults: bool = False,
    exclude_none: bool = False,
    is_coroutine: bool = True,
) -> Any:
    if field:
        errors = []
        response_content = _prepare_response_content(
            response_content,
            exclude_unset=exclude_unset,
            exclude_defaults=exclude_defaults,
            exclude_none=exclude_none,
        )
        if is_coroutine:
            value, errors_ = field.validate(response_content, {}, loc=("response",))
        else:
            value, errors_ = await run_in_threadpool(
                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_)
        return jsonable_encoder(
            value,
            include=include,
            exclude=exclude,
            by_alias=by_alias,
            exclude_unset=exclude_unset,
            exclude_defaults=exclude_defaults,
            exclude_none=exclude_none,
        )
    else:
        return jsonable_encoder(response_content) 
Example #8
Source File: utils.py    From fastapi with MIT License 4 votes vote down vote up
def request_params_to_args(
    required_params: Sequence[ModelField],
    received_params: Union[Mapping[str, Any], QueryParams, Headers],
) -> Tuple[Dict[str, Any], List[ErrorWrapper]]:
    values = {}
    errors = []
    for field in required_params:
        if is_scalar_sequence_field(field) and isinstance(
            received_params, (QueryParams, Headers)
        ):
            value = received_params.getlist(field.alias) or field.default
        else:
            value = received_params.get(field.alias)
        field_info = get_field_info(field)
        assert isinstance(
            field_info, params.Param
        ), "Params must be subclasses of Param"
        if value is None:
            if field.required:
                if PYDANTIC_1:
                    errors.append(
                        ErrorWrapper(
                            MissingError(), loc=(field_info.in_.value, field.alias)
                        )
                    )
                else:  # pragma: nocover
                    errors.append(
                        ErrorWrapper(  # type: ignore
                            MissingError(),
                            loc=(field_info.in_.value, field.alias),
                            config=BaseConfig,
                        )
                    )
            else:
                values[field.name] = deepcopy(field.default)
            continue
        v_, errors_ = field.validate(
            value, values, loc=(field_info.in_.value, field.alias)
        )
        if isinstance(errors_, ErrorWrapper):
            errors.append(errors_)
        elif isinstance(errors_, list):
            errors.extend(errors_)
        else:
            values[field.name] = v_
    return values, errors