package com.microservices.common.core.utils;

import com.microservices.common.core.exception.ServiceException;
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.error.YAMLException;

import java.util.Base64;

public class YamlValidatorUtils {

    public static void validateYaml(String base64Yaml) {
        // 将base64编码的字符串解码为原始YAML字符串
        byte[] decodedBytes = Base64.getDecoder().decode(base64Yaml);
        String yamlString = new String(decodedBytes);

        // 创建YAML解析器
        Yaml yaml = new Yaml();

        try {
            // 尝试解析YAML字符串
            yaml.load(yamlString);
        } catch (YAMLException e) {
            // 捕获YAML格式错误，并返回错误提示信息
            throw new ServiceException("YAML格式错误");
        }
    }
}
