<commit_before>package OpenXPKI::Server::Workflow::Validator::Regex;

use strict;
use warnings;
use base qw( Workflow::Validator );
use OpenXPKI::Debug;
use Data::Dumper;
use OpenXPKI::Server::Context qw( CTX );
use Workflow::Exception qw( validation_error );
use OpenXPKI::Serialization::Simple;

__PACKAGE__->mk_accessors(qw(regex error));

sub _init {
    my ( $self, $params ) = @_;
    $self->regex( $params->{regex} ) if ($params->{regex});
    $self->error( 'I18N_OPENXPKI_SERVER_WORKFLOW_VALIDATOR_REGEX_FAILED' );
    $self->error( $params->{error} ) if ($params->{error});
}

sub validate {
    my ( $self, $wf, $field, $regex ) = @_;

    ##! 1: 'start'

    my $value = $wf->context()->param($field);
    return 1 if (!defined $value || $value eq '');

    $regex = $self->regex() unless($regex);

    ##! 16: 'Value ' . $value
    ##! 16: 'Regex ' . $regex

    # replace named regexes
    if ($regex eq 'email') {
        $regex = qr/ \A [a-z0-9\.-]+\@([\w_-]+\.)+(\w+) \z /xi;
    # or quote the string if no named match
    } else {
        $regex = qr/$regex/;
    }

    # Array Magic
    my @errors;
    if (ref $value eq 'ARRAY' || $value =~ /^ARRAY/) {
        if (!ref $value) {
            $value = OpenXPKI::Serialization::Simple->new()->deserialize( $value );
        }
        foreach my $val (@{$value}) {
            # skip empty
            next if (!defined $val || $val eq '');
            push @errors, $val if ($val !~ $regex);
        }
    } else {
        push @errors, $value if ($value !~ $regex);
    }

    if (@errors) {
        # Need to implement this in New UI first
        #$wf->context()->param( '__error' => [ $self->error(), { FIELD => $field, VALUES => \@errors }]);<commit_msg>Add x modifier to Regex Validator<commit_after>package OpenXPKI::Server::Workflow::Validator::Regex;

use strict;
use warnings;
use base qw( Workflow::Validator );
use OpenXPKI::Debug;
use Data::Dumper;
use OpenXPKI::Server::Context qw( CTX );
use Workflow::Exception qw( validation_error );
use OpenXPKI::Serialization::Simple;

__PACKAGE__->mk_accessors(qw(regex error));

sub _init {
    my ( $self, $params ) = @_;
    $self->regex( $params->{regex} ) if ($params->{regex});
    $self->error( 'I18N_OPENXPKI_SERVER_WORKFLOW_VALIDATOR_REGEX_FAILED' );
    $self->error( $params->{error} ) if ($params->{error});
}

sub validate {
    my ( $self, $wf, $field, $regex ) = @_;

    ##! 1: 'start'

    my $value = $wf->context()->param($field);
    return 1 if (!defined $value || $value eq '');

    $regex = $self->regex() unless($regex);

    ##! 16: 'Value ' . $value
    ##! 16: 'Regex ' . $regex

    # replace named regexes
    if ($regex eq 'email') {
        $regex = qr/ \A [a-z0-9\.-]+\@([\w_-]+\.)+(\w+) \z /xi;
    # or quote the string if no named match
    } else {
        $regex = qr/$regex/x;
    }

    # Array Magic
    my @errors;
    if (ref $value eq 'ARRAY' || $value =~ /^ARRAY/) {
        if (!ref $value) {
            $value = OpenXPKI::Serialization::Simple->new()->deserialize( $value );
        }
        foreach my $val (@{$value}) {
            # skip empty
            next if (!defined $val || $val eq '');
            push @errors, $val if ($val !~ $regex);
        }
    } else {
        push @errors, $value if ($value !~ $regex);
    }

    if (@errors) {
        # Need to implement this in New UI first
        #$wf->context()->param( '__error' => [ $self->error(), { FIELD => $field, VALUES => \@errors }]);
{'fraction_non_alphanumeric': 0.19557089444923784, 'fraction_numerical': 0.005752085130859937, 'mean_word_length': 2.6765327695560255, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 41, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1688922', 'n_tokens_mistral': 1226, 'n_tokens_neox': 1194, 'n_words': 309}