Skip to main content

PyPI - Python Version PyPI - Status PyPI - License

validify is a rule-based validation module for assessing the structure of an xml tree, written in Python and built on top of the lxml library. It currently covers a subset of the XML Schema 1.1 Definition.

Requirements

Installation

The validify module can be found on PyPI. It can be installed by using pip:

pip install validify

Dependencies will be automatically fetched by pip.

Basic usage

import validify
validation_result = validify.validate(input_file="validify/test.xml", input_elementtree=None, xmlns_def=None, validation_rules=None, message_lang=None, log_to_console=True, log_debug=False)

Parameters

  • input_file (default: None): path to xml file which should be validated.
  • input_elementtree (default: None): an etree.ElementTree object (parameter 'input_elementtree') can also be passed, instead of an input file string.
  • xmlns_def (default: None): a namespace definition can be supplied as a python dictionary object ({None: "default_namespace", "namespace_prefix": "another_namespace"}}).
  • validation_rules (default: None): a python dictionary object containing the validation rules (see "Defining validation rules" below for an example.). An example rules dictionary is used if no value is supplied here.
  • message_lang (default: de): language for validation message strings. Supported values are en and de.
  • log_to_console (default: True): if True, validation and status messages are logged to console. If false, validation messages are only added to the results dict returned by validfy.
  • log_debug (default: False): if True, debug status messages are logged to console.

One of the paramaters input_file or input_elementtree should be passed for the library to produce validation results.

Defining validation rules

Validation rules are defined in a dictionary object (JSON-like structure):

validation_rules = {}

validation_rules["element"] = []
ruleset = {}

ruleset["element_content_optional"] = False
ruleset["element_children_optional"] = False
ruleset["optional_attributes"] = ["valid_optional_attribute_01", "valid_optional_attribute_02"]
ruleset["obligatory_attributes"] = ["obligatory_attribute_01", "obligatory_attribute_02"]
ruleset["optional_subelements"] = ["optional_subelement_01", "optional_subelement_02"]
ruleset["obligatory_subelements"] = ["obligatory_subelement_01", "obligatory_subelement_02"]
ruleset["max_occurence"] = 2
ruleset["text_character_content_allowed"] = True
ruleset["tail_character_content_allowed"] = False

ruleset["allowed_values"] = ["valid_value_01", "valid_value_02"]
ruleset["allowed_patterns"] = ["^test-\d{4}$", "^test-\d{3}$"]
ruleset["allowed_datatypes"] = []
ruleset["attribute_def"] = []
ruleset["attribute_def"].append({"attribute_name": "valid_optional_attribute_01", "allowed_values": ["valid_value_01", "valid_value_02"],
                                 "allowed_patterns": ["^test-\d{4}$", "^test-\d{3}$"]})
ruleset["attribute_def"].append(
    {"attribute_name": "obligatory_attribute_01", "allowed_values": ["valid_value_01", "valid_value_02"],
     "allowed_patterns": []})


ruleset["rule_conditions"] = []
ruleset["rule_conditions"].append(
    {"text_values": ["valid_text_value"],
     "attribute_def": [{"attribute_name": "valid_attribute_name", "allowed_values": ["valid_value"]},
                       {"attribute_name": "another_valid_attribute_name", "allowed_values": ["valid_value"]}],
     "reference_elements": [{"element_name": "reference_element", "attribute_def": [{"attribute_name": "reference_test", "allowed_values": ["valid_value"]}],"preceding_elements": 1}]})


validation_rules["element"].append(ruleset)

Each element can be provided with one or more rulesets. The rule_conditions definiton can be used when the ruleset should only be applied if the validated element contains the defined attribute(s) and attribute value(s). Besides the validated element, a reference element can also be defined and checked for attribute values. Currently, it must be a parent element of the validated element (parent level defined by preceding_elements).

Validation output

validify.validate returns a list containing the validation messages as dictionaries:

[{'message_id': '0001', 'message_text ': 'Element example_element does not contain any subelements, although one or more subelements are expected.', 'element_name': '{namespace}example_element', 'local_name': 'example_element', 'element_sourceline': '23'}]

XML Schema feature coverage

For now, a small subset of the XML Schema features is provided:

  • Test if a ruleset applies by checking a reference element's text and attribute values
  • Define if element childen and content are optional
  • Define optional and obligatory attributes
  • Define optional and obligatory subelements
  • Define maximum occurence of an element
  • Define if character content is allowed
  • Define an element's allowed values (~ xs:enumeration)
  • Define an element's allowed patterns (~ xs:pattern)
  • Define an attribute's allowed values and patterns

This module is currently used for validating data deliveries in the EAD XML application profile, which are processed for ingesting in the metadata portals Deutsche Digitale Bibliothek and Archivportal-D. Therefore, supported features currently are nowhere near those provided by the XML Schema standard. Feature support is supposed to be gradually expanded, however.

The following features are planned for a future release:

  • checking max/min text and attribute values
  • validating string length
  • support for pre-defined data types (i.e. xs:ID, xs:NMTOKEN)
  • rule conditions: direct support for XPath and lxml's itersiblings and iterancestors methods.

Development status

This package is in an early development stage. It should already work reliably for intended use cases, but documentation and stability of API are still lacking.

Release files for validify 0.0.15

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

Source distribution (sdist)

Source distribution for validify 0.0.15
File Size Uploaded
validify-0.0.15.tar.gz 12.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for validify 0.0.15
File Interpreter ABI Platform
validify-0.0.15-py3-none-any.whl Python 3 none any Details

Total release size: 26.8 kB

Release files / validify-0.0.15.tar.gz

Download URL validify-0.0.15.tar.gz
Size 12.9 kB
Tags Source
SHA-256 checksum
How to use checksums
97a9d8d63209a41de2be2c0c8faa6c3c7974783b991dd50d304c3f7221f2b489
BLAKE2b-256 checksum
How to use checksums
48784f009a16d9d894b0149797f27960471f114e038c1692aa735036eea1e1ca
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.41.1 CPython/3.7.6

Release files / validify-0.0.15-py3-none-any.whl

Download URL validify-0.0.15-py3-none-any.whl
Size 13.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
96300c3b41e97f7e810c97ecaa04861608842a5288ec680377daea3ecbc94058
BLAKE2b-256 checksum
How to use checksums
fe8dba6ad6436bad0f020eba5f2f83e78faff518645cd9a9791d88e031730401
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.41.1 CPython/3.7.6

Release history Release notifications | RSS feed

This release

0.0.15 This release

2 release files

0.0.14

2 release files

0.0.13

2 release files

0.0.9

2 release files

0.0.8

2 release files

0.0.7

2 release files

0.0.6

2 release files

0.0.5

2 release files

0.0.4

2 release files

0.0.3

2 release files

0.0.2

2 release files

0.0.1

2 release files

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