Skip to main content

No project description provided

Project description

sarif-pydantic

An implementation of the SARIF (Static Analysis Results Interchange Format) format using Pydantic.

Overview

This library provides Pydantic models for working with the SARIF specification (version 2.1.0). It enables Python developers to:

  • Create, validate, and manipulate SARIF data
  • Parse existing SARIF files into typed Python objects
  • Export SARIF data to JSON with proper validation

Installation

pip install sarif-pydantic

Usage

Creating a SARIF Log

from sarif_pydantic import (
    ArtifactLocation, 
    Invocation, 
    Level, 
    Location, 
    Message, 
    PhysicalLocation, 
    Region, 
    Result, 
    Run, 
    Sarif, 
    Tool, 
    ToolDriver
)

# Create a tool driver
tool_driver = ToolDriver(
    name="Example Analyzer",
    version="1.0.0",
)

# Create a tool with the driver
tool = Tool(driver=tool_driver)

# Create a physical location
physical_location = PhysicalLocation(
    artifact_location=ArtifactLocation(
        uri="src/example.py",
    ),
    region=Region(
        start_line=42,
        start_column=5,
        end_line=42,
        end_column=32,
    ),
)

# Create a result
result = Result(
    rule_id="EX001",
    level=Level.WARNING,
    message=Message(
        text="Example warning message",
    ),
    locations=[Location(
        physical_location=physical_location,
    )],
)

# Create a SARIF log
sarif_log = Sarif(
    version="2.1.0",
    runs=[Run(
        tool=tool,
        invocations=[Invocation(
            execution_successful=True,
        )],
        results=[result],
    )],
)

# Export to JSON
sarif_json = sarif_log.model_dump_json(indent=2, exclude_none=True)
print(sarif_json)

Loading a SARIF Log from JSON

import json
from sarif_pydantic import Sarif

# Load from a file
with open("example.sarif", "r") as f:
    sarif_data = json.load(f)

# Parse into a Sarif object
sarif_log = Sarif.model_validate(sarif_data)

# Access data via typed objects
for run in sarif_log.runs:
    for result in run.results or []:
        print(f"Rule: {result.rule_id}, Level: {result.level}")
        print(f"Message: {result.message.text}")

SARIF Specification

This implementation follows the SARIF 2.1.0 specification.

License

[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 Distribution

sarif_pydantic-0.5.1.tar.gz (6.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

sarif_pydantic-0.5.1-py3-none-any.whl (6.9 kB view details)

Uploaded Python 3

File details

Details for the file sarif_pydantic-0.5.1.tar.gz.

File metadata

  • Download URL: sarif_pydantic-0.5.1.tar.gz
  • Upload date:
  • Size: 6.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.1 CPython/3.13.2 Darwin/24.3.0

File hashes

Hashes for sarif_pydantic-0.5.1.tar.gz
Algorithm Hash digest
SHA256 0078d58e81ca906b9b0f3866c45127cdbd23061b38c022eb95fe0e559c7fd117
MD5 f503dbca39a52186835c9100e6d072aa
BLAKE2b-256 32e558662feb775ca046d47a46d0cb7204036db7b0078899ae6569a75e2e332c

See more details on using hashes here.

File details

Details for the file sarif_pydantic-0.5.1-py3-none-any.whl.

File metadata

  • Download URL: sarif_pydantic-0.5.1-py3-none-any.whl
  • Upload date:
  • Size: 6.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.1 CPython/3.13.2 Darwin/24.3.0

File hashes

Hashes for sarif_pydantic-0.5.1-py3-none-any.whl
Algorithm Hash digest
SHA256 29d57af8f43c68545b4a7e7a2199c402b9362dde3832392a2d781b45b3115ecd
MD5 cc837f069f782459aec7cb36626da25a
BLAKE2b-256 872934a60e21b87f1a90ed71bf63124ad3f9fc9e75ea7ec36ec0a9b329fe1169

See more details on using hashes here.

Supported by

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