All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.kestra.plugin.serdes.avro.AvroSchemaValidator Maven / Gradle / Ivy

The newest version!
package io.kestra.plugin.serdes.avro;

import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;
import org.apache.avro.Schema;
import org.apache.avro.SchemaParseException;

public class AvroSchemaValidator implements ConstraintValidator {

    @Override
    public boolean isValid(String value, ConstraintValidatorContext context) {
        if (value == null) {
            return true; // nulls are allowed according to spec
        }

        // pebble variable so can't validate
        if (value.contains("{{") && value.contains("}}")) {
            return true;
        }

        try {
            final Schema.Parser parser = new Schema.Parser();
            parser.parse(value);
        } catch (SchemaParseException e) {
            context.disableDefaultConstraintViolation();
            context.buildConstraintViolationWithTemplate("invalid avro schema '({validatedValue})': " + e.getMessage())
                .addConstraintViolation();

            return false;
        }
        return true;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy