Skip to content

Commit f44729f

Browse files
committed
enhanced readme code blocks
1 parent 278e6ee commit f44729f

File tree

1 file changed

+127
-100
lines changed

1 file changed

+127
-100
lines changed

README.md

Lines changed: 127 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -23,27 +23,35 @@ JSON Schema's main use-case is validating JSON documents and API responses, but
2323

2424
Install from npm:
2525

26-
$ npm install chai-json-schema
26+
````bash
27+
$ npm install chai-json-schema
28+
````
2729

2830
Have chai use the chai-json-schema module:
2931

30-
var chai = require('chai');
31-
chai.use(require('chai-json-schema'));
32+
````js
33+
var chai = require('chai');
34+
chai.use(require('chai-json-schema'));
35+
````
3236

3337
### browser-side
3438

3539
Using globals:
3640

3741
Include chai-json-schema after [jsonpointer.js](https://github.com/alexeykuzmin/jsonpointer.js/), [Tiny Validator tv4](https://github.com/geraintluff/tv4) and [Chai](http://chaijs.com/):
3842

39-
<script src="jsonpointer.js"></script>
40-
<script src="tv4.js"></script>
41-
<script src="chai.js"></script>
42-
<script src="chai-json-schema.js"></script>
43+
````html
44+
<script src="jsonpointer.js"></script>
45+
<script src="tv4.js"></script>
46+
<script src="chai.js"></script>
47+
<script src="chai-json-schema.js"></script>
48+
````
4349

4450
Install from bower:
4551

46-
$ bower install chai-json-schema
52+
````bash
53+
$ bower install chai-json-schema
54+
````
4755

4856
The module supports CommonJS, AMD and browser globals. You might need to shim `tv4`'s global and make sure `jsonpointer.js` can be required as `'jsonpointer'`.
4957

@@ -53,68 +61,74 @@ The module supports CommonJS, AMD and browser globals. You might need to shim `t
5361

5462
Validate that the given javascript value conforms to the specified JSON Schema. Both the value and schema would likely be JSON loaded from a external datasource but could also be literals or object instances.
5563

56-
var goodApple = {
57-
skin: "thin",
58-
colors: ["red", "green", "yellow"],
59-
taste: 10
60-
};
61-
var badApple = {
62-
colors: ["brown"],
63-
taste: 0,
64-
worms: 2
65-
};
66-
var fruitSchema = {
67-
"title": "fresh fruit schema v1",
68-
"type": "object",
69-
"required": ["skin", "colors", "taste"],
70-
"properties": {
71-
"colors": {
72-
"type": "array",
73-
"minItems": 1,
74-
"uniqueItems": true,
75-
"items": {
76-
"type": "string"
77-
}
78-
},
79-
"skin": {
80-
"type": "string"
81-
},
82-
"taste": {
83-
"type": "number",
84-
"minimum": 5
85-
}
86-
}
87-
};
88-
89-
//bdd style
90-
expect(goodApple).to.be.jsonSchema(fruitSchema);
91-
expect(badApple).to.not.be.jsonSchema(fruitSchema);
92-
93-
goodApple.should.be.jsonSchema(fruitSchema);
94-
badApple.should.not.be.jsonSchema(fruitSchema);
95-
96-
//tdd style
97-
assert.jsonSchema(goodApple, fruitSchema);
98-
assert.notJsonSchema(badApple, fruitSchema);
64+
````js
65+
var goodApple = {
66+
skin: "thin",
67+
colors: ["red", "green", "yellow"],
68+
taste: 10
69+
};
70+
var badApple = {
71+
colors: ["brown"],
72+
taste: 0,
73+
worms: 2
74+
};
75+
var fruitSchema = {
76+
"title": "fresh fruit schema v1",
77+
"type": "object",
78+
"required": ["skin", "colors", "taste"],
79+
"properties": {
80+
"colors": {
81+
"type": "array",
82+
"minItems": 1,
83+
"uniqueItems": true,
84+
"items": {
85+
"type": "string"
86+
}
87+
},
88+
"skin": {
89+
"type": "string"
90+
},
91+
"taste": {
92+
"type": "number",
93+
"minimum": 5
94+
}
95+
}
96+
};
97+
98+
//bdd style
99+
expect(goodApple).to.be.jsonSchema(fruitSchema);
100+
expect(badApple).to.not.be.jsonSchema(fruitSchema);
101+
102+
goodApple.should.be.jsonSchema(fruitSchema);
103+
badApple.should.not.be.jsonSchema(fruitSchema);
104+
105+
//tdd style
106+
assert.jsonSchema(goodApple, fruitSchema);
107+
assert.notJsonSchema(badApple, fruitSchema);
108+
````
99109

100110
## Additional API
101111

102112
The `tv4` instance is 'exported' as `chai.tv4` and can be accessed to add schemas for use in validations:
103113

104-
chai.tv4.addSchema(uri, schema);
114+
````js
115+
chai.tv4.addSchema(uri, schema);
116+
````
105117

106118
There are other useful methods:
107119

108-
var list = chai.tv4.getMissingUris();
109-
var list = chai.tv4.getMissingUris(/^https?:/);
120+
````js
121+
var list = chai.tv4.getMissingUris();
122+
var list = chai.tv4.getMissingUris(/^https?:/);
110123

111-
var list = chai.tv4.getSchemaUris();
112-
var list = chai.tv4.getSchemaUris(/example.com/);
113-
114-
var schema = chai.tv4.getSchema('http://example.com/item');
115-
var schema = chai.tv4.getSchema('http://example.com/item/#sub/type');
116-
117-
chai.tv4.dropSchemas();
124+
var list = chai.tv4.getSchemaUris();
125+
var list = chai.tv4.getSchemaUris(/example.com/);
126+
127+
var schema = chai.tv4.getSchema('http://example.com/item');
128+
var schema = chai.tv4.getSchema('http://example.com/item/#sub/type');
129+
130+
chai.tv4.dropSchemas();
131+
````
118132

119133
For more API methods and info on the validator see the [tv4 documentation](https://github.com/geraintluff/tv4#api).
120134

@@ -124,13 +138,17 @@ For more API methods and info on the validator see the [tv4 documentation](https
124138

125139
This will be passed to the internal `tv4` validate call to enable [support for cyclical objects](https://github.com/geraintluff/tv4#cyclical-javascript-objects). It allows tv4 to validate normal javascipt structures (instead of pure JSON) without risk of entering a loop on cyclical references.
126140

127-
chai.tv4.cyclicCheck = true;
128-
141+
````js
142+
chai.tv4.cyclicCheck = true;
143+
````
144+
129145
This is slightly slower then regular validation so it is disabled by default.
130146

131147
**Ban unknown properties**
132148

133-
chai.tv4.banUnknown = true;
149+
````js
150+
chai.tv4.banUnknown = true;
151+
````
134152

135153
Passed to the internal `tv4` validate call makes validation fail on unknown schema properties. Use this to make sure your schema do not contain undesirable data.
136154

@@ -140,50 +158,53 @@ Due to the synchronous nature of assertions there will be no support for dynamic
140158

141159
Use the asynchronous preparation feature of your favourite test runner to preload remote schemas:
142160

143-
// simplified example using a bdd-style async before();
144-
// as used in mocha, jasmine etc.
145-
146-
before(function (done) {
147-
148-
// iterate missing
149-
var checkMissing = function (callback) {
150-
var missing = chai.tv4.getMissingUris();
151-
if (missing.length === 0) {
152-
// all $ref's solved
153-
callback();
154-
return;
155-
}
156-
// load a schema using your favourite JSON loader
157-
// (jQuery, request, SuperAgent etc)
158-
var uri = missing.pop();
159-
myFavoriteJsonLoader.load(uri, function (err, schema) {
160-
if (err || !schema) {
161-
callback(err || 'no data loaded');
162-
return;
163-
}
164-
// add it
165-
chai.tv4.addSchema(uri, schema);
166-
// iterate
167-
checkMissing(callback);
168-
});
169-
};
170-
171-
// load first instance manually
161+
````js
162+
// simplified example using a bdd-style async before();
163+
// as used in mocha, jasmine etc.
164+
165+
before(function (done) {
166+
167+
// iterate missing
168+
var checkMissing = function (callback) {
169+
var missing = chai.tv4.getMissingUris();
170+
if (missing.length === 0) {
171+
// all $ref's solved
172+
callback();
173+
return;
174+
}
175+
// load a schema using your favourite JSON loader
176+
// (jQuery, request, SuperAgent etc)
177+
var uri = missing.pop();
172178
myFavoriteJsonLoader.load(uri, function (err, schema) {
173179
if (err || !schema) {
174-
done(err || 'no data loaded');
180+
callback(err || 'no data loaded');
175181
return;
176182
}
177183
// add it
178184
chai.tv4.addSchema(uri, schema);
179-
180-
// start checking
181-
checkMissing(done);
185+
// iterate
186+
checkMissing(callback);
182187
});
188+
};
189+
190+
// load first instance manually
191+
myFavoriteJsonLoader.load(uri, function (err, schema) {
192+
if (err || !schema) {
193+
done(err || 'no data loaded');
194+
return;
195+
}
196+
// add it
197+
chai.tv4.addSchema(uri, schema);
198+
199+
// start checking
200+
checkMissing(done);
183201
});
202+
});
203+
````
184204

185205
## History
186206

207+
* 1.1.0 - Dependency update
187208
* 1.0.10 - AMD loader support
188209
* 1.0.9 - Published to bower.
189210
* 1.0.7 - Updated tv4 dependency, improved error formatting.
@@ -196,15 +217,21 @@ Use the asynchronous preparation feature of your favourite test runner to preloa
196217

197218
Install development dependencies in your git checkout:
198219

199-
$ npm install
220+
````bash
221+
$ npm install
222+
````
200223

201224
You need the global [grunt](http://gruntjs.com) command:
202225

203-
$ npm install grunt-cli -g
226+
````bash
227+
$ npm install grunt-cli -g
228+
````
204229

205230
Build and run tests:
206231

207-
$ grunt
232+
````bash
233+
$ grunt
234+
````
208235

209236
See the `Gruntfile` for additional commands.
210237

0 commit comments

Comments
 (0)