Meteor mdg:validated-不使用简单模式的方法

Meteor mdg:validated-method without using simple schema

嗯,

Simple Schema is in the middle of transitioning to a new version 2.0

同时,我不太确定是否要在我的项目中使用它。

我正在编写一个基于 ES6 React 的 Meteor 应用程序,因此我想使用 The "Advanced" way to create meteor methods。但是,我根本不想使用 aldeed:simple-schema。

有没有办法在这里输入自定义验证方法?

这不起作用:

export const addSuggestion = new ValidatedMethod({
  name: 'suggestion.add',
  validate() {
    return true
  },
  run(incommingSuggestion) {
    // do things which are not relevant to this question.
  }
})

它产生以下错误:

Error: Returning from validate doesn't do anything; perhaps you meant to throw an error?(…)

有没有其他的写法?

Perhaps I need to not use this validated-method, instead I should maybe write it out long-form? Edit: Edit: This works, you can write everything out long-form with all of this extra boiler plate if you want to avoid validation all together for the time being. For now this is the solution I will use until I finally decide on how I will be validating everything. I'm not sure what to do right now because Simple Schema is in transition. - Actually that doesn't work, it for some reason never returns a value and I couldn't get around that.

有人知道如何解决这个问题吗?

显然谷歌搜索没有找到任何结果,我已经研究了三天多了。

来自Meteor Forums

首先,Meteor method 应该期待这样的对象:

myMethod.call({
  arg1: 'hello',
  arg2: 25,
}, (error, result) => { /* do stuff */ });

然后您的方法将定义为:(注意, validate() { } 是空的,在我返回之前 true -这就是我遇到的问题)

import { ValidatedMethod } from 'meteor/mdg:validated-method';
import { check } from 'meteor/check';

export const myMethod = new ValidatedMethod({
  name: 'myMethod',
  validate() { }, // always valid
  run({ arg1, arg2 }) {
    // do things
  }
};

当您准备好验证参数时,只需相应地更改验证即可:

validate(opts) {
  check(opts, {
    arg1: String,
    arg2: Number,
  }
},

这样我们就可以避免在从 1.0 过渡到 2.0 时使用 Simple-Schema(祝 Simple Schema 好运!!!)

感谢流星论坛上的ffxsam for providing the solution

您可以重新实现 SimpleSchema 的 API,但为什么呢?

SimpleSchema 是 Meteor 中事实上的标准,就像 Mongoose for Node<->MongoDB。作品中有替代方案,但我认为你正在推动最前沿。

如果您有工作要做,请使用 SimpleSchema。