Skip to content

Commit ee4b0ff

Browse files
author
bruno_p_reis
committed
added the possibility of arrays (associative) be validated ok against object declarations when using cast_type_mode git-svn-id: https://jsonschemaphpv.svn.sourceforge.net/svnroot/jsonschemaphpv/trunk@5 14558f9d-7ea9-46ec-92da-52a2cad6a683
1 parent 5d84741 commit ee4b0ff

File tree

1 file changed

+31
-5
lines changed

1 file changed

+31
-5
lines changed

JsonSchema.php

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ class JsonSchema {
5656
* @return unknown
5757
*/
5858
static public function validate($instance, $schema = null, $formatValidator = null) {
59+
self::$errors = array();
60+
self::$formatValidator = null;
61+
5962
if($formatValidator) self::$formatValidator = $formatValidator;
6063
$res = self::_validate($instance,$schema,false);
6164
self::$formatValidator = null;
@@ -146,6 +149,7 @@ static function checkArray($value,$schema,$path,$i,$_changing) {
146149
}
147150

148151
static function checkProp($value, $schema, $path, $i = '', $_changing = false) {
152+
Dbg::func(1,3);
149153
if (!is_object($schema)) {
150154
return;
151155
}
@@ -199,10 +203,23 @@ static function checkProp($value, $schema, $path, $i = '', $_changing = false) {
199203
}
200204
//verify the itens on an array and min and max number of items.
201205
if(is_array($value)) {
206+
if(
207+
self::$checkMode == self::CHECK_MODE_TYPE_CAST &&
208+
$schema->type == 'object'
209+
) {
210+
self::checkObj(
211+
$value,
212+
$schema->properties,
213+
$path,
214+
isset($schema->additionalProperties) ? $schema->additionalProperties : null,
215+
$_changing
216+
);
217+
}
202218
self::checkArray($value,$schema,$path,$i,$_changing);
203219
}
204220
############ verificar!
205221
elseif(isset($schema->properties) && is_object($value)) {
222+
Dbg::mark('calling checkObj');
206223
self::checkObj(
207224
$value,
208225
$schema->properties,
@@ -294,7 +311,12 @@ static function checkType($type, &$value, $path) {
294311
&& $type == 'integer'
295312
) {
296313
$wrongType = !self::checkTypeCast($type,$value);
297-
} elseif($type !== gettype($value)) {
314+
} elseif (
315+
self::$checkMode == self::CHECK_MODE_TYPE_CAST
316+
&& $type == 'object' && is_array($value)
317+
) {
318+
$wrongType = false;
319+
} elseif ($type !== gettype($value)) {
298320
$wrongType = true;
299321
}
300322
}
@@ -361,7 +383,7 @@ static function checkTypeCast($type,&$value) {
361383

362384
static function checkObj($instance, $objTypeDef, $path, $additionalProp,$_changing) {
363385
if($objTypeDef instanceOf StdClass) {
364-
if( ! ($instance instanceOf StdClass) || is_array($instance) ) {
386+
if( ! (($instance instanceOf StdClass) || is_array($instance)) ) {
365387
self::$errors[] = array(
366388
'property'=>$path,
367389
'message'=>"an object is required"
@@ -370,7 +392,7 @@ static function checkObj($instance, $objTypeDef, $path, $additionalProp,$_changi
370392
foreach($objTypeDef as $i=>$value) {
371393
$value =
372394
array_key_exists($i,$instance) ?
373-
$instance->$i :
395+
(is_array($instance) ? $instance[$i] : $instance->$i) :
374396
new JsonSchemaUndefined();
375397
$propDef = $objTypeDef->$i;
376398
self::checkProp($value,$propDef,$path,$i,$_changing);
@@ -395,7 +417,7 @@ static function checkObj($instance, $objTypeDef, $path, $additionalProp,$_changi
395417
);
396418
}
397419
}
398-
$value = $instance->$i;
420+
$value = is_array($instance) ? $instance[$i] : $instance->$i;
399421

400422
// To verify additional properties types.
401423
if ($objTypeDef && is_object($objTypeDef) && !isset($objTypeDef->$i)) {
@@ -418,6 +440,10 @@ class Dbg {
418440

419441
static public $quietMode = false;
420442

443+
static function includeJqueryJs() {
444+
echo "<script type='text/javascript' src='/js/jquery.js'></script>";
445+
}
446+
421447
static function func($print = true, $numStackSteps = 1) {
422448
$ar = debug_backtrace();
423449
$ret = '';
@@ -440,7 +466,7 @@ static function mark($title,$print = true) {
440466
return $ret;
441467
}
442468

443-
static function object($object,$linkTitle,$varDump = false,$print = true) {
469+
static function object($object,$linkTitle = 'object',$varDump = false,$print = true) {
444470
static $divCount = 0;
445471
$divCount++;
446472
$ar = debug_backtrace();

0 commit comments

Comments
 (0)