How to reuse array schema with SimpleSchema

98 views Asked by At

I'm trying to define the GeoJSON schema in node-simple-schema to use in my application.

I'm trying to define the position, linestring, linering and polygon arrays to use them later when defining the Point, LineString,.. geometries.

This is what I'm doing now, and it does not work.

const Position = new SimpleSchema({
  position: {
    type: Array,
    label: 'A single position. ...',
    minCount: 2,
    maxCount: 3,
  },
  'position.$': {
    type: Number,
    label: 'A number representing...',
  },
});

const Point = new SimpleSchema({
  type: {
    type: String,
    label: 'The type of the feature.',
    allowedValues: ['Point'],
  },
  coordinates: {
    type: Position.pick('position'),
    // this does not work either
    // type: Position.getObjectSchema('position'),
    label: 'A single position',
  },
});

When I try to validate like this I get an error.

const PointExample = {
  type: 'Point',
  coordinates: [180.0, 46.5, 100],
};

Point.validate(PointExample);
1

There are 1 answers

1
Season On

Extracted schemas matches objects with both keys and values. Therefore the following provided the key 'position' validates.

const PointExample = {
  type: 'Point',
  coordinates: {
    position: [180.0, 46.5, 100] // This is what matches the 'Position' schema.
  }
};

Point.validate(PointExample);