You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+57-43
Original file line number
Diff line number
Diff line change
@@ -42,11 +42,14 @@ Set a valid csv file path, pass the CSV delimiter and pass in your validation ru
42
42
```php
43
43
use Oshomo\CsvUtils\Validator\Validator;
44
44
45
-
$validator = new Validator("some/valid/file_path", ",", [
46
-
"name" => ["ascii_only"],
47
-
"uri" => ["url"],
48
-
"stars" => ["between:0,5"]
49
-
]);
45
+
$validator = new Validator(
46
+
"some/valid/file_path",
47
+
[
48
+
"name" => ["ascii_only"],
49
+
"uri" => ["url"],
50
+
"stars" => ["between:0,5"]
51
+
]
52
+
);
50
53
```
51
54
52
55
##### Validating the CSV
@@ -58,9 +61,12 @@ A better implementation:
58
61
```php
59
62
use Oshomo\CsvUtils\Validator\Validator;
60
63
61
-
$validator = new Validator("some/valid/file_path", ",", [
62
-
'title' => ["ascii_only", "url"]
63
-
]);
64
+
$validator = new Validator(
65
+
"some/valid/file_path",
66
+
[
67
+
'title' => ["ascii_only", "url"]
68
+
]
69
+
);
64
70
65
71
if ($validator->fails()) {
66
72
// Do something when validation fails
@@ -77,20 +83,22 @@ You can also customize the error messages for different validation rules and dif
77
83
```php
78
84
use Oshomo\CsvUtils\Validator\Validator;
79
85
80
-
$validator = new Validator("some/valid/file_path", ",", [
81
-
'title' => ["ascii_only", "url"]
82
-
], [
83
-
'ascii_only' => 'The :value supplied for :attribute attribute is invalid on line :line of the CSV.',
84
-
// This specifies a custom message for a given attribute.
85
-
'hotel_link:url' => 'The :attribute must be a valid link. This error occured on line :line of the CSV.',
86
-
]);
86
+
$validator = new Validator(
87
+
"some/valid/file_path",
88
+
['title' => ["ascii_only", "url"]],
89
+
[
90
+
'ascii_only' => 'The :value supplied for :attribute attribute is invalid on line :line of the CSV.',
91
+
// This specifies a custom message for a given attribute.
92
+
'hotel_link:url' => 'The :attribute must be a valid link. This error occured on line :line of the CSV.',
93
+
]
94
+
);
87
95
```
88
96
89
-
In this above example:
97
+
In this above example:
90
98
91
-
The `:attribute` place-holder will be replaced by the actual name of the field under validation.
92
-
The `:value` place-holder will be replaced with value being validated.
93
-
The `:line` place-holder will also be replaced with the row/line number in the CSV in which the error happened.
99
+
The `:attribute` place-holder will be replaced by the actual name of the field under validation.
100
+
The `:value` place-holder will be replaced with value being validated.
101
+
The `:line` place-holder will also be replaced with the row/line number in the CSV in which the error happened.
94
102
95
103
You may also utilize other place-holders in validation messages. For example the `between` rule exposes two other placeholder `min` and `max`. Find more about this in the available rules section
96
104
@@ -100,18 +108,18 @@ You may also utilize other place-holders in validation messages. For example the
100
108
```
101
109
Validates that a cell value is between a :min and :max. The rule exposes the :min and :max placeholder for inline messages
102
110
```
103
-
`ascii_only`:
111
+
`ascii_only`:
104
112
```
105
113
Validates that a cell value does not contain a non-ascii character
106
114
```
107
-
`url`:
115
+
`url`:
108
116
```
109
-
Validates that a cell value is a valid URL. By valid URL we mean
117
+
Validates that a cell value is a valid URL. By valid URL we mean
110
118
111
-
(#protocol)
112
-
(#basic auth)
113
-
(#a domain name or #an IP address or #an IPv6 address)
114
-
(#a port(optional)) then
119
+
(#protocol)
120
+
(#basic auth)
121
+
(#a domain name or #an IP address or #an IPv6 address)
122
+
(#a port(optional)) then
115
123
(#a /, nothing, a / with something, a query or a fragment)
116
124
117
125
```
@@ -125,11 +133,14 @@ use Oshomo\CsvUtils\Validator\Validator;
125
133
use Oshomo\CsvUtils\Converter\JsonConverter;
126
134
use Oshomo\CsvUtils\Converter\XmlConverter;
127
135
128
-
$validator = new Validator('some/valid/file_path', ',', [
129
-
"stars" => ["between:0,5"],
130
-
"name" => ["ascii_only"],
131
-
"uri" => ["url"],
132
-
]);
136
+
$validator = new Validator(
137
+
'some/valid/file_path',
138
+
[
139
+
"stars" => ["between:0,5"],
140
+
"name" => ["ascii_only"],
141
+
"uri" => ["url"],
142
+
]
143
+
);
133
144
134
145
if(!$validator->fails()) {
135
146
$validator->write(new JsonConverter());
@@ -166,9 +177,10 @@ Passing a custom rule to the validator is easy. Create a CustomRule class the im
166
177
```php
167
178
use Oshomo\CsvUtils\Validator\Validator;
168
179
169
-
$validator = new Validator('some/valid/file_path', ',', [
170
-
"name" => ["ascii_only", new UppercaseRule]
171
-
]);
180
+
$validator = new Validator(
181
+
'some/valid/file_path',
182
+
["name" => ["ascii_only", new UppercaseRule]]
183
+
);
172
184
```
173
185
174
186
The class definition for `UppercaseRule`. Follow the same approach if you want to create your own rule.
@@ -193,7 +205,7 @@ class UppercaseRule implements ValidationRuleInterface
193
205
194
206
/**
195
207
* Get the validation error message. Specify the message that should
196
-
* be returned if the validation fails. You can make use of the
208
+
* be returned if the validation fails. You can make use of the
197
209
* :attribute and :value placeholders in the message string
198
210
*
199
211
* @return string
@@ -215,13 +227,15 @@ If you only need the functionality of a custom rule once throughout your applica
215
227
```php
216
228
use Oshomo\CsvUtils\Validator\Validator;
217
229
218
-
$validator = new Validator("some/valid/file_path", ",", [
219
-
"uri" => ["url", function($value, $fail) {
220
-
if (strpos($value, "https://") !== 0) {
221
-
return $fail('The URL passed must be https i.e it must start with https://');
222
-
}
223
-
}]
224
-
]);
230
+
$validator = new Validator(
231
+
"some/valid/file_path",
232
+
[
233
+
"uri" => ["url", function($value, $fail) {
234
+
if (strpos($value, "https://") !== 0) {
235
+
return $fail('The URL passed must be https i.e it must start with https://');
236
+
}
237
+
}]
238
+
]);
225
239
```
226
240
227
241
##### Writing CSV Output Data to Other Formats
@@ -291,7 +305,7 @@ Run `composer test` from the root of the Package.
291
305
### Contributing to this Repo
292
306
293
307
Feel free to submit a pull request for a feature or bug fix. However, do note that before your pull request can be merged it must have test written or updated as the case maybe.
294
-
The project run's automatic checks to make sure that the Symfony code standards are met using [php-cs-fixer](https://symfony.com/doc/current/contributing/code/standards.html).
308
+
The project run's automatic checks to make sure that the Symfony code standards are met using [php-cs-fixer](https://symfony.com/doc/current/contributing/code/standards.html).
295
309
296
310
So, before pushing or making any pull request run the below command:
0 commit comments