Skip to content
This repository has been archived by the owner on Jul 24, 2018. It is now read-only.

Commit

Permalink
Validation facilities for detail_values.
Browse files Browse the repository at this point in the history
  • Loading branch information
ralphbean committed Jan 17, 2014
1 parent 183def9 commit 9af3ddf
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions fmn/lib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import inspect
import logging
import re

import bs4
import docutils.examples
Expand All @@ -16,6 +17,10 @@

log = logging.getLogger(__name__)

irc_regex = r'^[\w-]+$'
email_regex = r'^([a-zA-Z0-9_\-\.]+)@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$'
gcm_regex = r'^[\w-]+$'


def recipients(session, config, valid_paths, message):
""" The main API function.
Expand Down Expand Up @@ -102,3 +107,19 @@ def strip_anchor_tags(soup):
yield tag.string
else:
yield tag


def validate_detail_value(ctx, value):
if ctx.name == 'irc':
if re.match(irc_regex, value) is None:
raise ValueError("irc nick must be alphanumeric")
elif ctx.name == 'email':
if re.match(email_regex, value) is None:
raise ValueError("value must be an email address")
elif ctx.name == 'gcm':
if re.match(gcm_regex, value) is None:
raise ValueError("not a valid android registration id")
else:
raise NotImplementedError("No validation scheme for %r" % ctx.name)
# Happy.
return

0 comments on commit 9af3ddf

Please sign in to comment.