|
25 | 25 | execute/1, |
26 | 26 | transaction/1, |
27 | 27 | validate_record/1, |
| 28 | + validate_record_types/1, |
28 | 29 | type/1, |
29 | 30 | data_type/2]). |
30 | 31 |
|
@@ -228,12 +229,58 @@ save_record(Record) -> |
228 | 229 | %% if the `TestFunction' returns `false' on this particular BossRecord. |
229 | 230 | validate_record(Record) -> |
230 | 231 | Type = element(1, Record), |
231 | | - Errors = case erlang:function_exported(Type, validation_tests, 1) of |
232 | | - true -> [String || {TestFun, String} <- Record:validation_tests(), not TestFun()]; |
233 | | - false -> [] |
| 232 | + Errors1 = case validate_record_types(Record) of |
| 233 | + ok -> []; |
| 234 | + {error, Errors} -> Errors |
234 | 235 | end, |
235 | | - case length(Errors) of |
| 236 | + Errors2 = case Errors1 of |
| 237 | + [] -> |
| 238 | + case erlang:function_exported(Type, validation_tests, 1) of |
| 239 | + true -> [String || {TestFun, String} <- Record:validation_tests(), not TestFun()]; |
| 240 | + false -> [] |
| 241 | + end; |
| 242 | + _ -> Errors1 |
| 243 | + end, |
| 244 | + case length(Errors2) of |
236 | 245 | 0 -> ok; |
| 246 | + _ -> {error, Errors2} |
| 247 | + end. |
| 248 | + |
| 249 | +%% @spec validate_record_types( BossRecord ) -> ok | {error, [ErrorMessages]} |
| 250 | +%% @doc Validate the parameter types of the given BossRecord without saving it |
| 251 | +%% to the database. |
| 252 | +validate_record_types(Record) -> |
| 253 | + Errors = lists:foldl(fun |
| 254 | + ({Attr, Type}, Acc) -> |
| 255 | + Data = Record:Attr(), |
| 256 | + GreatSuccess = case {Data, Type} of |
| 257 | + {Data, string} when is_list(Data) -> |
| 258 | + true; |
| 259 | + {Data, binary} when is_binary(Data) -> |
| 260 | + true; |
| 261 | + {{{D1, D2, D3}, {T1, T2, T3}}, datetime} when is_integer(D1), is_integer(D2), is_integer(D3), |
| 262 | + is_integer(T1), is_integer(T2), is_integer(T3) -> |
| 263 | + true; |
| 264 | + {Data, integer} when is_integer(Data) -> |
| 265 | + true; |
| 266 | + {Data, float} when is_float(Data) -> |
| 267 | + true; |
| 268 | + {Data, number} when is_number(Data) -> |
| 269 | + true; |
| 270 | + {{N1, N2, N3}, now} when is_integer(N1), is_integer(N2), is_integer(N3) -> |
| 271 | + true; |
| 272 | + {_Data, Type} -> |
| 273 | + false |
| 274 | + end, |
| 275 | + if |
| 276 | + GreatSuccess -> |
| 277 | + Acc; |
| 278 | + true -> |
| 279 | + [lists:concat(["Invalid data type for ", Attr])|Acc] |
| 280 | + end |
| 281 | + end, [], Record:attribute_types()), |
| 282 | + case Errors of |
| 283 | + [] -> ok; |
237 | 284 | _ -> {error, Errors} |
238 | 285 | end. |
239 | 286 |
|
|
0 commit comments