You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -23,27 +23,35 @@ JSON Schema's main use-case is validating JSON documents and API responses, but
23
23
24
24
Install from npm:
25
25
26
-
$ npm install chai-json-schema
26
+
````bash
27
+
$ npm install chai-json-schema
28
+
````
27
29
28
30
Have chai use the chai-json-schema module:
29
31
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
+
````
32
36
33
37
### browser-side
34
38
35
39
Using globals:
36
40
37
41
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/):
38
42
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
+
<scriptsrc="jsonpointer.js"></script>
45
+
<scriptsrc="tv4.js"></script>
46
+
<scriptsrc="chai.js"></script>
47
+
<scriptsrc="chai-json-schema.js"></script>
48
+
````
43
49
44
50
Install from bower:
45
51
46
-
$ bower install chai-json-schema
52
+
````bash
53
+
$ bower install chai-json-schema
54
+
````
47
55
48
56
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'`.
49
57
@@ -53,68 +61,74 @@ The module supports CommonJS, AMD and browser globals. You might need to shim `t
53
61
54
62
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.
The `tv4` instance is 'exported' as `chai.tv4` and can be accessed to add schemas for use in validations:
103
113
104
-
chai.tv4.addSchema(uri, schema);
114
+
````js
115
+
chai.tv4.addSchema(uri, schema);
116
+
````
105
117
106
118
There are other useful methods:
107
119
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?:/);
110
123
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
+
````
118
132
119
133
For more API methods and info on the validator see the [tv4 documentation](https://github.com/geraintluff/tv4#api).
120
134
@@ -124,13 +138,17 @@ For more API methods and info on the validator see the [tv4 documentation](https
124
138
125
139
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.
126
140
127
-
chai.tv4.cyclicCheck = true;
128
-
141
+
````js
142
+
chai.tv4.cyclicCheck=true;
143
+
````
144
+
129
145
This is slightly slower then regular validation so it is disabled by default.
130
146
131
147
**Ban unknown properties**
132
148
133
-
chai.tv4.banUnknown = true;
149
+
````js
150
+
chai.tv4.banUnknown=true;
151
+
````
134
152
135
153
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.
136
154
@@ -140,50 +158,53 @@ Due to the synchronous nature of assertions there will be no support for dynamic
140
158
141
159
Use the asynchronous preparation feature of your favourite test runner to preload remote schemas:
142
160
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
+
varcheckMissing=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();
172
178
myFavoriteJsonLoader.load(uri, function (err, schema) {
173
179
if (err ||!schema) {
174
-
done(err || 'no data loaded');
180
+
callback(err ||'no data loaded');
175
181
return;
176
182
}
177
183
// add it
178
184
chai.tv4.addSchema(uri, schema);
179
-
180
-
// start checking
181
-
checkMissing(done);
185
+
// iterate
186
+
checkMissing(callback);
182
187
});
188
+
};
189
+
190
+
// load first instance manually
191
+
myFavoriteJsonLoader.load(uri, function (err, schema) {
0 commit comments