# 09:29:48 0 (master|✚ 3…4) /srv/www/json-schema $ php address-ref.php
string(927) "{
"$schema": "http://json-schema.org/draft-04/schema#",
"definitions": {
"address": {
"type": "object",
"properties": {
"street_address": {
"type": "string"
},
"city": {
"type": "string"
},
"state": {
"type": "string"
}
},
"required": [
"street_address",
"city",
"state"
]
}
},
"type": "object",
"properties": {
"billing_address": {
"$ref": "#/definitions/address"
},
"shipping_address": {
"$ref": "#/definitions/address"
}
},
"required": [
"billing_address",
"shipping_address"
],
"id": "file:///srv/www/json-schema/schema-address.json"
}"
string(94) "============================== RESOLVED SCHEMA ==============================================="
string(2011) "{
"$schema": "http://json-schema.org/draft-04/schema#",
"definitions": {
"address": {
"type": "object",
"properties": {
"street_address": {
"type": "string"
},
"city": {
"type": "string"
},
"state": {
"type": "string"
}
},
"required": [
"street_address",
"city",
"state"
],
"id": "file:///srv/www/json-schema/schema-address.json#/definitions/address"
}
},
"type": "object",
"properties": {
"billing_address": {
"type": "object",
"properties": {
"street_address": {
"type": "string"
},
"city": {
"type": "string"
},
"state": {
"type": "string"
}
},
"required": [
"street_address",
"city",
"state"
],
"id": "file:///srv/www/json-schema/schema-address.json#/definitions/address"
},
"shipping_address": {
"type": "object",
"properties": {
"street_address": {
"type": "string"
},
"city": {
"type": "string"
},
"state": {
"type": "string"
}
},
"required": [
"street_address",
"city",
"state"
],
"id": "file:///srv/www/json-schema/schema-address.json#/definitions/address"
}
},
"required": [
"billing_address",
"shipping_address"
],
"id": "file:///srv/www/json-schema/schema-address.json"
}"
string(94) "============================== JSON DATA ====================================================="
string(271) "{
"shipping_address": {
"street_address": "1600 Pennsylvania Avenue NW",
"city": "Washington",
"state": "DC"
},
"billing_address": {
"street_address": "1st Street SE",
"city": "Washington",
"state": "DC"
}
}"
The supplied JSON validates against the schema.
Last active
December 1, 2021 15:54
-
-
Save graste/2fccfa710a375f93105d to your computer and use it in GitHub Desktop.
JSON-Schema with simple $ref resolution (reference to local definition)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
ini_set('display_errors', 1); | |
require __DIR__ . '/vendor/autoload.php'; | |
$json = '{ | |
"shipping_address": { | |
"street_address": "1600 Pennsylvania Avenue NW", | |
"city": "Washington", | |
"state": "DC" | |
}, | |
"billing_address": { | |
"street_address": "1st Street SE", | |
"city": "Washington", | |
"state": "DC" | |
} | |
}'; | |
$retriever = new JsonSchema\Uri\UriRetriever; | |
$schema = $retriever->retrieve('file://' . realpath('schema-address.json')); | |
var_dump(json_encode($schema, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE)); | |
$refResolver = new JsonSchema\RefResolver($retriever); | |
//$refResolver->resolve($schema, 'file://' . __DIR__); | |
$refResolver->resolve($schema); | |
var_dump('============================== RESOLVED SCHEMA ==============================================='); | |
var_dump(json_encode($schema, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE)); | |
var_dump('============================== JSON DATA ====================================================='); | |
var_dump(json_encode(json_decode($json), JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE)); | |
$validator = new JsonSchema\Validator(); | |
$validator->check(json_decode($json), $schema); | |
if ($validator->isValid()) { | |
// this will be the output with master version as of 20150903 | |
echo "The supplied JSON validates against the schema.\n"; | |
} else { | |
echo "JSON does not validate. Violations:\n"; | |
foreach ($validator->getErrors() as $error) { | |
echo sprintf("[%s] %s\n", $error['property'], $error['message']); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"$schema": "http://json-schema.org/draft-04/schema#", | |
"definitions": { | |
"address": { | |
"type": "object", | |
"properties": { | |
"street_address": { "type": "string" }, | |
"city": { "type": "string" }, | |
"state": { "type": "string" } | |
}, | |
"required": ["street_address", "city", "state"] | |
} | |
}, | |
"type": "object", | |
"properties": { | |
"billing_address": { "$ref": "#/definitions/address" }, | |
"shipping_address": { "$ref": "#/definitions/address" } | |
}, | |
"required": ["billing_address", "shipping_address"] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment