UNPKG

1.58 kBJavaScriptView Raw
1'use strict';
2
3var Ajv = require('ajv');
4var addKeywords = require('..');
5var test = require('./test_validate');
6var assert = require('assert');
7
8describe('async schema loading', function() {
9 var ajv, loadCount;
10
11 beforeEach(function() {
12 ajv = new Ajv({loadSchema: loadSchema});
13 addKeywords(ajv);
14 loadCount = 0;
15 });
16
17 describe('$merge', function() {
18 it('should load missing schemas', function() {
19 var schema = {
20 "$merge": {
21 "source": { "$ref": "obj.json#" },
22 "with": {
23 "properties": { "q": { "type": "number" } }
24 }
25 }
26 };
27
28 return testAsync(schema, '$merge');
29 });
30 });
31
32 describe('$patch', function() {
33 it('should load missing schemas', function() {
34 var schema = {
35 "$patch": {
36 "source": { "$ref": "obj.json#" },
37 "with": [
38 { "op": "add", "path": "/properties/q", "value": { "type": "number" } }
39 ]
40 }
41 };
42
43 return testAsync(schema, '$patch');
44 });
45 });
46
47 function testAsync(schema, keyword) {
48 return ajv.compileAsync(schema)
49 .then(function (validate) {
50 assert.strictEqual(loadCount, 1);
51 test(validate, keyword);
52 });
53 }
54
55 function loadSchema(ref) {
56 if (ref == 'obj.json') {
57 loadCount++;
58 var schema = {
59 "id": "obj.json#",
60 "type": "object",
61 "properties": { "p": { "type": "string" } },
62 "additionalProperties": false
63 };
64 return Promise.resolve(schema);
65 }
66 return Promise.reject(new Error('404: ' + ref));
67 }
68});