json-schema icon indicating copy to clipboard operation
json-schema copied to clipboard

How to reslove local ref definition

Open mbury opened this issue 12 years ago • 10 comments

my schema:

{
  "$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" }
  }
}

examle json:

{
  "shipping_address": {
    "street_address": "1600 Pennsylvania Avenue NW",
    "city": "Washington",
    "state": "DC"
  },
  "billing_address": {
    "street_address": "1st Street SE",
    "city": "Washington",
    "state": "DC"
  }
}

PHP code:

$schema = '{
    "$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"]
}';

$json = '{
  "shipping_address": {
    "street_address": "1600 Pennsylvania Avenue NW",
    "city": "Washington",
    "state": "DC"
  },
  "billing_address": {
    "street_address": "1st Street SE",
    "city": "Washington",
    "state": "DC"
  }
}';
$validator = new JsonSchema\Validator();
$validator->check(json_decode($json), json_decode($schema));

Can someone explain me how too use JsonSchema\RefResolver to get this code works? Now script don't use schema from ref definitions and always return true even if json is not valid.

mbury avatar Dec 02 '14 13:12 mbury

Hi @mbury, there is a pull request regarding this issue here: #119

@co3k has improved my initial fix (many thanks :) ), but we are waiting for approval.

If it's urgent, you can clone their clone.

The initial issue is: #93

stoiev avatar Dec 02 '14 14:12 stoiev

Any info when this will be release to composer repository?

mbury avatar Dec 02 '14 14:12 mbury

+1 I woud like to have local ref definitions implemented too.

Max-Might avatar Jan 29 '15 13:01 Max-Might

@justinrainbow please merge this pull request : #119

gribos avatar Feb 19 '15 15:02 gribos

+1 from me. Would appreciate it, if you could merge this one. Thanks.

tornography avatar Jun 18 '15 10:06 tornography

Another +1. Would really like this merge, please :-)

neural-wetware avatar Jul 22 '15 00:07 neural-wetware

Is there any news on $ref support ? Thx.

remss avatar Jul 30 '15 10:07 remss

My bad, i did not call the UriRetriever... now it works fine. See example in README.md

remss avatar Jul 30 '15 13:07 remss

This works fine when resolver/retriever is being used – see https://gist.github.com/graste/2fccfa710a375f93105d

graste avatar Sep 03 '15 09:09 graste

https://github.com/justinrainbow/json-schema/releases/tag/1.5.0 includes the fix asked for by @gribos per #119

bighappyface avatar Sep 08 '15 22:09 bighappyface