diff --git a/lib/type-mapper.js b/lib/type-mapper.js index 6165b44..4be9ef0 100644 --- a/lib/type-mapper.js +++ b/lib/type-mapper.js @@ -7,6 +7,7 @@ const BOOLEAN = { type: 'boolean' }; const INTEGER = { type: 'integer' }; const NUMBER = { type: 'number' }; const STRING = { type: 'string' }; +const GEOMETRY = { type: 'object' }; const ANY = { anyOf: [OBJECT, ARRAY, BOOLEAN, INTEGER, NUMBER, STRING] }; /** @@ -186,9 +187,73 @@ class TypeMapper { // @todo GEOMETRY('POINT', 4326) // ---------------------------------------------------------------------- case 'GEOMETRY': { - throw new TypeError( - 'sequelize-to-json-schemas has not yet implemented the GEOMETRY DataType', - ); + const geometrySubType = properties.type?.options?.type?.toUpperCase(); + + result = { + ...GEOMETRY, + required: ['type', 'coordinates'], + properties: { + type: { + type: 'string', + enum: geometrySubType + ? [geometrySubType[0] + geometrySubType.slice(1).toLowerCase()] + : [ + 'Point', + 'LineString', + 'Polygon', + 'MultiPoint', + 'MultiLineString', + 'MultiPolygon', + 'GeometryCollection', + ], + }, + coordinates: { type: 'array' }, // refined below + }, + }; + + switch (geometrySubType) { + case 'POINT': + result.properties.coordinates = { + type: 'array', + items: { type: 'number' }, + minItems: 2, + }; + break; + + case 'MULTIPOINT': + case 'LINESTRING': + result.properties.coordinates = { + type: 'array', + items: { type: 'array', items: { type: 'number' }, minItems: 2 }, + }; + break; + + case 'MULTILINESTRING': + case 'POLYGON': + result.properties.coordinates = { + type: 'array', + items: { + type: 'array', + items: { type: 'array', items: { type: 'number' }, minItems: 2 }, + }, + }; + break; + + case 'MULTIPOLYGON': + result.properties.coordinates = { + type: 'array', + items: { + type: 'array', + items: { + type: 'array', + items: { type: 'array', items: { type: 'number' }, minItems: 2 }, + }, + }, + }; + break; + } + + break; } // ----------------------------------------------------------------------