@@ -1304,6 +1304,71 @@ $schema->toJson();
13041304}
13051305```
13061306
1307+ ### From JSON Schema
1308+
1309+ You can also create schema objects from existing JSON Schema definitions:
1310+
1311+ ``` php
1312+ use Cortex\JsonSchema\SchemaFactory;
1313+ use Cortex\JsonSchema\Enums\SchemaVersion;
1314+
1315+ // From a JSON string
1316+ $jsonString = '{
1317+ "type": "object",
1318+ "title": "User",
1319+ "properties": {
1320+ "name": {"type": "string", "minLength": 2},
1321+ "email": {"type": "string", "format": "email"},
1322+ "age": {"type": "integer", "minimum": 0}
1323+ },
1324+ "required": ["name", "email"]
1325+ }';
1326+
1327+ $schema = SchemaFactory::fromJson($jsonString);
1328+
1329+ // From an array representation
1330+ $jsonArray = [
1331+ 'type' => 'string',
1332+ 'title' => 'ProductCode',
1333+ 'pattern' => '^[A-Z]{3}-\d{4}$',
1334+ 'examples' => ['ABC-1234', 'XYZ-5678']
1335+ ];
1336+
1337+ $schema = SchemaFactory::fromJson($jsonArray, SchemaVersion::Draft_2019_09);
1338+
1339+ // The resulting schema objects work the same as any other schema
1340+ $schema->isValid('ABC-1234'); // true
1341+ $schema->isValid('invalid'); // false
1342+
1343+ // You can also modify the schema after creation
1344+ $schema->description('Product identification code')
1345+ ->maxLength(8);
1346+ ```
1347+
1348+ This is particularly useful when:
1349+
1350+ - Migrating from existing JSON Schema definitions
1351+ - Loading schemas from configuration files or databases
1352+ - Converting between different schema representations
1353+ - Working with external APIs that provide JSON Schema specifications
1354+
1355+ ``` php
1356+ // Load from a file
1357+ $jsonSchema = file_get_contents('user-schema.json');
1358+ $schema = SchemaFactory::fromJson($jsonSchema);
1359+
1360+ // Validate some data
1361+ $userData = [
1362+ 'name' => 'John Doe',
1363+ 'email' => 'john@example.com',
1364+ 'age' => 30
1365+ ];
1366+
1367+ if ($schema->isValid($userData)) {
1368+ echo "User data is valid!";
1369+ }
1370+ ```
1371+
13071372## Credits
13081373
13091374- [ Sean Tymon] ( https://github.com/tymondesigns )
0 commit comments