Match question - urls, emails and regexs

Hey,

Quick question, is there a community favoured way to use check and Match for URL’s, emails or regexes? I was wondering if there is a nice way to do this or some smart cookie has done it earlier.

I could write one, but loathe writing code for simple stuff like this, because I know it is quite complex really with the edge cases, and I’m not that good a code writer to write super elegant code.

Thanks so much.

Tat

SimpleSchema.RegEx.Email for emails (uses a permissive regEx recommended by W3C, which most browsers use)

yeah - i mean I already use this. So for example, this is what I am talking about. Lets say I have a db schema called entities in my project. Then I use something like,

/**
 * @function checkEntity
 * @param { String } entityId
 * @throws { Meteor.Error }
 */
export function checkEntity(entityId) {
  check(entityId, String);
  const entityCursor = Entities.find(
    { _id: entityId },
    { skip: 0, limit: 1 },
  ).fetch().length;

  if (entityCursor === 0) {
    throw new Meteor.Error(`checkEntity: ${entityId} check failure...`);
  }
}

So I could put this at the start of any function which needs an entity id for something. I could use it like so:

/**
 * @function getEFSGlobalActions
 * @param { String } entityId
 * @returns { Array } globalActions
 * @throws { Meteor.Error }
 */
export function getEFSGlobalActions(entityId) {
  checkEntity(entityId); // using the function here.

  return getEntityFlowSettings(entityId).globalActions;
}

As an added complication, this means my unit tests have to be much nicer and more focused, and my integration tests are more likely to pick up errors. The whole thing revolves around a pattern similar to using check or Match.OneOf nicely.

So if I had:

export function someFunc(entityId, stringArg1, stringArg2, boolArg1, objArg1) {
checkEntity(entityId);
check(stringArg1, String);
// etc etc
// this is basically where I want to use something similar to a check 
// for emails and regex's
}

I’m trying to work out how to do this nicely. With Match.OneOf etc I can do really cool object validations, which simplify server side code significantly. But cannot do emails, regex’s etc. I want to use this for object validation for UI stuff, not so much at the db level.

Thanks.
Tat