io.convergence_platform.common.validations.ValidateYamlValidator Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of service-lib Show documentation
Show all versions of service-lib Show documentation
Holds the common functionality needed by all Convergence Platform-based services written in Java.
The newest version!
package io.convergence_platform.common.validations;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.Map;
public class ValidateYamlValidator implements ConstraintValidator {
public static void pathVariable(String value, String varName) {
boolean valid = true;
if ("".equals(value)) {
valid = false;
} else {
valid = validate(value);
}
ValidationHelper.raiseValidationErrorForPathVariable(valid, "yaml", varName);
}
@Override
public boolean isValid(String value, ConstraintValidatorContext context) {
return validate(value);
}
private static boolean validate(String value) {
if (value == null) {
return false;
}
try {
ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
mapper.readValue(value.getBytes(StandardCharsets.UTF_8), Map.class);
} catch (Exception ex) {
return false;
}
return true;
}
}