23
23
*/
24
24
class SchemaConstraint extends Constraint
25
25
{
26
+ const DEFAULT_SCHEMA_SPEC = 'http://json-schema.org/draft-04/schema# ' ;
27
+
26
28
/**
27
29
* {@inheritdoc}
28
30
*/
@@ -42,13 +44,16 @@ public function check(&$element, $schema = null, JsonPointer $path = null, $i =
42
44
throw new InvalidArgumentException ('no schema found to verify against ' );
43
45
}
44
46
45
- // validate schema against whatever is defined in $validationSchema->$schema
47
+ // validate schema against whatever is defined in $validationSchema->$schema. If no
48
+ // schema is defined, assume self::DEFAULT_SCHEMA_SPEC (currently draft-04).
46
49
if ($ this ->factory ->getConfig (self ::CHECK_MODE_VALIDATE_SCHEMA )) {
47
50
if (!$ this ->getTypeCheck ()->isObject ($ validationSchema )) {
48
51
throw new RuntimeException ('cannot validate non-object schema ' );
49
52
}
50
- if (!$ this ->getTypeCheck ()->propertyExists ($ validationSchema , '$schema ' )) {
51
- throw new RuntimeException ('$schema is not set ' );
53
+ if ($ this ->getTypeCheck ()->propertyExists ($ validationSchema , '$schema ' )) {
54
+ $ schemaSpec = $ this ->getTypeCheck ()->propertyGet ($ validationSchema , '$schema ' );
55
+ } else {
56
+ $ schemaSpec = self ::DEFAULT_SCHEMA_SPEC ;
52
57
}
53
58
54
59
// preload standard schema specs
@@ -58,7 +63,6 @@ public function check(&$element, $schema = null, JsonPointer $path = null, $i =
58
63
'http://json-schema.org/draft-04/schema ' => 'json-schema-draft-04.json ' ,
59
64
'http://json-schema.org/draft-04/schema# ' => 'json-schema-draft-04.json '
60
65
);
61
- $ schemaSpec = $ this ->getTypeCheck ()->propertyGet ($ validationSchema , '$schema ' );
62
66
$ schemaStorage = $ this ->factory ->getSchemaStorage ();
63
67
foreach ($ preload as $ schemaID => $ schemaFile ) {
64
68
$ schemaStorage ->addSchema (
0 commit comments