-
Notifications
You must be signed in to change notification settings - Fork 10
/
schema.js
60 lines (54 loc) · 2.02 KB
/
schema.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
const jp = require('jsonpath');
const deRef = require('json-schema-deref');
const { logger } = require('../../config/logger');
const { defaultConfig } = require('../../config/normalizer');
/**
* Method that replace all '$ref' references by definition to obtain a full schema without '$ref'.
* @param schema
* @returns {object}
*/
const deReferenceSchema = async schema => new Promise((resolve) => {
deRef(schema, (err, fullSchema) => {
resolve(fullSchema);
});
});
/**
* Method that extract each field path witch has a type different than 'object'.
* @param {object} schema - json-schema
* @param config - normalization configuration parameters
* @returns {array} paths - array of json-path
*/
const getFieldPaths = (schema, config = defaultConfig) => {
const typeFieldName = config.fieldNames.type; // 'type' by default
let paths = jp.paths(schema, `$..[?(@.${typeFieldName} && @.${typeFieldName}!="object")]`);
// Array path ['$','properties','field'] to jsonPath '$.properties.field'
paths = paths.map(path => jp.stringify(path));
// Remove 'definitions' fields
paths = paths.filter(path => !path.includes('.definitions.'));
logger.debug(`Paths that must be normalized ${JSON.stringify(paths)}`);
return paths;
};
/**
* Method that extract each fields that must be normalized.
* @param {object} schema - json-schema
* @param config - normalization configuration parameters
* @returns {object} map - object that contain json field 'path' + 'type'
*/
const getFieldsToNormalize = (schema, config = defaultConfig) => {
const paths = getFieldPaths(schema, config);
const fields = [];
const typeFieldName = config.fieldNames.type; // 'type' by default
paths.forEach((path) => {
fields.push({
path: path.replace(/.properties./g, '.').replace(/.items/g, '[*]'),
type: jp.value(schema, path)[typeFieldName],
});
});
logger.debug(`Fields that must be normalized ${JSON.stringify(fields)}`);
return fields;
};
module.exports = {
deReferenceSchema,
getFieldPaths,
getFieldsToNormalize
};