@@ -33,7 +33,7 @@ $data = json_decode(file_get_contents('data.json'));
3333
3434// Validate
3535$validator = new JsonSchema\Validator;
36- $validator->check ($data, (object)['$ref' => 'file://' . realpath('schema.json')]);
36+ $validator->validate ($data, (object)['$ref' => 'file://' . realpath('schema.json')]);
3737
3838if ($validator->isValid()) {
3939 echo "The supplied JSON validates against the schema.\n";
@@ -63,22 +63,31 @@ $request = (object)[
6363 'refundAmount'=>"17"
6464];
6565
66- $validator->coerce($request, (object) [
66+ $validator->validate(
67+ $request, (object) [
6768 "type"=>"object",
68- "properties"=>(object)[
69- "processRefund"=>(object)[
70- "type"=>"boolean"
71- ],
72- "refundAmount"=>(object)[
73- "type"=>"number"
69+ "properties"=>(object)[
70+ "processRefund"=>(object)[
71+ "type"=>"boolean"
72+ ],
73+ "refundAmount"=>(object)[
74+ "type"=>"number"
75+ ]
7476 ]
75- ]
76- ]); // validates!
77+ ],
78+ Constraint::CHECK_MODE_COERCE_TYPES
79+ ); // validates!
7780
7881is_bool($request->processRefund); // true
7982is_int($request->refundAmount); // true
8083```
8184
85+ A shorthand method is also available:
86+ ``` PHP
87+ $validator->coerce($request, $schema);
88+ // equivalent to $validator->validate($data, $schema, Constraint::CHECK_MODE_COERCE_TYPES);
89+ ```
90+
8291### With inline references
8392
8493``` php
@@ -130,9 +139,23 @@ $jsonValidator = new Validator( new Factory($schemaStorage));
130139$jsonToValidateObject = json_decode('{"data":123}');
131140
132141// Do validation (use isValid() and getErrors() to check the result)
133- $jsonValidator->check ($jsonToValidateObject, $jsonSchemaObject);
142+ $jsonValidator->validate ($jsonToValidateObject, $jsonSchemaObject);
134143```
135144
145+ ### Configuration Options
146+ A number of flags are available to alter the behavior of the validator. These can be passed as the
147+ third argument to ` Validator::validate() ` , or can be provided as the third argument to
148+ ` Factory::__construct() ` if you wish to persist them across multiple ` validate() ` calls.
149+
150+ | Flag | Description |
151+ | ------| -------------|
152+ | ` Constraint::CHECK_MODE_NORMAL ` | Validate in 'normal' mode - this is the default |
153+ | ` Constraint::CHECK_MODE_TYPE_CAST ` | Enable fuzzy type checking for associative arrays and objects |
154+ | ` Constraint::CHECK_MODE_COERCE_TYPES ` | Convert data types to match the schema where possible |
155+ | ` Constraint::CHECK_MODE_EXCEPTIONS ` | Throw an exception immediately if validation fails |
156+
157+ Please note that using ` Constraint::CHECK_MODE_COERCE_TYPES ` will modify your original data.
158+
136159## Running the tests
137160
138161``` bash
0 commit comments