def __init__(self, allow_extra):
     schema = {
         '+id': int,
         '+client_name': V.String(max_length=255),
         '+sort_index': float,
         'client_phone': V.Nullable(V.String(max_length=255)),
         'location': {'latitude': float, 'longitude': float},
         'contractor': V.Range(V.AdaptTo(int), min_value=1),
         'upstream_http_referrer': V.Nullable(V.String(max_length=1023)),
         '+grecaptcha_response': V.String(min_length=20, max_length=1000),
         'last_updated': V.AdaptBy(dateutil.parser.parse),
         'skills': V.Nullable(
             [
                 {
                     '+subject': str,
                     '+subject_id': int,
                     '+category': str,
                     '+qual_level': str,
                     '+qual_level_id': int,
                     'qual_level_ranking': V.Nullable(float, default=0),
                 }
             ],
             default=[],
         ),
     }
     self.validator = V.parse(schema, additional_properties=allow_extra)
Пример #2
0
 def setUp(self):
     V.Object.REQUIRED_PROPERTIES = True
     V.base.reset_type_names()
     self.complex_validator = self.parse({
         "n":
         "number",
         "?i":
         V.Nullable("integer", 0),
         "?b":
         bool,
         "?e":
         V.Enum(["r", "g", "b"]),
         "?d":
         V.AnyOf("date", "datetime"),
         "?s":
         V.String(min_length=1, max_length=8),
         "?p":
         V.Nullable(re.compile(r"\d{1,4}$")),
         "?l": [{
             "+s2": "string"
         }],
         "?t": (unicode, "number"),
         "?h":
         V.Mapping(int, ["string"]),
         "?o":
         V.NonNullable({"+i2": "integer"}),
     })
Пример #3
0
 def setUp(self):
     super(OptionalPropertiesTestValidator, self).setUp()
     V.Object.REQUIRED_PROPERTIES = False
     self.complex_validator = self.parse({
         "+n":
         "+number",
         "i":
         V.Nullable("integer", 0),
         "b":
         bool,
         "e":
         V.Enum(["r", "g", "b"]),
         "d":
         V.AnyOf("date", "datetime"),
         "s":
         V.String(min_length=1, max_length=8),
         "p":
         V.Nullable(re.compile(r"\d{1,4}$")),
         "l": [{
             "+s2": "string"
         }],
         "t": (unicode, "number"),
         "h":
         V.Mapping(int, ["string"]),
         "o":
         V.NonNullable({"+i2": "integer"}),
     })
Пример #4
0
 def test_string(self):
     for obj in "string", V.String, V.String():
         self._testValidation(obj,
                              valid=["foo", u"bar"],
                              invalid=[1, 1.1, {}, [], False, True])
Пример #5
0
 def test_string_max_length(self):
     self._testValidation(V.String(max_length=2),
                          valid=["", "f", u"fo"],
                          invalid=[u"foo", [1, 2, 3]])
Пример #6
0
 def test_string_min_length(self):
     self._testValidation(V.String(min_length=2),
                          valid=["foo", u"fo"],
                          invalid=[u"f", "", False])
Пример #7
0
    def _harvest_args(self, key, seed, value, multi):
        agg = value[0] if type(value) is tuple else None
        value = value[1] if type(value) is tuple else value

        # adapt to proper variable types
        # if value and multi and type(value) not in (list, tuple):
        #     value = [str(value).lower()]
        # if type(value) in (int, long, float, bool):
        #     value = str(value).lower()
        if multi and type(value) not in (list, tuple):
            multi = False

        # plant this arguments seed
        key = key if not agg else ("%s_%s" % (agg, key))
        seed["id"] = key
        self.plant(seed)

        # set the value
        # userkwargs[key] = value

        # add to the parser
        # parse key "+" if required
        pkey = ("+" + key) if seed.get('required') else key

        # validation methods
        # 1) choose
        if 'options' in seed:
            replace_with = []
            if seed.get('*') and value in ('*', 'all'):
                map(self.plant, seed['options'].values())

            else:
                for _value in array(value):
                    found = False
                    for pattern, _seed in seed['options'].iteritems():
                        if re.match(pattern, _value):
                            found = True

                            # replace the value provided
                            replace_with.append(_seed.get('value', _value))

                            # add the seed
                            self.plant(_seed)

                            # add the parser, though we have already parsed properly
                            break

                    # no seed found: invalid
                    if not found:
                        raise valideer.ValidationError("Invalid value", key)

            if type(value) in (list, tuple):
                value = replace_with
                validate = valideer.HomogeneousSequence(valideer.String())
            else:
                value = replace_with[0] if replace_with else ""
                validate = valideer.String()

        elif 'validator' in seed:
            validator = seed.get('validator')
            if validator == 'currency':
                # somehow change currency to respect stores... (future)
                validator = "float"
            validate = valideer.HomogeneousSequence(
                validator) if multi else validator

        else:
            raise EnvironmentError("Missing validation method for " + key)

        return {key: value}, {pkey: validate}