Package: django-easy-validator
Django Easy Validatorthis piece of software is inspired by the validation system oflaravel,
you can use this software to validate your POST/GET data easily and softly.TODOIPv4 pattern supportIPv6 pattern supportURL pattern supportRequirementsePython 3.4Python 3.5Python 3.6Python 3.7Django 1.6 +Installationinstall from pypi :pip install django-easy-validatorinstall from source code:python setup.py installUsagecreate your own validator with Validator class.fromvalidatorimportValidatorclassLoginValidator(Validator):username='required|email'password='required'message={'username':{'required':_('username is required'),'email':_('username is not an email address')},'password':{'required':_('password is required')}}spawn your validation class then call validate method to check it out.validator=LoginValidator({'username':'michael','password':'12345678'})status=validator.validate()ifstatus:print("login succeed")else:errors=validator.get_message()print(errors)Supported Rulesrequiredaccepteddatedate_beforedate_afterdate_rangedatetimedatetime_beforedatetime_afterdatetime_rangeactive_urlnumbericdigitsregexemailmin_lengthmax_lengthidscellphonealphabetswitchuniquesizeminmaxfileimagevideoaudioattachementalpha_dashalpha_numberarraydate_before_equaldate_after_equaldatetime_before_equaldatetime_after_equalbetweenbooleanusernamepasswordASCIIsamedecimalexistunique_againstrequiredclassLoginValidator(Validator):username='required'the field with required rule cant be null or empty.acceptedclassLoginValidator(Validator):username='required'remember_me='accepted'the field with accepted must be one of the following strings, and the
string case is insensitive.['yes','no','true','false','0','1']dateclassRegisterValidator(Validator):birthday='required|date:%Y-%m-%d'the field with date must be a python date format string, the parameter
should be a format suit the datetime.datetime.strptime function.date_beforeclassSubmitValidator(Validator):due='required|date_before:2017-12-31'the field with date_before must be a date format string suit xxxx-mm-dd.date_afterclassSubmitValidator(Validator):date='required|date_after:2017-11-12'the field with date_after must be a date format string suit xxxx-mm-dd.date_rangeclassSubmitValidator(Validator):date_range='required|date_range:2017-01-01,2017-12-31'the field with date_range must be 2 date format strings and it should suit xxxx-mm-dd.datetimeclassRegisterValidator(Validator):login_time='required|datetime:%Y-%m-%d%H:%M:%S'the field with datetime and the parameter must be a datetime string suit the datetime.datetime.strptime function.datetime_beforeclassLoginValidator(Validator):time='required|datetime_before:2017-12-12 13:14:00'the field with datetime_before must be a datetime string can strap to a datetime object and so as the parameter.datetime_afterclassLoginValidator(Validator):time='required|datetime_after:2017-12-12 13:14:00'the field with datetime_after must be a datetime string can strap to a datetime object and so as the parameter.datetime_rangeclassLoginValidator(Validator):period='datetime_range:2017-12-12 13:14:00, 2017-12-15 13:14:00'active_urlclassActiveURLValidator(Validator):url='active_url'the field with active_url must be a active url you could connect to and get reply.numbericclassRegisterValidator(Validator):id_number='required|numberic'the field with numberic must be a number, it should be a integer not a float or something.digitsclassRegisterValidator(Validator):product_series='required|digits'the field with digits must only contains digits token.regexclassRegisterValidator(Validator):id_number='required|regex:^0[0-9]{5,10}$'the field with regex must be a string, the parameter could be any regex pattern string, this
rule will match the field value with the paramter pattern out.emailclassRegisterValidator(Validator):username='required|email'the field with email must be a valid email address string.min_lengthclassRegister(Validator):password='min_length:8'the field withmin_lengthmust has the minimal length of string or value of number.max_lengthclassRegisterValidator(Validator):username='max_length:20'the field withmin_lengthmust has the minimal length of string or value of number.idsclassDeleteValidator(Validator):ids='ids'the field with ids must be a integer string splited by the comma, such as '1,2,3,4,5'cellphoneclassRegisterValidator(Validator):phone='cellphone'the field with cellphone rule must be a real cellphone number , it could be '+8613811754531' or just '13811754531'alphabetclassRegisterValidator(Validator):name='alphabet'the field with alphabet must be a standard alphabet string.switchclassLoginValidator(Validator):rememberme='switch:yes,no'the field with switch must be in the params array, it this case , rememberme must be yes or no.uniqueclassRegisterValidator(Validator):username='unique:AUTH_USER_MODEL,username'email='unique:account.User,email'the field with unique must has 2 parameters , the first is appname.modelname, the second is the field to check,
actually it is also the column to check if is already exists in this table, if you want to validate the django auth
user, the first paramater must be AUTH_USER_MODEL.sizeclassUpdateProfileValidator(Validator)avatar='image|size:2048'the field with size has 4 kind of types , if the given field is an file, the parameter means the size of the file with KB,
if the field is a string , the parameter means the size is the string length, if the field is an integer , the size means
the integer value, if the field is an array, the size means the array size.minclassUpdateProfileValidator(Validator):profile='image|min:2048'the field with min has the same meaning of size, it's just check the minimal of the field , the size is check the equal of the field.maxclassUpdateProfileValidator(Validator):profile='image|min:2048'the field with min has the same meaning of size, it's just check the maximal of the field , the size is check the equal of the field.fileclassUploadValidator(Validator):file='file:png,jpeg,zip,rar'the field with file rule is to check the file type. parameters needed to be a string array.imageclassUploadValidator(Validator):file='image'the field with file image rule just do the same thing like file , it is a convenient way to check the common image type , in this way you do not need to add image ext parameter.
the check type is ['png', 'jpeg', 'gif', 'jpg', 'svg'] .videoclassUploadValidator(Validator):file='video'the field with video rule just do the same thing like file , it is a convenient way to check the common video type , in this way you do not need to add video ext parameter.
the check type is  ['mp4', 'avi', 'mkv', 'flv', 'rmvb'].audioclassUploadValidator(Validator):file='audio'the field with audio rule just do the same thing like file , it is a convenient way to check the common audio type , in this way you do not need to add video ext parameter.
the check type is  ['mp3', 'wma', 'flac', 'ape', 'ogg'].attachementclassUploadValidator(Validator):file='attachement'the field with attachement rule just do the same thing like file , it is a convenient way to check the common attachement type , in this way you do not need to add video ext parameter.
the check type is ['doc', 'zip', 'ppt', 'docx', 'excel', 'rar'].alpha_dashclassRegisterValidator(Validator):username='alpha_dash'the field with alpha_dash rule is just check out if the string only includes alphabet characters and dash character.alpha_numberclassRegisterValidator(Validator):username='alpha_number'the field with alpha_number the given value must conbines with only alphabets and numbers.arrayclassRegisterValidator(Validator):hobbies='array'the field with array must be a array string ,such as 'guitar, computer, music, food'.date_before_equalclassRegisterValidator(Validator):due_at='date_before_equal:2018-01-08'the field with date_before_equal just check the given value must be a date string and before or equal the given parameter.date_after_equalclassRegisterValidator(Validator):due_at='date_after_equal:2018-01-08'the field with date_after_equal just check the given value must be a date string and afer or equal the given parameter.datetime_before_equalclassRegisterValidator(Validator):due_at='datetime_before_equal:1990-12-12 06:08:26'the field with datetime_before_equal just check the given value must be a datetime string and befor the given parameter.datetime_after_equalclassRegisterValidator(Validator):due_at='datetime_after_equal:1990-12-12 06:08:26'the field with datetime_after_equal just check the given value must be a datetime string and after the given parameter.betweenclassRegisterValidator(Validator):age='between:10, 15'the field with between requires the given field value must be a integer number and it's value must between the parameters.booleanclassRegisterValidator(Valiadtor):remember='boolean'the field with boolean requires the given value should be one of this '['0', '1', 'true', 'false']'usernameclassRegisterValidator(Validator):username='username'the field with username requires the given value starts with an alphabet character and it could includes with numbers , dash, underscore.passwordclassRegisterValidator(Validator):password='password:low'the field with password ruls requires an parameter , it could be : low, middle , high.
the 3 different check methods has different check level.the low method means the password length must longer than 7the middle method means the password length must longer than 7 and it sould contains lower , upper latin characters and digitsthe high method means the password lenght must longer than 7 and it sould contains lower , upper latin characters and digits, and specialcharacters.ASCIIclassASCIIValidator(Validator):ascii='ascii'the ascii requires the given value only includes ascii characters.sameclassSameValidator(Validator):password='required|min_lengh:8'password_confirm='same:password'the same rule just validate the give field vale checks the value if is same as the other value.deciamlclassDecimalValidator(Validator):price='required|decimal'the decimal rule just validate the give field value if is a decimal or a float number or number string.existclassExistValidator(Validator):username='required|exist:AUTH_USER_MODEL,username'the exist rule just check the given field record weather exists in the database table.unique_againstclassUniqueAgainstValidator(Validator):username='required|unique_against:AUTH_USER_MODEL, username, youngershen'the unique_against validator check weather the given value column exists in the database and against the third parameter.pasciiclassPASCIIValidator(Validator):username='required|pascii'pascii ensure the input string must be a ascii string and it can't contians the unprintable
ascii characters.unblankunblank make the given field must be a none-blank string, it could be empty, but if it is not empyt, it should be a unblank
string.the rule indicates the given field shoud be empty string or it should be a none-empty string, like '\r\n\r\n' it will return a
False, becaus  '\r\n' is Aemptystring, the '' wille return True, because it is a normal empty string.integerthis rule check the give value if it's a proper decimal integerpos_integerthe positive value version the integer valueneg_integerthe negative value version the integer valuepercentagecheck the give value if it's a integer  value from 0 to 100ip_addressjust check the given value if it's a ip address, both checks for ip v4 and v6 address.Advanced TopicCustom Validation Rules# define the rulefromvalidatorimportBaseRuleclassTestRule(BaseRule):name='test_rule'message='test custom rule failed'description='just for custom rule test'defcheck_value(self):self.status=Trueifself.field_value=='test'elseFalsedefcheck_null(self):pass# define a validator to use the ruleclassTestRuleValidator(Validator):name='test_rule'# to run the validationextra_rules={TestRule.get_name():TestRule}validator=TestRuleValidator(extra_rules=extra_rules,data={'name':'test'})assertvalidator.validate()custom a validation rule is very easy, you just import the BaseRule and implements the method ,
the most important thing is before you use your rule , you should pass it to your validator
class when it init through the extra_rules parameter.developmentclone the projectpip install dev.txtfix some bugsadd test case in tests/tests.pyrun pytest command in terminal and make sure your code is okpush and make a pull requestor you can run python setup.py bdist_wheel / sdist to upload the packagethen just run twine uplaod ./dist/* to upload package to pypi