コンテンツにスキップ

pymaiml.validation

validation

pymaiml.validation

Validates a MaiML (JIS K 0200 / MaiML-Schema-1_0) file against:

  1. The official XSD schema (well-formedness, element/attribute cardinality and order, id/IDREF resolvability, xsi:type <-> content model consistency, UUID string format, etc).
  2. Supplementary business rules from the MaiML AI Common Specification that XSD alone cannot express (ref-target element-type checks, the lifecycle "complete" event bookkeeping, XES namespace exact-URI binding, the "layer-1 elements must never be concealed by EncryptedData" rule, chain/parent structure, file extension/encoding conventions).

This is a library-friendly port of the same checks used by the maiml-schema-validator Claude skill's validate_maiml.py, restructured around a ValidationResult object instead of argparse + stdout, so any Python code (a CI step, another pymaiml module, a notebook) can call it directly:

from pymaiml.validation import validate
result = validate("sample.maiml")
if not result.ok:
    for finding in result.errors:
        print(finding)

The official schema files (MaiML-Schema-1_0) are bundled under pymaiml/schema/MaiML-Schema-1_0/ so validation works offline with zero configuration; pass schema_dir= to point at a different schema version.

Known gap vs. the full JIS K 0200 specification (documented, not silently skipped): XML digital signature cryptographic verification, full Petri-net reachability simulation of the material->operation->result flow (the "complete" event check here is a simplified file-wide presence check, not a per-instruction trace), cross-file UUID consistency, and thesaurus (Annex A/B) vocabulary conformance for key= values. See the skill's reference/maiml_validation_rules.md for the full rationale if/when those need porting too.

ValidationResult dataclass

ValidationResult(path: str, findings: List[Finding] = list())

ok property

ok: bool

True iff there are no MUST-level (error) findings.

validate

validate(path: Union[str, Path], schema_dir: Optional[Union[str, Path]] = None) -> ValidationResult

Validate a .maiml / .maiml.zip / .mai file.

schema_dir defaults to the MaiML-Schema-1_0 bundled with pymaiml (pymaiml/schema/MaiML-Schema-1_0/). Pass a different directory to validate against another schema version.

ソースコード位置: pymaiml/validation.py
def validate(path: Union[str, Path], schema_dir: Optional[Union[str, Path]] = None) -> ValidationResult:
    """
    Validate a .maiml / .maiml.zip / .mai file.

    schema_dir defaults to the MaiML-Schema-1_0 bundled with pymaiml
    (pymaiml/schema/MaiML-Schema-1_0/). Pass a different directory to
    validate against another schema version.
    """
    path = Path(path)
    schema_dir = Path(schema_dir) if schema_dir is not None else _BUNDLED_SCHEMA_DIR
    findings: List[Finding] = []
    _check_file_extension(path, findings)

    try:
        xml_bytes, _display_name = _extract_xml_bytes(path)
    except Exception as e:
        findings.append(Finding("error", "IO-01", f"ファイルを読み込めません: {e}"))
        return ValidationResult(path=str(path), findings=findings)

    _check_encoding_declaration(xml_bytes, findings)
    schema = _load_schema(schema_dir)
    tree = _xsd_validate(xml_bytes, schema, findings)
    if tree is not None:
        _run_supplementary_checks(tree, findings)

    return ValidationResult(path=str(path), findings=findings)