mongoose-geojson-schema icon indicating copy to clipboard operation
mongoose-geojson-schema copied to clipboard

Feature.properties should be mandatory

Open OlivierMartineau opened this issue 2 years ago • 2 comments

https://datatracker.ietf.org/doc/html/rfc7946#section-3.2

A Feature object has a member with the name "properties". The value of the properties member is an object (any JSON object or a JSON null value).

I found that you could remove a Feature "properties" property and still have the format validated. Such property can be null or any object, possibly empty, but should always be there.

Delete line 609 to 611 in test/GeoJSON.integration.js: all tests are still successful.

OlivierMartineau avatar Jul 16 '21 07:07 OlivierMartineau

I suggest to change index.js line 496 to the following:

Feature.schemaName = 'Feature'

function validateFeature(feature) {
  if (!feature.type) {
    throw new mongoose.Error('Feature must have a type')
  }
  // type must be Feature
  if (feature.type !== 'Feature') {
    throw new mongoose.Error(feature.type + ' is not a valid GeoJSON type')
  }
  if (!feature.geometry) {
    throw new mongoose.Error('Feature must have a geometry')
  }
  // check for crs
  if (feature.crs) {
    crs = feature.crs
    validateCrs(crs)
  }
  validateGeometry(feature.geometry)
  validateFeatureProperties(feature.properties)
}

function validateFeatureProperties(properties) {
  if (properties === null) return
  if (!properties) {
    throw new mongoose.Error('Feature must have a "properties" member')
  }
  if (typeof properties !== 'object') {
    throw new mongoose.Error('Feature properties must be a JSON object or a null value.')
  }
}

OlivierMartineau avatar Jul 16 '21 08:07 OlivierMartineau

Hi @OlivierMartineau. Great suggestion. Feel free to make a PR for this.

joshkopecek avatar Jul 16 '21 09:07 joshkopecek