GeoJSON coordinates should be easier to schema
See original GitHub issueMongo 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:
- Created 7 years ago
- Reactions:3
- Comments:6
Top GitHub Comments
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.@clayne11 @pushplaybang