propertyNames.js
1.41 KB
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
'use strict';
module.exports = function defFunc(ajv) {
defFunc.definition = {
type: 'object',
compile: function(schema) {
var validate = ajv.compile(schema);
return ajv._opts.allErrors ? vAllErrors : vBreakOnError;
function vBreakOnError(data) {
for (var prop in data) {
if (!validate(prop)) {
vBreakOnError.errors = validate.errors;
addPropertyNameError(vBreakOnError.errors, prop);
return false;
}
}
return true;
}
function vAllErrors(data) {
var errors = [];
for (var prop in data) {
if (!validate(prop)) {
errors = errors.concat(validate.errors);
addPropertyNameError(errors, prop);
}
}
if (errors.length) vAllErrors.errors = errors;
return errors.length == 0;
}
function addPropertyNameError(errors, propName) {
errors.push({
keyword: 'propertyNames',
params: { propertyName: propName },
message: 'should have valid property name of "' + propName + '"'
});
}
},
metaSchema: {
$ref: ajv._opts.v5
? 'https://raw.githubusercontent.com/epoberezkin/ajv/master/lib/refs/json-schema-v5.json#'
: 'http://json-schema.org/draft-04/schema#'
},
errors: true
};
ajv.addKeyword('propertyNames', defFunc.definition);
return ajv;
};