Skip to main content

Validator with feature to change unexpected values.

Features

  • recursive validation dictionary values

  • expanding the list of validators with custom validators

  • ability to assign handler functions globally or on single validator to change unexpected values

  • ability to throw exceptions on first error or after complete validation process

  • ability to return unexpected results and info about them

Installation

As usually use pip:

pip install data-validator

Usage examples

Basic dictionary validation:

# user to validate
user = {
    'first_name': 'Very long name to validate',
    'last_name': 'Smith',
    'email': 'smith@gmail.com',
    'second_email': 'bad email',
    'address': {'state': 'Texas', 'city': 'Dallas'},
    'career': [{'name': 'google', 'from': 2012, 'until': 'now'}, {'name': 'facebook', 'from': -1,
                'until': 44444444444444}]
}

# We want to validate:
# user['first_name'] and user['last_name'] length must be more then 2 and less then 10
# user['email'] and user['second_email'] must match regex pattern r"^[\w\.\+\-]+\@[\w]+\.[a-z]{2,3}$"
# user['address']['state'] may be Texas, Alabama or Alaska and it case sensitive
# user['address']['city'] may be New York, Los Angeles or Washington and it not case sensitive
# user['career'][*]['from'] and user['career'][*]['until'] must be int16. P.s. * is list index

# And if we have unexpected values we need to change them with handler function - lambda x: -1
# and if user['career']['until'] is unexpected we need to change this with handler function - lambda x: -2

# user validation rules
rules = {
    'first_name': [validators.StringLength([2, 10])],
    'last_name': [validators.StringLength([2, 10])],
    'email': [validators.Regex(r"^[\w\.\+\-]+\@[\w]+\.[a-z]{2,3}$")],
    'second_email': [validators.Regex(r"^[\w\.\+\-]+\@[\w]+\.[a-z]{2,3}$")],
    'address.state': [validators.Variants(['Texas', 'Alabama', 'Alaska'])],
    'address.city': [validators.Variants(['New York', 'Los Angeles','Washington'], case_sensitive=False)],
    'career.from': [validators.IntNumberType(numpy.int16)],
    'career.until': [validators.IntNumberType(numpy.int16, handler=lambda x: -2)]
}

# validate user
validator = Validator(rules, common_handler=lambda x: -1)
validator_result = validator.validate(user)
if not validator_result:
    [print(unexpected) for unexpected in validator.unexpected_values]

# result will be
# path: career.0.until, value: now, validator: <data_validator.validators.types.IntNumberType object at 0x7ffada681af8>, expected: <class 'numpy.int16'>, unexpected: <class 'str'>
# path: career.1.until, value: 2015555555, validator: <data_validator.validators.types.IntNumberType object at 0x7ffada681af8>, expected: <class 'numpy.int16'>, unexpected: <class 'numpy.int32'>
# path: address.city, value: Dallas, validator: <data_validator.validators.variants.Variants object at 0x7ffada681a68>, expected: ['new york', 'los angeles'], unexpected: dallas
# path: second_email, value: bad email, validator: <data_validator.validators.regex.Regex object at 0x7ffada6819d8>, expected: ^[\w\.\+\-]+\@[\w]+\.[a-z]{2,3}$, unexpected: bad email
# path: first_name, value: Very long name to validate, validator: <data_validator.validators.length.StringLength object at 0x7ffada6c7120>, expected: [2, 10], unexpected: 26

# We can find original item in validator.original_item and changed dict in validator.processed_item:
{
    'first_name': -1,
    'last_name': 'Smith',
    'email': 'smith@gmail.com',
    'second_email': -1,
    'address': {'state': 'Texas', 'city': -1},
    'career': [{'name': 'google', 'from': -1, 'until': -2}, {'name': 'facebook', 'from': -1, 'until': -2}]
}

Metadata

Release files for data-validator 0.1.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for data-validator 0.1.2
File Size Uploaded
data-validator-0.1.2.tar.gz 6.4 kB Details

Release files / data-validator-0.1.2.tar.gz

Download URL data-validator-0.1.2.tar.gz
Size 6.4 kB
Tags Source
SHA-256 checksum
How to use checksums
22ab289985335bba5731c05ac6a67a2b729f33860c7f5b851662cd072cd0c6fe
BLAKE2b-256 checksum
How to use checksums
80ea0751a6dad0bf9b033dbe5c95b5e04063b05e2c132127c5f6aa5c7161aff0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

0.1.2 This release

1 release file

0.1.1

1 release file

0.1.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page