| 1 | package JMAP::Validation::Checks::ContactGroup; | ||
| 2 | |||
| 3 | 4 | ▶ | use JMAP::Validation; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 4 | ▶ | use JMAP::Validation::Checks::String; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 5 | use JMAP::Validation::Tests::ContactGroup; | ||
| 6 | use JMAP::Validation::Tests::Object; | ||
| 7 | use JMAP::Validation::Tests::SetError; | ||
| 8 | use JMAP::Validation::Tests::String; | ||
| 9 | use Test2::Bundle::Extended; | ||
| 10 | |||
| 11 | # record types {{{ | ||
| 12 | |||
| 13 | my %ContactGroup_checks = ( | ||
| 14 | id => $JMAP::Validation::Checks::String::is_id, | ||
| 15 | |||
| 16 | name => check_set( | ||
| 17 | $JMAP::Validation::Checks::String::is_string, | ||
| 18 | $JMAP::Validation::Checks::String::has_at_least_one_character, | ||
| 19 | $JMAP::Validation::Checks::String::has_at_most_256_bytes, | ||
| 20 | ), | ||
| 21 | |||
| 22 | contactIds => $JMAP::Validation::Checks::String::is_array_of_ids, | ||
| 23 | ); | ||
| 24 | |||
| 25 | our $is_ContactGroup = hash { | ||
| 26 | field id => $ContactGroup_checks{id}; | ||
| 27 | field name => $ContactGroup_checks{name}; | ||
| 28 | field contactIds => $ContactGroup_checks{contactIds}; | ||
| 29 | end(); | ||
| 30 | }; | ||
| 31 | |||
| 32 | our $is_ContactGroup_for_create = hash { | ||
| 33 | field name => $ContactGroup_checks{name}; | ||
| 34 | field contactIds => $ContactGroup_checks{contactIds}; | ||
| 35 | end(); | ||
| 36 | }; | ||
| 37 | |||
| 38 | our $is_ContactGroup_for_update = hash{ | ||
| 39 | field name => in_set( | ||
| 40 | FDNE(), | ||
| 41 | $ContactGroup_checks{name}, | ||
| 42 | ); | ||
| 43 | |||
| 44 | field contactIds => in_set( | ||
| 45 | FDNE(), | ||
| 46 | $ContactGroup_checks{contactIds}, | ||
| 47 | ); | ||
| 48 | |||
| 49 | end(); | ||
| 50 | }; | ||
| 51 | |||
| 52 | # }} | ||
| 53 | |||
| 54 | # requests {{{ | ||
| 55 | |||
| 56 | our $getContactGroups_args = hash { | ||
| 57 | field accountId => validator(sub { | ||
| 58 | my (%params) = @_; | ||
| 59 | |||
| 60 | if (defined $params{got}) { | ||
| 61 | return unless JMAP::Validation::validate( | ||
| 62 | $params{got}, | ||
| 63 | $JMAP::Validation::Checks::String::is_string | ||
| 64 | ); | ||
| 65 | } | ||
| 66 | |||
| 67 | return 1; | ||
| 68 | }); | ||
| 69 | |||
| 70 | field ids => validator(sub { | ||
| 71 | my (%params) = @_; | ||
| 72 | |||
| 73 | if (defined $params{got}) { | ||
| 74 | return unless JMAP::Validation::validate( | ||
| 75 | $params{got}, | ||
| 76 | $JMAP::Validation::Checks::String::is_array_of_ids, | ||
| 77 | ); | ||
| 78 | } | ||
| 79 | |||
| 80 | return 1; | ||
| 81 | }); | ||
| 82 | |||
| 83 | end(); | ||
| 84 | }; | ||
| 85 | |||
| 86 | |||
| 87 | our $setContactGroups_args = hash { | ||
| 88 | field accountId => in_set( | ||
| 89 | FDNE(), | ||
| 90 | $JMAP::Validation::Checks::String::is_string_or_null, | ||
| 91 | ); | ||
| 92 | |||
| 93 | field ifInState => in_set( | ||
| 94 | FDNE(), | ||
| 95 | $JMAP::Validation::Checks::String::is_string_or_null, | ||
| 96 | ); | ||
| 97 | |||
| 98 | field create => in_set( | ||
| 99 | FDNE(), | ||
| 100 | hash{ all_values $is_ContactGroup_for_create }, | ||
| 101 | ); | ||
| 102 | |||
| 103 | field update => in_set( | ||
| 104 | FDNE(), | ||
| 105 | hash{ all_values $is_ContactGroup_for_update }, | ||
| 106 | ); | ||
| 107 | |||
| 108 | field destroy => in_set( | ||
| 109 | FDNE(), | ||
| 110 | array { all_items $JMAP::Validation::Checks::String::is_id }, | ||
| 111 | ); | ||
| 112 | |||
| 113 | end(); | ||
| 114 | }; | ||
| 115 | |||
| 116 | # }}} | ||
| 117 | |||
| 118 | # response types {{{ | ||
| 119 | |||
| 120 | our $is_contactGroups = hash { | ||
| 121 | field accountId => $JMAP::Validation::Checks::String::is_id; | ||
| 122 | field state => $JMAP::Validation::Checks::String::is_string; | ||
| 123 | |||
| 124 | # TODO: https://github.com/Test-More/Test2-Suite/issues/9 | ||
| 125 | # field list => array { all_items($is_ContactGroup) }; | ||
| 126 | |||
| 127 | field list => array{ | ||
| 128 | filter_items { grep { ! JMAP::Validation::Tests::ContactGroup::is_ContactGroup($_) } @_ }; | ||
| 129 | end(); | ||
| 130 | }; | ||
| 131 | |||
| 132 | field notFound => validator(sub { | ||
| 133 | my (%params) = @_; | ||
| 134 | |||
| 135 | if (defined $params{got}) { | ||
| 136 | return unless JMAP::Validation::validate( | ||
| 137 | $params{got}, | ||
| 138 | array { | ||
| 139 | filter_items { grep { ! JMAP::Validation::Tests::String::is_string($_) } @_ }; | ||
| 140 | end(); | ||
| 141 | }, | ||
| 142 | ); | ||
| 143 | } | ||
| 144 | |||
| 145 | return 1; | ||
| 146 | }); | ||
| 147 | |||
| 148 | end(); | ||
| 149 | }; | ||
| 150 | |||
| 151 | our $is_contactGroupUpdates = hash { | ||
| 152 | field accountId => $JMAP::Validation::Checks::String::is_id; | ||
| 153 | field oldState => $JMAP::Validation::Checks::String::is_string; | ||
| 154 | field newState => $JMAP::Validation::Checks::String::is_string; | ||
| 155 | field changed => $JMAP::Validation::Checks::String::is_array_of_ids; | ||
| 156 | field removed => $JMAP::Validation::Checks::String::is_array_of_ids; | ||
| 157 | end(); | ||
| 158 | }; | ||
| 159 | |||
| 160 | our $is_contactGroupsSet = hash { | ||
| 161 | field accountId => $JMAP::Validation::Checks::String::is_id; | ||
| 162 | field oldState => $JMAP::Validation::Checks::String::is_string_or_null; | ||
| 163 | # TODO: field newState => $JMAP::Validation::Checks::String::is_string; | ||
| 164 | |||
| 165 | field created => check_set( | ||
| 166 | hash {}, | ||
| 167 | validator(sub { | ||
| 168 | my (%params) = @_; | ||
| 169 | |||
| 170 | foreach my $createdId (keys %{$params{got}}) { | ||
| 171 | return unless JMAP::Validation::Tests::Object::is_object($params{got}{$createdId}); | ||
| 172 | return unless scalar(keys %{$params{got}{$createdId}}) == 1; | ||
| 173 | return unless JMAP::Validation::Tests::String::is_id($params{got}{$createdId}{id}); | ||
| 174 | } | ||
| 175 | |||
| 176 | return 1; | ||
| 177 | }), | ||
| 178 | ); | ||
| 179 | |||
| 180 | field updated => $JMAP::Validation::Checks::String::is_array_of_ids; | ||
| 181 | field destroyed => $JMAP::Validation::Checks::String::is_array_of_ids; | ||
| 182 | |||
| 183 | field notCreated => check_set( | ||
| 184 | hash {}, | ||
| 185 | validator(sub { | ||
| 186 | my (%params) = @_; | ||
| 187 | |||
| 188 | foreach my $creationId (keys %{$params{got}}) { | ||
| 189 | return unless JMAP::Validation::Tests::SetError::is_SetError_invalidProperties( | ||
| 190 | $params{got}{$creationId}, qw{name contactIds}, | ||
| 191 | ); | ||
| 192 | } | ||
| 193 | |||
| 194 | return 1; | ||
| 195 | }), | ||
| 196 | ); | ||
| 197 | |||
| 198 | field notUpdated => check_set( | ||
| 199 | hash {}, | ||
| 200 | validator(sub { | ||
| 201 | my (%params) = @_; | ||
| 202 | |||
| 203 | foreach my $updatedId (keys %{$params{got}}) { | ||
| 204 | my $is_notFound = JMAP::Validation::Tests::SetError::is_SetError_notFound( | ||
| 205 | $params{got}{$updatedId} | ||
| 206 | ); | ||
| 207 | |||
| 208 | my $is_invalidProperties = JMAP::Validation::Tests::SetError::is_SetError_invalidProperties( | ||
| 209 | $params{got}{$updatedId}, qw{name contactIds}, | ||
| 210 | ); | ||
| 211 | |||
| 212 | return unless ($is_notFound or $is_invalidProperties); | ||
| 213 | } | ||
| 214 | |||
| 215 | return 1; | ||
| 216 | }), | ||
| 217 | ); | ||
| 218 | |||
| 219 | field notDestroyed => check_set( | ||
| 220 | hash {}, | ||
| 221 | validator(sub { | ||
| 222 | my (%params) = @_; | ||
| 223 | |||
| 224 | foreach my $destroyedId (keys %{$params{got}}) { | ||
| 225 | return unless JMAP::Validation::Tests::SetError::is_SetError_notFound( | ||
| 226 | $params{got}{$destroyedId} | ||
| 227 | ); | ||
| 228 | } | ||
| 229 | |||
| 230 | return 1; | ||
| 231 | }), | ||
| 232 | ); | ||
| 233 | |||
| 234 | # TODO: end(); once newState works | ||
| 235 | }; | ||
| 236 | |||
| 237 | # }}} | ||
| 238 | |||
| 239 | 1; | ||