2015-11-22 03:31:01 +03:00
|
|
|
'use strict';
|
|
|
|
|
2015-12-13 00:18:41 +03:00
|
|
|
var META_SCHEMA_ID = 'https://raw.githubusercontent.com/epoberezkin/ajv/master/lib/refs/json-schema-v5.json';
|
2015-11-22 03:31:01 +03:00
|
|
|
|
|
|
|
module.exports = {
|
2015-12-13 00:18:41 +03:00
|
|
|
enable: enableV5,
|
|
|
|
META_SCHEMA_ID: META_SCHEMA_ID
|
2015-11-22 03:31:01 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
function enableV5(ajv) {
|
2015-12-13 00:18:41 +03:00
|
|
|
if (ajv.opts.meta !== false) {
|
|
|
|
var metaSchema = require('./refs/json-schema-v5.json');
|
|
|
|
ajv.addMetaSchema(metaSchema, META_SCHEMA_ID);
|
|
|
|
}
|
2015-11-22 03:31:01 +03:00
|
|
|
ajv.addKeyword('constant', { macro: constantMacro });
|
|
|
|
ajv.addKeyword('contains', { macro: containsMacro });
|
2015-12-06 23:54:12 +03:00
|
|
|
ajv.addKeyword('formatMaximum', { type: 'string', inline: formatLimit('maximum'), errors: false });
|
|
|
|
ajv.addKeyword('formatMinimum', { type: 'string', inline: formatLimit('minimum'), errors: false });
|
2015-12-05 23:53:40 +03:00
|
|
|
ajv.addKeyword('exclusiveFormatMaximum');
|
|
|
|
ajv.addKeyword('exclusiveFormatMinimum');
|
2015-12-12 16:41:51 +03:00
|
|
|
ajv.addKeyword('patternGroups'); // implemented in properties.jst
|
2015-12-13 01:29:45 +03:00
|
|
|
ajv.addKeyword('switch', { inline: require('./dotjs/switch'), statements: true, errors: true });
|
2015-11-22 03:31:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function constantMacro(schema) {
|
2015-12-06 23:54:12 +03:00
|
|
|
return { "enum": [schema] };
|
2015-11-22 03:31:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function containsMacro(schema) {
|
2015-12-07 00:57:41 +03:00
|
|
|
return {
|
|
|
|
"not": {
|
|
|
|
"type": "array",
|
|
|
|
"items": { "not": schema }
|
|
|
|
}
|
|
|
|
};
|
2015-11-22 03:31:01 +03:00
|
|
|
}
|
2015-12-05 23:53:40 +03:00
|
|
|
|
2015-12-06 23:54:12 +03:00
|
|
|
function formatLimit(limit) {
|
|
|
|
var operation = limit == 'maximum' ? '<' : '>';
|
|
|
|
var exclusiveLimit = 'exclusiveFormat' +
|
2015-12-07 00:57:41 +03:00
|
|
|
(limit == 'maximum' ? 'Maximum' : 'Minimum');
|
2015-12-06 23:54:12 +03:00
|
|
|
|
2015-12-05 23:53:40 +03:00
|
|
|
return function(it, schema, parentSchema) {
|
|
|
|
var format = parentSchema.format;
|
|
|
|
var compare = it.formats[format].compare;
|
2015-12-06 23:54:12 +03:00
|
|
|
if (!compare) return 'true';
|
|
|
|
var exclusive = parentSchema[exclusiveLimit];
|
2015-12-05 23:53:40 +03:00
|
|
|
var data = 'data' + (it.dataLevel || '');
|
2015-12-06 23:54:12 +03:00
|
|
|
var op = operation;
|
2015-12-05 23:53:40 +03:00
|
|
|
if (!exclusive) op += '=';
|
|
|
|
return 'formats' + it.util.getProperty(format) + '.compare('
|
|
|
|
+ data + ', ' + it.util.toQuotedString(schema)
|
|
|
|
+ ') ' + op + ' 0';
|
2015-12-06 00:31:56 +03:00
|
|
|
};
|
2015-12-05 23:53:40 +03:00
|
|
|
}
|