【问题标题】:How to reuse array schema with SimpleSchema如何使用 SimpleSchema 重用数组模式
【发布时间】:2017-09-24 10:42:03
【问题描述】:

我正在尝试在 node-simple-schema 中定义 GeoJSON 架构以在我的应用程序中使用。

我正在尝试定义位置、线串、衬线和多边形数组,以便稍后在定义点、线串、.. 几何图形时使用它们。

这是我现在正在做的,但它不起作用。

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',
  },
});

当我尝试像这样进行验证时,我得到一个错误。

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

Point.validate(PointExample);

【问题讨论】:

    标签: node.js meteor geojson simple-schema


    【解决方案1】:

    提取的模式匹配具有键和值的对象。因此,以下提供了关键“位置”验证。

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

    【讨论】:

    • 是的,我知道这是被定义的模式,而不是我真正想要的模式。如果您了解我想要实现的目标,您知道获得我想要的 Schema 的方法吗?
    猜你喜欢
    • 2017-10-22
    • 1970-01-01
    • 2020-01-06
    • 1970-01-01
    • 1970-01-01
    • 2019-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多