es-mapping-to-schema
Advanced tools
Comparing version
34
index.js
@@ -16,13 +16,2 @@ const _ = require('lodash'); | ||
const RecurseMappingToSchema = (mapping, schema, schemaType, options, localOptions) => { | ||
schema = determineType(mapping, schema, schemaType, options, localOptions); | ||
if (mapping.properties || mapping.type) { | ||
return recurseMappingObjects(mapping, schema, schemaType, options, localOptions); | ||
} else { | ||
return recurseMappingProperties(mapping, schema, schemaType, options); | ||
} | ||
}; | ||
const recurseMappingProperties = (mapping, schema, schemaType, options) => { | ||
@@ -37,4 +26,4 @@ const nextOptions = _.cloneDeep(options); | ||
schema[name] = {}; | ||
result[name] = RecurseMappingToSchema(mapping[name], schema[name], schemaType, nextOptions, nextLocalOptions); | ||
schema[name] = determineType(mapping[name], {}, schemaType, nextOptions); | ||
result[name] = recurseMappingObjects(mapping[name], schema[name], schemaType, nextOptions, nextLocalOptions); | ||
@@ -55,3 +44,4 @@ return result; | ||
if (mapping.properties) { | ||
schema.items.properties = RecurseMappingToSchema(mapping.properties, {}, schemaType, options, {}); | ||
const nextSchema = determineType(mapping.properties, {}, schemaType, options); | ||
schema.items.properties = recurseMappingProperties(mapping.properties, nextSchema, schemaType, options, {}); | ||
@@ -64,3 +54,4 @@ if (strict) { | ||
if (mapping.properties) { | ||
schema.properties = RecurseMappingToSchema(mapping.properties, {}, schemaType, options, {}); | ||
const nextSchema = determineType(mapping.properties, {}, schemaType, options); | ||
schema.properties = recurseMappingProperties(mapping.properties, nextSchema, schemaType, options, {}); | ||
@@ -85,3 +76,3 @@ if (strict) { | ||
const determineType = (mapping, schema, schemaType, options, localOptions) => { | ||
const determineType = (mapping, schema, schemaType, options) => { | ||
const mappingType = mapping.properties ? 'object' : mapping.type; | ||
@@ -146,2 +137,6 @@ const type = convertEsTypeToSchemaType(mappingType, options.isArray); | ||
const MappingToSchema = (mapping, options) => { | ||
if (!_.isString(mapping.type) && !_.isObject(mapping.properties)) { | ||
throw new Error(`root of mapping must have 'type' or 'properties' fields`); | ||
} | ||
options = options || {}; | ||
@@ -152,5 +147,8 @@ options = new Options(options); | ||
const baseValidationSchema = determineType(mapping, {}, VALIDATION_SCHEMA, options); | ||
const baseSanitizationSchema = determineType(mapping, {}, SANITIZATION_SCHEMA, options); | ||
return { | ||
validation: RecurseMappingToSchema(mapping, {}, VALIDATION_SCHEMA, options, {}), | ||
sanitization: RecurseMappingToSchema(mapping, {}, SANITIZATION_SCHEMA, options, {}) | ||
validation: recurseMappingObjects(mapping, baseValidationSchema, VALIDATION_SCHEMA, options, {}), | ||
sanitization: recurseMappingObjects(mapping, baseSanitizationSchema, SANITIZATION_SCHEMA, options, {}) | ||
}; | ||
@@ -157,0 +155,0 @@ }; |
{ | ||
"name": "es-mapping-to-schema", | ||
"version": "3.1.3", | ||
"version": "3.2.2", | ||
"description": "Convert Elasticsearch mappings to Schema Inspector schemas", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -9,2 +9,4 @@ [](https://coveralls.io/github/groupby/es-mapping-to-schema?branch=master) [](https://circleci.com/gh/groupby/es-mapping-to-schema) | ||
### Changelog: | ||
3.2 - Fix bug preventing usage of 'type' or 'properties' as field names. May be a breaking change depending on previous use. | ||
3.1 - Add ability to wildcard paths | ||
@@ -11,0 +13,0 @@ |
54
test.js
@@ -1019,2 +1019,56 @@ const chai = require('chai'); | ||
it('should throw if the root object does not have a type or properties field', ()=> { | ||
const mapping = { | ||
thing: { | ||
type: 'string' | ||
} | ||
}; | ||
expect(() => MappingToSchema(mapping)).to.throw(/must have 'type' or 'properties' field/); | ||
}); | ||
it('should handle mappings with properties named "type" or "properties"', ()=> { | ||
const mapping = { | ||
properties: { | ||
type: { | ||
type: 'string' | ||
}, | ||
properties: { | ||
type: 'string' | ||
}, | ||
notType: { | ||
type: 'string' | ||
} | ||
} | ||
}; | ||
const expectedValidataionSchema = { | ||
type: 'object', | ||
properties: { | ||
type: { | ||
type: 'string' | ||
}, | ||
properties: { | ||
type: 'string' | ||
}, | ||
notType: { | ||
type: 'string' | ||
} | ||
} | ||
}; | ||
const expectedSanitizationSchema = { | ||
properties: { | ||
type: {}, | ||
properties: {}, | ||
notType: {} | ||
} | ||
}; | ||
const schemas = MappingToSchema(mapping); | ||
expect(schemas.validation).to.eql(expectedValidataionSchema); | ||
expect(schemas.sanitization).to.eql(expectedSanitizationSchema); | ||
}); | ||
it('should apply path value to wildcarded path', ()=> { | ||
@@ -1021,0 +1075,0 @@ const mapping = { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
URL strings
Supply chain riskPackage contains fragments of external URLs or IP addresses, which the package may be accessing at runtime.
Found 1 instance in 1 package
URL strings
Supply chain riskPackage contains fragments of external URLs or IP addresses, which the package may be accessing at runtime.
Found 1 instance in 1 package
1427
3.33%154
1.32%43965
-64.06%11
-50%