| 1 | package Daje::Workflow::Checks::Mandatory; | ||
| 2 | use Mojo::Base 'Daje::Workflow::Checks::Super::Base', -base, -signatures; | ||
| 3 | use v5.42; | ||
| 4 | |||
| 5 | # NAME | ||
| 6 | # ==== | ||
| 7 | # | ||
| 8 | # Daje::Workflow::Checks::Mandatory - Checks for mandatory fields | ||
| 9 | # | ||
| 10 | # SYNOPSIS | ||
| 11 | # ======== | ||
| 12 | # | ||
| 13 | # check($self) | ||
| 14 | # | ||
| 15 | # Checks can either come from the workflow checks tag as a comma separated string of fields | ||
| 16 | # | ||
| 17 | # { | ||
| 18 | # "name": "Mandatory fields", | ||
| 19 | # "class": "Daje::Workflow::Checks::Mandatory", | ||
| 20 | # "checks": "name,tools_object_types_fkey,active" | ||
| 21 | # } | ||
| 22 | # | ||
| 23 | # or a database model class containing a mandatory method containing a comma separated list of fields | ||
| 24 | # | ||
| 25 | # { | ||
| 26 | # "name": "Mandatory fields", | ||
| 27 | # "class": "Daje::Workflow::Checks::Mandatory", | ||
| 28 | # "checks": "Daje::Database::Model::SentinelStatus" | ||
| 29 | # } | ||
| 30 | # | ||
| 31 | # Assumed format of data $self->context->{context}->{payload}->{field to be checked for existence) | ||
| 32 | # | ||
| 33 | # | ||
| 34 | # DESCRIPTION | ||
| 35 | # =========== | ||
| 36 | # | ||
| 37 | # Daje::Workflow::Checks::Mandatory is used to check and make sure | ||
| 38 | # mandatory fields are included in the context | ||
| 39 | # | ||
| 40 | # LICENSE | ||
| 41 | # ======= | ||
| 42 | # | ||
| 43 | # Copyright (C) janeskil1525. | ||
| 44 | # | ||
| 45 | # This library is free software; you can redistribute it and/or modify | ||
| 46 | # it under the same terms as Perl itself. | ||
| 47 | # | ||
| 48 | # AUTHOR | ||
| 49 | # ====== | ||
| 50 | # janeskil1525 E<lt>janeskil1525@gmail.comE<gt> | ||
| 51 | # | ||
| 52 | |||
| 53 | use Mojo::Loader qw(load_class); | ||
| 54 | use Data::Dumper; | ||
| 55 | |||
| 56 | sub check($self) { | ||
| 57 | my $result = 1; | ||
| 58 | my @fields = (); | ||
| 59 | if (length($self->checks()) > 0) { | ||
| 60 | my $checks = $self->checks(); | ||
| 61 | if ($checks =~ /\Q::\E/) { | ||
| 62 | if (my $e = load_class $checks) { | ||
| 63 | $self->error->add_error($e); | ||
| 64 | } | ||
| 65 | if ($self->error->has_error() == 0) { | ||
| 66 | my $mandatory = $checks->new()->mandatory(); | ||
| 67 | @fields = split(',', $mandatory); | ||
| 68 | } | ||
| 69 | } else { | ||
| 70 | @fields = split(',', $self->checks()); | ||
| 71 | } | ||
| 72 | my $length = scalar @fields; | ||
| 73 | for (my $i = 0; $i < $length; $i++) { | ||
| 74 | if (!exists $self->context->{context}->{payload}->{$fields[$i]}) { | ||
| 75 | $result = 0; | ||
| 76 | $self->error->add_error("Mandatory field '$fields[$i]' is missing") | ||
| 77 | } | ||
| 78 | } | ||
| 79 | } | ||
| 80 | return $result; | ||
| 81 | } | ||
| 82 | |||
| 83 | 1; | ||
| 84 | |||
| 85 | |||
| 86 | |||
| 87 | |||
| 88 | |||
| 89 | |||
| 90 | |||
| 91 | |||
| 92 | |||
| 93 | |||
| 94 | |||
| 95 | |||
| 96 | |||
| 97 | #################### pod generated by Pod::Autopod - keep this line to make pod updates possible #################### | ||
| 98 | |||
| 99 | =head1 NAME | ||
| 100 | |||
| 101 | |||
| 102 | Daje::Workflow::Checks::Mandatory - Checks for mandatory fields | ||
| 103 | |||
| 104 | |||
| 105 | |||
| 106 | =head1 SYNOPSIS | ||
| 107 | |||
| 108 | |||
| 109 | check($self) | ||
| 110 | |||
| 111 | Checks can either come from the workflow checks tag as a comma separated string of fields | ||
| 112 | |||
| 113 | { | ||
| 114 | "name": "Mandatory fields", | ||
| 115 | "class": "Daje::Workflow::Checks::Mandatory", | ||
| 116 | "checks": "name,tools_object_types_fkey,active" | ||
| 117 | } | ||
| 118 | |||
| 119 | or a database model class containing a mandatory method containing a comma separated list of fields | ||
| 120 | |||
| 121 | { | ||
| 122 | "name": "Mandatory fields", | ||
| 123 | "class": "Daje::Workflow::Checks::Mandatory", | ||
| 124 | "checks": "Daje::Database::Model::SentinelStatus" | ||
| 125 | } | ||
| 126 | |||
| 127 | Assumed format of data $self->context->{context}->{payload}->{field to be checked for existence) | ||
| 128 | |||
| 129 | |||
| 130 | |||
| 131 | |||
| 132 | =head1 DESCRIPTION | ||
| 133 | |||
| 134 | |||
| 135 | Daje::Workflow::Checks::Mandatory is used to check and make sure | ||
| 136 | mandatory fields are included in the context | ||
| 137 | |||
| 138 | |||
| 139 | |||
| 140 | =head1 REQUIRES | ||
| 141 | |||
| 142 | L<Data::Dumper> | ||
| 143 | |||
| 144 | L<Mojo::Loader> | ||
| 145 | |||
| 146 | L<v5.42> | ||
| 147 | |||
| 148 | L<Mojo::Base> | ||
| 149 | |||
| 150 | |||
| 151 | =head1 METHODS | ||
| 152 | |||
| 153 | =head2 check($self) | ||
| 154 | |||
| 155 | check($self)(); | ||
| 156 | |||
| 157 | |||
| 158 | =head1 AUTHOR | ||
| 159 | |||
| 160 | janeskil1525 E<lt>janeskil1525@gmail.comE<gt> | ||
| 161 | |||
| 162 | |||
| 163 | |||
| 164 | =head1 LICENSE | ||
| 165 | |||
| 166 | |||
| 167 | Copyright (C) janeskil1525. | ||
| 168 | |||
| 169 | This library is free software; you can redistribute it and/or modify | ||
| 170 | it under the same terms as Perl itself. | ||
| 171 | |||
| 172 | |||
| 173 | |||
| 174 | =cut | ||
| 175 |