Skip to content

Commit b88ee4e

Browse files
docs: Add documentation on how to use strict mode (#1)
1 parent 2eb6924 commit b88ee4e

File tree

2 files changed

+49
-13
lines changed

2 files changed

+49
-13
lines changed

_docs/advanced-topics.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,40 @@ This paragraph needs to be written, want to help out? Checkout GitHub repo!
6868

6969
## Using custom error messages
7070
This paragraph needs to be written, want to help out? Checkout GitHub repo!
71+
72+
# Validating using strict mode (Draft 6 only)
73+
```php
74+
<?php
75+
76+
$data = json_decode(file_get_contents('data.json'));
77+
$jsonSchemaAsString = <<<'JSON'
78+
{
79+
"$schema": "http://json-schema.org/draft-06/schema#",
80+
"$id": "https://example.com/schemas/user.json",
81+
"type": "object",
82+
"properties": {
83+
"name": { "type": "string" },
84+
"email": { "type": "string" }
85+
},
86+
"required": ["name", "email"]
87+
}
88+
JSON;
89+
90+
$jsonSchema = json_decode($jsonSchemaAsString);
91+
$schemaStorage = new JsonSchema\SchemaStorage();
92+
$schemaStorage->addSchema($jsonSchema->$id, $jsonSchema);
93+
$validator = new JsonSchema\Validator(
94+
new JsonSchema\Constraints\Factory($schemaStorage)
95+
);
96+
$checkMode = Constraint::CHECK_MODE_NORMAL | Constraint::CHECK_MODE_STRICT;
97+
$validator->validate($data, $jsonSchemaObject, $checkMode);
98+
99+
if ($validator->isValid()) {
100+
echo "The supplied JSON validates against the schema.\n";
101+
} else {
102+
echo "JSON does not validate. Violations:\n";
103+
foreach ($validator->getErrors() as $error) {
104+
printf("[%s] %s\n", $error['property'], $error['message']);
105+
}
106+
}
107+
```

_docs/check-mode.md

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,16 @@ $validator->validate(
2929
);
3030
```
3131

32-
33-
3432
## Available flags
35-
| Flag | | Value | Description |
36-
|-------------------------------------------------|:--|--------------|---------------------------------------------------------------|
37-
| `Constraint::CHECK_MODE_NORMAL` | | `0x00000001` | Validate in 'normal' mode - this is the default |
38-
| `Constraint::CHECK_MODE_TYPE_CAST` | | `0x00000002` | Enable fuzzy type checking for associative arrays and objects |
39-
| `Constraint::CHECK_MODE_COERCE_TYPES` | | `0x00000004` | Convert data types to match the schema where possible |
40-
| `Constraint::CHECK_MODE_APPLY_DEFAULTS` | | `0x00000008` | Apply default values from the schema if not set |
41-
| `Constraint::CHECK_MODE_EXCEPTIONS` | | `0x00000010` | Throw an exception immediately if validation fails |
42-
| `Constraint::CHECK_MODE_DISABLE_FORMAT` | | `0x00000020` | Do not validate "format" constraints |
43-
| `Constraint::CHECK_MODE_EARLY_COERCE` | | `0x00000040` | Apply type coercion as soon as possible |
44-
| `Constraint::CHECK_MODE_ONLY_REQUIRED_DEFAULTS` | | `0x00000080` | When applying defaults, only set values that are required |
45-
| `Constraint::CHECK_MODE_VALIDATE_SCHEMA` | | `0x00000100` | Validate the schema as well as the provided document |
33+
| Flag | | Value | Description |
34+
|-------------------------------------------------|:--|--------------|--------------------------------------------------------------------------|
35+
| `Constraint::CHECK_MODE_NORMAL` | | `0x00000001` | Validate in 'normal' mode - this is the default |
36+
| `Constraint::CHECK_MODE_TYPE_CAST` | | `0x00000002` | Enable fuzzy type checking for associative arrays and objects |
37+
| `Constraint::CHECK_MODE_COERCE_TYPES` | | `0x00000004` | Convert data types to match the schema where possible |
38+
| `Constraint::CHECK_MODE_APPLY_DEFAULTS` | | `0x00000008` | Apply default values from the schema if not set |
39+
| `Constraint::CHECK_MODE_EXCEPTIONS` | | `0x00000010` | Throw an exception immediately if validation fails |
40+
| `Constraint::CHECK_MODE_DISABLE_FORMAT` | | `0x00000020` | Do not validate "format" constraints |
41+
| `Constraint::CHECK_MODE_EARLY_COERCE` | | `0x00000040` | Apply type coercion as soon as possible |
42+
| `Constraint::CHECK_MODE_ONLY_REQUIRED_DEFAULTS` | | `0x00000080` | When applying defaults, only set values that are required |
43+
| `Constraint::CHECK_MODE_VALIDATE_SCHEMA` | | `0x00000100` | Validate the schema as well as the provided document |
44+
| `Constraint::CHECK_MODE_STRICT` | | `0x00000200` | Validate the schema using strict mode, respecting the $schema identifier |

0 commit comments

Comments
 (0)