-
-
Notifications
You must be signed in to change notification settings - Fork 242
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
How to build a complex schema when using with Rails #328
Comments
I had to do this exact thing, came to issues to check questions -- went to understanding json schema like you did. poked around a little bit and figured it out. There are two ways you can do this you can do it inside the same json file with a "definitions" key, which would work for certain things and is well documented on the website. Or you can require a different file, which is super simple. So, updating your example, where we have Foo which lists the Bar bar.json:
and the foo.json:
and there you go. Keep in mind there are benefits to using both versions, I'd really reserve the latter in the event that you need to reuse the specific schema configuration in multiple different places where it would require an awful amount of duplication. Hope this is what you are asking for, it's not really clear fully. |
@jblac I am terribly sorry for the super late response. Thank you very much for your answer. So my question - how does |
You seem to confuse what $ref is semantically used/meant for in the JSONschema standard and how json-schema resolves it. $ref may hold any valid JSON Pointer, which usually is in the form of a fragment '#definitions/address' as illustrated here or a full URI like 'https://schemas.example.com/some/path/schema.json'. json-schema gives you free control over whether and how to load schemas from files and remote resources. You can also override the schema loading mechanism (we do that in our project to map remote URIs to locally cached schema files to avoid the additional roundtrips). The basic answer is: File loading and remote resource loading are enabled by default. File references are resolved from the current folder (CWD I think), remote resources fetched via Rubys |
@RST-J, you are completely right, until I read your answer I was confused about how
Once again, thanks a lot for the info! |
I am using
json-schema
together withrspec
to assert that responses from a rails api match certain schemas. I have used this article as a guiding example. The schemas are placed underspec/support/schemas
. My question is - how can I reuse the schemas in one another. For example, let's say we have the schema for aFoo
model in the filespec/support/schemas/foo.json
:We would use this schema to assert the response from
FoosController#show
. If we want to assert that the response ofFoosController#index
is an array offoos
, inspec/support/schemas/foos.json
we would place something like:The value of the
items
key infoos.json
should be the value of theproperties
key offoo.json
.I have read the relevant part of Understanding JSON Schema, but I am not sure how to do that in the context of Rails. How can I do that, without having to host my json schemas on a public domain, but make it work as some kind of a Rails asset, so that schemas are aware of the environment and refer to the particular host, where they are located (say
localhost:3000
, when in development mode, but some public domain, when the app is deployed).Thank you very much for your answer.
The text was updated successfully, but these errors were encountered: