Improve docs for check, moving it to the Meteor.methods section

This commit is contained in:
Sashko Stubailo
2014-09-02 20:16:15 -07:00
parent e0f96cce55
commit afef45585d
5 changed files with 48 additions and 37 deletions

View File

@@ -429,6 +429,36 @@ even if the method's writes are not available yet, you can specify an
passed as an array rather than directly as arguments, and you can specify
options about how the client executes the method.
{{> autoApiBox "check"}}
Meteor methods and publish functions take arbitrary [EJSON](#ejson) types as
arguments, but most arguments are expected to be of a particular type. `check`
is a lightweight function for checking that arguments and other
values are of the expected type. For example:
Meteor.publish("chats-in-room", 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: function (roomId, message) {
check(roomId, String);
check(message, {
text: String,
timestamp: Date,
// Optional, but if present must be an array of strings.
tags: Match.Optional([String])
});
// ... do something with the message ...
}});
If the match fails, `check` throws a `Match.Error` describing how it failed. If
this error gets sent over the wire to the client, it will appear only as
`Meteor.Error(400, "Match Failed")`; the failure details will be written to the
server logs but not revealed to the client.
<h2 id="connections"><span>Server connections</span></h2>
These functions manage and inspect the network connection between the
@@ -2717,41 +2747,16 @@ public API.
<h2 id="match"><span>Match</span></h2>
Meteor methods and publish functions take arbitrary [EJSON](#ejson) types as
arguments, but most arguments are expected to be of a particular type. Meteor's
`check` package is a lightweight library for checking that arguments and other
values are of the expected type. For example:
The `check` package which exports the [`check`](#check) function for checking
method arguments also includes an extensible library of patterns to specify
which types and values you are expecting.
Meteor.publish("chats-in-room", function (roomId) {
// Make sure roomId is a string, not an arbitrary mongo selector object.
check(roomId, String);
return Chats.find({room: roomId});
});
{{> autoApiBox "Match.test"}}
Meteor.methods({addChat: function (roomId, message) {
check(roomId, String);
check(message, {
text: String,
timestamp: Date,
// Optional, but if present must be an array of strings.
tags: Match.Optional([String])
});
{{> apiBoxTitle name="Match Patterns" id="matchpatterns"}}
// ... do something with the message ...
}});
{{> autoApiBox "check"}}
If the match fails, `check` throws a `Match.Error` describing how it failed. If
this error gets sent over the wire to the client, it will appear only as
`Meteor.Error(400, "Match Failed")`; the failure details will be written to the
server logs but not revealed to the client.
{{> api_box match_test}}
{{#api_box matchpatterns}}
The following patterns can be used as pattern arguments to `check` and `Match.test`:
The following patterns can be used as pattern arguments to
[`check`](#check) and `Match.test`:
<dl>
@@ -2829,8 +2834,6 @@ from the call to `check` or `Match.test`. Examples:
{{/dtdd}}
</dl>
{{/api_box}}
<h2 id="timers"><span>Timers</span></h2>
Meteor uses global environment variables

File diff suppressed because one or more lines are too long

View File

@@ -153,7 +153,8 @@ var toc = [
],
"Meteor.Error",
"Meteor.call",
"Meteor.apply"
"Meteor.apply",
"check"
],
{name: "Server connections", id: "connections"}, [
@@ -288,7 +289,6 @@ var toc = [
],
"Match", [
"check",
"Match.test",
{name: "Match patterns", style: "noncode"}
],

View File

@@ -29,6 +29,7 @@
"EJSON.stringify",
"EJSON.toJSONValue",
"HTTP.call",
"Match.test",
"Meteor",
"Meteor.Collection",
"Meteor.Collection#allow",

View File

@@ -76,6 +76,13 @@ Match = {
// XXX maybe also implement a Match.match which returns more information about
// failures but without using exception handling or doing what check()
// does with _failIfArgumentsAreNotAllChecked and Meteor.Error conversion
/**
* @summary Returns true if the value matches the pattern.
* @locus Anywhere
* @param {Any} value The value to check
* @param {MatchPattern} pattern The pattern to match `value` against
*/
test: function (value, pattern) {
try {
checkSubtree(value, pattern);