简体   繁体   English

验证 JSON Respect/Validation Request

[英]Validate JSON Request with Respect/Validation

I have a JSON request which exactly follow the rules, but the result keeps failing.我有一个 JSON 请求,完全符合规则,但结果一直失败。 Let's say I have a request like this:假设我有这样的请求:

{
"name"  :   "Angela White",
"dateOfBirth" : "01/01/98"
}

and this is my controller:这是我的 controller:

class RegisterController extends Controller {
    
    public function register($request, $response) {
        $parsedBody = $request->getParsedBody();
        
        $name = trim($parsedBody['name']);
        $dateOfBirth = $parsedBody['dateOfBirth'];
        
        $customer = new Customer();
        $customer->setName($name);
        $customer->setDateOfBirth($dateOfBirth);
        
        $rules = $customer->customerValidator();

        $data = array(
            'name'  => $name,
            'dateOfBirth'  => $dateOfBirth,
        );

        
        foreach($data as $key => $val) {
            try{
                $rules->check($val);
            } catch(\InvalidArgumentException $e) {
                $errors = $e->getMessage();
                return $response->withJson($errors, 401);
                
            }
        }
    }
}

This is class to validate the data:这是 class 来验证数据:

class Customer {
    private $name;
    private $dateOfBirth;
    
    public function setName($input) {
        $this->name = $input;
    }

    public function getName() {
        return $this->name;
    }

    public function setDateOfBirth($input) {
        $this->dateOfBirth = $input;
    }

    public function getDateOfBirth() {
        return $this->dateOfBirth;
    }

    
    public function customerValidator() {
        
        return v::attribute($this->getName(), v::stringType()->length(2, null)->setName('Name'))
            ->attribute('dateOfBirth', v::notEmpty()->date('d/m/y')->setName('Date of birth'));

    }


}

And as the result I got this:结果我得到了这个:

{
    "API_Response": {
        "Status": {
            "Message": "Attribute Name must be present",
            "_ErrorCode": 401,
            "_TimeStamp": 1647247097
        }
    }
}

I expect the result is success but why is the message still "Attribute Name must be present"?我希望结果是成功的,但为什么消息仍然是“属性名称必须存在”? Does anyone can help me, what have I missed?有谁能帮助我,我错过了什么? Thanks!谢谢!

There are different issues.有不同的问题。

1. Usage of the attribute validator 1.属性验证器的使用

The one that's inside the customerValidator function. Here you are using the attribute rule that accepts the name of the attribute and not its value.位于customerValidator function 中的那个。在这里,您使用的是接受属性名称而不是其值的属性规则 Replacing $this->getName() with 'name' should be enough:$this->getName()替换为'name'应该就足够了:

class Customer {
  private $name;
  private $dateOfBirth;

  public function setName($input) {
    $this->name = $input;
  }

  public function getName() {
    return $this->name;
  }

  public function setDateOfBirth($input) {
    $this->dateOfBirth = $input;
  }

  public function getDateOfBirth() {
    return $this->dateOfBirth;
  }

  public function customerValidator() {
    return v::attribute('name', v::stringType()->length(2, null)->setName('Name'))
        ->attribute('dateOfBirth', v::notEmpty()->date('d/m/y')->setName('Date of birth'));

  }

}

2. Validation check in the controller 2.验证检查在controller

You already have the object with all the properties set, so it's better to validate it, instead of iterating over all the keys you defined.您已经拥有设置了所有属性的 object,因此最好对其进行验证,而不是遍历您定义的所有键。

class RegisterController extends Controller {
    
    public function register($request, $response) {
        $parsedBody = $request->getParsedBody();
        
        $name = trim($parsedBody['name']);
        $dateOfBirth = $parsedBody['dateOfBirth'];
        
        $customer = new Customer();
        $customer->setName($name);
        $customer->setDateOfBirth($dateOfBirth);
        
        $rules = $customer->customerValidator();

        $data = array(
            'name'  => $name,
            'dateOfBirth'  => $dateOfBirth,
        );

        try{
            $rules->check($customer);
        } catch(\InvalidArgumentException $e) {
            $errors = $e->getMessage();
            return $response->withJson($errors, 401);
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM