question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

GeoJSON coordinates should be easier to schema

See original GitHub issue

Mongo uses GeoJSON data structures for storing location. And the way they store coordinates is in an array [ <longitude>, <latitude> ]. However, creating a schema for that type of coordinate seems harder than it needs to be.

Here is how I’m solving the problem today:

  RectangleCoordinatesSchema = new SimpleSchema({
    coordinates: {
      type: Array,
      minCount: 2,
      maxCount: 2,
    },
    'coordinates.$': {
      type: [Number],
      decimal: true,
      minCount: 2,
      maxCount: 2,
      custom: function custom() {
        if (!(-90 <= this.value[0] <= 90)) {
          return 'lngOutOfRange';
        }
        if (!(-180 < this.value[1] <= 180)) {
          return 'latOutOfRange';
        }
        return true;
      },
    },
  })

However, anytime I want to have another schema object that includes a GeoJSON type coordinate value I need to copy and paste around the following code.

      type: [Number],
      decimal: true,
      minCount: 2,
      maxCount: 2,
      custom: function custom() {
        if (!(-90 <= this.value[0] <= 90)) {
          return 'lngOutOfRange';
        }
        if (!(-180 < this.value[1] <= 180)) {
          return 'latOutOfRange';
        }
        return true;
      },

This is not ideal, and is not good coding. However, if I could have a schema object that I could reuse for coordinates and then my example would be more like the following:

CoordinatesSchema = new SimpleSchema({
  0: {
    label: 'longitude',
    type: Number,
    decimal: true,
    max: 180.0,
    min: -180.0,
    exclusiveMin: true,
  },
  1: {
    label: 'latitude',
    type: Number,
    decimal: true,
    max: 90.0,
    min: -90.0,
  },
});

  RectangleCoordinatesSchema = new SimpleSchema({
    coordinates: {
      type: CoordinatesSchema,
      minCount: 2,
      maxCount: 2,
    },
  })

NOTE: the above code does not work because you can not have numbered keys. An error is raised TypeError: Cannot read property 'blackbox' of undefined. There are some other issues that touch on this bug.

First, is there a better way to accomplish what I’m doing? And if not can simple-schema potentially add support for validating against specific array indexes (and implicitly the length of an array value)?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:3
  • Comments:6

github_iconTop GitHub Comments

1reaction
aldeedcommented, Sep 13, 2016

If the numeric keys are the only problem here, then this is a duplicate of https://github.com/aldeed/meteor-simple-schema/issues/310.

I didn’t get that fixed in v2 since it is rather tricky, but I will try for a v3 that fixes it. It will have to be another major version bump because we’ll have to change array dot notation to a.[1].b or something similar so that we can distinguish between numeric keys and array indexes.

0reactions
proehlencommented, Aug 4, 2016

@clayne11 @pushplaybang

NOTE: the above code does not work because you can not have numbered keys. An error is raised TypeError: Cannot read property ‘blackbox’ of undefined.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Schema for GeoJSON Point/Polygon · Issue #9465 - GitHub
Hi, I am currently trying out the Point/Polygon schema as below, while Point seem to ... GeoJSON coordinates must be an array of...
Read more >
geoJSON formatting needs to be nested more deeply
I think the export format might be old but I'm not the one that built it so perhaps a manual replacement of some...
Read more >
GeoJSON schema - Google Groups
Does anyone know of a JSON Schema that describes the GeoJSON spec? ... See the site in my signature: you can test your...
Read more >
GeoJSON specification
an array of Polygon coordinates in the case of a MultiPolygon geometry. A position is an array of numbers. There MUST be two...
Read more >
About GeoJson Data - Oracle Help Center
According to the GeoJson specification, for a json object to be a geometry object it must have two fields called "type" and "coordinates",...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found