@@ -46,19 +46,26 @@ if ($validator->isValid()) {
46
46
###Type Coercion
47
47
If you're validating data passed to your application via HTTP, you can cast strings and booleans to the expected types defined by your schema:
48
48
```
49
+ use JsonSchema\SchemaStorage;
50
+ use JsonSchema\Validator;
51
+ use JsonSchema\Constraints\Factory;
52
+ use JsonSchema\Constraints\Constraint;
53
+
49
54
$request = (object)[
50
55
'processRefund'=>"true",
51
56
'refundAmount'=>"17"
52
57
];
53
58
54
- $validator = new \JsonSchema\Validator(\JsonSchema\Constraints\Constraint::CHECK_MODE_TYPE_CAST | \JsonSchema\Constraints\Constraint::CHECK_MODE_COERCE);
59
+ $factory = new Factory( null, null, Constraint::CHECK_MODE_TYPE_CAST | Constraint::CHECK_MODE_COERCE );
60
+
61
+ $validator = new Validator($factory);
55
62
$validator->check($request, (object) [
56
63
"type"=>"object",
57
- "properties"=>[
58
- "processRefund"=>[
64
+ "properties"=>(object) [
65
+ "processRefund"=>(object) [
59
66
"type"=>"boolean"
60
67
],
61
- "refundAmount"=>[
68
+ "refundAmount"=>(object) [
62
69
"type"=>"number"
63
70
]
64
71
]
@@ -77,6 +84,7 @@ Note that the ```CHECK_MODE_COERCE``` flag will only take effect when an object
77
84
78
85
use JsonSchema\SchemaStorage;
79
86
use JsonSchema\Validator;
87
+ use JsonSchema\Constraints\Factory;
80
88
81
89
$jsonSchema = <<<'JSON'
82
90
{
@@ -114,7 +122,7 @@ $schemaStorage = new SchemaStorage();
114
122
$schemaStorage->addSchema('file://mySchema', $jsonSchemaObject);
115
123
116
124
// Provide $schemaStorage to the Validator so that references can be resolved during validation
117
- $jsonValidator = new Validator(Validator::CHECK_MODE_NORMAL, $schemaStorage);
125
+ $jsonValidator = new Validator( new Factory( $schemaStorage) );
118
126
119
127
// JSON must be decoded before it can be validated
120
128
$jsonToValidateObject = json_decode('{"data":123}');
0 commit comments