@@ -238,182 +238,44 @@ be enabled by hooking in a format-checking object into an :class:`IValidator`.
238
238
239
239
.. attribute :: checkers
240
240
241
- A mapping of currently known formats to functions that validate them.
242
- New checkers can be added and removed either per-instance or globally
243
- for all checkers using the :meth: `FormatChecker.checks ` or
241
+ A mapping of currently known formats to tuple of functions that
242
+ validate them and errors that should be caught. New checkers can be
243
+ added and removed either per-instance or globally for all checkers
244
+ using the :meth: `FormatChecker.checks ` or
244
245
:meth: `FormatChecker.cls_checks ` decorators respectively.
245
246
246
- .. method :: cls_checks(format)
247
+ .. method :: cls_checks(format, raises=() )
247
248
248
249
Register a decorated function as *globally * validating a new format.
249
250
250
251
Any instance created after this function is called will pick up the
251
252
supplied checker.
252
253
253
254
:argument str format: the format that the decorated function will check
255
+ :argument Exception raises: the exception(s) raised by the decorated
256
+ function when an invalid instance is found. The exception object
257
+ will be accessible as the :attr: `ValidationError.cause ` attribute
258
+ of the resulting validation error.
254
259
255
260
256
261
257
262
There are a number of default checkers that :class: `FormatChecker `\s know how
258
263
to validate. Their names can be viewed by inspecting the
259
- :attr: `FormatChecker.checkers ` attribute.
264
+ :attr: `FormatChecker.checkers ` attribute. Certain checkers will only be
265
+ available if an appropriate package is available for use, these are listed
266
+ below.
260
267
261
- The actual functions that do the validation are also exposed, in case there is
262
- any use for them. They are listed below, along with any limitations they come
263
- with.
268
+ On OSes with the ``socket.inet_pton `` function, a checker for
269
+ ``ipv6 `` will be present.
264
270
271
+ If the rfc3987 _ library is present, a checker for ``uri `` will be present.
265
272
266
- .. autofunction :: is_date
273
+ If the isodate _ library is present, a ``date-time `` checker will also be
274
+ present.
267
275
268
- Check if the instance is a date in ``YYYY-MM-DD `` format.
276
+ Additionally, if the webcolors _ library is present, a checker for ``color ``
277
+ related to CSS will be present.
269
278
270
- >>> is_date(" 1970-12-31" )
271
- True
272
- >>> is_date(" 12/31/1970" )
273
- False
274
- >>> is_date(" 0000-13-32" )
275
- False
276
-
277
- .. autofunction :: is_time
278
-
279
- Check if the instance is a time in ``hh:mm:ss `` format.
280
-
281
- >>> is_time(" 23:59:59" )
282
- True
283
- >>> is_time(" 11:59:59 PM" )
284
- False
285
- >>> is_time(" 59:60:61" )
286
- False
287
-
288
- .. autofunction :: is_regex
289
-
290
- Check if the instance is a well-formed regular expression.
291
-
292
- >>> is_regex(" ^(bob)?cat$" )
293
- True
294
- >>> is_ipv6(" ^(bob?cat$" )
295
- False
296
-
297
- .. autofunction :: is_email
298
-
299
- Check if the instance is a valid e-mail address.
300
-
301
-
302
- True
303
- >>> is_email(" joe.bloggs" )
304
- False
305
-
306
- .. note ::
307
-
308
- This is *not * done in strict compliance of `RFC 2822 `_ and / or `RFC
309
- 5322 `_.
310
-
311
- The only constraint is that the instance contain an ``@ `` sign. If you
312
- want stricter compliance, either change what you want or add and
313
- register your own checker.
314
-
315
- .. _RFC 2822 : http://tools.ietf.org/html/rfc2822
316
- .. _RFC 5322 : http://tools.ietf.org/html/rfc5322
317
-
318
- .. autofunction :: is_host_name
319
-
320
- Check if the instance is a valid host name.
321
-
322
- >>> is_host_name(" www.example.com" )
323
- True
324
- >>> is_host_name(" my laptop" )
325
- False
326
- >>> is_host_name(
327
- ... " a.vvvvvvvvvvvvvvvvveeeeeeeeeeeeeeeeerrrrrrrrrrrrrrrrryyyyyyyy"
328
- ... " yyyyyyyyy.long.host.name"
329
- ... )
330
- False
331
-
332
- .. note :: Does not perform a DNS lookup.
333
-
334
- >>> is_host_name(" www.example.doesnotexist" )
335
- True
336
-
337
- .. autofunction :: is_ipv4
338
-
339
- Check if the instance is a valid IP address.
340
-
341
- >>> is_ipv4(" 192.168.0.1" )
342
- True
343
- >>> is_ipv4(" ::1" )
344
- False
345
- >>> is_ipv4(" 256.256.256.256" )
346
- False
347
-
348
- On OSes with the ``socket.inet_pton `` function, an additional checker for
349
- ``ipv6 `` will be enabled:
350
-
351
- .. function :: is_ipv6(instance)
352
-
353
- Check if the instance is a valid IPv6 address.
354
-
355
- >>> is_ipv6(" ::1" )
356
- True
357
- >>> is_ipv6(" 192.168.0.1" )
358
- False
359
- >>> is_ipv6(" 1:1:1:1:1:1:1:1:1" )
360
- False
361
-
362
-
363
- If the rfc3987 _ library is present, a checker for URIs will be present.
364
-
365
- .. function :: is_uri
366
-
367
- Check if the instance is a valid URI.
368
-
369
- Also supports relative URIs.
370
-
371
- >>>
is_uri(" ftp://[email protected] :8080/pub/os/" )
372
- True
373
- >>> is_uri(" http://www2.example.com:8000/pub/#os?user=joe.bloggs" )
374
- True
375
- >>> is_uri(r " \\\\ WINDOWS\M y Files" )
376
- False
377
- >>> is_uri(" #/properties/foo" )
378
- True
379
-
380
-
381
- If the isodate _ library is present, a date-time checker will also be present.
382
-
383
- .. function :: is_date_time(instance)
384
-
385
- Check if the instance is in ISO 8601 ``YYYY-MM-DDThh:mm:ssZ `` format.
386
-
387
- >>> is_date_time(" 1970-01-01T00:00:00.0Z" )
388
- True
389
- >>> is_date_time(" 0000-58-59T60:61:62" )
390
- False
391
-
392
-
393
- Additionally, if the webcolors _ library is present, some checkers related to
394
- CSS will be enabled:
395
-
396
- .. function :: is_css21_color(instance)
397
-
398
- Check if the instance is a valid CSS 2.1 color name or code.
399
-
400
- >>> is_css21_color(" fuchsia" )
401
- True
402
- >>> is_css21_color(" pink" )
403
- False
404
- >>> is_css_color_code(" #CC8899" )
405
- True
406
-
407
- .. function :: is_css3_color(instance)
408
-
409
- Check if the instance is a valid CSS 3 color name or code.
410
-
411
- >>> is_css3_color(" pink" )
412
- True
413
- >>> is_css3_color(" puce" )
414
- False
415
- >>> is_css_color_code(" #CC8899" )
416
- True
417
279
418
280
.. _isodate : http://pypi.python.org/pypi/isodate/
419
281
.. _rfc3987 : http://pypi.python.org/pypi/rfc3987/
0 commit comments