Skip to content

Commit a615f91

Browse files
authored
Merge pull request #11 from swaggest/skip-validation
Skip validation
2 parents 8f1f270 + 6f78824 commit a615f91

33 files changed

+3082
-83
lines changed

README.md

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -270,11 +270,13 @@ $properties->dateTime = Schema::string()->meta(new FieldName('date_time'));
270270

271271
```php
272272
$mapper = new NameMapper();
273+
$options = new Context();
274+
$options->dataPreProcessor = $mapper;
273275

274276
$order = new Order();
275277
$order->id = 1;
276278
$order->dateTime = '2015-10-28T07:28:00Z';
277-
$exported = Order::export($order, $mapper);
279+
$exported = Order::export($order, $options);
278280
$json = <<<JSON
279281
{
280282
"id": 1,
@@ -283,7 +285,7 @@ $json = <<<JSON
283285
JSON;
284286
$this->assertSame($json, json_encode($exported, JSON_PRETTY_PRINT));
285287

286-
$imported = Order::import(json_decode($json), $mapper);
288+
$imported = Order::import(json_decode($json), $options);
287289
$this->assertSame('2015-10-28T07:28:00Z', $imported->dateTime);
288290
```
289291

@@ -305,4 +307,21 @@ And get back.
305307
// Retrieving meta
306308
$myMeta = FieldName::get($schema);
307309
$this->assertSame('my-value', $myMeta->name);
310+
```
311+
312+
313+
#### Mapping without validation
314+
315+
If you want to tolerate invalid data or improve mapping performance you can specify `skipValidation` flag in processing `Context`
316+
317+
```
318+
$schema = Schema::object();
319+
$schema->setProperty('one', Schema::integer());
320+
$schema->properties->one->minimum = 5;
321+
322+
$options = new Context();
323+
$options->skipValidation = true;
324+
325+
$res = $schema->in(json_decode('{"one":4}'), $options);
326+
$this->assertSame(4, $res->one);
308327
```

src/Context.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ class Context extends MagicMap
1919
/** @var \SplObjectStorage */
2020
public $circularReferences;
2121

22+
public $skipValidation = false;
23+
2224
/**
2325
* ProcessingOptions constructor.
2426
* @param RemoteRefProvider $remoteRefProvider

src/Helper.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,18 @@ public static function resolveURI($parent, $current)
6666
}
6767

6868

69+
public static function padLines($with, $text, $skipFirst = true)
70+
{
71+
$lines = explode("\n", $text);
72+
foreach ($lines as $index => $line) {
73+
if ($skipFirst && !$index) {
74+
continue;
75+
}
76+
if ($line) {
77+
$lines[$index] = $with . $line;
78+
}
79+
}
80+
return implode("\n", $lines);
81+
}
82+
6983
}

0 commit comments

Comments
 (0)