Check

The check package includes pattern checking functions useful for checking the types and structureof variables and an extensible library of patterns to specify which types you areexpecting.

To add check (or Match) to your application, run this command in your terminal:

  1. meteor add check

Anywhere

check(value, pattern)

import { check } from 'meteor/check' (check/match.js, line 20)

Check that a value matches a pattern.If the value does not match the pattern, throw a Match.Error.

Particularly useful to assert that arguments to a function have the righttypes and structure.

Arguments

  • valueAny
  • The value to check

  • patternMatch Pattern

  • The pattern to match value against

Meteor methods and publish functions can take arbitrary EJSON types as arguments, but mostfunctions expect their arguments to be of a particular type. check is a lightweight function forchecking that arguments and other values are of the expected type. For example:

  1. Meteor.publish('chatsInRoom', function (roomId) {
  2. // Make sure `roomId` is a string, not an arbitrary Mongo selector object.
  3. check(roomId, String);
  4. return Chats.find({ room: roomId });
  5. });
  6. Meteor.methods({
  7. addChat(roomId, message) {
  8. check(roomId, String);
  9. check(message, {
  10. text: String,
  11. timestamp: Date,
  12. // Optional, but if present must be an array of strings.
  13. tags: Match.Maybe([String])
  14. });
  15. // Do something with the message...
  16. }
  17. });

If the match fails, check throws a Match.Error describing how it failed. Ifthis error gets sent over the wire to the client, it will appear only asMeteor.Error(400, 'Match Failed'). The failure details will be written to theserver logs but not revealed to the client.

Anywhere

Match.test(value, pattern)

import { Match } from 'meteor/check' (check/match.js, line 107)

Returns true if the value matches the pattern.

Arguments

  • valueAny
  • The value to check

  • patternMatch Pattern

  • The pattern to match value against

Match.test can be used to identify if a variable has a certain structure.

  1. // Will return true for `{ foo: 1, bar: 'hello' }` or similar.
  2. Match.test(value, { foo: Match.Integer, bar: String });
  3. // Will return true if `value` is a string.
  4. Match.test(value, String);
  5. // Will return true if `value` is a string or an array of numbers.
  6. Match.test(value, Match.OneOf(String, [Number]));

This can be useful if you have a function that accepts several different kindsof objects, and you want to determine which was passed in.

Match Patterns

The following patterns can be used as pattern arguments tocheck and Match.test:

  • Match.Any
  • Matches any value.

  • String, Number, Boolean, undefined, null

  • Matches a primitive of the given type.

  • Match.Integer

  • Matches a signed 32-bit integer. Doesn’t match Infinity, -Infinity, or NaN.

  • [pattern]

  • A one-element array matches an array of elements, each of which matchpattern. For example, [Number] matches a (possibly empty) array of numbers;[Match.Any] matches any array.

  • { key1: pattern1, key2: pattern2, … }

  • Matches an Object with the given keys, with values matching the given patterns.If any pattern is a Match.Maybe or Match.Optional, that key does not need to existin the object. The value may not contain any keys not listed in the pattern.The value must be a plain Object with no special prototype.
  • Match.ObjectIncluding({ key1: pattern1, key2: pattern2, … })
  • Matches an Object with the given keys; the value may also have other keyswith arbitrary values.
  • Object
  • Matches any plain Object with any keys; equivalent toMatch.ObjectIncluding({}).

  • Match.Maybe(pattern)

  • Matches either undefined, null, or pattern. If used in an object, matches only if the key isnot set as opposed to the value being set to undefined or null. This set of conditions waschosen because undefined arguments to Meteor Methods are converted to null when sent over thewire.
  1. // In an object
  2. const pattern = { name: Match.Maybe(String) };
  3. check({ name: 'something' }, pattern); // OK
  4. check({}, pattern); // OK
  5. check({ name: undefined }, pattern); // Throws an exception
  6. check({ name: null }, pattern); // Throws an exception
  7. // Outside an object
  8. check(null, Match.Maybe(String)); // OK
  9. check(undefined, Match.Maybe(String)); // OK
  • Match.Optional(pattern)
  • Behaves like Match.Maybe except it doesn’t accept null. If used in an object, the behavior isidentical to Match.Maybe.

  • Match.OneOf(pattern1, pattern2, …)

  • Matches any value that matches at least one of the provided patterns.

  • Any constructor function (eg, Date)

  • Matches any element that is an instance of that type.

  • Match.Where(condition)

  • Calls the function condition with the value as the argument. If condition_returns true, this matches. If _condition throws a Match.Error or returnsfalse, this fails. If condition throws any other error, that error is thrownfrom the call to check or Match.test. Examples:
  1. check(buffer, Match.Where(EJSON.isBinary));
  2. const NonEmptyString = Match.Where((x) => {
  3. check(x, String);
  4. return x.length > 0;
  5. });
  6. check(arg, NonEmptyString);