Jentic OpenAPI SpecLynx Validator Backend
Project description
jentic-openapi-validator-speclynx
A SpecLynx ApiDOM validator backend for the Jentic OpenAPI Tools ecosystem. This package provides OpenAPI document validation using SpecLynx ApiDOM with comprehensive error reporting and flexible configuration options.
Features
- Multiple input formats: Validate OpenAPI documents from file URIs or Python dictionaries
- Custom plugins: Use built-in plugins or provide your own validation plugins
- Configurable timeouts: Control execution time limits for different use cases
- Rich diagnostics: Detailed validation results with line/column information
- Type-safe API: Full typing support with Literal types and comprehensive docstrings
Installation
pip install jentic-openapi-validator-speclynx
Prerequisites:
- Node.js and npm (for SpecLynx CLI)
- Python 3.11+
The SpecLynx dependencies will be automatically installed via npm on first use.
Quick Start
Basic Usage
from jentic.apitools.openapi.validator.backends.speclynx import SpeclynxValidatorBackend
# Create validator with defaults
validator = SpeclynxValidatorBackend()
# Validate from file URI
result = validator.validate("file:///path/to/openapi.yaml")
print(f"Valid: {result.valid}")
# Check for validation issues
if not result.valid:
for diagnostic in result.diagnostics:
print(f"Error: {diagnostic.message}")
Validate Dictionary Documents
# Validate from dictionary
openapi_doc = {
"openapi": "3.0.0",
"info": {"title": "My API", "version": "1.0.0"},
"paths": {}
}
result = validator.validate(openapi_doc)
print(f"Document is valid: {result.valid}")
Configuration Options
Custom Plugins Directory
# Use additional plugins directory (merged with built-in plugins)
validator = SpeclynxValidatorBackend(plugins_dir="/path/to/custom-plugins")
# The validator loads all .mjs files from both:
# 1. Built-in plugins directory (always loaded)
# 2. Custom plugins directory (merged with built-in plugins)
Timeout Configuration
# Short timeout for CI/CD (10 seconds)
validator = SpeclynxValidatorBackend(timeout=10.0)
# Extended timeout for large documents (2 minutes)
validator = SpeclynxValidatorBackend(timeout=120.0)
# Combined configuration (45 seconds)
validator = SpeclynxValidatorBackend(
plugins_dir="/path/to/custom-plugins",
timeout=45.0
)
Custom SpecLynx Path
By default, the validator uses the bundled SpecLynx CLI via npx. You can override this to use a custom installation:
# Use custom speclynx installation
validator = SpeclynxValidatorBackend(speclynx_path="/usr/local/bin/speclynx")
# Install additional npm packages for custom plugins that have dependencies
# The -p flag installs packages before running the CLI
validator = SpeclynxValidatorBackend(
speclynx_path="npx --yes -p lodash -p moment jentic-openapi-validator-speclynx-0.1.0.tgz"
)
Path Security
Use allowed_base_dir to restrict file access when processing untrusted input or running as a web service:
from jentic.apitools.openapi.common.path_security import (
PathTraversalError,
InvalidExtensionError,
)
# Restrict file access to /var/app/documents directory
validator = SpeclynxValidatorBackend(
allowed_base_dir="/var/app/documents"
)
# Valid paths within allowed directory work normally
result = validator.validate("/var/app/documents/specs/openapi.yaml")
# Path traversal attempts are blocked
try:
result = validator.validate("/var/app/documents/../../etc/passwd")
except PathTraversalError as e:
print(f"Security violation: {e}")
# Invalid file extensions are rejected
try:
result = validator.validate("/var/app/documents/malicious.exe")
except InvalidExtensionError as e:
print(f"Invalid file type: {e}")
# HTTP(S) URLs bypass path validation (as expected)
result = validator.validate("https://example.com/openapi.yaml")
# Combined security configuration for web services
validator = SpeclynxValidatorBackend(
allowed_base_dir="/var/app/uploads",
plugins_dir="/var/app/config/custom-plugins",
timeout=600.0
)
Security Benefits:
- Prevents path traversal attacks (
../../etc/passwd) - Restricts access to allowed directories only (when
allowed_base_diris set) - Validates file extensions (
.yaml,.yml,.json) - always enforced, even whenallowed_base_dir=None - Checks symlinks don't escape boundaries (when
allowed_base_diris set)
Note: File extension validation (.yaml, .yml, .json) is always performed for filesystem paths, regardless of
whether allowed_base_dir is set. When allowed_base_dir=None, only the base directory containment check is skipped.
Advanced Usage
Error Handling
from jentic.apitools.openapi.common.subproc import SubprocessExecutionError
try:
result = validator.validate("file:///path/to/openapi.yaml")
if result.valid:
print("Document is valid")
else:
print("Validation failed:")
for diagnostic in result.diagnostics:
severity = diagnostic.severity.name
line = diagnostic.range.start.line + 1
print(f" {severity}: {diagnostic.message} (line {line})")
except SubprocessExecutionError as e:
print(f"SpecLynx execution failed: {e}")
except TypeError as e:
print(f"Invalid document type: {e}")
Supported Document Formats
# Check what formats the validator supports
formats = validator.accepts()
print(formats) # ['uri', 'dict']
# Validate different input types
if "uri" in validator.accepts():
result = validator.validate("file:///path/to/spec.yaml")
if "dict" in validator.accepts():
result = validator.validate({"openapi": "3.0.0", ...})
Custom Plugins
Create custom validation plugins as ES modules (.mjs files). Plugins use the ApiDOM visitor pattern:
// custom-plugin.mjs
import {toValue} from '@speclynx/apidom-core';
import {DiagnosticSeverity} from 'vscode-languageserver-types';
export default ({diagnostics}) => () => ({
pre() {
},
visitor: {
InfoElement(path) {
const info = path.node;
const version = info.get('version');
if (version && typeof toValue(version) !== 'string') {
diagnostics.push({
severity: DiagnosticSeverity.Error,
message: 'info.version must be a string',
code: 'invalid-info-version-type',
range: {
start: {line: 0, character: 0},
end: {line: 0, character: 0}
},
data: {path: ['info', 'version']}
});
}
}
},
post() {
},
});
Use it with the validator:
validator = SpeclynxValidatorBackend(plugins_dir="./custom-plugins")
result = validator.validate("file:///path/to/openapi.yaml")
Note: Custom plugins are merged with built-in plugins. Both the built-in plugins and your custom plugins will run during validation.
Testing
Integration Tests
The integration tests require Node.js to be available. They will be automatically skipped if Node.js is not installed.
Run the integration test:
uv run --package jentic-openapi-validator-speclynx pytest packages/jentic-openapi-validator-speclynx -v
API Reference
SpeclynxValidatorBackend
class SpeclynxValidatorBackend(BaseValidatorBackend):
def __init__(
self,
speclynx_path: str = "npx --yes jentic-openapi-validator-speclynx-0.1.0.tgz",
timeout: float = 600.0,
allowed_base_dir: str | Path | None = None,
plugins_dir: str | Path | None = None,
) -> None
Parameters:
speclynx_path: Path to the SpecLynx CLI executable. Default uses bundled npm tarball via npx. Can be a custom path like/usr/local/bin/speclynxor an npx command with additional packages (optional)timeout: Maximum execution time in seconds (default: 600.0)allowed_base_dir: Optional base directory for path security validation. When set, all document paths are validated to be within this directory, providing defense against path traversal attacks. WhenNone(default), only file extension validation is performed (no base directory containment check). Recommended for web services or untrusted input (optional)plugins_dir: Optional directory containing additional validation plugins (.mjsfiles). When specified, custom plugins are merged with built-in plugins (both are loaded). IfNone(default), only built-in plugins are used (optional)
Methods:
accepts() -> list[Literal["uri", "dict"]]: Returns supported document format identifiersvalidate(document: str | dict, *, base_url: str | None = None, target: str | None = None) -> ValidationResult: Validates an OpenAPI document
Exceptions:
RuntimeError: SpecLynx execution failsSubprocessExecutionError: SpecLynx times out or fails to startTypeError: Unsupported document typePathTraversalError: Document path attempts to escape allowed_base_dir (only whenallowed_base_diris set)InvalidExtensionError: Document path has disallowed file extension (always checked for filesystem paths)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file jentic_openapi_validator_speclynx-1.0.0a36.tar.gz.
File metadata
- Download URL: jentic_openapi_validator_speclynx-1.0.0a36.tar.gz
- Upload date:
- Size: 16.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
12544c43b4862d31fab1c2ab01b0f6926109e71d5367803a45845e6e1a28c8f0
|
|
| MD5 |
f71d29e9acb350d45f54da0fa35e6001
|
|
| BLAKE2b-256 |
64f773e7887e099c3d850aa053a3518c4cd1806b6f9d23abd418f171320b557e
|
Provenance
The following attestation bundles were made for jentic_openapi_validator_speclynx-1.0.0a36.tar.gz:
Publisher:
release.yml on jentic/jentic-openapi-tools
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
jentic_openapi_validator_speclynx-1.0.0a36.tar.gz -
Subject digest:
12544c43b4862d31fab1c2ab01b0f6926109e71d5367803a45845e6e1a28c8f0 - Sigstore transparency entry: 947455855
- Sigstore integration time:
-
Permalink:
jentic/jentic-openapi-tools@c78b9649b07f09143dd4a7b9ba020ead7d03a3be -
Branch / Tag:
refs/heads/main - Owner: https://github.com/jentic
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@c78b9649b07f09143dd4a7b9ba020ead7d03a3be -
Trigger Event:
workflow_dispatch
-
Statement type:
File details
Details for the file jentic_openapi_validator_speclynx-1.0.0a36-py3-none-any.whl.
File metadata
- Download URL: jentic_openapi_validator_speclynx-1.0.0a36-py3-none-any.whl
- Upload date:
- Size: 19.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2a57a03be2ff5200dddda27f4dd024ac0aefe171545e1f822909d18412c4cdf8
|
|
| MD5 |
5462b64d67f726e68be11e664a332871
|
|
| BLAKE2b-256 |
c54ea7e52bc83df638d8f46f3c63db7b710ba9958e4a6c44af6a5fa7b093dfd7
|
Provenance
The following attestation bundles were made for jentic_openapi_validator_speclynx-1.0.0a36-py3-none-any.whl:
Publisher:
release.yml on jentic/jentic-openapi-tools
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
jentic_openapi_validator_speclynx-1.0.0a36-py3-none-any.whl -
Subject digest:
2a57a03be2ff5200dddda27f4dd024ac0aefe171545e1f822909d18412c4cdf8 - Sigstore transparency entry: 947456012
- Sigstore integration time:
-
Permalink:
jentic/jentic-openapi-tools@c78b9649b07f09143dd4a7b9ba020ead7d03a3be -
Branch / Tag:
refs/heads/main - Owner: https://github.com/jentic
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@c78b9649b07f09143dd4a7b9ba020ead7d03a3be -
Trigger Event:
workflow_dispatch
-
Statement type: