django-rulebase

Django rule base validation , inspired by laravel Request


Keywords
django, python, rules, validation
License
GPL-3.0
Install
pip install django-rulebase==0.1.3

Documentation

Django Rule-base Validator image image image

inspired by laravel Requests , for making django validations painless.

to use:

pip install django-rulebase

in app.views:

from django_rulebase.validator import require_validation

@require_validation({
    "egg":"file|mimetypes:application/octet-stream"
})
def view_function(request):
    ...

or

from django.http import HttpResponse,HttpResponseBadRequest
from django_rulebase import Request  

def  good_request(request,is_valid,errors):
	if is_valid:return HttpResponse("good request")
	return HttpResponseBadRequest(errors['date'])
   
class  CustomRequest(Request):
	view = good_request
	def  rules(self):
		return {
			"date":"date|required|before:tomorrow|after:yesterday"
		}

in app.urls

from django.conf.urls import url
from .views import CustomRequest  
urlpatterns = [
	url(r'^validate/$', CustomRequest.asView()),
]

with custom rule :

from django_rulebase import Request,Rule

class divisible(Rule):
    name = "divisible"
    def passes(self, value):
		# should return boolean
        return all([value % self.parse_value(field)[1]==0 for field in self.options])

    def message(self):
        return "{value} is not divisible to {options} "

class CustomRequest(Request):
    view = good_request
    def rules(self):
        return {
            "date":"date|required|string|before:tomorrow|after:yesterday",
            "some":"required|integer",
            "num":["required|integer",divisible(["some"])]
        }

or use everywhere:

from django_rulebase import Validator

def good_request(request):
    data = json.loads(request.body)
    validator = Validator({
        "date":"date|required|string|before:tomorrow|after:yesterday",
        "some":"required|integer",
        "num":["required|integer",divisible(["some"])]
    })
    validator.run_validation(data)
    if not validator.valid :
        return JsonResponse(validator.errors,safe=False)

built-in rules

Below is a list of all available validation rules and their function:

rule rule rule
Accepted Active URL After (Date)
After Or Equal (Date) Alpha Alpha Dash
Alpha Numeric Array Before (Date)
Before Or Equal (Date) Between Boolean
Confirmed Date Date Equals
Date Format Different Digits
Digits Between Distinct EMail
Exists (Database) File Filled
Greater Than Greater Than Or Equal Image (File)
In In Array Integer
IP Address JSON Less Than
Less Than Or Equal Max MIME Types
MIME Type By File Extension Min Not In
Not Regex Nullable Numeric
Present Regular Expression Required
Required If Required Unless Required With
Required With All Required Without Required Without All
Same Size String
Timezone Unique (Database) URL
UUID - -

accepted

The field under validation must be yes, on, 1, or true. This is useful for validating "Terms of Service" acceptance.

active-url

The field under validation must have a valid A or AAAA record according to the dnspython.

after:date

The field under validation must be a value after a given date. The dates will be passed into the dateparser:

'finish_date' : 'required|date|after:start_date'
'another_date' : 'required|date|after:tomorrow'

after-or-equal:date

The field under validation must be a value after or equal to the given date. For more information, see the after rule.

alpha

The field under validation must be entirely alphabetic characters.

alpha-dash

The field under validation may have alpha-numeric characters, as well as dashes and underscores.

alpha-num

The field under validation must be entirely alpha-numeric characters.

array

The field under validation must be a list array.

before:date

The field under validation must be a value preceding the given date. The dates will be passed into dateparser. In addition, like the after rule, the name of another field under validation may be supplied as the value of date.

before-or-equal:date

The field under validation must be a value preceding or equal to the given date. The dates will be passed into dateparser. In addition, like the after rule, the name of another field under validation may be supplied as the value of date.

between:min,max

The field under validation must have a size between the given min and max. Strings, numerics, arrays, and files are evaluated in the same fashion as the size rule.

boolean

The field under validation must be able to be cast as a boolean. Accepted input are true, false, 1, 0, "1", and "0".

confirmed

The field under validation must have a matching field of foo_confirmation. For example, if the field under validation is password, a matching password_confirmation field must be present in the input.

date

|The field under validation must be a valid date according to the dateparser.

date-equals:date

The field under validation must be equal to the given date. The dates will be passed into dateparser.

date-format:format

The field under validation must match the given format. You should use either date or date_format when validating a field, not both.

different:field

The field under validation must have a different value than field.

digits:value

The field under validation must be numeric and must have an exact length of value.

digits-between:min,max

The field under validation must have a length between the given min and max.

distinct

When working with arrays, the field under validation must not have any duplicate values.

'foo.*.id' : 'distinct'

email

The field under validation must be formatted as an e-mail address.

exists:table,column

The field under validation must exist on a given database table.

Basic Usage Of Exists Rule

'state' : 'exists:states'

If the column option is not specified, the field name will be used.

Specifying A Custom Column Name

'state' : 'exists:states,abbreviation'

Specifying A Custom connection

'state' : 'exists:connection.states,abbreviation'

or usring model:

from app.models import SomeModel
from django_rulebase.rule  import exists
...
'state' : exists(SomeModel,"attribute")

file

The field under validation must be a successfully uploaded file.

filled

The field under validation must not be empty when it is present.

gt:field

The field under validation must be greater than the given field. The two fields must be of the same type. Strings, numerics, arrays, and files are evaluated using the same conventions as the size rule.

gte:field

The field under validation must be greater than or equal to the given field. The two fields must be of the same type. Strings, numerics, arrays, and files are evaluated using the same conventions as the size rule.

image

The file under validation must be an image (jpeg, png, bmp, gif, or svg)

in:foo,bar,...

The field under validation must be included in the given list of values. Since this rule often requires you to implode an array, you can use _in() from .rule to fluently construct the rule:

from djanfo_rulebase.rule import _in
...
"field" : ["required",_in(1,3,"string")]

in-array:anotherfield.*

The field under validation must exist in anotherfield's values.

integer

The field under validation must be an integer.

ip

The field under validation must be an IP address.

ipv4

The field under validation must be an IPv4 address.

ipv6

The field under validation must be an IPv6 address.

json

The field under validation must be a valid JSON string.

lt:field

The field under validation must be less than the given field. The two fields must be of the same type. Strings, numerics, arrays, and files are evaluated using the same conventions as the size rule.

lte:field

The field under validation must be less than or equal to the given field. The two fields must be of the same type. Strings, numerics, arrays, and files are evaluated using the same conventions as the size rule.

max:value

The field under validation must be less than or equal to a maximum value. Strings, numerics, arrays, and files are evaluated in the same fashion as the size rule.

mimetypes:text/plain,...

The file under validation must match one of the given MIME types:

'video' : 'mimetypes:video/avi,video/mpeg,video/quicktime'

To determine the MIME type of the uploaded file, the file's contents will be read and the framework will attempt to guess the MIME type, which may be different from the client provided MIME type.

mimes:foo,bar,...

The file under validation must have a MIME type corresponding to one of the listed extensions.

Basic Usage Of MIME Rule

'photo' : 'mimes:jpeg,bmp,png'

Even though you only need to specify the extensions, this rule actually validates against the MIME type of the file by reading the file's contents and guessing its MIME type.

A full listing of MIME types and their corresponding extensions may be found at the following location: read more

min:value

The field under validation must have a minimum value. Strings, numerics, arrays, and files are evaluated in the same fashion as the size rule.

not-in:foo,bar,...

The field under validation must not be included in the given list of values. The Rule::notIn method may be used to fluently construct the rule:

not-regex:pattern

The field under validation must not match the given regular expression.

Internally, this rule uses the re.search. The pattern specified should obey the same formatting required by preg_match and thus also include valid delimiters. For example:

'email' : 'not_regex:^.+$'.

Note: When using the regex / not_regex patterns, it may be necessary to specify rules in an array instead of using pipe delimiters, especially if the regular expression contains a pipe character.

nullable

The field under validation may be null. This is particularly useful when validating primitive such as strings and integers that can contain null values.

numeric

The field under validation must be numeric.

present

The field under validation must be present in the input data but can be empty.

regex:pattern

The field under validation must match the given regular expression.

Internally, this rule uses the re.search. The pattern specified should obey the same formatting required by re.compile and thus also include valid delimiters. For example:

'email' : 'regex:^.+@.+$'.

Note: When using the regex / not_regex patterns, it may be necessary to specify rules in an array instead of using pipe delimiters, especially if the regular expression contains a pipe character.

required

The field under validation must be present in the input data and not empty. A field is considered "empty" if one of the following conditions are true:

The value is null. The value is an empty string. The value is an empty array or empty Countable object. The value is an uploaded file with no path.

required-if:anotherfield,value,...

The field under validation must be present and not empty if the anotherfield field is equal to any value.

required-unless:anotherfield,value,...

The field under validation must be present and not empty unless the anotherfield field is equal to any value.

required-with:foo,bar,...

The field under validation must be present and not empty only if any of the other specified fields are present.

required-with-all:foo,bar,...

The field under validation must be present and not empty only if all of the other specified fields are present.

required-without:foo,bar,...

The field under validation must be present and not empty only when any of the other specified fields are not present.

required-without-all:foo,bar,...

The field under validation must be present and not empty only when all of the other specified fields are not present.

same:field

The given field must match the field under validation.

size:value

The field under validation must have a size matching the given value. For string data, value corresponds to the number of characters. For numeric data, value corresponds to a given integer value. For an array, size corresponds to the count of the array. For files, size corresponds to the file size in kilobytes.

string

The field under validation must be a string. If you would like to allow the field to also be null, you should assign the nullable rule to the field.

timezone

The field under validation must be a valid timezone identifier according to the pytz.

unique:table,column

The field under validation must be unique in a given database table. If the column option is not specified, the field name will be used.

Specifying A Custom Column Name:

'email' : 'unique:users,email_address'

url

The field under validation must be a valid URL.

uuid

The field under validation must be a valid RFC 4122 (version 1, 3, 4, or 5) universally unique identifier (UUID).

to do :

  • integrate with django form
  • integrate with rest_framwork