Skip to main content

Validate Field

Project description

=======================

This is a project that is used to validate fields which is empty or it contain accurate values. Before touching the database we can check and raise appropriate error message if any mistmatch on it, else return True.

1)  Check value is missed or empty
2)  Check wether the datatype is correct or not
    2.1)    int = Specifies the integer value 
    2.2)    str = Specifies the string value
    2.3)    email = Specifies the email value
    2.4)    phone = Specifies the phone number value
    2.5)    alpha = Specifies the alphabetes value
    2.6)    '' = Specifies the null value, is equal to str
    2.7)    bool = Specifies the boolean value

Installing

pip install validate-field

Usage

Enter received_field(values that comes from the front-end side) and required_field(list of values that need to be check in th back-end)

from validate_field.validation import validate_field

received_filed = {
    'id':1,
    'name':"testuser",
    'email':'testmail@gmail.com',
    'mobile':'+918330069872',
    'password':"testpass@122#",
    'is_active':True
}
required_filed = [
    ['id','int'],
    ['name','alpha'],
    ['email','email'],
    ['mobile','phone'],
    ['password','str'],
    ['is_active','bool']
]

validation_result = validate_field(received_filed, required_filed)
print(validation_result)

Result
====================
>> True

Usecase 1 :- Check field is missing or not

Rule : name field is mandatory(required field)

Scenario : Avoid 'name' field

received_field = {
    'id':1,
    'email':'testmail@gmail.com',
    'mobile':'+918330069872',
    'password':"testpass@122#",
    'is_active':True
}
required_field = [
    ['id','int'],
    ['name','alpha'],
    ['email','email'],
    ['mobile','phone'],
    ['password','str'],
    ['is_active','bool']
]

validation_result = validate_field(received_field, required_field)
print(validation_result)

Result
====================
>> name is not found

Usecase 2 :- Check field is empty or not("")

Rule : name field is mandatory(required field)

Scenario : Avoid 'name' field value(name = "")

received_field = {
    'id':1,
    'name':"",
    'email':'testmail@gmail.com',
    'mobile':'+918330069872',
    'password':"testpass@122#",
    'is_active':True
}
required_field = [
    ['id','int'],
    ['name','alpha'],
    ['email','email'],
    ['mobile','phone'],
    ['password','str'],
    ['is_active','bool']
]

validation_result = validate_field(received_field, required_field)
print(validation_result)

Result
====================
>> name is not found

Usecase 3 :- Check integer field(int)

Rule : 'id' field only allow integer values

Scenario : Pass string value to the field 'id'

received_field = {
    'id':"a",
    'name':"testuser",
    'email':'testmail@gmail.com',
    'mobile':'+918330069872',
    'password':"testpass@122#",
    'is_active':True
}
required_field = [
    ['id','int'],
    ['name','alpha'],
    ['email','email'],
    ['mobile','phone'],
    ['password','str'],
    ['is_active','bool']
]

validation_result = validate_field(received_field, required_field)
print(validation_result)

Result
====================
>> id is not an integer value

Usecase 4 :- Check alpha field(alpha)

Rule : 'name' field only allow alphabetes

Scenario : Pass integer values along with the field 'name'

received_field = {
    'id':1,
    'name':"testuser123",
    'email':'testmail@gmail.com',
    'mobile':'+918330069872',
    'password':"testpass@122#",
    'is_active':True
}
required_field = [
    ['id','int'],
    ['name','alpha'],
    ['email','email'],
    ['mobile','phone'],
    ['password','str'],
    ['is_active','bool']
]

validation_result = validate_field(received_field, required_field)
print(validation_result)

Result
====================
>> name is only allow alphabets

Usecase 5 :- Check email field(email)

Rule : 'email' should be in correct format

Scenario : Pass incorrect format to the field 'email'

received_field = {
    'id':1,
    'name':"testuser",
    'email':'testmail.com',
    'mobile':'+918330069872',
    'password':"testpass@122#",
    'is_active':True
}
required_field = [
    ['id','int'],
    ['name','alpha'],
    ['email','email'],
    ['mobile','phone'],
    ['password','str'],
    ['is_active','bool']
]

validation_result = validate_field(received_field, required_field)
print(validation_result)

Result
====================
>>email is not a valid email

Usecase 6 :- Check phonenumber field(phone)

Rule : 'mobile' should be in correct format(Correct country code)

Scenario : Pass 'mobile' field with invalid country code +90

received_field = {
    'id':1,
    'name':"testuser",
    'email':'testmail@gmail.com',
    'mobile':'+908330069872',
    'password':"testpass@122#",
    'is_active':True
}
required_field = [
    ['id','int'],
    ['name','alpha'],
    ['email','email'],
    ['mobile','phone'],
    ['password','str'],
    ['is_active','bool']
]

validation_result = validate_field(received_field, required_field)
print(validation_result)

Result
====================
>> mobile is not a valid phone number

Usecase 7 :- Check phonenumber field(phone)

Rule : 'mobile' should be in correct format(Correct length)

Scenario : Pass 'mobile' field with invalid length +918330 or +918330069872333333

received_field = {
    'id':1,
    'name':"testuser",
    'email':'testmail@gmail.com',
    'mobile':'+918330',
    'password':"testpass@122#",
    'is_active':True
}
required_field = [
    ['id','int'],
    ['name','alpha'],
    ['email','email'],
    ['mobile','phone'],
    ['password','str'],
    ['is_active','bool']
]

validation_result = validate_field(received_field, required_field)
print(validation_result)

Result
====================
>> mobile is not a valid phone number

Usecase 8 :- Check string field(str)

Rule : 'password' field only allow string

Scenario : Pass 'password' field with integer value

received_field = {
    'id':1,
    'name':"testuser",
    'email':'testmail@gmail.com',
    'mobile':'+918330069872',
    'password':123,
    'is_active':True
}
required_field = [
    ['id','int'],
    ['name','alpha'],
    ['email','email'],
    ['mobile','phone'],
    ['password','str'],
    ['is_active','bool']
]

validation_result = validate_field(received_field, required_field)
print(validation_result)

Result
====================
>> password is not a string value

If you are does not specify the field type, then the field type will be consider as string(str)

Scenario : Specify 'password' field type as " "

received_field = {
    'id':1,
    'name':"testuser",
    'email':'testmail@gmail.com',
    'mobile':'+918330069872',
    'password':"testpass@122#",
    'is_active':True
}
required_field = [
    ['id','int'],
    ['name','alpha'],
    ['email','email'],
    ['mobile','phone'],
    ['password','']
]

Here password consider as string value.

Usecase 9 :- Check boolean field(bool)

Rule : 'is_active' field allow only True or False.

Scenario : Pass any other values unless True or False.

received_field = {
    'id':1,
    'name':"testuser",
    'email':'testmail@gmail.com',
    'mobile':'+918330',
    'password':"testpass@122#",
    'is_active':1
}
required_field = [
    ['id','int'],
    ['name','alpha'],
    ['email','email'],
    ['mobile','phone'],
    ['password','str'],
    ['is_active','bool']
]

validation_result = validate_field(received_field, required_field)
print(validation_result)

Result
====================
>> is_active is not a valid boolean value

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

validate_field-2.0.1.tar.gz (4.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

validate_field-2.0.1-py3-none-any.whl (4.5 kB view details)

Uploaded Python 3

File details

Details for the file validate_field-2.0.1.tar.gz.

File metadata

  • Download URL: validate_field-2.0.1.tar.gz
  • Upload date:
  • Size: 4.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.8.10

File hashes

Hashes for validate_field-2.0.1.tar.gz
Algorithm Hash digest
SHA256 bbba106f99c0da38d5ff5454910b3adbdb8e27994020d28622f2fb98934159ca
MD5 5448933acf7d7e77fcf0afe4e9fdec84
BLAKE2b-256 ef45d84181efecd898bba2ce324aeae0d4eba9eb761c1aea5d3c99915802fc44

See more details on using hashes here.

File details

Details for the file validate_field-2.0.1-py3-none-any.whl.

File metadata

  • Download URL: validate_field-2.0.1-py3-none-any.whl
  • Upload date:
  • Size: 4.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.8.10

File hashes

Hashes for validate_field-2.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 76b0a031920284d6c047491776d59c57ef60c4c25a30ed8507760c77beb640df
MD5 9d6863f6114fcc2ffbd9d95aaad08ccd
BLAKE2b-256 eee358e4e9d609b11d1b7cf7101be74a8acc2ec4563df4c04d6209ac1cd1ac9c

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page