简体   繁体   中英

Meteor mdg:validated-method without using simple schema

Well,

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

And at the same time, I'm not really sure I'd like to use this in my project.

I am writing an ES6 React based Meteor application therefor I'd like to make use of The "Advanced" way to create meteor methods . However, I don't want to use aldeed:simple-schema at all.

Is there a way to put in a custom validation method here?

This doesn't work:

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

It produces the following error:

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

Is there a different way to write this?

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.

Anyone out there know how to get around this?

Obviously googling has not turned up any results, I've been looking at this problem now for more than three days.

You could re-implement SimpleSchema's API, but why?

SimpleSchema is a de facto standard in Meteor, like Mongoose for Node<->MongoDB. There are alternatives in the works, but I think you're pushing the bleeding edge.

If you've got work to do, use SimpleSchema.

From the Meteor Forums :

Firstly, the Meteor method should expect an object like so:

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

And then your method would be defined as such: (Note, that validate() { } is empty, before I was returning true - this was the problem I was having)

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
  }
};

When you're ready to validate the arguments, just change validate accordingly:

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

And this way we can avoid using Simple-Schema while it works out it's transition from 1.0 to 2.0 (Good luck Simple Schema!!!)

And thanks to ffxsam for providing the solution on the meteor forums!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM