Package: checkr
CheckrCheckr is a powerful validation library that enables developers to easily and accurately validate user input and other
data. With Checkr, you can quickly perform a wide range of checks, and ensuring that data conforms to specific
formatting requirements. With a simple and intuitive API, Checkr is easy to integrate into any project, and its flexible
configuration options make it a powerful
tool for building robust and reliable applications.InstallationpipinstallcheckrUsageFirst of all, You need to create a request class that has two methods:fromcheckr.BaseRequestimportBaseRequestfromtypingimportDictclassSampleRequest(BaseRequest):@staticmethoddefauthorize()->bool:returnTrue@staticmethoddefrules()->Dict:return{'name':'required|string|min:3',# You can use the pipe'age':['required','numeric','gte:18'],# Or you can just provide a list}Use the request class to validate data:fromcheckrimportvalidatefromcheckr.exceptionsimportValidationErrorrequest_data={'name':'john doe'}try:validate(SampleRequest,request_data)exceptValidationErrorase:# Do anything with the validation result here.print(e.errors)Authorizing the requestThis method is useful when you want to ensure that validation is performed only by certain parties or by specific users.
In your request class, include a static method namedauthorizethat returns a boolean.Customizing the validation messagesTo customize the validation messages returned when the validation of a field fails,
you can use themessagesmethod. Here's an example:fromtypingimportDictfromcheckr.BaseRequestimportBaseRequestclassSampleRequest(BaseRequest):...@classmethoddefmessages(cls)->Dict:return{'name.required':'Please provide a name!','age.number':'Oops! not a number!'}...Adding fields before starting the validationSometimes, you may find it useful to add a field before the process of validation starts.
You can accomplish this by using theprepare_for_validationhook as demonstrated in the following example:fromtypingimportDictfromcheckr.BaseRequestimportBaseRequestclassSampleRequest(BaseRequest):...@classmethoddefprepare_for_validation(cls)->Dict:cls.merge(data_to_add={'foo':'bar'})...Adding fields after finishing the validationIt's almost identical to adding fields before starting the validation. You'll usepassed_validationhook as demonstrated in the following example:Notice that this method will not run if the validation finds invalid data.fromtypingimportDictfromcheckr.BaseRequestimportBaseRequestclassSampleRequest(BaseRequest):...@classmethoddefpassed_validation(cls)->Dict:cls.merge(data_to_add={'foo':'bar'})...Available rulesRule NameDescriptionUsagebetweenChecks if a value is between a minimum and maximum valuebetween:x,ywherexandyare required. If provided with more than 2 numbers, the first and last numbers will be taken into accountstringChecks if a value is a stringstringintChecks if a value is an integerintrequiredChecks if a value is presentrequirednumericChecks if a value is numericnumericarrayChecks if a value is a valid Python listarrayarray_ofChecks if an array contains only elements of a specific typearray_of:stringin_arrayChecks if a value is in an array of valid optionsin_array:a,b,cnot_in_arrayChecks if a value is not in a list of invalid optionsnot_in_array:a,b,cdate_afterChecks if a date is after a specific datedate_after:YYYY-mm-dddate_after_or_equalChecks if a date is after or equal to a specific datedate_after_or_equal:YYYY-mm-dddate_beforeChecks if a date is before a specific datedate_before:YYYY-mm-dddate_before_or_equalChecks if a date is before or equal to a specific datedate_before_or_equal:YYYY-mm-ddminChecks if a value is greater than or equal to a minimum value if it's a number. If provided a string, it checks if the string length is greater than or equal to the provided value.min:xwherexis a number amd requiredmaxChecks if a value is greater than or equal to a maximum value if it's a number. If provided a string, it checks if the string length is less than or equal to the provided value.max:xwherexis a number and requiredmin_digitsChecks if a numeric value has at least a specified number of digitsmin_digits:nmax_digitsChecks if a numeric value has at most a specified number of digitsmax_digits:njsonChecks if a value is a valid JSON objectjsonnullableAllows a value to be null or undefinednullabledecimalChecks if a value is a decimal numberdecimal:xwherexis the number of decimal places and is optionalgtChecks if a value is greater than a specified valuegt:xwherexis a numbergteChecks if a value is greater than or equal to a specified valuegte:xwherexis a numberltChecks if a value is less than a specified valuelt:xwherexis a numberlteChecks if a value is less than or equal to a specified valuelte:xwherexis a numberipChecks if a value is a valid IPv4 or IPv6 addressipipv4Checks if a value is a valid IPv4 addressipv4ipv6Checks if a value is a valid IPv6 addressipv6lowercaseChecks if a string is lowercaselowercaseuppercaseChecks if a string is uppercaseuppercasemac_addressChecks if a value is a valid MAC addressmac_addressuuidChecks if a value is a valid UUIDuuid:vwherevis the version and defaults to v4 if no version providedulidChecks if a value is a valid ULIDulidurlChecks if a value is a valid URLurlstartswithChecks if a string starts with a specific valuestartswithendswithChecks if a string ends with a specific valueendswithemailChecks if a value is a valid email addressemailboolChecks if a value is a boolean (true or false)boolAdding custom validation rules:In some cases, it is useful to create a rule that validates an input in custom way like checking if a phone number has a
specific format or checking that a city name exists in reality. The following example demonstrates how to accomplish
this:fromtypingimportAnyfromcheckr.rules.BaseRuleimportBaseRuleclassCustomRule(BaseRule):def__str__(self):return'custom_rule'# required. If not provided, incorrect errors dictionary will be returneddefvalidate(self,attribute:str,value:Any)->bool:returnTruedefmessage(self,attribute:str):returnf'This field failed because something was incorrect'You can use thevalidatemethod to validate against a condition.
note that this method takes two arguments:attributewhich is the name of the fieldvaluewhich is the actual value.There's also another method calledmessagewhich is used to return a message in case the validation fails. This method
receives one parameter,attribute, which is the name of the field under validation.