chaijs / chai-json-schema

Chai plugin for JSON Schema v4
MIT License
75 stars 17 forks source link

example for #ref #1

Closed atian25 closed 10 years ago

atian25 commented 11 years ago

want to reuse some schema, could you plz show some example?

Bartvds commented 11 years ago

I'm not sure if I understand your question. Note the schema is essentially just a standard JavaScript object structure, so usually you can either load it from a .json file or create it in the code.

If you want to load it from a file you can use any method you like and use mocha's before() handler to load and keep the schema object for re-use. Is this what you mean?

I can make an example for that if you let me know your setup: are you testing in node.js or a browser? And do you already use jQuery or something else? (like node's file system, superagent, request.js or mabe even a RequireJS's text plugin?)

As noted in the readme I've not have a test for preloading #ref, but I'll try to add somoe asap. If you can give me some real-world examples of schema and data (link or in a gist) it'll work even quicker.

atian25 commented 11 years ago

mocha + request.js at nodejs.

I use this to test my server's REST interface, to validate responese JSON format.

thanks. sorry I found the real question is about json-schema usage, could you show some example for #ref ?

Bartvds commented 11 years ago

Makes sense. I'll try to add a test for #ref's and create an example for loading and using them.

Bartvds commented 11 years ago

I have a temporary solution:

First you check this example on the json-schema website and then the readme from tv4.

The first shows how you use #ref's in json-schema in general, and the second shows how to use tv4.addSchema(url, schema); to preset them for synchronous validation.

The tv4 is a global and I've verified in node.js that you can require('tv4') it, then use tv4.addSchema(url, schema); to add your external schema, and then use my chai-json-schema plugin and it will use the same tv4 instance and resolve and use the schema in the assertions.

I'm in discussion with the maintainer of tv4 to expose a way to automate adding/loading the external uri's so you don't have to add each one yourself.

Bartvds commented 10 years ago

I'm closing this issue for now. Feel free to re-open if you have any remaining problems.

Note some facility to easily load schemas is still on the list for a later date.