-
Notifications
You must be signed in to change notification settings - Fork 356
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Question] local relative $ref #446
Comments
Local references within the same document must start with If you're wanting to refer to another file, then you simply concatenate the file and the reference to the part of the file you wish to reference, separated by There is no limit to how many layers of referencing you can use. Definitions are dereferenced as needed during validation. |
@erayd Thank you very much for such a quick reply! 👍 Let's just use an example to be more clear, sorry about the verbosity:
Scenario: Fetching a list of existing images when images match
When I send a "GET" request to "/api/images"
Then the JSON should be valid according to this schema:
"""
{
"definitions": {
"user": {
"type": "object",
"required": ["id", "username"],
"properties": {
"id": {"type": "string", "format": "udid"},
"username": {"type": "string"}
},
"additionalProperties": false
},
"image": {
"type": "object",
"required": ["id", "name"],
"properties": {
"id": {"type": "string", "format": "udid"},
"name": {"type": "string"},
"created_at": {"type": "string", "format": "date-time"},
"created_by": {"$ref": "#/definitions/user"},
"updated_at": {"type": "string", "format": "date-time"},
"updated_by": {"$ref": "#/definitions/user"}
},
"additionalProperties": false
}
},
"type": "array",
"items": {"$ref": "#/definitions/image"},
"minItems": 30,
"maxItems": 30,
"uniqueItems": true,
"additionalItems": false
}
"""
{
"title": "user",
"type": "object",
"required": ["id", "username"],
"properties": {
"id": {"type": "string", "format": "udid"},
"username": {"type": "string"}
},
"additionalProperties": false
} {
"title": "image",
"type": "object",
"required": ["id", "name"],
"properties": {
"id": {"type": "string", "format": "udid"},
"name": {"type": "string"},
"created_at": {"type": "string", "format": "date-time"},
"created_by": {"$ref": "user.json"},
"updated_at": {"type": "string", "format": "date-time"},
"updated_by": {"$ref": "user.json"}
},
"additionalProperties": false
}
Scenario: Fetching a list of existing images when images match
When I send a "GET" request to "/api/images"
And the JSON should be valid according to this schema:
"""
{
"type": "array",
"items": {"$ref": "schemas/image.json"},
"minItems": 30,
"maxItems": 30,
"uniqueItems": true,
"additionalItems": false
}
""" Obviously, this doesn't work currently so my questions is how would one do this properly? |
It's 1:34am here, so I might be missing something, but what you've proposed in your most recent post looks correct. When you say it doesn't work, what happens instead? Can you post the error(s) you are seeing? |
It says
|
That means that you haven't told the validator where your original schema lives, and you also haven't loaded the referenced schemas manually (i.e. you are relying on the autoloader). That is fine, however as your If you add |
I think I get it, makes perfect sense. 👍 I'm currently trying to get it to work with your suggestion, getting the same error but will try to track it down with this new found knowledge. Will report here if I succeed, thanks again for your time. |
I wish you the best of luck :-). If you get stuck and need to get it working ASAP, loading the schemas manually is an alternative approach. And if you think you've found a bug, please shout so we can fix it - the code is a mess with stuff from many years ago and from many people who are no longer around. We're cleaning it up, but it's taking a while to get through. I really need to get some sleep now, followed by a long drive - so further replies are likely to be delayed - however please feel free to ask if you have any more questions about this. I'm aware that the documentation for this library is in pretty poor shape at present. |
@erayd I think this doesn't work (as expected), I've tried with your Here's a repo reproducing the issue, if you could point me some sort of solution, that would be great: https://github.com/dkarlovi/json-schema-behat-external-ref |
It should work - if it's not working for you, then something is wrong. It may be a bug. I'll investigate this now; stand by. |
Can confirm there is a bug. Stand by for a fix. |
Thanks for having a look at this so quickly, appreciate it. Will be paying attention here and there. |
@erayd I might be wrong, but the relative resolution seems off to me. For example, in my repo files are layout like:
So, if I say This is more visible in my actual project where I have layout like:
and the working reference is:
If I specify
|
Hmm.... I did test this, before I did the PR, but it's possible I may have screwed something up. Could you let me know what the working directory is in your example? The initial id is set relative to that. |
Looks like I missed a condition in my original PR - this is fixed in #452. Thanks for spotting the issue. |
@erayd I can confirm this works as expected in 5.2.4. Thank you very much for your effort on this matter and a quick resolution. |
@dkarlovi You're most welcome :-). |
BTW it's pretty weird collaborating with you on this fix and getting a workstation system update (Fedora Linux, Remi) installing the new version. 👍 |
Haha, I had no idea this library was packaged outside of composer! Very speedy too... I wonder if it's an automated build? |
*outside of packagist |
I don't think it's fully automated, see: https://git.remirepo.net/cgit/rpms/php/justinrainbow/php-justinrainbow-json-schema5.git/ |
I'm using this with Behat to validate a JSON response for APIs. Using 5.2.1.
If my schema is embedded inside a Behat feature file (say
features/my.feature
), how do I reference a definition insideschemas/entity.json
? Would that definition be able to reference some other definition (schemas/other-entity.json
)?The text was updated successfully, but these errors were encountered: