CodeIgniter Forums
How to validate an API REST? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: How to validate an API REST? (/showthread.php?tid=76161)



How to validate an API REST? - MatheusCastro - 04-17-2020

Hello, 

I'm extending Resource Controller

In situations that are passed as GET I can validate this way:

PHP Code:
public function index()
    {
        // this
        if (!$this->validate([
            'page'    => 'required|is_natural_no_zero|max_length[11]',
            'city_id' => 'required|is_natural_no_zero|max_length[11]',
            'search'  => 'permit_empty|max_length[50]'

        ])) return $this->fail($this->validator->getErrors());

      // continue
    

However, when it is necessary to pass the parameter through the function, how to solve this?


PHP Code:
    public function show($id null)
    {
        
// error
        if (!$this->validate([
            $id => 'required|is_natural_no_zero|max_length[11]',
            
        
])) return $this->fail($this->validator->getErrors());

       // continue
    



Can anyone help?


RE: How to validate an API REST? - jreklund - 04-17-2020

There are a hidden function named setGlobal (hidden as it's not in the documentation yet). That allow you to set get, post etc.

PHP Code:
public function show($id null)
    {
        
$this->request->setGlobal('get', ['page' => $id]);

        
// error
        
if (!$this->validate([
            
'page' => 'required|is_natural_no_zero|max_length[11]',
            
        ])) return 
$this->fail($this->validator->getErrors());

       
// continue
    


Haven't tested it out myself.


RE: How to validate an API REST? - MatheusCastro - 04-17-2020

(04-17-2020, 12:59 PM)jreklund Wrote: There are a hidden function named setGlobal (hidden as it's not in the documentation yet). That allow you to set get, post etc.

PHP Code:
    public function show($id null)
    {
        $this->request->setGlobal('get', ['page' => $id]);

        // error
        if (!$this->validate([
            'page' => 'required|is_natural_no_zero|max_length[11]',
            
        
])) return $this->fail($this->validator->getErrors());

       // continue
    

Haven't tested it out myself.


Hello, interesting function.

I ran some tests with it separately and I think I found some bugs.

Try using:

PHP Code:
$this->request->setGlobal('post', ['id' => $id]);

print_r($this->request->getVar());

// response: array () 

When using getVar, the declaration is not recognized.

However, if you use getPost or getGet, the parameter will be recognized.

PHP Code:
 $this->request->setGlobal('post', ['id' => $id]);
 print_r($this->request->getPost());

// response: Array ( [id] => 30 )

 
$this->request->setGlobal('get', ['id' => $id]);
 
print_r($this->request->getGet());

// response: Array ( [id] => 30 ) 

And the $this->validate function within it has the withRequest that the request is getVar()

PHP Code:
public function withRequest(RequestInterface $request): ValidationInterface
{
     if (in_array($request->getMethod(), ['put''patch''delete'])) {

        
$this->data $request->getRawInput();
     } else {

        // this
    
$this->data $request->getVar() ?? [];
    }

   return 
$this;


Solved using:

PHP Code:
    public function show($id null)
    {

        $this->request->setGlobal('request', ['id' => $id]);

        if (!$this->validate([
            'id' => 'required|is_natural_no_zero|max_length[11]',

        ])) return $this->fail($this->validator->getErrors());


        // continue
    

Since withRequest identifies only the getVar that receives $ _REQUEST, I created the variable of this type.

However, an issue addressed in the post above is still open.