Skip to content

Commit 45ea6eb

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

File tree

5 files changed

+67
-42
lines changed

5 files changed

+67
-42
lines changed

src/JsonSchema/Constraints/Constraint.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -147,13 +147,13 @@ protected function checkArray(&$value, $schema = null, JsonPointer $path = null,
147147
* @param mixed $patternProperties
148148
* @param boolean $coerce
149149
*/
150-
protected function checkObject(&$value, $schema = null, JsonPointer $path = null, $i = null, $patternProperties = null, $coerce = false)
150+
protected function checkObject(&$value, $schema = null, JsonPointer $path = null, $i = null, $patternProperties = null, $coerce = false, $default = false)
151151
{
152152
$validator = $this->factory->createInstanceFor('object');
153153
if($coerce){
154-
$validator->coerce($value, $schema, $path, $i, $patternProperties);
154+
$validator->coerce($value, $schema, $path, $i, $patternProperties, $default);
155155
} else {
156-
$validator->check($value, $schema, $path, $i, $patternProperties);
156+
$validator->check($value, $schema, $path, $i, $patternProperties, $default);
157157
}
158158

159159
$this->addErrors($validator->getErrors());
@@ -189,14 +189,14 @@ protected function checkType(&$value, $schema = null, JsonPointer $path = null,
189189
* @param mixed $i
190190
* @param boolean $coerce
191191
*/
192-
protected function checkUndefined(&$value, $schema = null, JsonPointer $path = null, $i = null, $coerce = false)
192+
protected function checkUndefined(&$value, $schema = null, JsonPointer $path = null, $i = null, $coerce = false, $default = false)
193193
{
194194
$validator = $this->factory->createInstanceFor('undefined');
195195

196196
if($coerce){
197-
$validator->coerce($value, $this->factory->getSchemaStorage()->resolveRefSchema($schema), $path, $i);
197+
$validator->coerce($value, $this->factory->getSchemaStorage()->resolveRefSchema($schema), $path, $i, $default);
198198
} else {
199-
$validator->check($value, $this->factory->getSchemaStorage()->resolveRefSchema($schema), $path, $i);
199+
$validator->check($value, $this->factory->getSchemaStorage()->resolveRefSchema($schema), $path, $i, $default);
200200
}
201201

202202
$this->addErrors($validator->getErrors());

src/JsonSchema/Constraints/ObjectConstraint.php

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,20 @@ class ObjectConstraint extends Constraint
2222
/**
2323
* {@inheritDoc}
2424
*/
25-
public function check($element, $definition = null, JsonPointer $path = null, $additionalProp = null, $patternProperties = null)
25+
public function check($element, $definition = null, JsonPointer $path = null, $additionalProp = null, $patternProperties = null, $default = false)
2626
{
27-
$this->_check($element, $definition, $path, $additionalProp, $patternProperties);
27+
$this->_check($element, $definition, $path, $additionalProp, $patternProperties, false, $default);
2828
}
2929

3030
/**
3131
* {@inheritDoc}
3232
*/
33-
public function coerce(&$element, $definition = null, JsonPointer $path = null, $additionalProp = null, $patternProperties = null)
33+
public function coerce(&$element, $definition = null, JsonPointer $path = null, $additionalProp = null, $patternProperties = null, $default = false)
3434
{
35-
$this->_check($element, $definition, $path, $additionalProp, $patternProperties, true);
35+
$this->_check($element, $definition, $path, $additionalProp, $patternProperties, true, $default);
3636
}
3737

38-
protected function _check(&$element, $definition = null, JsonPointer $path = null, $additionalProp = null, $patternProperties = null, $coerce = false)
38+
protected function _check(&$element, $definition = null, JsonPointer $path = null, $additionalProp = null, $patternProperties = null, $coerce = false, $default = false)
3939
{
4040
if ($element instanceof UndefinedConstraint) {
4141
return;
@@ -48,7 +48,7 @@ protected function _check(&$element, $definition = null, JsonPointer $path = nul
4848

4949
if ($definition) {
5050
// validate the definition properties
51-
$this->validateDefinition($element, $definition, $path, $coerce);
51+
$this->validateDefinition($element, $definition, $path, $coerce, $default);
5252
}
5353

5454
// additional the element properties
@@ -134,7 +134,7 @@ public function validateElement($element, $matches, $objectDefinition = null, Js
134134
* @param JsonPointer|null $path Path?
135135
* @param boolean $coerce Whether to coerce strings to expected types or not
136136
*/
137-
public function validateDefinition(&$element, $objectDefinition = null, JsonPointer $path = null, $coerce = false)
137+
public function validateDefinition(&$element, $objectDefinition = null, JsonPointer $path = null, $coerce = false, $default = false)
138138
{
139139
$undefinedConstraint = $this->factory->createInstanceFor('undefined');
140140

@@ -143,8 +143,13 @@ 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 ($default && isset($definition->default) && $property === $undefinedConstraint) {
148+
$property = &$this->setProperty($element, $i, $definition->default);
149+
}
150+
146151
// Undefined constraint will check for is_object() and quit if is not - so why pass it?
147-
$this->checkUndefined($property, $definition, $path, $i, $coerce);
152+
$this->checkUndefined($property, $definition, $path, $i, $coerce, $default);
148153
}
149154
}
150155
}
@@ -169,6 +174,26 @@ protected function &getProperty(&$element, $property, $fallback = null)
169174
return $fallback;
170175
}
171176

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

src/JsonSchema/Constraints/SchemaConstraint.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,15 @@ class SchemaConstraint extends Constraint
2323
/**
2424
* {@inheritDoc}
2525
*/
26-
public function check($element, $schema = null, JsonPointer $path = null, $i = null)
26+
public function check($element, $schema = null, JsonPointer $path = null, $i = null, $default = false)
2727
{
28-
$this->_check($element, $schema, $path, $i);
28+
$this->_check($element, $schema, $path, $i, false, $default);
2929
}
3030

31-
protected function _check(&$element, $schema = null, JsonPointer $path = null, $i = null, $coerce = false){
31+
protected function _check(&$element, $schema = null, JsonPointer $path = null, $i = null, $coerce = false, $default = false){
3232
if ($schema !== null) {
3333
// passed schema
34-
$this->checkUndefined($element, $schema, $path, $i, $coerce);
34+
$this->checkUndefined($element, $schema, $path, $i, $coerce, $default);
3535
} elseif ($this->getTypeCheck()->propertyExists($element, $this->inlineSchemaProperty)) {
3636
$inlineSchema = $this->getTypeCheck()->propertyGet($element, $this->inlineSchemaProperty);
3737
if (is_array($inlineSchema)) {
@@ -44,8 +44,8 @@ protected function _check(&$element, $schema = null, JsonPointer $path = null, $
4444
}
4545
}
4646

47-
public function coerce(&$element, $schema = null, JsonPointer $path = null, $i = null)
47+
public function coerce(&$element, $schema = null, JsonPointer $path = null, $i = null, $default = false)
4848
{
49-
$this->_check($element, $schema, $path, $i, true);
49+
$this->_check($element, $schema, $path, $i, true, $default);
5050
}
5151
}

src/JsonSchema/Constraints/UndefinedConstraint.php

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,15 @@ class UndefinedConstraint extends Constraint
2323
/**
2424
* {@inheritDoc}
2525
*/
26-
public function check($value, $schema = null, JsonPointer $path = null, $i = null){
27-
$this->_check($value, $schema, $path, $i);
26+
public function check($value, $schema = null, JsonPointer $path = null, $i = null, $default = false){
27+
$this->_check($value, $schema, $path, $i, false, $default);
2828
}
2929

30-
public function coerce(&$value, $schema = null, JsonPointer $path = null, $i = null){
31-
$this->_check($value, $schema, $path, $i, true);
30+
public function coerce(&$value, $schema = null, JsonPointer $path = null, $i = null, $default = false){
31+
$this->_check($value, $schema, $path, $i, true, $default);
3232
}
3333

34-
protected function _check(&$value, $schema = null, JsonPointer $path = null, $i = null, $coerce = false)
34+
protected function _check(&$value, $schema = null, JsonPointer $path = null, $i = null, $coerce = false, $default = false)
3535
{
3636
if (is_null($schema) || !is_object($schema)) {
3737
return;
@@ -40,13 +40,13 @@ protected function _check(&$value, $schema = null, JsonPointer $path = null, $i
4040
$path = $this->incrementPath($path ?: new JsonPointer(''), $i);
4141

4242
// check special properties
43-
$this->validateCommonProperties($value, $schema, $path, $i, $coerce);
43+
$this->validateCommonProperties($value, $schema, $path, $i, $coerce, $default);
4444

4545
// check allOf, anyOf, and oneOf properties
46-
$this->validateOfProperties($value, $schema, $path, '', $coerce);
46+
$this->validateOfProperties($value, $schema, $path, '', $coerce, $default);
4747

4848
// check known types
49-
$this->validateTypes($value, $schema, $path, $i, $coerce);
49+
$this->validateTypes($value, $schema, $path, $i, $coerce, $default);
5050
}
5151

5252
/**
@@ -58,7 +58,7 @@ protected function _check(&$value, $schema = null, JsonPointer $path = null, $i
5858
* @param string $i
5959
* @param boolean $coerce
6060
*/
61-
public function validateTypes(&$value, $schema = null, JsonPointer $path, $i = null, $coerce = false)
61+
public function validateTypes(&$value, $schema = null, JsonPointer $path, $i = null, $coerce = false, $default = false)
6262
{
6363
// check array
6464
if ($this->getTypeCheck()->isArray($value)) {
@@ -73,7 +73,7 @@ public function validateTypes(&$value, $schema = null, JsonPointer $path, $i = n
7373
$path,
7474
isset($schema->additionalProperties) ? $schema->additionalProperties : null,
7575
isset($schema->patternProperties) ? $schema->patternProperties : null,
76-
$coerce
76+
$coerce, $default
7777
);
7878
}
7979

@@ -102,7 +102,7 @@ public function validateTypes(&$value, $schema = null, JsonPointer $path, $i = n
102102
* @param string $i
103103
* @param boolean $coerce
104104
*/
105-
protected function validateCommonProperties(&$value, $schema = null, JsonPointer $path, $i = "", $coerce=false)
105+
protected function validateCommonProperties(&$value, $schema = null, JsonPointer $path, $i = "", $coerce=false, $default = false)
106106
{
107107
// if it extends another schema, it must pass that schema as well
108108
if (isset($schema->extends)) {
@@ -111,10 +111,10 @@ protected function validateCommonProperties(&$value, $schema = null, JsonPointer
111111
}
112112
if (is_array($schema->extends)) {
113113
foreach ($schema->extends as $extends) {
114-
$this->checkUndefined($value, $extends, $path, $i, $coerce);
114+
$this->checkUndefined($value, $extends, $path, $i, $coerce, $default);
115115
}
116116
} else {
117-
$this->checkUndefined($value, $schema->extends, $path, $i, $coerce);
117+
$this->checkUndefined($value, $schema->extends, $path, $i, $coerce, $default);
118118
}
119119
}
120120

@@ -162,7 +162,7 @@ protected function validateCommonProperties(&$value, $schema = null, JsonPointer
162162

163163
if (isset($schema->not)) {
164164
$initErrors = $this->getErrors();
165-
$this->checkUndefined($value, $schema->not, $path, $i, $coerce);
165+
$this->checkUndefined($value, $schema->not, $path, $i, $coerce, $default);
166166

167167
// if no new errors were raised then the instance validated against the "not" schema
168168
if (count($this->getErrors()) == count($initErrors)) {
@@ -187,7 +187,7 @@ protected function validateCommonProperties(&$value, $schema = null, JsonPointer
187187
* @param string $i
188188
* @param boolean $coerce
189189
*/
190-
protected function validateOfProperties(&$value, $schema, JsonPointer $path, $i = "", $coerce = false)
190+
protected function validateOfProperties(&$value, $schema, JsonPointer $path, $i = "", $coerce = false, $default = false)
191191
{
192192
// Verify type
193193
if ($value instanceof UndefinedConstraint) {
@@ -198,7 +198,7 @@ protected function validateOfProperties(&$value, $schema, JsonPointer $path, $i
198198
$isValid = true;
199199
foreach ($schema->allOf as $allOf) {
200200
$initErrors = $this->getErrors();
201-
$this->checkUndefined($value, $allOf, $path, $i, $coerce);
201+
$this->checkUndefined($value, $allOf, $path, $i, $coerce, $default);
202202
$isValid = $isValid && (count($this->getErrors()) == count($initErrors));
203203
}
204204
if (!$isValid) {
@@ -211,7 +211,7 @@ protected function validateOfProperties(&$value, $schema, JsonPointer $path, $i
211211
$startErrors = $this->getErrors();
212212
foreach ($schema->anyOf as $anyOf) {
213213
$initErrors = $this->getErrors();
214-
$this->checkUndefined($value, $anyOf, $path, $i, $coerce);
214+
$this->checkUndefined($value, $anyOf, $path, $i, $coerce, $default);
215215
if ($isValid = (count($this->getErrors()) == count($initErrors))) {
216216
break;
217217
}
@@ -229,7 +229,7 @@ protected function validateOfProperties(&$value, $schema, JsonPointer $path, $i
229229
$startErrors = $this->getErrors();
230230
foreach ($schema->oneOf as $oneOf) {
231231
$this->errors = array();
232-
$this->checkUndefined($value, $oneOf, $path, $i, $coerce);
232+
$this->checkUndefined($value, $oneOf, $path, $i, $coerce, $default);
233233
if (count($this->getErrors()) == 0) {
234234
$matchedSchemas++;
235235
}

src/JsonSchema/Validator.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,10 @@ class Validator extends Constraint
3030
*
3131
* {@inheritDoc}
3232
*/
33-
public function check($value, $schema = null, JsonPointer $path = null, $i = null)
33+
public function check($value, $schema = null, JsonPointer $path = null, $i = null, $default = false)
3434
{
3535
$validator = $this->factory->createInstanceFor('schema');
36-
$validator->check($value, $schema);
36+
$validator->check($value, $schema, $path, $i, $default);
3737

3838
$this->addErrors(array_unique($validator->getErrors(), SORT_REGULAR));
3939
}
@@ -45,10 +45,10 @@ public function check($value, $schema = null, JsonPointer $path = null, $i = nul
4545
*
4646
* {@inheritDoc}
4747
*/
48-
public function coerce(&$value, $schema = null, JsonPointer $path = null, $i = null)
48+
public function coerce(&$value, $schema = null, JsonPointer $path = null, $i = null, $default = false)
4949
{
5050
$validator = $this->factory->createInstanceFor('schema');
51-
$validator->coerce($value, $schema);
51+
$validator->coerce($value, $schema, $path, $i, $default);
5252

5353
$this->addErrors(array_unique($validator->getErrors(), SORT_REGULAR));
5454
}

0 commit comments

Comments
 (0)