/*
 * Validates fields annotated with SafeContent for dangerous input.
 *
 * Author: Harkamal
 * Date: April 2026
 */

package com.petshop.backend.util;

import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;
import java.util.regex.Pattern;

public class SafeContentValidator implements ConstraintValidator<SafeContent, String> {

    private static final Pattern DANGEROUS = Pattern.compile(
            "<script|</script|javascript:|on\\w+\\s*=|<iframe|<object|<embed|<form|<input|<img[^>]+on\\w+",
            Pattern.CASE_INSENSITIVE);

    @Override
    public boolean isValid(String value, ConstraintValidatorContext context) {
        if (value == null || value.isBlank()) return true;
        return !DANGEROUS.matcher(value).find();
    }
}
