Skip to content

Commit 36cf891

Browse files
committed
Add option to apply default values from the schema
1 parent 325a0f8 commit 36cf891

File tree

3 files changed

+54
-0
lines changed

3 files changed

+54
-0
lines changed

README.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,37 @@ is_bool($request->processRefund); // true
7979
is_int($request->refundAmount); // true
8080
```
8181

82+
### Default Values
83+
84+
If your schema contains default values, you can have these automatically applied during validation:
85+
86+
```php
87+
<?php
88+
89+
use JsonSchema\Validator;
90+
use JsonSchema\Constraints\Factory;
91+
use JsonSchema\Constraints\Constraint;
92+
93+
$request = (object)[
94+
'refundAmount'=>17
95+
];
96+
97+
$validator = new Validator(new Factory(null, null, Constraint::CHECK_MODE_APPLY_DEFAULTS));
98+
99+
$validator->check($request, (object)[
100+
"type"=>"object",
101+
"properties"=>(object)[
102+
"processRefund"=>(object)[
103+
"type"=>"boolean",
104+
"default"=>true
105+
]
106+
]
107+
]); //validates, and sets defaults for missing properties
108+
109+
is_bool($request->processRefund); // true
110+
$request->processRefund; // true
111+
```
112+
82113
### With inline references
83114

84115
```php

src/JsonSchema/Constraints/Constraint.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ abstract class Constraint implements ConstraintInterface
2727

2828
const CHECK_MODE_NORMAL = 0x00000001;
2929
const CHECK_MODE_TYPE_CAST = 0x00000002;
30+
const CHECK_MODE_APPLY_DEFAULTS = 0x00000004;
3031

3132
/**
3233
* @var Factory

src/JsonSchema/Constraints/ObjectConstraint.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,12 @@ public function validateDefinition(&$element, $objectDefinition = null, JsonPoin
143143
$definition = $this->getProperty($objectDefinition, $i);
144144

145145
if (is_object($definition)) {
146+
// Set default values
147+
if ($this->factory->getCheckMode() & self::CHECK_MODE_APPLY_DEFAULTS && isset($definition->default) && $property === $undefinedConstraint) {
148+
$this->setProperty($element, $i, $definition->default);
149+
$property = &$this->getProperty($element, $i, $undefinedConstraint);
150+
}
151+
146152
// Undefined constraint will check for is_object() and quit if is not - so why pass it?
147153
$this->checkUndefined($property, $definition, $path, $i, $coerce);
148154
}
@@ -169,6 +175,22 @@ protected function &getProperty(&$element, $property, $fallback = null)
169175
return $fallback;
170176
}
171177

178+
/**
179+
* set and return a property on an object or array
180+
*
181+
* @param mixed $element Element to manipulate
182+
* @param string $property Property to set
183+
* @param mixed $value Value to set
184+
*/
185+
protected function setProperty(&$element, $property, $value)
186+
{
187+
if (is_array($element)) {
188+
$element[$property] = $value;
189+
} elseif (is_object($element)) {
190+
$element->$property = $value;
191+
}
192+
}
193+
172194
/**
173195
* validating minimum and maximum property constraints (if present) against an element
174196
*

0 commit comments

Comments
 (0)