File tree Expand file tree Collapse file tree 4 files changed +48
-3
lines changed Expand file tree Collapse file tree 4 files changed +48
-3
lines changed Original file line number Diff line number Diff line change @@ -176,3 +176,4 @@ Contributors (chronological)
176
176
- Peter C `@somethingnew2-0 <https://github.com/somethingnew2-0 >`_
177
177
- Marcel Jackwerth `@mrcljx ` <https://github.com/mrcljx>`_
178
178
- Fares Abubaker `@Fares-Abubaker <https://github.com/Fares-Abubaker >`_
179
+ - Nicolas Simonds `@0xDEC0DE <https://github.com/0xDEC0DE >`_
Original file line number Diff line number Diff line change @@ -17,6 +17,8 @@ Bug fixes:
17
17
- Correctly handle multiple `@post_load <marshmallow.post_load> ` methods where one method appends to
18
18
the data and another passes ``pass_original=True `` (:issue: `1755 `).
19
19
Thanks :user: `ghostwheel42 ` for reporting.
20
+ - ``URL `` fields now properly validate ``file `` paths (:issue: `2249 `).
21
+ Thanks :user: `0xDEC0DE ` for reporting and fixing.
20
22
21
23
Documentation:
22
24
@@ -25,8 +27,8 @@ Documentation:
25
27
26
28
Deprecations:
27
29
28
- - The ``ordered `` `class Meta <marshmallow.Schema.Meta> ` option is deprecated (:issue: `2146 `, :pr: `2762 `).
29
- Field order is already preserved by default. Set `marshmallow.Schema.dict_class ` to `collections.OrderedDict `
30
+ - The ``ordered `` `class Meta <marshmallow.Schema.Meta> ` option is deprecated (:issue: `2146 `, :pr: `2762 `).
31
+ Field order is already preserved by default. Set `marshmallow.Schema.dict_class ` to `collections.OrderedDict `
30
32
to maintain the previous behavior.
31
33
32
34
3.25.1 (2025-01-11)
Original file line number Diff line number Diff line change @@ -216,6 +216,7 @@ def __call__(self, value: str) -> str:
216
216
raise ValidationError (message )
217
217
218
218
# Check first if the scheme is valid
219
+ scheme = None
219
220
if "://" in value :
220
221
scheme = value .split ("://" )[0 ].lower ()
221
222
if scheme not in self .schemes :
@@ -225,7 +226,14 @@ def __call__(self, value: str) -> str:
225
226
relative = self .relative , absolute = self .absolute , require_tld = self .require_tld
226
227
)
227
228
228
- if not regex .search (value ):
229
+ # Hostname is optional for file URLS. If absent it means `localhost`.
230
+ # Fill it in for the validation if needed
231
+ if scheme == "file" and value .startswith ("file:///" ):
232
+ matched = regex .search (value .replace ("file:///" , "file://localhost/" , 1 ))
233
+ else :
234
+ matched = regex .search (value )
235
+
236
+ if not matched :
229
237
raise ValidationError (message )
230
238
231
239
return value
Original file line number Diff line number Diff line change @@ -205,6 +205,40 @@ def test_url_custom_scheme():
205
205
assert validator (url ) == url
206
206
207
207
208
+ @pytest .mark .parametrize (
209
+ "valid_url" ,
210
+ (
211
+ "file:///tmp/tmp1234" ,
212
+ "file://localhost/tmp/tmp1234" ,
213
+ "file:///C:/Users/test/file.txt" ,
214
+ "file://localhost/C:/Program%20Files/file.exe" ,
215
+ "file:///home/user/documents/test.pdf" ,
216
+ "file:///tmp/test%20file.txt" ,
217
+ "file:///" ,
218
+ "file://localhost/" ,
219
+ ),
220
+ )
221
+ def test_url_accepts_valid_file_urls (valid_url ):
222
+ validator = validate .URL (schemes = {"file" })
223
+ assert validator (valid_url ) == valid_url
224
+
225
+
226
+ @pytest .mark .parametrize (
227
+ "invalid_url" ,
228
+ (
229
+ "file://" ,
230
+ "file:/tmp/file.txt" ,
231
+ "file:tmp/file.txt" ,
232
+ "file://hostname/path" ,
233
+ "file:///tmp/test file.txt" ,
234
+ ),
235
+ )
236
+ def test_url_rejects_invalid_file_urls (invalid_url ):
237
+ validator = validate .URL (schemes = {"file" })
238
+ with pytest .raises (ValidationError , match = "Not a valid URL." ):
239
+ assert validator (invalid_url )
240
+
241
+
208
242
def test_url_relative_and_custom_schemes ():
209
243
validator = validate .URL (relative = True )
210
244
# By default, ws not allowed
You can’t perform that action at this time.
0 commit comments