Skip to content

Commit 20ba260

Browse files
committed
Refactor validation messages to include missing key values
1 parent 2d4c5a7 commit 20ba260

File tree

2 files changed

+33
-21
lines changed

2 files changed

+33
-21
lines changed

src/masonite/validation/Validator.py

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1065,20 +1065,20 @@ def __init__(self, validations, size=False, mimes=False, messages={}, raises={})
10651065
def message(self, attribute):
10661066
messages = []
10671067
if not self.file_check:
1068-
messages.append("The {} is not a valid file.".format(attribute))
1068+
messages.append("The {} is not a valid file.".format(attribute.replace("*", str(missing_key(self.dictionary, self.key)))))
10691069

10701070
if not self.size_check:
10711071
from hfilesize import FileSize
10721072

10731073
messages.append(
10741074
"The {} file size exceeds {:.02fH}.".format(
1075-
attribute, FileSize(self.size)
1075+
attribute.replace("*", str(missing_key(self.dictionary, self.key))), FileSize(self.size)
10761076
)
10771077
)
10781078
if not self.mimes_check:
10791079
messages.append(
10801080
"The {} mime type is not valid. Allowed formats are {}.".format(
1081-
attribute, ",".join(self.allowed_extensions)
1081+
attribute.replace("*", str(missing_key(self.dictionary, self.key))), ",".join(self.allowed_extensions)
10821082
)
10831083
)
10841084

@@ -1093,13 +1093,13 @@ def negated_message(self, attribute):
10931093

10941094
messages.append(
10951095
"The {} file size is less or equal than {:.02fH}.".format(
1096-
attribute, FileSize(self.size)
1096+
attribute.replace("*", str(missing_key(self.dictionary, self.key))), FileSize(self.size)
10971097
)
10981098
)
10991099
if self.mimes_check:
11001100
messages.append(
11011101
"The {} mime type is in {}.".format(
1102-
attribute, ",".join(self.allowed_extensions)
1102+
attribute.replace("*", str(missing_key(self.dictionary, self.key))), ",".join(self.allowed_extensions)
11031103
)
11041104
)
11051105
return messages
@@ -1120,14 +1120,14 @@ def __init__(self, validations, size=False, messages={}, raises={}):
11201120
def message(self, attribute):
11211121
messages = []
11221122
if not self.file_check:
1123-
messages.append("The {} is not a valid file.".format(attribute))
1123+
messages.append("The {} is not a valid file.".format(attribute.replace("*", str(missing_key(self.dictionary, self.key)))))
11241124

11251125
if not self.size_check:
11261126
from hfilesize import FileSize
11271127

11281128
messages.append(
11291129
"The {} file size exceeds {:.02fH}.".format(
1130-
attribute, FileSize(self.size)
1130+
attribute.replace("*", str(missing_key(self.dictionary, self.key))), FileSize(self.size)
11311131
)
11321132
)
11331133

@@ -1176,7 +1176,7 @@ def __init__(self, validations, size=False, messages={}, raises={}):
11761176
def message(self, attribute):
11771177
messages = []
11781178
if not self.file_check:
1179-
messages.append("The {} is not a valid file.".format(attribute))
1179+
messages.append("The {} is not a valid file.".format(attribute.replace("*", str(missing_key(self.dictionary, self.key)))))
11801180

11811181
if not self.size_check:
11821182
from hfilesize import FileSize
@@ -1239,6 +1239,8 @@ def __init__(self, validations, locale, messages={}, raises={}):
12391239
self.patterns_example.append(pattern_dict["example"])
12401240

12411241
def passes(self, attribute, key, dictionary):
1242+
self.key = key
1243+
self.dictionary = dictionary
12421244
for pattern in self.patterns:
12431245
# check that at least one pattern match attribute
12441246
if re.compile(r"{}".format(pattern)).match(attribute):
@@ -1247,7 +1249,7 @@ def passes(self, attribute, key, dictionary):
12471249

12481250
def message(self, attribute):
12491251
return "The {} is not a valid {} postal code. Valid {} {}.".format(
1250-
attribute,
1252+
attribute.replace("*", str(missing_key(self.dictionary, self.key))),
12511253
",".join(self.locales),
12521254
"examples are" if len(self.locales) > 1 else "example is",
12531255
",".join(self.patterns_example),
@@ -1265,17 +1267,19 @@ def __init__(self, validations, other_field, messages={}, raises={}):
12651267
self.other_field = other_field
12661268

12671269
def passes(self, attribute, key, dictionary):
1270+
self.key = key
1271+
self.dictionary = dictionary
12681272
other_value = dictionary.get(self.other_field, None)
12691273
return attribute != other_value
12701274

12711275
def message(self, attribute):
12721276
return "The {} value must be different than {} value.".format(
1273-
attribute, self.other_field
1277+
attribute.replace("*", str(missing_key(self.dictionary, self.key))), self.other_field
12741278
)
12751279

12761280
def negated_message(self, attribute):
12771281
return "The {} value be the same as {} value.".format(
1278-
attribute, self.other_field
1282+
attribute.replace("*", str(missing_key(self.dictionary, self.key))), self.other_field
12791283
)
12801284

12811285

@@ -1291,6 +1295,8 @@ def __init__(self, validations, version=4, messages={}, raises={}):
12911295
self.uuid_type = "UUID {0}".format(self.version)
12921296

12931297
def passes(self, attribute, key, dictionary):
1298+
self.key = key
1299+
self.dictionary = dictionary
12941300
from uuid import UUID
12951301

12961302
try:
@@ -1300,10 +1306,10 @@ def passes(self, attribute, key, dictionary):
13001306
return False
13011307

13021308
def message(self, attribute):
1303-
return "The {} value must be a valid {}.".format(attribute, self.uuid_type)
1309+
return "The {} value must be a valid {}.".format(attribute.replace("*", str(missing_key(self.dictionary, self.key))), self.uuid_type)
13041310

13051311
def negated_message(self, attribute):
1306-
return "The {} value must not be a valid {}.".format(attribute, self.uuid_type)
1312+
return "The {} value must not be a valid {}.".format(attribute.replace("*", str(missing_key(self.dictionary, self.key))), self.uuid_type)
13071313

13081314

13091315
class required_if(BaseValidation):
@@ -1316,19 +1322,21 @@ def __init__(self, validations, other_field, value, messages={}, raises={}):
13161322
self.value = value
13171323

13181324
def passes(self, attribute, key, dictionary):
1325+
self.key = key
1326+
self.dictionary = dictionary
13191327
if dictionary.get(self.other_field, None) == self.value:
1320-
return required.passes(self, attribute, key, dictionary)
1328+
return required.passes(self, attribute.replace("*", str(missing_key(self.dictionary, self.key))), key, dictionary)
13211329

13221330
return True
13231331

13241332
def message(self, attribute):
13251333
return "The {} is required because {}={}.".format(
1326-
attribute, self.other_field, self.value
1334+
attribute.replace("*", str(missing_key(self.dictionary, self.key))), self.other_field, self.value
13271335
)
13281336

13291337
def negated_message(self, attribute):
13301338
return "The {} is not required because {}={} or {} is not present.".format(
1331-
attribute, self.other_field, self.value, self.other_field
1339+
attribute.replace("*", str(missing_key(self.dictionary, self.key))), self.other_field, self.value, self.other_field
13321340
)
13331341

13341342

@@ -1347,6 +1355,8 @@ def __init__(self, validations, other_fields, messages={}, raises={}):
13471355
self.other_fields = other_fields
13481356

13491357
def passes(self, attribute, key, dictionary):
1358+
self.key = key
1359+
self.dictionary = dictionary
13501360
for field in self.other_fields:
13511361
if field in dictionary:
13521362
return required.passes(self, attribute, key, dictionary)
@@ -1356,15 +1366,15 @@ def passes(self, attribute, key, dictionary):
13561366
def message(self, attribute):
13571367
fields = ",".join(self.other_fields)
13581368
return "The {} is required because {} is present.".format(
1359-
attribute,
1369+
attribute.replace("*", str(missing_key(self.dictionary, self.key))),
13601370
"one in {}".format(fields)
13611371
if len(self.other_fields) > 1
13621372
else self.other_fields[0],
13631373
)
13641374

13651375
def negated_message(self, attribute):
13661376
return "The {} is not required because {} {} is not present.".format(
1367-
attribute,
1377+
attribute.replace("*", str(missing_key(self.dictionary, self.key))),
13681378
"none of" if len(self.other_fields) > 1 else "",
13691379
",".join(self.other_fields),
13701380
)
@@ -1375,14 +1385,16 @@ class distinct(BaseValidation):
13751385
duplicate values."""
13761386

13771387
def passes(self, attribute, key, dictionary):
1388+
self.key = key
1389+
self.dictionary = dictionary
13781390
# check if list contains duplicates
13791391
return len(set(attribute)) == len(attribute)
13801392

13811393
def message(self, attribute):
1382-
return "The {} field has duplicate values.".format(attribute)
1394+
return "The {} field has duplicate values.".format(attribute.replace("*", str(missing_key(self.dictionary, self.key))))
13831395

13841396
def negated_message(self, attribute):
1385-
return "The {} field has only different values.".format(attribute)
1397+
return "The {} field has only different values.".format(attribute.replace("*", str(missing_key(self.dictionary, self.key))))
13861398

13871399

13881400
class Validator:

tests/features/validation/test_validation.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1279,7 +1279,7 @@ def test_distinct(self):
12791279
)
12801280
self.assertEqual(
12811281
validate.get("users.*.last_name"),
1282-
["The users.*.last_name field has duplicate values."],
1282+
["The users.0.last_name field has duplicate values."],
12831283
)
12841284
validate = Validator().validate(
12851285
{

0 commit comments

Comments
 (0)