How to organize parsing and validation of REST API parameters? - php

I have a rest api that has many parameters via the query string. I am wondering if someone knows of a design pattern or has a nice way of organizing all the parameters (Objects, functions, array, json). Right now I am parsing and validating all my parameters in the same function, very ugly code.
Ideally I would like some way to handle the parameters similar to a database ORM or even a config file/array/json. However, I have tried to come up with a solution to this without any luck.
Any insight would be appreciated!
Example of my thoughts:
<?php
...
$parameters = [
// ?fields=id,name
'fields' => [
'default' => ['id', 'name'],
'valid' => ['id', 'name', 'date],
'type' => 'csv', // list of values (id & name)
'required' => ['id'],
'replace' => ['title' => 'name'], // if the database & api names don't match
'relation' => null, // related database table
],
// ?list=true
'list' => [
'default' => ['false'],
'valid' => ['true', 'false'],
'type' => 'boolean' // single value (true or false)
'required' => [],
'replace' => [], // if the database & api names don't match
'relation' => 'category', // related database table
],
....
];

Seems to me like you are looking for a validation library. My favorite is Symfony's: https://github.com/symfony/validator. I know Zend Framework 2 also has a validation component. I haven't used it personally, but I expect that to be very good too.
Example from the symfony/validator readme:
<?php
use Symfony\Component\Validator\Validation;
use Symfony\Component\Validator\Constraints as Assert;
$validator = Validation::createValidator();
$constraint = new Assert\Collection(array(
'name' => new Assert\Collection(array(
'first_name' => new Assert\Length(array('min' => 101)),
'last_name' => new Assert\Length(array('min' => 1)),
)),
'email' => new Assert\Email(),
'simple' => new Assert\Length(array('min' => 102)),
'gender' => new Assert\Choice(array(3, 4)),
'file' => new Assert\File(),
'password' => new Assert\Length(array('min' => 60)),
));
$input would be $_GET or something obtained with parse_str etc. It is also possible to define the validation rules in some other format, such as YAML.

Related

Yii 2 - Class 'CArrayDataProvider' not found

I'm new at yii and trying to find my way around. I have done a tutorial or two. I then decided to start editing/changing the example to allow me to learn more. I created a page that does a simple PING. That gets validated. On success, it loads a static page. This all works.
What I wanted to do next is to see how I can utilize a grid to populate that with some data. My real life example is the same. I will get a array of data coming in.
It seems that CArrayDataProvider is what I need. So, I am trying to get a very simple example to work. If I get this to work, I can move on.
I have tried a whole bunch of examples. The error is the same every time. It seems that I do not have CArrayDataProvider installed? If that is even possible.
I did a standard basic install:
composer create-project --prefer-dist yiisoft/yii2-app-basic basic
I have the following at the beginning of my controllers file:
use yii2\data\ArrayDataProvider;
I get no error here.
I searched for the file itself on the file system, could not find it. I did find ArrayDataProvider, so I tried that, same result:
use vendor\yiisoft\yii2\data\ArrayDataProvider;
The error is:
PHP Fatal Error – yii\base\ErrorException
Class 'CArrayDataProvider' not found
This is on line 24:
"dataProvider = new CArrayDataProvider($fruits);"
Here is my example code. Not that I think the issue is in here, but to show what I am trying to do:
$fruits = array(
array('id' => 1, 'name'=>'apple', 'color' => 'green'),
array('id' => 2, 'name'=>'orange', 'color' => 'orange'),
array('id' => 3, 'name'=>'banana', 'color' => 'yellow'),
array('id' => 4, 'name'=>'pineapple', 'color' => 'brown')
);
$dataProvider = new CArrayDataProvider($fruits);
$this->widget('zii.widgets.grid.CGridView', array(
'id' => 'fruits-grid',
'dataProvider' => $dataProvider ,
'columns' => array(
array(
'name' => 'ID',
'value' => '$data["id"]',
),
array(
'name' => 'Name',
'value' => '$data["name"]'
),
array(
'name' => 'Color',
'value' => '$data["color"]'
),
)
));
On the file-system (linux) itself I did a update:
composer update
I have been Googling for the last 2 days and I am finding nothing.
I tried adding a date picket. That worked. I used:
https://www.tutorialspoint.com/yii/yii_extensions.htm
So in short, the static page that I call, now displays a DateTimePicker.
At the start of the file I added:
use kartik\datetime\DateTimePicker;
And in the body:
<?php
echo DateTimePicker::widget([
'name' => 'dp_1',
'type' => DateTimePicker::TYPE_INPUT,
'value' => '23-Feb-1982 10:10',
'pluginOptions' => [
'autoclose'=>true,
'format' => 'dd-M-yyyy hh:ii'
]
]);
?>
How do I get yii2 to allow me to use ArrayDataProvider. Or how do I install the extension? Or who do I reference it?
In Yii2 there's not a CArrayDataProvider. Use ArrayDataProvider, like described in docs:
$provider = new yii\data\ArrayDataProvider([
'allModels' => $query->from('post')->all(),
'sort' => [
'attributes' => ['id', 'username', 'email'],
],
'pagination' => [
'pageSize' => 10,
],
]);
Pretty well documented here.
Yii2 supports ArrayDataProvider and yii 1.* supports CArrayDataProvider, so as per your code you are using Yii2, so just replace the below line with
$dataProvider = new CArrayDataProvider($fruits);
With this:
$dataProvider = new ArrayDataProvider($fruits);
Thx to everyone's help! I now have a working example.
In case anyone else has the same issues, I will post my working code here:
use kartik\grid\GridView;
$resultData = [
array("id"=>1,"name"=>"Cyrus","email"=>"risus#consequatdolorvitae.org"),
array("id"=>2,"name"=>"Justin","email"=>"ac.facilisis.facilisis#at.ca"),
array("id"=>3,"name"=>"Mason","email"=>"in.cursus.et#arcuacorci.ca"),
array("id"=>4,"name"=>"Fulton","email"=>"a#faucibusorciluctus.edu"),
array("id"=>5,"name"=>"Neville","email"=>"eleifend#consequatlectus.com"),
array("id"=>6,"name"=>"Jasper","email"=>"lectus.justo#miAliquam.com"),
array("id"=>7,"name"=>"Neville","email"=>"Morbi.non.sapien#dapibusquam.org"),
array("id"=>8,"name"=>"Neville","email"=>"condimentum.eget#egestas.edu"),
array("id"=>9,"name"=>"Ronan","email"=>"orci.adipiscing#interdumligulaeu.com"),
array("id"=>10,"name"=>"Raphael","email"=>"nec.tempus#commodohendrerit.co.uk"),
];
$dataProvider = new \yii\data\ArrayDataProvider([
'key'=>'id',
'allModels' => $resultData,
'sort' => [
'attributes' => ['id', 'name', 'email'],
],
]);
echo GridView::widget([
'dataProvider' => $dataProvider,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'id',
[
'attribute' => 'name',
'value' => 'name',
],
[
"attribute" => "email",
'value' => 'email',
]
]
]);

How can I prevent CakePHP validating fields not in $fieldList?

I use CakePHP2.7.5 and my Model's save function is failing because it is trying to validate fields that are not specified in the $fieldList parameter.
According to the Cookbook, save method takes $fieldList as third parameter to limit the saved fields to those listed in $fieldList.
Model::save(array $data = null, boolean $validate = true, array $fieldList = array())
I call save in the model like this:
$this->save($data, true, ['name', 'place']);
$data is like this:
[ 'ModelName' => [ 'id' => $id, 'name' => 'abcdef', 'place' => 'ghijklmn' ] ]
but it fails because validation errors occur for the required fields that are not presented in $data. Is it supposed to work in this way?
Do I have to put some dummy data in $data for the required fields?
You probably need to set required to false in the validation rule:
public $validate = array(
'description' => array(
'alphaNumeric' => array(
'rule' => 'alphaNumeric',
'required' => false,
'message' => 'You have to enter a valid description'
)
);
}
CakePHP 2.x Validation: 'required'
I found the answer myself. In Cake's Model::save function, the second parameter $validate could be an array, and this problem occurs only when it is an array. (so, my example in the question above was not accurate.)
So, this one should work as intended (validates and saves 'name' and 'place' only),
$this->save($data, true, ['name', 'place']);
but this one doesn't,
$this->save($data, ['validate' => true], ['name', 'place']);
and actually I was doing like this. (because I had to make 'atomic' false and this is how to do it.)
$this->save($data, ['validate' => true, 'atomic' => false], ['name', 'place']);
In my case, this is the solution.
$this->save($data, ['validate' => true, 'atomic' => false, 'fieldList' => ['name', 'place']);

ZF2 apigility - How can we validate collections in json data

How can I get validated json value using Apigility. For example, I need to get validated user_id under users collection in the following json data.
{
"log_type": "split food",
"meal_type": "Break Fast",
"meal_date": "12-2-2015",
"users": [
{
"user_id": 1,
"food_details": [
{
"food_id":101
}
]
}
]
}
I know fields can be validated through apigility but here is from json.
Thank you
You should look into the documentation of ZF2 validation for validating (form) collections. Some documentation on this can be found here.
You should set the type field like this:
'type' => 'Zend\InputFilter\CollectionInputFilter',
for validation of nested objects (or form field sets) you need to set the type field as follows:
'type' => 'Zend\InputFilter\InputFilter'
You use it like this:
'input_filter' => array(
'log_type' => array(
'validators' => array(
// ... validators ...
),
'filters' => array(
// ... filters ...
),
),
'meal_type' => array(
'validators' => array(
// ... validators ...
),
'filters' => array(
// ... filters ...
),
),
'meal_date' => array(
'validators' => array(
// ... validators ...
),
'filters' => array(
// ... filters ...
),
),
'users' => array(
'required' => true,
'count' => ... optional count ...
'input_filter' => ... input filter or input filter config to use for each element ...
'type' => 'Zend\InputFilter\CollectionInputFilter',
),
'some_complex_element' => array(
'property_of_complex_element' => array(
'name' => 'property_of_complex_element',
'required' => false,
'validators' => array(
// ... validators ...
),
'filters' => array(
// ... filters ...
),
),
'type' => 'Zend\InputFilter\InputFilter',
)
),
An example on how to use this can be found here on stackoverflow
To achieve what you want you most likely have to combine those two solutions. Not sure if it is the easiest way to do it, but it is definitely possible!
EDIT
For people who haven't setup validation at all yet:
For content validation in Apigility You have to use the zfcampus/zf-content-validation module and follow the documentation for configuration. This module allows you to configure your input-filters and validators in a input_filter_spec like you would normally do for form validation in ZF2. Here inside these input-filter config arrays you can use the configs that I referenced above.
So first properly install that module and once set-up you will be able to use these validation types in Apigility.

Nested Parameters/Values in POST Requests

I have been thinking about a good way to handle nested/complex values in POST requests to a apigility resource.
For example, an order might contain a collection of order items in a single POST requested that is used to create an order. Both, order and order-item do exist as a resource. However, I would very much like to have only one request that would create order and order item entities. Handling that in the resource is not a problem, but I wonder how you would configure that resource (let´s call it order-place) using the apigiliy UI - or, if at all impossible, using the configuration. Applying validators and filters is one of the key features of apigility, and i´d like to keep using that, even for complex request data.
And before you ask, using an underscore to separate the values scopes, for example order_comment and order_item_comment should not be an option.
Any ideas?:)
Addition: A sample json request payload could look like this:
{
"created_at": "2000-01-01",
"amount" : "5000.00",
"address" : {
"name": "some name",
"street": "some street"
...
},
"items" : [
{"productId":99,"qty":1}
...
]
}
Starting from Wilt's answer, I've found that the following code works as well:
# file path: /module/MyApi/config/module.config.php
// some other stuff
'MyApi\\V1\\Rest\\MyRestService\\Validator' => array(
'address' => array(
0 => array(
'name' => 'name',
'required' => true,
'filters' => array(),
'validators' => array(),
),
1 => array(
'name' => 'street',
'required' => true,
'filters' => array(),
'validators' => array(),
),
'type' => 'Zend\InputFilter\InputFilter'
),
'amount' => array(
'name' => 'amount',
'required' => true,
'filters' => array(),
'validators' => array()
)
The only problem I get is when address is passed as a field (string or numeric) rather then an array or object. In this case Apigility throws an exception:
Zend\InputFilter\Exception\InvalidArgumentException: Zend\InputFilter\BaseInputFilter::setData expects an array or Traversable argument; received string in /var/www/api/vendor/zendframework/zendframework/library/Zend/InputFilter/BaseInputFilter.php on line 175
Adding address as a further simple (required) field avoids the exception, but then Apigility doesn't see any difference whether we pass address as an array of name and street or a dummy string.
If you are using the ContentValidation module then you can configure an input filter for the nested resources by assigning it to a variable. Then you have to add a type key (essential otherwise reusing the filter won't work). Now you are able to use this variable in your input_filter_specs and you can reuse the whole filter inside another filter. So something like this in your config.php:
<?php
namespace Application;
// Your address config as if it was used independently
$addressInputFilter => array(
'name' => array(
'name' => 'name',
'required' => true,
'filters' => array(
//...
)
'validators' => array(
//...
)
),
'street' => array(
'name' => 'street',
'required' => true,
'filters' => array(
//...
)
'validators' => array(
//...
)
),
// 'type' key necessary for reusing this input filter in other configs
'type' => 'Zend\InputFilter\InputFilter'
),
'input_filter_specs' => array(
// The key for your address if you also want to be able to use it independently
'Application\InputFilter\Address'=> $addressInputFilter,
// The key and config for your other resource containing a nested address
'Application\InputFilter\ItemContainingAddress'=> array(
'address' => $addressInputFilter,
'amount' => array(
'name' => 'amount',
'required' => true,
'filters' => array(
//...
),
'validators' => array(
//...
)
)
//... your other fields
)
)

how to validate a registration form in zend framework 2?

i am trying to validate a user registration form in Zend Framework 2.
More specifically how to validate the email, ZF1 i could do:
$email->setValidators( array(new Zend_Validate_EmailAddress()) );
I'm wondering if i can just call something similar like this.
Also I'm wondering how to validate two fields that need to be the same like the password field and the password verification.
I guess that when i say if($form->isValid()).. this will check the getInputFilter() method for all validation.
I've been taking a look at ZfcUser module but, right now, i can't understand much since i don't have a full grasp on how ZF2 works
Any ideas, maybe a simple example?
thanks
Have you read the official tutorial to see how the new ZF2 Form component works?
At a very high level, you need a Form object and a Filter object working together. The Filter object is where you place your filters and validators. However, if you use a form element of type EmailAddress in your Form, then it will automatically add the correct validator. There is more information in the manual.
I recently did a webinar on forms for Zend which you should be able to find on this page.
i've figure it out.
the validators are multidimensional arrays and each array has a name and some options. It might be a bit wired in the beginning to notice it, but much configuration in zf2 is in this way
see an example for the password:
$inputFilter->add($factory->createInput([
'name' => 'password',
'required' => true,
'filters' => [ ['name' => 'StringTrim'], ],
'validators' => [
[
'name' => 'StringLength',
'options' => [
'encoding' => 'UTF-8',
'min' => 6,
'max' => 128,
],
],
],
]));
$inputFilter->add($factory->createInput([
'name' => 'password_verify',
'required' => true,
'filters' => [ ['name' => 'StringTrim'], ],
'validators' => [
array(
'name' => 'StringLength',
'options' => array( 'min' => 6 ),
),
array(
'name' => 'identical',
'options' => array('token' => 'password' )
),
],
]));
note, in php 5.3 > an array could be written like array() or [], in the above example i mix them up for no particular reason.

Categories