Skip to main content

Library for creating a custom validator

Reason this release was yanked:

# Project Relocation. **The project has been successfully moved to a new name and address**. Now you can find it at the following link: https://github.com/m62624/flexible_inspect

Project description

PYSTVAL

codecov changelog Documentation Status CI/CD Pipeline PyPI - Downloads

Logo

The Library

Pystval is a powerful Rust library for text validation and analysis. It allows you to create custom errors with modifiers and define sub-corrections for each rule. With Pystval, you can build versatile validators that handle any text and enforce specific requirements. Customize error behavior and apply sub-corrections based on specific cases or conditions. Pystval empowers you to create accurate and adaptable validators for various text processing needs.

The difference between this library and normal use of regular expressions is that you can combine different rules with modifiers, and the results of these rules are automatically passed to subexpressions, etc.

Quick Look

Click me
from pystval import Rule, MatchRequirement, TemplateValidator, PystvalException

# Creating a validator cartridge
class ErrorInvalidFormat(PystvalException):
    message = ":: Invalid Format ::"
    rules = [
        # root rule
        Rule(r"(?i)abc.+\d+", MatchRequirement.MustBeFound).extend([
            # 1-subrule of root rule
            Rule(r"\d{3}-\d{4}-\d{2}", MatchRequirement.MustBeFound).extend([
                # 1-subrule of 1-subrule
                Rule(r"^\d{3}", MatchRequirement.MustBeFound).extend(
                    # 1-subrule of 1-subrule of 1-subrule
                    [Rule(r"[0-1][1-2][1-3]", MatchRequirement.MustBeFound)]),
                # 2-subrule of 1-subrule
                Rule(r"-", MatchRequirement.MustBeFound),
            ]),
        ])
    ]

# Creating a validator cartridge
# We get this class as an error because `MustNotBeFound` is specified, but we find in the text, at the very end of `12345`
class ErrorNumber(PystvalException):
    message = "Custom error with value : {num}"
    rules = [
        Rule(r"(?<num>\d+(?!\d|-|\s))", MatchRequirement.MustNotBeFound)
    ]


text = b"This is a complex text with aBc_122-4567-99 and def. The number is 12345"

# In the validator we load the cartridges, which will be used to check the data
validator = TemplateValidator([ErrorInvalidFormat, ErrorNumber])

# We get a list of errors based on those classes whose rules failed
list_error = validator.validate(text)

# errors
if list_error is not None:
    for error in list_error:
        try:
            raise error
        except PystvalException as e:

            # show a specific message based on the class
            print(e.report)

OUTPUT: Custom error with value : 12345

Installation

PyPi.

Installation from PyPi

pip install pystval

Github packages

Under Releases choose your platform. Unpack the archive and install

pip install pystval-version-platform.whl

Documentation

Documentation of use: docs

License

MIT License.

Project details


Download files

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

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

pystval-1.1.0-cp37-abi3-win_arm64.whl (960.9 kB view hashes)

Uploaded CPython 3.7+ Windows ARM64

pystval-1.1.0-cp37-abi3-win_amd64.whl (1.0 MB view hashes)

Uploaded CPython 3.7+ Windows x86-64

pystval-1.1.0-cp37-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.4 MB view hashes)

Uploaded CPython 3.7+ manylinux: glibc 2.17+ x86-64

pystval-1.1.0-cp37-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (2.3 MB view hashes)

Uploaded CPython 3.7+ manylinux: glibc 2.17+ ARM64

pystval-1.1.0-cp37-abi3-macosx_11_0_arm64.whl (2.5 MB view hashes)

Uploaded CPython 3.7+ macOS 11.0+ ARM64

pystval-1.1.0-cp37-abi3-macosx_10_7_x86_64.whl (2.7 MB view hashes)

Uploaded CPython 3.7+ macOS 10.7+ x86-64

Supported by

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