How to use the voluptuous.Invalid function in voluptuous

To help you get started, we’ve selected a few voluptuous examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github home-assistant / home-assistant / tests / components / template / test_fan.py View on Github external
async def test_set_invalid_osc(hass, calls):
    """Test set invalid oscillating when fan has valid osc."""
    await _register_components(hass)

    # Turn on fan
    await common.async_turn_on(hass, _TEST_FAN)

    # Set fan's osc to True
    await common.async_oscillate(hass, _TEST_FAN, True)

    # verify
    assert hass.states.get(_OSC_INPUT).state == "True"
    _verify(hass, STATE_ON, None, True, None)

    # Set fan's osc to None
    with pytest.raises(vol.Invalid):
        await common.async_oscillate(hass, _TEST_FAN, None)

    # verify osc is unchanged
    assert hass.states.get(_OSC_INPUT).state == "True"
    _verify(hass, STATE_ON, None, True, None)
github home-assistant / home-assistant / homeassistant / components / mqtt / __init__.py View on Github external
def valid_topic(value: Any) -> str:
    """Validate that this is a valid topic name/filter."""
    value = cv.string(value)
    try:
        raw_value = value.encode("utf-8")
    except UnicodeError:
        raise vol.Invalid("MQTT topic name/filter must be valid UTF-8 string.")
    if not raw_value:
        raise vol.Invalid("MQTT topic name/filter must not be empty.")
    if len(raw_value) > 65535:
        raise vol.Invalid(
            "MQTT topic name/filter must not be longer than " "65535 encoded bytes."
        )
    if "\0" in value:
        raise vol.Invalid("MQTT topic name/filter must not contain null " "character.")
    return value
github esphome / esphome / esphome / components / light / rgbww.py View on Github external
def validate_cold_white_colder(value):
    cw = value[CONF_COLD_WHITE_COLOR_TEMPERATURE]
    ww = value[CONF_WARM_WHITE_COLOR_TEMPERATURE]
    if cw > ww:
        raise vol.Invalid("Cold white color temperature cannot be higher than warm white")
    if cw == ww:
        raise vol.Invalid("Cold white color temperature cannot be the same as warm white")
    return value
github Mergifyio / mergify-engine / mergify_engine / web.py View on Github external
@app.errorhandler(voluptuous.Invalid)
def voluptuous_errors(error):
    # FIXME(sileht): remove error at payload root
    payload = voluptuous_error(error)
    payload["errors"] = []
    if isinstance(error, voluptuous.MultipleInvalid):
        payload["errors"].extend(map(voluptuous_error, error.errors))
    else:
        payload["errors"].extend(voluptuous_error(error))
    return flask.make_response(flask.jsonify(payload), 400)
github home-assistant / home-assistant / homeassistant / components / wemo.py View on Github external
def coerce_host_port(value):
    """Validate that provided value is either just host or host:port.

    Returns (host, None) or (host, port) respectively.
    """
    host, _, port = value.partition(':')

    if not host:
        raise vol.Invalid('host cannot be empty')

    if port:
        port = cv.port(port)
    else:
        port = None

    return host, port
github gnocchixyz / gnocchi / gnocchi / rest / __init__.py View on Github external
def Timespan(value):
    try:
        return utils.to_timespan(value)
    except ValueError as e:
        raise voluptuous.Invalid(e)
github home-assistant / home-assistant / homeassistant / components / mysensors.py View on Github external
def is_persistence_file(value):
    """Validate that persistence file path ends in either .pickle or .json."""
    if value.endswith(('.json', '.pickle')):
        return value
    else:
        raise vol.Invalid(
            '{} does not end in either `.json` or `.pickle`'.format(value))
github home-assistant / home-assistant / homeassistant / components / mqtt / __init__.py View on Github external
def valid_topic(value: Any) -> str:
    """Validate that this is a valid topic name/filter."""
    value = cv.string(value)
    try:
        raw_value = value.encode("utf-8")
    except UnicodeError:
        raise vol.Invalid("MQTT topic name/filter must be valid UTF-8 string.")
    if not raw_value:
        raise vol.Invalid("MQTT topic name/filter must not be empty.")
    if len(raw_value) > 65535:
        raise vol.Invalid(
            "MQTT topic name/filter must not be longer than " "65535 encoded bytes."
        )
    if "\0" in value:
        raise vol.Invalid("MQTT topic name/filter must not contain null " "character.")
    return value
github home-assistant / home-assistant / homeassistant / helpers / config_validation.py View on Github external
def validate(obj: Dict) -> Dict:
        """Test zero keys exist or one key exists in dict."""
        if not isinstance(obj, dict):
            raise vol.Invalid("expected dictionary")

        if len(set(keys) & set(obj)) > 1:
            raise vol.Invalid("must contain at most one of {}.".format(", ".join(keys)))
        return obj
github openstack / vitrage / vitrage / evaluator / template_validation / template_syntax_validator.py View on Github external
def f(v):
        if str(v) in action_types:
            return str(v)
        else:
            raise Invalid(msg or 120)
    return f