Exemple #1
0
def _assert_valid_schema_level(schema, allow_default, deduce_required):
    schema = _build_level_schema(schema)

    if MK.Required in schema:
        warnings.warn(_REQUIRED_DEPRECATION_MSG, DeprecationWarning)

    if MK.Default in schema and not allow_default:
        fmt = "Default value is only allowed for contents in NamedDict"
        raise ValueError(fmt)

    meta_schema = _build_meta_schema(deduce_required, schema.get(MK.Type))
    level_validator = configsuite.Validator(meta_schema)
    result = level_validator.validate(schema)

    if not result.valid:
        for error in result.errors:

            def gen_err_msg(error):
                return "{} at {}".format(error.msg, error.key_path)

            if isinstance(error, configsuite.MissingKeyError):
                raise KeyError(gen_err_msg(error))
            elif isinstance(error, configsuite.UnknownKeyError):
                raise KeyError(gen_err_msg(error))
            elif isinstance(error, configsuite.InvalidTypeError):
                raise TypeError(gen_err_msg(error))
            elif isinstance(error, configsuite.InvalidValueError):
                raise ValueError(gen_err_msg(error))
            else:
                raise AssertionError(
                    "Internal error: Unknown validation error: {}".format(
                        error))
Exemple #2
0
def _assert_valid_schema_level(schema):
    level_schema = copy.deepcopy(schema)
    if MK.Content in level_schema:
        level_schema.pop(MK.Content)

    level_validator = configsuite.Validator(_META_SCHEMA)
    result = level_validator.validate(level_schema)

    if not result.valid:
        for error in result.errors:
            gen_err_msg = lambda error: "{} at {}".format(
                error.msg, error.key_path)

            if isinstance(error, configsuite.MissingKeyError):
                raise KeyError(gen_err_msg(error))
            elif isinstance(error, configsuite.UnknownKeyError):
                raise KeyError(gen_err_msg(error))
            elif isinstance(error, configsuite.InvalidTypeError):
                raise TypeError(gen_err_msg(error))
            elif isinstance(error, configsuite.InvalidValueError):
                raise ValueError(gen_err_msg(error))
            else:
                raise AssertionError(
                    "Internal error: Unknown validation error: {}".format(
                        error))
Exemple #3
0
    def _validate_final(self):
        if not self.readable:
            err_msg = "Internal error: Not readable when doing final validation"
            raise AssertionError(err_msg)
        self._assert_state()

        validator = configsuite.Validator(self._schema)
        val_res = validator.validate(self._merged_config,
                                     self._validation_context)
        self._valid &= val_res.valid
        self._errors += val_res.errors
Exemple #4
0
    def _validate_readability(self, layers):
        readable_errors = (configsuite.UnknownKeyError,
                           configsuite.MissingKeyError)

        def _not_container(schema):
            return not isinstance(schema[MK.Type],
                                  configsuite.types.Collection)

        container_validator = configsuite.Validator(
            self._schema, stop_condition=_not_container)
        container_errors = []
        for idx, layer in enumerate(layers):
            val_res = container_validator.validate(layer)
            container_errors += [
                error.create_layer_error(idx) for error in val_res.errors
                if not isinstance(error, readable_errors)
            ]

        self._readable &= len(container_errors) == 0
        self._valid &= self._readable
        self._errors += tuple(container_errors)