Schema Management
Relevant source files
The following files were used as context for generating this wiki page:
Schema Management in the FLEXT-LDIF library provides a comprehensive framework for parsing, extracting, building, and validating LDAP directory schemas. It ensures compliance with RFC 4512 standards while providing specialized support for server-specific extensions (e.g., OID, OUD). The system handles complex LDAP elements including attributeTypes and objectClasses, maintaining high fidelity during round-trip conversions through detailed metadata preservation.
The module facilitates the extraction of schema definitions from raw LDIF lines, the construction of new schema entries via builders, and the validation of directory entries against defined schemas to ensure data integrity.
Sources: src/flext_ldif/_utilities/schema_extract.py:1-10, examples/05_schema_operations.py:1-15, src/flext_ldif/_constants/base.py:1-10
Schema Extraction and Parsing
The extraction layer is responsible for decomposing RFC 4512 schema strings into structured domain models. It utilizes specialized utility classes to isolate specific fields such as OIDs, names, descriptions, and operational flags.
Extraction Logic
The system uses FlextLdifUtilitiesSchemaExtract to pull specific metadata from attribute and object class definitions. This includes identifying boolean flags like SINGLE-VALUE and NO-USER-MODIFICATION, as well as complex relationships like superior classes (SUP) and required/optional attributes (MUST/MAY).
The flow above illustrates how a raw schema definition is systematically parsed into individual components for model validation.
Sources: src/flext_ldif/_utilities/schema_extract.py:14-80
Key Extraction Methods
| Component | Function | Description |
|---|---|---|
| Attribute Flags | extract_attribute_flags | Extracts SINGLE-VALUE and NO-USER-MODIFICATION statuses. |
| Matching Rules | extract_attribute_matching_rules | Identifies EQUALITY, SUBSTR, and ORDERING rules. |
| Syntax | extract_attribute_syntax | Extracts the Syntax OID and optional character length limits. |
| Object Class Kind | extract_objectclass_kind | Determines if a class is ABSTRACT, STRUCTURAL, or AUXILIARY. |
| Dependencies | extract_objectclass_must_may | Parses lists of mandatory and optional attributes. |
Sources: src/flext_ldif/_utilities/schema_extract.py:18-70
Schema Building and Formatting
Schema building involves the programmatic generation of RFC 4512 compliant definition strings. The library supports a "Metadata Restoration" pattern that allows the system to recreate the exact formatting (quoting, spacing, field order) of the original source during round-trips.
The Building Pipeline
The FlextLdifUtilitiesSchemaBuild class coordinates the assembly of schema elements. It can either generate a clean standard format or leverage FlextLdifUtilitiesSchemaFormat to restore original formatting details stored in the entry's metadata.
This sequence ensures that schema definitions maintain their original aesthetic and functional extensions when moved between systems.
Sources: src/flext_ldif/_utilities/schema_build.py:15-60, src/flext_ldif/_utilities/schema_format.py:15-50
Metadata Preservation
The SchemaFormatDetails model stores specific stylistic information:
- Original String: The complete original definition for perfect round-trips.
- Quoting Style: Preservation of single vs double quotes.
- Field Order: The specific sequence of keywords (e.g.,
NAMEbeforeDESC). - Extensions: Preservation of
X-ORIGIN,X-ORDERED, and other non-standardX-attributes.
Sources: src/flext_ldif/_models/domain_metadata.py:126-155, src/flext_ldif/_utilities/schema_format.py:92-130
Schema-Driven Validation
Validation ensures that LDIF entries adhere to the rules defined in the schema. This includes checking for required attributes, verifying single-value constraints, and ensuring attribute syntax compliance.
Validation Workflow
The validation process involves comparing active Ldif.Entry objects against a SchemaReport. Errors are categorized into RFC violations, attribute errors, and schema-specific mismatches.
# Example of a validation pipeline
def railway_schema_pipeline() -> p.Result[t.JsonMapping]:
api = ldif()
# 1. Build Schema
# 2. Validate Schema Entries
# 3. Validate Test Entries against Schema
validated_pipeline = (
intelligent_schema_building()
.flat_map(lambda schema_entries: api.validate_entries(schema_entries))
.flat_map(lambda report: api.validate_entries(test_entries))
)
Sources: examples/05_schema_operations.py:270-320
Validation Metrics
Validation results are tracked in the ValidationMetadata model, providing a detailed report of the directory's health:
| Field | Description |
|---|---|
rfc_violations | Hard failures against RFC 2849/4512 standards. |
server_specific_violations | Failures against proprietary server rules (e.g., OID specific constraints). |
compliance_rate | Ratio of valid entries to total entries. |
error_analysis | Categorization of errors into schema, attribute, or other. |
Sources: src/flext_ldif/_models/domain_metadata.py:43-70, examples/05_schema_operations.py:130-150
Constants and Regex Authorities
The Schema Management system relies on a centralized set of regex patterns to ensure consistent parsing across the library. These are defined in the FlextLdifConstantsBase class.
- SCHEMA_NAME: Matches
NAMEfield with optional parentheses and single quotes. - SCHEMA_SYNTAX_LENGTH: Captures Numeric OID and optional
{length}constraints. - SCHEMA_OBJECTCLASS_MUST/MAY: Handles both single attribute names and
$-separated lists inside parentheses. - NUMERIC_OID: Validates the standard dot-notation format for OIDs.
Sources: src/flext_ldif/_constants/base.py:128-160, src/flext_ldif/_constants/base.py:170-220
Summary
Schema Management in FLEXT-LDIF provides a robust bridge between raw LDAP data and structured Python models. By combining strict RFC-compliant parsing with a sophisticated metadata preservation system, it allows for complex migrations between different LDAP server types (like OID to OUD) without losing formatting or server-specific extensions. The integrated validation pipeline further ensures that data remains compliant throughout its lifecycle.
Sources: examples/05_schema_operations.py:155-200, src/flext_ldif/_utilities/schema_build.py:120-140