Пример #1
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"}),
     })
Пример #2
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"}),
     })
Пример #3
0
 def test_anyof(self):
     self._testValidation(V.AnyOf("integer", {"foo": "integer"}),
                          valid=[1, {
                              "foo": 1
                          }],
                          invalid=[{
                              "foo": 1.1
                          }])
Пример #4
0
    def test_error_message_json_type_names(self):
        V.set_name_for_types("null", type(None))
        V.set_name_for_types("integer", int, long)
        V.set_name_for_types("number", float)
        V.set_name_for_types("string", str, unicode)
        V.set_name_for_types("array", list, collections.Sequence)
        V.set_name_for_types("object", dict, collections.Mapping)

        self._testValidation(
            {
                "+foo": "number",
                "?bar": ["integer"],
                "?baz": V.AnyOf("number", ["number"]),
                "?opt": "?string"
            },
            errors=
            [(42, "Invalid value 42 (integer): must be object"),
             ({},
              "Invalid value {} (object): missing required properties: ['foo']"
              ),
             ({
                 "foo": "3"
             }, "Invalid value '3' (string): must be number (at foo)"),
             ({
                 "foo": None
             }, "Invalid value None (null): must be number (at foo)"),
             ({
                 "foo": 3,
                 "bar": None
             }, "Invalid value None (null): must be array (at bar)"),
             ({
                 "foo": 3,
                 "bar": [1, "2", 3]
             }, "Invalid value '2' (string): must be integer (at bar[1])"),
             ({
                 "foo": 3,
                 "baz": "23"
             },
              "Invalid value '23' (string): must be number or must be array (at baz)"
              ),
             ({
                 "foo": 3,
                 "opt": 12
             }, "Invalid value 12 (integer): must be string (at opt)")])
Пример #5
0
    def test_humanized_names(self):
        class DummyValidator(V.Validator):
            name = "dummy"

            def validate(self, value, adapt=True):
                return value

        self.assertEqual(DummyValidator().humanized_name, "dummy")
        self.assertEqual(
            V.Nullable(DummyValidator()).humanized_name, "dummy or null")
        self.assertEqual(
            V.AnyOf("boolean", DummyValidator()).humanized_name,
            "boolean or dummy")
        self.assertEqual(
            V.AllOf("boolean", DummyValidator()).humanized_name,
            "boolean and dummy")
        self.assertEqual(
            V.ChainOf("boolean", DummyValidator()).humanized_name,
            "boolean chained to dummy")
        self.assertEqual(Date().humanized_name, "date or datetime")
Пример #6
0
import msgpack
import toml
import valideer as V
import zmq
from flask import Flask, request
from werkzeug.exceptions import (BadRequest, InternalServerError,
                                 ServiceUnavailable, Unauthorized)

import xbbs.messages as msgs
import xbbs.util as xutils

app = Flask(__name__)
zctx = zmq.Context.instance()


@V.accepts(x=V.AnyOf("string", ["string"]))
def _list_wrap(x):
    if not isinstance(x, str):
        return x
    return [x]


with V.parsing(required_properties=True,
               additional_properties=V.Object.REMOVE):
    CONFIG_VALIDATOR = V.parse({
        "coordinator_endpoint":
        xutils.Endpoint(xutils.Endpoint.Side.CONNECT),
        "?coordinator_timeout":
        "integer",
        "?start_delay":
        "integer",
Пример #7
0
def check_output_logged(cmd, **kwargs):
    _kwargs = kwargs.copy()
    if "input" in _kwargs:
        del _kwargs["input"]
    log.info("running command {} (params {})", cmd, _kwargs)
    return subprocess.check_output(cmd, **kwargs)


# more properties are required than not
with V.parsing(required_properties=True,
               additional_properties=V.Object.REMOVE):

    @V.accepts(x=V.AnyOf(
        xutils.Endpoint(), {
            "bind": xutils.Endpoint(xutils.Endpoint.Side.BIND),
            "connect": xutils.Endpoint(xutils.Endpoint.Side.CONNECT)
        }))
    def _receive_adaptor(x):
        if isinstance(x, str):
            return {"bind": x, "connect": x}
        return x

    @V.accepts(x="string")
    def _path_exists(x):
        return os.access(x, os.R_OK)

    CONFIG_VALIDATOR = V.parse({
        "command_endpoint":
        V.AdaptBy(_receive_adaptor),
        "project_base":
Пример #8
0
class WorkMessage(BaseMessage):
    project = attr.ib()
    git = attr.ib()
    revision = attr.ib()
    _validator = V.parse({
        "project": "string",
        "git": "string",
        "revision": "string"
    })


PKG_TOOL_VALIDATOR = V.parse(
    V.Mapping(
        "string", {
            "version": "string",
            "architecture": V.AnyOf("string", V.AdaptBy(xutils.list_to_set)),
        }))


@attr.s
class JobMessage(BaseMessage):
    @staticmethod
    def _filter(x, v):
        return v is not None

    project = attr.ib()
    job = attr.ib()
    repository = attr.ib()
    revision = attr.ib()
    output = attr.ib()
    build_root = attr.ib()