我在项目中只使用aldeed:simple-schema
包check
和audit-argument-checks
。使用我的简单模式的检查功能工作正常。
但后来我想使用 collection2。 集合 2 需要 npm 包simpl-schema
。 当我安装aldeed:collection2
并且 npm 包simpl-schema
时,我使用 SimpleSchema 的检查停止工作,现在显示以下错误:
匹配错误:字段标题中的未知键
Check(( 正在使用aldeed:simple-schema
而不是 npm 包simpl-schema
。
import SimpleSchema from 'simpl-schema';
NoteUpsertSchema = new SimpleSchema({
title: {
type: String,
max: 50
},
description: {
type: String,
max: 500
}
});
我的流星法
updateNote(noteId, note){
check(noteId, String);
check(note, NoteUpsertSchema);
// some code
}
我的软件包版本:
// Meteor packages
aldeed:collection2 3.0.0
audit-argument-checks 1.0.7
check 1.3.0*
// Npm package
"simpl-schema": "^1.5.0"
(I tried with simpl-schema: 1.4.3 same result.)
如何同时使用check
、audit-argument-checks
、simple-schema
和collection2
四个软件包?
感谢您的回答
你不能在 Meteorcheck
实用程序中使用NoteUpsertSchema
。
check
、audit-argument-checks
、simpl-schema
和collection2
同步工作得很好,互兼容就没有这样的问题。Check
只允许存在定义的参数,您可以根据这些参数交叉检查有效性。 单击此处了解check
允许类型的详细信息。
考虑到audit-argument-checks
,您需要使用下面作为示例的方法检查 Meteor 方法中传递的参数。若要避免在使用 SimpleSchema 验证 Meteor 方法参数时未检查所有参数的错误,必须在创建 SimpleSchema 实例时将 check 作为选项传递。
import SimpleSchema from 'simpl-schema';
import { check } from 'meteor/check';
import { Meteor } from 'meteor/meteor';
SimpleSchema.defineValidationErrorTransform(error => {
const ddpError = new Meteor.Error(error.message);
ddpError.error = 'validation-error';
ddpError.details = error.details;
return ddpError;
});
const myMethodObjArgSchema = new SimpleSchema({ name: String }, { check });
Meteor.methods({
myMethod(obj) {
myMethodObjArgSchema.validate(obj);
// Now do other method stuff knowing that obj satisfies the schema
},
});
确保aldeed:simple-schema
未列.meteor/versions
文件中。
此外,问题可能是从客户端发送一个完整的对象,并且只在 meteor 方法中验证它的某些字段。确保发送到方法的参数仅包含正在验证的内容,并且客户端代码中没有额外的字段。