pyveritas

class pyveritas.BooleanRule(field: str)

Bases: Rule

Checks if a field is a boolean.

The field must be a boolean value for the BooleanRule to be valid.

error_message(data: Dict, context: RuleContext = None) str

Returns an error message if the field is not a boolean.

Args:

data (t.Dict): The data that failed validation. context (RuleContext, optional): Contextual information for the rule. Defaults to None.

Returns:

str: An error message describing the validation failure.

is_valid(data: Dict, context: RuleContext = None) bool

Checks if the field is a boolean.

Args:

data (t.Dict): The data to validate. context (RuleContext, optional): Contextual information for the rule. Defaults to None.

Returns:

bool: True if the field is a boolean, False otherwise.

class pyveritas.DataContract(rules: List[Rule] = None)

Bases: ABC

Base class for all data contracts.

A data contract defines the structure and constraints for a particular type of data. Subclasses must implement the validate method.

add_rule(rule: Rule)

Adds a rule to the contract.

Args:

rule (Rule): The rule to add.

abstract validate(data: Dict, context: RuleContext = None) List[str]

Validates the given data against the contract’s rules.

Args:

data (t.Dict): A dictionary containing the data to validate. context (RuleContext, optional): A RuleContext object providing additional

context for the validation. Defaults to None.

Returns:

t.List[str]: A list of error messages. If the list is empty, the data is valid.

class pyveritas.DateTimeFormatRule(field: str, format_string: str)

Bases: DateTimeRule

Checks if a datetime string matches a specified format.

The datetime string must match the specified format for the DateTimeFormatRule to be valid.

error_message(data: Dict, context: RuleContext = None) str

Returns an error message if the datetime string does not match the specified format.

Args:

data (t.Dict): The data that failed validation. context (RuleContext, optional): Contextual information for the rule. Defaults to None.

Returns:

str: An error message describing the validation failure.

is_valid(data: Dict, context: RuleContext = None) bool

Checks if the datetime string matches the specified format.

Args:

data (t.Dict): The data to validate. context (RuleContext, optional): Contextual information for the rule. Defaults to None.

Returns:

bool: True if the datetime string matches the specified format, False otherwise.

class pyveritas.JSONRule(field: str)

Bases: Rule

Checks if a field contains valid JSON.

The field must contain a valid JSON string for the JSONRule to be valid.

is_valid(data: Dict, context: RuleContext = None) bool

Checks if the field contains valid JSON.

Args:

data (t.Dict): The data to validate. context (RuleContext, optional): Contextual information for the rule. Defaults to None.

Returns:

bool: True if the field contains valid JSON, False otherwise.

class pyveritas.NumberRangeRule(field: str, min_value: int | float = None, max_value: int | float = None)

Bases: NumberRule

Checks if a number falls within a specified range (inclusive).

The number must be greater than or equal to min_value and less than or equal to max_value (inclusive).

error_message(data: Dict, context: RuleContext = None) str

Returns an error message if the number is not within the specified range.

Args:

data (t.Dict): The data that failed validation. context (RuleContext, optional): Contextual information for the rule. Defaults to None.

Returns:

str: An error message describing the validation failure.

is_valid(data: Dict, context: RuleContext = None) bool

Checks if the number is within the specified range.

Args:

data (t.Dict): The data to validate. context (RuleContext, optional): Contextual information for the rule. Defaults to None.

Returns:

bool: True if the number is within the specified range, False otherwise.

class pyveritas.RequiredRule(field: str)

Bases: Rule

Checks if a field is present in the data.

The field must be present in the data for the RequiredRule to be valid.

error_message(data: Dict, context: RuleContext = None) str

Returns an error message if the field is not present in the data.

Args:

data (t.Dict): The data that failed validation. context (RuleContext, optional): Contextual information for the rule. Defaults to None.

Returns:

str: An error message describing the validation failure.

is_valid(data: Dict, context: RuleContext = None) bool

Checks if a field is present in the data.

Args:

data (t.Dict): The data to validate. context (RuleContext, optional): Contextual information for the rule. Defaults to None.

Returns:

bool: True if the field is present in the data, False otherwise.

class pyveritas.Rule

Bases: ABC

Base class for all validation rules.

Rules define a specific validation check. Subclasses must implement the is_valid and error_message methods.

abstract error_message(data: Dict, context: RuleContext = None) str

Returns an error message if the rule is not valid.

Args:

data (t.Dict): The data that failed validation. context (RuleContext, optional): Contextual information for the rule.

Defaults to None.

Returns:

str: An error message describing the validation failure.

abstract is_valid(data: Dict, context: RuleContext = None) bool

Checks if the rule is valid for the given data.

Args:

data (t.Dict): The data to validate. context (RuleContext, optional): Contextual information for the rule.

Defaults to None.

Returns:

bool: True if the data is valid, False otherwise.

class pyveritas.RuleContext(data: Dict = {})

Bases: object

Provides context to rules during validation.

Can be extended for application-specific needs.

Attributes:

context (t.Dict, optional): A dictionary containing contextual data. Defaults to {}.

class pyveritas.StringLengthRule(field: str, min_length: int = None, max_length: int = None)

Bases: StringRule

Checks if a string’s length falls within a specified range.

The string length must be greater than or equal to min_length and less than or equal to max_length (inclusive).

error_message(data: Dict, context: RuleContext = None) str

Returns an error message if the string’s length is not within the specified range.

Args:

data (t.Dict): The data that failed validation. context (RuleContext, optional): Contextual information for the rule. Defaults to None.

Returns:

str: An error message describing the validation failure.

is_valid(data: Dict, context: RuleContext = None) bool

Checks if the string’s length is within the specified range.

Args:

data (t.Dict): The data to validate. context (RuleContext, optional): Contextual information for the rule. Defaults to None.

Returns:

bool: True if the string’s length is within the specified range, False otherwise.

class pyveritas.StringRegexRule(field: str, regex: str)

Bases: StringRule

Checks if a string matches a specified regular expression.

The string must match the regular expression for the StringRegexRule to be valid.

error_message(data: Dict, context: RuleContext = None) str

Returns an error message if the string does not match the regular expression.

Args:

data (t.Dict): The data that failed validation. context (RuleContext, optional): Contextual information for the rule. Defaults to None.

Returns:

str: An error message describing the validation failure.

is_valid(data: Dict, context: RuleContext = None) bool

Checks if the string matches the regular expression.

Args:

data (t.Dict): The data to validate. context (RuleContext, optional): Contextual information for the rule. Defaults to None.

Returns:

bool: True if the string matches the regular expression, False otherwise.

class pyveritas.TestRunner(name: str)

Bases: object

A test runner for DataContracts.

This class is responsible for running test cases and validating data against data contracts.

add(test_case: Dict)

Adds a test case to the suite.

Args:
test_case (t.Dict): A dictionary containing test case details

(description, contract, data, expected_errors)

run()

Runs all test cases in the suite.

For each test case, it dynamically loads the contract class, instantiates it, validates the data against the contract, and prints the results.

summary()

Prints a summary of test results.

This method has been removed and is not used for the “base.py” file has been removed

class pyveritas.UserContract

Bases: DataContract

Example data contract for a User object.

This contract checks that the ‘name’ field is a string with a length between 3 and 20 characters, the ‘email’ field is a valid email address, and the ‘age’ field is a number between 0 and 120.

validate(data: Dict, context: RuleContext = None) List[str]

Validates the given data against the contract’s rules.

Args:

data (t.Dict): A dictionary containing the data to validate. context (RuleContext, optional): A RuleContext object providing additional

context for the validation. Defaults to None.

Returns:

t.List[str]: A list of error messages. If the list is empty, the data is valid.

class pyveritas.Validator(contract: DataContract)

Bases: object

A simple validator class that validates data against a DataContract.

The Validator takes a DataContract as input and provides methods for validating data against that contract.

is_valid(data: Dict, context: RuleContext = None) bool

Checks if the given data is valid according to the contract.

Args:

data (t.Dict): The data to validate. context (RuleContext, optional): A RuleContext object providing additional

context for the validation. Defaults to None.

Returns:

bool: True if the data is valid, False otherwise.

validate(data: Dict, context: RuleContext = None) List[str]

Validates the given data against the contract.

Args:

data (t.Dict): The data to validate. context (RuleContext, optional): A RuleContext object providing additional

context for the validation. Defaults to None.

Returns:

t.List[str]: A list of error messages. If the list is empty, the data is valid.

pyveritas.contracts

class pyveritas.contracts.DataContract(rules: List[Rule] = None)

Bases: ABC

Base class for all data contracts.

A data contract defines the structure and constraints for a particular type of data. Subclasses must implement the validate method.

add_rule(rule: Rule)

Adds a rule to the contract.

Args:

rule (Rule): The rule to add.

abstract validate(data: Dict, context: RuleContext = None) List[str]

Validates the given data against the contract’s rules.

Args:

data (t.Dict): A dictionary containing the data to validate. context (RuleContext, optional): A RuleContext object providing additional

context for the validation. Defaults to None.

Returns:

t.List[str]: A list of error messages. If the list is empty, the data is valid.

class pyveritas.contracts.UserContract

Bases: DataContract

Example data contract for a User object.

This contract checks that the ‘name’ field is a string with a length between 3 and 20 characters, the ‘email’ field is a valid email address, and the ‘age’ field is a number between 0 and 120.

validate(data: Dict, context: RuleContext = None) List[str]

Validates the given data against the contract’s rules.

Args:

data (t.Dict): A dictionary containing the data to validate. context (RuleContext, optional): A RuleContext object providing additional

context for the validation. Defaults to None.

Returns:

t.List[str]: A list of error messages. If the list is empty, the data is valid.

pyveritas.rules

class pyveritas.rules.AndRule(rule1: Rule, rule2: Rule)

Bases: Rule

Combines two rules with a logical AND.

The data must be valid according to both rules for the AndRule to be valid.

error_message(data: Dict, context: RuleContext = None) str

Returns an error message if the data is not valid according to either rule.

Args:

data (t.Dict): The data that failed validation. context (RuleContext, optional): Contextual information for the rules. Defaults to None.

Returns:

str: An error message describing the validation failure.

is_valid(data: Dict, context: RuleContext = None) bool

Checks if the data is valid according to both rules.

Args:

data (t.Dict): The data to validate. context (RuleContext, optional): Contextual information for the rules. Defaults to None.

Returns:

bool: True if the data is valid according to both rules, False otherwise.

class pyveritas.rules.BooleanRule(field: str)

Bases: Rule

Checks if a field is a boolean.

The field must be a boolean value for the BooleanRule to be valid.

error_message(data: Dict, context: RuleContext = None) str

Returns an error message if the field is not a boolean.

Args:

data (t.Dict): The data that failed validation. context (RuleContext, optional): Contextual information for the rule. Defaults to None.

Returns:

str: An error message describing the validation failure.

is_valid(data: Dict, context: RuleContext = None) bool

Checks if the field is a boolean.

Args:

data (t.Dict): The data to validate. context (RuleContext, optional): Contextual information for the rule. Defaults to None.

Returns:

bool: True if the field is a boolean, False otherwise.

class pyveritas.rules.DateTimeFormatRule(field: str, format_string: str)

Bases: DateTimeRule

Checks if a datetime string matches a specified format.

The datetime string must match the specified format for the DateTimeFormatRule to be valid.

error_message(data: Dict, context: RuleContext = None) str

Returns an error message if the datetime string does not match the specified format.

Args:

data (t.Dict): The data that failed validation. context (RuleContext, optional): Contextual information for the rule. Defaults to None.

Returns:

str: An error message describing the validation failure.

is_valid(data: Dict, context: RuleContext = None) bool

Checks if the datetime string matches the specified format.

Args:

data (t.Dict): The data to validate. context (RuleContext, optional): Contextual information for the rule. Defaults to None.

Returns:

bool: True if the datetime string matches the specified format, False otherwise.

class pyveritas.rules.DateTimeRule(field: str)

Bases: Rule

Base class for datetime-based rules.

Provides a helper method for retrieving the datetime value from the data.

class pyveritas.rules.EndDateAfterStartDateRule(start_date_field: str, end_date_field: str)

Bases: Rule

Checks if an end date happens after a start date.

The end date must happen after the start date for the EndDateAfterStartDateRule to be valid.

error_message(data: Dict, context: RuleContext = None) str

Returns an error message if the end date is not after the start date.

Args:

data (t.Dict): The data that failed validation. context (RuleContext, optional): Contextual information for the rule. Defaults to None.

Returns:

str: An error message describing the validation failure.

is_valid(data: Dict, context: RuleContext = None) bool

Checks if the end date is after the start date in the given data.

Args:

data (t.Dict): The data to validate. context (RuleContext, optional): A RuleContext object providing additional

context for the validation. Defaults to None.

Returns:

bool: True if the end date is after the start date, False otherwise.

class pyveritas.rules.JSONRule(field: str)

Bases: Rule

Checks if a field contains valid JSON.

The field must contain a valid JSON string for the JSONRule to be valid.

is_valid(data: Dict, context: RuleContext = None) bool

Checks if the field contains valid JSON.

Args:

data (t.Dict): The data to validate. context (RuleContext, optional): Contextual information for the rule. Defaults to None.

Returns:

bool: True if the field contains valid JSON, False otherwise.

class pyveritas.rules.NotRule(rule: Rule)

Bases: Rule

Negates a rule.

The data must not be valid according to the rule for the NotRule to be valid.

error_message(data: Dict, context: RuleContext = None) str

Returns an error message if the data is valid according to the rule.

Args:

data (t.Dict): The data that passed validation (but shouldn’t have). context (RuleContext, optional): Contextual information for the rule. Defaults to None.

Returns:

str: An error message describing the validation failure (i.e., that the data was unexpectedly valid).

is_valid(data: Dict, context: RuleContext = None) bool

Checks if the data is not valid according to the rule.

Args:

data (t.Dict): The data to validate. context (RuleContext, optional): Contextual information for the rule. Defaults to None.

Returns:

bool: True if the data is not valid according to the rule, False otherwise.

class pyveritas.rules.NumberRangeRule(field: str, min_value: int | float = None, max_value: int | float = None)

Bases: NumberRule

Checks if a number falls within a specified range (inclusive).

The number must be greater than or equal to min_value and less than or equal to max_value (inclusive).

error_message(data: Dict, context: RuleContext = None) str

Returns an error message if the number is not within the specified range.

Args:

data (t.Dict): The data that failed validation. context (RuleContext, optional): Contextual information for the rule. Defaults to None.

Returns:

str: An error message describing the validation failure.

is_valid(data: Dict, context: RuleContext = None) bool

Checks if the number is within the specified range.

Args:

data (t.Dict): The data to validate. context (RuleContext, optional): Contextual information for the rule. Defaults to None.

Returns:

bool: True if the number is within the specified range, False otherwise.

class pyveritas.rules.NumberRule(field: str)

Bases: Rule

Base class for number-based rules.

Provides a helper method for retrieving the number value from the data.

class pyveritas.rules.OrRule(rule1: Rule, rule2: Rule)

Bases: Rule

Combines two rules with a logical OR.

The data must be valid according to at least one of the rules for the OrRule to be valid.

error_message(data: Dict, context: RuleContext = None) str

Returns an error message if the data is not valid according to either rule.

Args:

data (t.Dict): The data that failed validation. context (RuleContext, optional): Contextual information for the rules. Defaults to None.

Returns:

str: An error message describing the validation failure.

is_valid(data: Dict, context: RuleContext = None) bool

Checks if the data is valid according to at least one of the rules.

Args:

data (t.Dict): The data to validate. context (RuleContext, optional): Contextual information for the rules. Defaults to None.

Returns:

bool: True if the data is valid according to at least one of the rules, False otherwise.

class pyveritas.rules.RequiredRule(field: str)

Bases: Rule

Checks if a field is present in the data.

The field must be present in the data for the RequiredRule to be valid.

error_message(data: Dict, context: RuleContext = None) str

Returns an error message if the field is not present in the data.

Args:

data (t.Dict): The data that failed validation. context (RuleContext, optional): Contextual information for the rule. Defaults to None.

Returns:

str: An error message describing the validation failure.

is_valid(data: Dict, context: RuleContext = None) bool

Checks if a field is present in the data.

Args:

data (t.Dict): The data to validate. context (RuleContext, optional): Contextual information for the rule. Defaults to None.

Returns:

bool: True if the field is present in the data, False otherwise.

class pyveritas.rules.Rule

Bases: ABC

Base class for all validation rules.

Rules define a specific validation check. Subclasses must implement the is_valid and error_message methods.

abstract error_message(data: Dict, context: RuleContext = None) str

Returns an error message if the rule is not valid.

Args:

data (t.Dict): The data that failed validation. context (RuleContext, optional): Contextual information for the rule.

Defaults to None.

Returns:

str: An error message describing the validation failure.

abstract is_valid(data: Dict, context: RuleContext = None) bool

Checks if the rule is valid for the given data.

Args:

data (t.Dict): The data to validate. context (RuleContext, optional): Contextual information for the rule.

Defaults to None.

Returns:

bool: True if the data is valid, False otherwise.

class pyveritas.rules.RuleContext(data: Dict = {})

Bases: object

Provides context to rules during validation.

Can be extended for application-specific needs.

Attributes:

context (t.Dict, optional): A dictionary containing contextual data. Defaults to {}.

class pyveritas.rules.StringChoicesRule(field: str, choices: List[str])

Bases: StringRule

Checks if a string is one of a specified set of choices.

The string must be present in the choices list for the StringChoicesRule to be valid.

error_message(data: Dict, context: RuleContext = None) str

Returns an error message if the string is not one of the specified choices.

Args:

data (t.Dict): The data that failed validation. context (RuleContext, optional): Contextual information for the rule. Defaults to None.

Returns:

str: An error message describing the validation failure.

is_valid(data: Dict, context: RuleContext = None) bool

Checks if the string is one of the specified choices.

Args:

data (t.Dict): The data to validate. context (RuleContext, optional): Contextual information for the rule. Defaults to None.

Returns:

bool: True if the string is one of the specified choices, False otherwise.

class pyveritas.rules.StringLengthRule(field: str, min_length: int = None, max_length: int = None)

Bases: StringRule

Checks if a string’s length falls within a specified range.

The string length must be greater than or equal to min_length and less than or equal to max_length (inclusive).

error_message(data: Dict, context: RuleContext = None) str

Returns an error message if the string’s length is not within the specified range.

Args:

data (t.Dict): The data that failed validation. context (RuleContext, optional): Contextual information for the rule. Defaults to None.

Returns:

str: An error message describing the validation failure.

is_valid(data: Dict, context: RuleContext = None) bool

Checks if the string’s length is within the specified range.

Args:

data (t.Dict): The data to validate. context (RuleContext, optional): Contextual information for the rule. Defaults to None.

Returns:

bool: True if the string’s length is within the specified range, False otherwise.

class pyveritas.rules.StringRegexRule(field: str, regex: str)

Bases: StringRule

Checks if a string matches a specified regular expression.

The string must match the regular expression for the StringRegexRule to be valid.

error_message(data: Dict, context: RuleContext = None) str

Returns an error message if the string does not match the regular expression.

Args:

data (t.Dict): The data that failed validation. context (RuleContext, optional): Contextual information for the rule. Defaults to None.

Returns:

str: An error message describing the validation failure.

is_valid(data: Dict, context: RuleContext = None) bool

Checks if the string matches the regular expression.

Args:

data (t.Dict): The data to validate. context (RuleContext, optional): Contextual information for the rule. Defaults to None.

Returns:

bool: True if the string matches the regular expression, False otherwise.

class pyveritas.rules.StringRule(field: str)

Bases: Rule

Base class for string-based rules.

Provides a helper method for retrieving the string value from the data.

class pyveritas.rules.TypeRule(field: str, expected_type: type)

Bases: Rule

Checks if a field is of a specific type.

The field must be of the specified type for the TypeRule to be valid.

error_message(data: Dict, context: RuleContext = None) str

Returns an error message if the field is not of the specified type.

Args:

data (t.Dict): The data that failed validation. context (RuleContext, optional): Contextual information for the rule. Defaults to None.

Returns:

str: An error message describing the validation failure.

is_valid(data: Dict, context: RuleContext = None) bool

Checks if the field is of the specified type.

Args:

data (t.Dict): The data to validate. context (RuleContext, optional): Contextual information for the rule. Defaults to None.

Returns:

bool: True if the field is of the specified type, False otherwise.

pyveritas.validator

class pyveritas.validator.Validator(contract: DataContract)

Bases: object

A simple validator class that validates data against a DataContract.

The Validator takes a DataContract as input and provides methods for validating data against that contract.

is_valid(data: Dict, context: RuleContext = None) bool

Checks if the given data is valid according to the contract.

Args:

data (t.Dict): The data to validate. context (RuleContext, optional): A RuleContext object providing additional

context for the validation. Defaults to None.

Returns:

bool: True if the data is valid, False otherwise.

validate(data: Dict, context: RuleContext = None) List[str]

Validates the given data against the contract.

Args:

data (t.Dict): The data to validate. context (RuleContext, optional): A RuleContext object providing additional

context for the validation. Defaults to None.

Returns:

t.List[str]: A list of error messages. If the list is empty, the data is valid.

pyveritas.runner

class pyveritas.runner.TestRunner(name: str)

Bases: object

A test runner for DataContracts.

This class is responsible for running test cases and validating data against data contracts.

add(test_case: Dict)

Adds a test case to the suite.

Args:
test_case (t.Dict): A dictionary containing test case details

(description, contract, data, expected_errors)

run()

Runs all test cases in the suite.

For each test case, it dynamically loads the contract class, instantiates it, validates the data against the contract, and prints the results.

summary()

Prints a summary of test results.

This method has been removed and is not used for the “base.py” file has been removed