Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ repos:
- id: check-json
- id: check-executables-have-shebangs
- id: pretty-format-json
args: ['--autofix']
- id: check-merge-conflict
- id: check-symlinks
- id: check-toml
Expand Down
10 changes: 10 additions & 0 deletions .pre-commit-config.yaml.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Example pre-commit configuration for generate_parameter_library projects
# Copy this to .pre-commit-config.yaml in your project root

repos:
# Validate parameter YAML files against JSON schema
- repo: https://github.com/Greenroom-Robotics/generate_parameter_library
rev: main
hooks:
- id: validate-parameter-schema
files: .*parameters\.yaml$
8 changes: 8 additions & 0 deletions .pre-commit-hooks.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
- id: validate-parameter-schema
name: Validate ROS2 PickNikRobotics Generate Parameter Library Schema
description: Validates generate_parameter_library YAML files against JSON schema
entry: python -m validate_schema
language: python
types: [yaml]
files: .*parameters\.yaml$
additional_dependencies: ['./schema_validator']
19 changes: 19 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[build-system]
requires = ["setuptools>=45", "wheel"]
build-backend = "setuptools.build_meta"

[project]
name = "generate-parameter-library-hooks"
version = "0.1.0"
description = "Pre-commit hooks for generate_parameter_library"
authors = [
{name = "Greenroom Robotics"}
]
requires-python = ">=3.6"
dependencies = [
"pyyaml>=5.0",
"jsonschema>=3.0",
]

[tool.setuptools]
packages = []
2 changes: 2 additions & 0 deletions schema_validator/MANIFEST.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
include *.json
include README.md
120 changes: 120 additions & 0 deletions schema_validator/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
# Parameter Schema Validator

A pre-commit hook to validate `generate_parameter_library` YAML files against the JSON schema.

## Installation

### As a pre-commit hook

Add this to your `.pre-commit-config.yaml`:

```yaml
repos:
- repo: https://github.com/Greenroom-Robotics/generate_parameter_library
rev: main # Use a specific tag/version in production
hooks:
- id: validate-parameter-schema
```

Then install the hook:

```bash
pre-commit install
```

### Standalone usage

Install dependencies:

```bash
pip install pyyaml jsonschema
```

Run the validator:

```bash
python schema_validator/validate_schema.py path/to/your/parameters.yaml
```

Or with a custom schema:

```bash
python schema_validator/validate_schema.py --schema path/to/schema.json path/to/parameters.yaml
```

To also validate `additional_constraints` JSON content against the UI schema:

```bash
python schema_validator/validate_schema.py --ui-schema path/to/parameters.yaml
```

## What it validates

The validator checks:

1. **YAML syntax**: Ensures the file is valid YAML
2. **Single root element**: The YAML must have exactly one root element (the namespace)
3. **Required fields**: Each parameter must have a `type` field
4. **Valid types**: Type must be one of the supported parameter types
5. **Valid field names**: Only recognized fields are allowed (type, default_value, description, read_only, validation, additional_constraints)
6. **Nested structure**: Validates nested parameter groups and mapped parameters
7. **Validation syntax**: Ensures validation rules follow the correct structure
8. **Additional constraints (optional)**: When `--ui-schema` is provided, validates that the `additional_constraints` field contains valid JSON that conforms to the UI schema

## Example output

Successful validation:
```
✓ config/parameters.yaml is valid
```

Failed validation:
```
❌ Validation failed for config/parameters.yaml:
Path: my_controller -> pid -> p
Error: 'type' is a required property
Value: {'default_value': 1.0, 'description': 'Proportional gain'}
```

## Alpha UI Schema for additional_constraints

The `alpha_ui_schema.json` defines the expected format for JSON content embedded in the `additional_constraints` field. This is useful when `additional_constraints` contains UI metadata for form generation.

### Format

The `additional_constraints` field should contain a JSON-encoded string like:

```yaml
my_parameter:
type: double
default_value: 100.0
additional_constraints: '{"type": "number", "units": "hertz"}'
```

The JSON content can include:
- `type` (required): JSON Schema type (number, string, integer, boolean, array, object)
- `units` (optional): Physical units (seconds, hertz, metersPerSecond, meters, radians, degrees, etc.)
- Any React JSON Schema Form properties: `minimum`, `maximum`, `enum`, `pattern`, `title`, etc.

### Validation

To validate `additional_constraints` content:

```bash
python schema_validator/validate_schema.py --ui-schema path/to/parameters.yaml
```

This will:
1. Parse the JSON string in each `additional_constraints` field
2. Validate it conforms to the UI schema structure
3. Report any malformed JSON or schema violations

## Limitations

Note that this validator checks the **structure** of the YAML file, but does not perform:

- Type checking of default values against declared types (use generate_parameter_library for this)
- Custom validator verification
- Deep semantic validation

For complete validation, always run `generate_parameter_library` during your build process.
7 changes: 7 additions & 0 deletions schema_validator/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/usr/bin/env python3
"""Allow running validate_schema as a module: python -m validate_schema"""
import sys
from validate_schema import main

if __name__ == '__main__':
sys.exit(main())
Loading
Loading