检查
有关如何使用 check(Meteor 的类型检查库)的文档。
check
包包含用于检查变量的类型和结构的有用模式检查函数,以及一个可扩展的模式库,用于指定您期望的类型。
要将check
(或Match
)添加到您的应用程序中,请在您的终端中运行以下命令
meteor add check
check
摘要
检查值是否与模式匹配。如果值与模式不匹配,则抛出Match.Error
。默认情况下,它会在遇到第一个错误时立即抛出。传入 { throwAllErrors: true } 以抛出所有错误。
特别有用的是断言函数的参数具有正确的类型和结构。
参数
源代码名称 | 类型 | 描述 | 必填 |
---|---|---|---|
value | 任意 | 要检查的值 | 是 |
pattern | MatchPattern | 要将 | 是 |
options | 对象 | check 的其他选项 | 否 |
Meteor 方法和发布函数可以将任意EJSON类型作为参数,但大多数函数期望其参数为特定类型。check
是一个轻量级函数,用于检查参数和其他值是否为预期类型。例如
import { check } from "meteor/check";
import { Meteor } from "meteor/meteor";
Meteor.publish("chatsInRoom", function (roomId) {
// Make sure `roomId` is a string, not an arbitrary Mongo selector object.
check(roomId, String);
return Chats.find({ room: roomId });
});
Meteor.methods({
addChat(roomId, message) {
check(roomId, String);
check(message, {
text: String,
timestamp: Date,
// Optional, but if present must be an array of strings.
tags: Match.Maybe([String]),
});
// Do something with the message...
},
});
如果匹配失败,check
将抛出一个Match.Error
,描述其失败方式。如果此错误通过网络发送到客户端,它将仅显示为Meteor.Error(400, 'Match Failed')
。故障详细信息将写入服务器日志,但不会泄露给客户端。
Match.test
可用于识别变量是否具有特定的结构。
import { Match } from "meteor/check";
// Will return true for `{ foo: 1, bar: 'hello' }` or similar.
Match.test(value, { foo: Match.Integer, bar: String });
// Will return true if `value` is a string.
Match.test(value, String);
// Will return true if `value` is a string or an array of numbers.
Match.test(value, Match.OneOf(String, [Number]));
如果您有一个接受几种不同类型的对象的函数,并且您想确定传递了哪一个,这将非常有用。
匹配模式
以下模式可用作check
和Match.test
的模式参数
Match.Any
匹配任何值。
import { Match } from "meteor/check";
// Will return true for any value.
Match.test(value, Match.Any);
String
、Number
、Boolean
、undefined
、null
匹配给定类型的基本类型。
import { Match } from "meteor/check";
let result;
// Will return true if `value` is a string.
result = Match.test(value, String);
console.log(result); // true
// Will return true if `value` is a number.
result = Match.test(value, Number);
console.log(result); // true
// Will return true if `value` is a boolean.
result = Match.test(value, Boolean);
console.log(result); // true
Match.Integer
匹配有符号的 32 位整数。不匹配Infinity
、-Infinity
或NaN
。
import { Match } from "meteor/check";
let result;
// Will return true if `value` is an integer.
result = Match.test(value, Match.Integer);
console.log(result); // true
[pattern]
一个元素数组匹配一个元素数组,其中每个元素都与pattern匹配。例如,[Number]
匹配一个(可能为空的)数字数组;[Match.Any]
匹配任何数组。
import { Match } from "meteor/check";
let result;
// Will return true if `value` is an array of numbers.
result = Match.test(value, [Number]);
console.log(result); // true
{ key1: pattern1, key2: pattern2, ... }
匹配具有给定键的对象,其值与给定模式匹配。如果任何pattern是Match.Maybe
或Match.Optional
,则该键不需要存在于对象中。该值可能不包含模式中未列出的任何键。该值必须是一个普通对象,没有特殊的原型。
import { Match } from "meteor/check";
let result;
// Will return true if `value` is an object with keys 'foo' and 'bar'.
result = Match.test(value, { foo: String, bar: Number });
console.log(result); // true
Match.ObjectIncluding({ key1: pattern1, key2: pattern2, ... })
匹配具有给定键的对象;该值也可以具有其他键和任意值。
import { Match } from "meteor/check";
let result;
// Will return true if `value` is an object with keys 'foo' and 'bar'.
result = Match.test(value, Match.ObjectIncluding({ foo: String, bar: Number }));
console.log(result); // true
Object
匹配任何具有任何键的普通对象;等效于Match.ObjectIncluding({})
。
import { Match } from "meteor/check";
let result;
// Will return true if `value` is an object.
result = Match.test(value, Object);
console.log(result); // true
Match.Maybe(pattern)
匹配undefined
、null
或pattern。如果在对象中使用,则仅当未设置键时才匹配,而不是值设置为undefined
或null
。选择这组条件是因为发送到网络时,Meteor 方法的undefined
参数会转换为null
。
import { Match, check } from "meteor/check";
// In an object
const pattern = { name: Match.Maybe(String) };
check({ name: "something" }, pattern); // OK
check({}, pattern); // OK
check({ name: undefined }, pattern); // Throws an exception
check({ name: null }, pattern); // Throws an exception
// Outside an object
check(null, Match.Maybe(String)); // OK
check(undefined, Match.Maybe(String)); // OK
Match.Optional(pattern)
行为类似于Match.Maybe
,但它不接受null
。如果在对象中使用,则行为与Match.Maybe
相同。
Match.OneOf(pattern1, pattern2, ...)
匹配与提供的模式中的至少一个模式匹配的任何值。
任何构造函数(例如,Date
)
匹配该类型的任何实例。
import { Match } from "meteor/check";
let result;
// Will return true if `value` is a Date.
result = Match.test(value, Date);
Match.Where(condition)
使用值为参数的函数condition进行调用。如果condition返回 true,则匹配。如果condition抛出Match.Error
或返回 false,则失败。如果condition抛出任何其他错误,则该错误将从对check
或Match.test
的调用中抛出。示例
import { Match, check } from "meteor/check";
check(buffer, Match.Where(EJSON.isBinary));
const NonEmptyString = Match.Where((x) => {
check(x, String);
return x.length > 0;
});
check(arg, NonEmptyString);