This repository has been archived by the owner on May 31, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
169 lines (144 loc) · 5.72 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
'use strict';
const { resolve: urlResolve } = require('url');
const { ValidationError } = require('ajv');
const compilePointer = require('./pointer');
/**
* Modifies an Ajv instance to add `'$ref$data'` and `'async$ref$data'` as new
* keywords (for sync and async schemas, respectively).
*
* @param {Object} ajv - the Ajv instance
* @param {Object} [opts={}] - the plugin options
*/
module.exports = function (ajv, opts={}) {
let compile = (...args) => $ref$data(opts, ...args);
let metaSchema = { items: { type: 'string' }, type: 'array' };
ajv.addKeyword('$ref$data', { compile, errors: true, metaSchema });
ajv.addKeyword('async$ref$data', {
async: true, compile, errors: true, metaSchema
});
return ajv;
};
let $pathToPtr = /(?:\[([0-9]+)\]|\.([^.[]+)|\['((?:[^\\']|\\\\|\\')*)'\])/g;
/**
* (Compilation function for our keywords.)
* @private
*/
function $ref$data(opts, schema, parentSchema, it) {
let $async = it.async;
let $keyword = $async ? 'async$ref$data' : '$ref$data';
// check whether there are other validation keywords
checkForExtraRules(parentSchema, $keyword, it);
// check and compile each pointer
let $pieces = schema.map((piece, idx) => idx % 2 ? compilePointer(
piece, `${$keyword}: reference "${piece}" at path "${it.errSchemaPath}`,
it.dataLevel
) : piece);
// schema to validate (and possibly coerce) the resolved pieces
let $fragsSchema = it.self.compile({
items: $pieces.map((_, idx) => idx % 2 ? { type: 'string' } : {})
});
// isolate only the values we need within the closure
let {
baseId: $baseId,
opts: { jsonPointers: $jsonPointers }
} = it;
let { missingRefs: $missingRefs=it.opts.missingRefs } = opts;
let $getSchema = it.self.getSchema.bind(it.self);
// run-time validation function
return function validate(data, path, parent, keyword, root) {
// if needed, convert JS paths (such as `.a[0]['~'].b['a\\'b']`) to
// equivalent JSON pointer (such as "/a/0/~0/b/a'b")
let jsonPath = path;
if (!$jsonPointers) jsonPath = jsonPath.replace('~','~0').replace('/','~1')
.replace($pathToPtr, '/$1$2$3').replace(/\\([\\'])/,'$1');
// split JSON pointer to current position into path fragments
let pathFrags = [];
for (let frag of jsonPath.split('/').slice(1)) pathFrags.push(
frag.replace('~1', '/').replace('~0', '~')
);
// replace odd-position pieces with the pointer outcomes
let resolvedPieces = [];
for (let [idx, piece] of $pieces.entries()) resolvedPieces.push(
idx % 2 ? piece(root, pathFrags) : piece
);
// check whether resolved & coerced pieces meet the schema
if (!$fragsSchema(resolvedPieces)) {
validate.errors = [];
for (let err of $fragsSchema.errors) validate.errors.push({
keyword: $keyword,
message: 'element pointed to does not meet data schema',
params: err
});
if ($async) throw new ValidationError(validate.errors);
return false;
}
// obtain desired ref, fetch corresponding schema (or report missing)
let ref = resolvedPieces.join('');
let refSchema = $getSchema(urlResolve($baseId, ref), it);
if (!refSchema && $missingRefs != 'ignore') {
validate.errors = [{
keyword: $keyword,
message: `can't resolve reference ${ref} from id ${$baseId}`,
params: { ref }
}];
if ($async) throw new ValidationError(validate.errors);
return false;
}
// if missing schemas are ok, report success if schema is missing
if (!refSchema) return $async ? Promise.resolve(true) : true;
// report result
let refSchemaAsync = refSchema && refSchema.root.$async;
if ($async) { // if outer schema is async
let result = refSchema(data, path, parent, keyword, root);
if (refSchemaAsync) return result; // if inner is async
// otherwise, inner schema is sync, and we must wrap result
if (result) return Promise.resolve(true);
throw new ValidationError(refSchema.errors);
} else if (refSchemaAsync) { // if outer is sync and inner is async
validate.errors = [{
keyword: $keyword,
message: 'cannot invoke asynchronous subschema',
params: { ref }
}];
return false;
}
// otherwise, both outer and inner schemas are sync
let result = refSchema(data, path, parent, keyword, root);
validate.errors = refSchema.errors;
return result;
};
}
/**
* Test whether other validation keywords are present. Throw if both keywords
* are present, or the wrong (sync vs async) version is used. If user set Ajv's
* `extendRefs` to `'fail'`, throw if more keywords are present. If user set it
* to `'ignore'`, log a warning that it's not possible.
*
* @private
* @param {Object} parentSchema - the parent schema
* @param {string} $keyword - the current version of the keyword
* @param {Object} it - the Ajv schema compilation context
* @throws {Error} in any of the situations listed above
*/
function checkForExtraRules(parentSchema, $keyword, it) {
if (Object.keys(parentSchema).every(
key => key == $keyword || !it.RULES.all[key]
)) return;
let where = `in schema at path "${it.errSchemaPath}"`;
if (!parentSchema[$keyword]) {
throw new Error(
`${$keyword}: switched '$ref$data' and 'async$ref$data' ${where}"`
);
} else if (parentSchema.async$ref$data && parentSchema.$ref$data) {
throw new Error(
`${$keyword}: both '$ref$data' and 'async$ref$data' used ${where}"`
);
} else if (it.opts.extendRefs == 'fail') {
throw new Error(
`${$keyword}: other keywords used ${where} (see option extendRefs)`
);
} else if (it.opts.extendRefs !== true) {
// eslint-disable-next-line no-console
console.warn(`${$keyword}: other keywords ${where} cannot be ignored`);
}
}