-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidators.py
39 lines (32 loc) · 1.08 KB
/
validators.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# -*- coding: utf-8 -*-
from re import compile as regex
from django.core.exceptions import ValidationError
from django.core.validators import RegexValidator
def zeroslash_slug(value):
'''
validate if a value is a valid slug with no slashes
valid slugs those don't contain any of the following chars:
! @ ~ # \ > < : ; [ ] { } % & * ( ) $ / \s ?
'''
re = r'^[^!@~#\\\\><:;\[\]\{\}%&\*\(\)\$/\s\?]+$'
RegexValidator(re, r'Invalid Slug Value').__call__(value)
def oneslash_slug(value):
'''
validate if a value is a valid slug
valid slugs those don't contain any of the following chars:
! @ ~ # \ > < : ; [ ] { } % & * ( ) $ \s ?
and they are in the form of two slugs seprated by
one slash
'''
re = r'^[^!@~#\\\\><:;\[\]\{\}%&\*\(\)\$/\s\?]+$'
RegexValidator(
re,
r'Invalid Slug Value, value should contain one slash').__call__(value)
def valid_regex(value):
"""
Validate if a value is a valid regular expression
"""
try:
regex(value)
except:
raise ValidationError('Invalid regex')