def schema_from_data(data, allow_empty):
    if isinstance(data, dict):
        if len(data) == 0:
            if allow_empty:
                return EmptyDict()
            raise YAMLSerializationError(
                "Empty dicts are not serializable to StrictYAML unless schema is used."
            )
        return Map({
            key: schema_from_data(value, allow_empty)
            for key, value in data.items()
        })
    elif isinstance(data, list):
        if len(data) == 0:
            if allow_empty:
                return EmptyList()
            raise YAMLSerializationError(
                "Empty lists are not serializable to StrictYAML unless schema is used."
            )
        return FixedSeq([schema_from_data(item, allow_empty) for item in data])
    elif isinstance(data, bool):
        return Bool()
    elif isinstance(data, int):
        return Int()
    elif isinstance(data, float):
        return Float()
    else:
        return Str()
Exemple #2
0
    def __init__(self, validator, key_validator=None):
        self._validator = validator
        self._key_validator = Str() if key_validator is None else key_validator
        assert isinstance(
            self._key_validator,
            ScalarValidator), "key validator must be ScalarValidator"

        self._validator_dict = {
            key.key if isinstance(key, Optional) else key: value
            for key, value in validator.items()
        }

        self._required_keys = [
            key for key in validator.keys() if not isinstance(key, Optional)
        ]

        for key_val, value_val in validator.items():
            if isinstance(key_val, Optional):
                if key_val.default is not None:
                    try:
                        value_val.to_yaml(key_val.default)
                    except YAMLSerializationError as error:
                        raise InvalidOptionalDefault(
                            "Optional default for '{}' failed validation:\n  {}"
                            .format(key_val.key, error))

        self._defaults = {
            key.key: key.default
            for key in validator.keys()
            if isinstance(key, Optional) and key.default is not None
        }
Exemple #3
0
def schema_from_data(document):
    if isinstance(document, CommentedMap):
        return Map({key: schema_from_data(value) for key, value in document.items()})
    elif isinstance(document, CommentedSeq):
        return FixedSeq([schema_from_data(item) for item in document])
    else:
        return Str()
    def __init__(self, validator, key_validator=None):
        self._validator = validator
        self._key_validator = Str() if key_validator is None else key_validator
        assert isinstance(self._key_validator, ScalarValidator), \
            "key validator must be ScalarValidator"

        self._validator_dict = {
            key.key if isinstance(key, Optional) else key: value for key, value in validator.items()
        }

        self._required_keys = [key for key in validator.keys() if not isinstance(key, Optional)]
Exemple #5
0
def schema_from_data(data):
    if isinstance(data, dict):
        if len(data) == 0:
            raise YAMLSerializationError(
                "Empty dicts are not serializable to StrictYAML unless schema is used."
            )
        return Map(
            {key: schema_from_data(value)
             for key, value in data.items()})
    elif isinstance(data, list):
        if len(data) == 0:
            raise YAMLSerializationError(
                "Empty lists are not serializable to StrictYAML unless schema is used."
            )
        return FixedSeq([schema_from_data(item) for item in data])
    else:
        return Str()
Exemple #6
0
    def __init__(self, validator, key_validator=None, doc=None):
        super().__init__(doc=doc)
        self._validator = validator
        self._key_validator = Str() if key_validator is None else key_validator
        assert isinstance(
            self._key_validator,
            ScalarValidator), "key validator must be ScalarValidator"

        self._validator_dict = {
            key.key if isinstance(key, Optional) else key: value
            for key, value in validator.items()
        }

        self._required_keys = [
            key for key in validator.keys() if not isinstance(key, Optional)
        ]

        for key_val, value_val in validator.items():
            if isinstance(key_val, Optional):
                value_val.doc["Optional"] = True
                if key_val.default is not None:
                    try:
                        value_val.to_yaml(key_val.default)
                    except YAMLSerializationError as error:
                        raise InvalidOptionalDefault(
                            "Optional default for '{}' failed validation:\n  {}"
                            .format(key_val.key, error))

        self._defaults = {
            key.key: key.default
            for key in validator.keys()
            if isinstance(key, Optional) and key.default is not None
        }

        if self._key_validator.doc != {}:
            self.doc["_key"] = self._key_validator.doc
        d = {}
        for k, v in validator.items():
            if isinstance(k, Optional):
                d[k.key] = v.doc
            else:
                d[k] = v.doc
        self.doc["keys"] = d
        self.doc["_type"] = "Object"
 def key_validator(self):
     return Str()