The openIMIS Backend calcrule_validations reference module.
Project description
openIMIS Backend calcrule_validations reference module
How we can configure strategy for validation?
- The schema within the
Benefit Plan/Programme
entity holds a crucial role in this process. - For instance,
validationCalculation
in the schema triggers a specific validation strategy. Similarly, in the duplications section of the schema, settinguniqueness: true
signifies the need for duplication checks based on the record's field value. - Based on the provided schema below (from
programme/benefit plan
), it indicates that validations will run for theemail
field (validationCalculation
), and duplication checks will be performed fornational_id
(uniqueness: true
)
{
"$id":"https://example.com/beneficiares.schema.json",
"type":"object",
"title":"Record of beneficiares",
"$schema":"http://json-schema.org/draft-04/schema#",
"properties":{
"email":{
"type":"string",
"description":"email address to contact with beneficiary",
"validationCalculation":{
"name":"EmailValidationStrategy"
}
},
"able_bodied":{
"type":"boolean",
"description":"Flag determining whether someone is able bodied or not"
},
"national_id":{
"type":"string",
"uniqueness":true,
"description":"national id"
},
"educated_level":{
"type":"string",
"description":"The level of person when it comes to the school/education/studies"
},
"chronic_illness":{
"type":"boolean",
"description":"Flag determining whether someone has such kind of illness or not"
},
"national_id_type":{
"type":"string",
"description":"A type of national id"
},
"number_of_elderly":{
"type":"integer",
"description":"Number of elderly"
},
"number_of_children":{
"type":"integer",
"description":"Number of children"
},
"beneficiary_data_source":{
"type":"string",
"description":"The source from where such beneficiary comes"
}
},
"description":"This document records the details beneficiares"
}
- In essence, to create a custom validation rule applicable to a schema field,
you'll need to define a class in python file within the
strategies
folder (The filename should conclude with_validation_strategy.py
). - The example of validation class implementation:
from dataclasses import asdict
from calcrule_validations.strategies.base_strategy import BaseValidationsStrategy
from calcrule_validations.strategies.validation_strategy_interface import ValidationResult
class YourCustomValidationStrategy(BaseValidationsStrategy):
VALIDATION_CLASS = "YourCustomValidationStrategy"
@classmethod
def validate(cls, field_name, field_value, **kwargs):
return asdict(ValidationResult(
success=False,
field_name=field_name,
note="<YOUR VALIDATION NOTE>"
))
- The
VALIDATION_CLASS
property plays a significant role in defining the value for thevalidationCalculation: name
property within the JSON schema of the schema for theBenefit Plan/Programme
entity. - The validation implementation needs to be executed within the
validate
method. It's expected to return aValidationResult
as a dictionary structured as follows:
return asdict(ValidationResult(
success=False,
field_name=field_name,
note="<YOUR VALIDATION NOTE>"
))
- For a valid condition, the expected return should resemble the following:
return asdict(ValidationResult(
success=True,
field_name=field_name,
note="Ok"
))
- The signature of
validate
method isdef validate(cls, field_name, field_value, **kwargs)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file openimis_be_calcrule_validations-1.1.0.tar.gz
.
File metadata
- Download URL: openimis_be_calcrule_validations-1.1.0.tar.gz
- Upload date:
- Size: 9.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 52aaa51cab5aae49f89b64cfda5dcedddf940ecb221fd350fb08b4b64ce57ea4 |
|
MD5 | f3209451d187c56c4b67a3b855fa48e5 |
|
BLAKE2b-256 | be815ab815858629d6a74ce532121258218242dba6be8143dfb5b1458fd5cfc3 |
File details
Details for the file openimis_be_calcrule_validations-1.1.0-py3-none-any.whl
.
File metadata
- Download URL: openimis_be_calcrule_validations-1.1.0-py3-none-any.whl
- Upload date:
- Size: 13.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4cbaba98f37b726a628b2c9ad266f93e55903e32e69f046479a205ecb045f229 |
|
MD5 | a736c2cfcc5a94697186e38aa1ef0fac |
|
BLAKE2b-256 | ae316c43e97211ee97932ac3be90c2e9d68ac0928fcbe74e1362bc5955b2c810 |