Skip to content

Commit 1081a70

Browse files
shmaxbighappyface
authored andcommitted
update readme (#326)
1 parent 43158d3 commit 1081a70

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

README.md

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,19 +46,26 @@ if ($validator->isValid()) {
4646
###Type Coercion
4747
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:
4848
```
49+
use JsonSchema\SchemaStorage;
50+
use JsonSchema\Validator;
51+
use JsonSchema\Constraints\Factory;
52+
use JsonSchema\Constraints\Constraint;
53+
4954
$request = (object)[
5055
'processRefund'=>"true",
5156
'refundAmount'=>"17"
5257
];
5358
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);
5562
$validator->check($request, (object) [
5663
"type"=>"object",
57-
"properties"=>[
58-
"processRefund"=>[
64+
"properties"=>(object)[
65+
"processRefund"=>(object)[
5966
"type"=>"boolean"
6067
],
61-
"refundAmount"=>[
68+
"refundAmount"=>(object)[
6269
"type"=>"number"
6370
]
6471
]
@@ -77,6 +84,7 @@ Note that the ```CHECK_MODE_COERCE``` flag will only take effect when an object
7784

7885
use JsonSchema\SchemaStorage;
7986
use JsonSchema\Validator;
87+
use JsonSchema\Constraints\Factory;
8088

8189
$jsonSchema = <<<'JSON'
8290
{
@@ -114,7 +122,7 @@ $schemaStorage = new SchemaStorage();
114122
$schemaStorage->addSchema('file://mySchema', $jsonSchemaObject);
115123

116124
// 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));
118126

119127
// JSON must be decoded before it can be validated
120128
$jsonToValidateObject = json_decode('{"data":123}');

0 commit comments

Comments
 (0)