rambler-digital-solutions / actix-web-validator

Rust library for providing validation mechanism to actix-web with Validator crate.
MIT License
99 stars 26 forks source link

Name conflict with actix::web::Json #25

Open vimmerru opened 3 years ago

vimmerru commented 3 years ago

Current suggested way is to import json extractor as use actix_web_validator::Json and ValidatedJson is deprecated.

The problem with this approach actix::web::Json used not only as Extractor, but also as Responder and usual signature is

pub(crate) async fn create(
    req: web::Json<CreateRequest>,
) -> Result<web::Json<CreateResponse>> {}

As a result it can't be in-place replacement and i have use actix_web_validator::Json as ValidatedJson in near all my endpoints.

singulared commented 3 years ago

You can steel use web::Json for actix Json. Something like this:

use actix_web::web;
use actix_web_validator::Json;

async fn create(
    req: Json<CreateRequest>,
) -> Result<web::Json<CreateResponse>> {}

or

use actix_web::web::Json;

async fn decreate(
    req: actix_web_validator::Json<CreateRequest>,
) -> Result<Json<CreateResponse>> {}
vimmerru commented 3 years ago

@singulared Sure, the issue is more aesthetic, but there are still some practical aspects:

  1. In first option you are provided we mix prefixed and non-prefixed names. Practical problem here is it quite easy to use web::Json instead of Json by mistake and hard to find on code review based on diff. Code will compile well, but there will be significant issue.
  2. actix_web_validator just too long name for prefixed import. I stopped on use actix_web_validator as web_validator
singulared commented 3 years ago

Btw, you can create a type alias for actix_web_validator::Json in your own code:

type ValidatedJson<T> = actix_web_validator::Json<T>;
vimmerru commented 3 years ago

Btw, you can create a type alias for actix_web_validator::Json in your own code

Usually if i need define my own types it is just sign of not well defined interface. This issue is a proposal to solve this.

singulared commented 3 years ago

In fact, this is not a new type, it is just an alias to an existing one.

use std::any::TypeId;

struct A;
type B = A;

fn main() {
    assert!(TypeId::of::<A>() == TypeId::of::<B>());
}