1 | 'use strict';
|
2 |
|
3 | var $ = require('./util/uri-helpers');
|
4 |
|
5 | $.findByRef = require('./util/find-reference');
|
6 | $.resolveSchema = require('./util/resolve-schema');
|
7 | $.normalizeSchema = require('./util/normalize-schema');
|
8 |
|
9 | var instance = module.exports = function() {
|
10 | function $ref(fakeroot, schema, refs, ex) {
|
11 | if (typeof fakeroot === 'object') {
|
12 | ex = refs;
|
13 | refs = schema;
|
14 | schema = fakeroot;
|
15 | fakeroot = undefined;
|
16 | }
|
17 |
|
18 | if (!Array.isArray(refs)) {
|
19 | ex = !!refs;
|
20 | refs = [];
|
21 | }
|
22 |
|
23 | function push(ref) {
|
24 | if (typeof ref.id === 'string') {
|
25 | var id = $.resolveURL(fakeroot, ref.id).replace(/\/#?$/, '');
|
26 |
|
27 | if (id.indexOf('#') > -1) {
|
28 | var parts = id.split('#');
|
29 |
|
30 | if (parts[1].charAt() === '/') {
|
31 | id = parts[0];
|
32 | } else {
|
33 | id = parts[1] || parts[0];
|
34 | }
|
35 | }
|
36 |
|
37 | if (!$ref.refs[id]) {
|
38 | $ref.refs[id] = ref;
|
39 | }
|
40 | }
|
41 | }
|
42 |
|
43 | refs.concat([schema]).forEach(function(ref) {
|
44 | schema = $.normalizeSchema(fakeroot, ref, push);
|
45 | push(schema);
|
46 | });
|
47 |
|
48 | return $.resolveSchema(schema, $ref.refs, ex);
|
49 | }
|
50 |
|
51 | $ref.refs = {};
|
52 | $ref.util = $;
|
53 |
|
54 | return $ref;
|
55 | };
|
56 |
|
57 | instance.util = $;
|