简体   繁体   English

流星SimpleSchema不会引发错误

[英]Meteor SimpleSchema does not throw error

I'm trying to implement checks in Meteor method, but facing some strange behavior of SimpleSchema package (i'm using latest one which is 1.3.3 at the moment); 我正在尝试在Meteor方法中实现检查,但是面临一些SimpleSchema包的奇怪行为(我正在使用最新的1.3.3版本)。

From the Docs: 从文档中:

Call mySimpleSchema.validate(doc) to validate doc against the schema and throw a ValidationError if invalid. 调用mySimpleSchema.validate(doc)以针对架构验证doc,如果无效,则抛出ValidationError。 This is like check(doc, mySimpleSchema) but without the check dependency and with the ability to pass full schema error details back to a callback on the client. 这类似于check(doc,mySimpleSchema),但是没有检查依赖项,并且能够将完整的模式错误详细信息传递回客户端上的回调。

I defining a simple Schema like this: 我定义了一个简单的模式,如下所示:

var mySchema = new SimpleSchema({ name: {type: String} });

var invalidDoc = { name: 123 };

However mySchema object does not have "validate" method. 但是,mySchema对象没有“验证”方法。 I can only call validation using 我只能使用

mySchema.namedContext().validate(invalidDoc);

This method return false, but didn't throw any exception. 此方法返回false,但未引发任何异常。 The only way to go about I found so far is to call Meteor "check" function like this: check(invaidDoc, mySchema) And this does work as expected. 到目前为止,我发现的唯一方法是调用Meteor的“ check”函数,如下所示: check(invaidDoc, mySchema)并且这确实按预期工作。

So, my question is: how to perform checking of object in Meteor method context, without using check(); 因此,我的问题是:如何在流星方法上下文中执行对象检查,而无需使用check();

Meteor.methods({
  'myMethod'(someObject) {

    var schema = new SimpleSchema({
        name: { type: String }
    });

    schema.namedContext().validate(someObject); //DOES NOT THROW!

    console.log('This should not be here!');
  }
});

This feature has apparently been added in version 1.4.0 of the SimpleSchema package, so it is not available yet in v1.3.3. 显然,此功能在SimpleSchema软件包的1.4.0版中添加 ,因此v1.3.3中尚不可用。 I guess you will have to use check() until 1.4.0 hits atmosphere! 我猜你将不得不使用check()直到1.4.0达到大气!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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