Skip to content

Implement JSON Schema support for ktmidi-ci module #100

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from

Conversation

devin-ai-integration[bot]
Copy link

JSON Schema Support Implementation for ktmidi-ci

This PR implements comprehensive JSON Schema support for the ktmidi-ci module following the JSON Schema Draft 2020-12 specification.

🎯 Overview

Added a complete JSON Schema API in the dev.atsushieno.ktmidi.ci.jsonschema package that integrates seamlessly with the existing MIDI-CI property exchange system and device configuration.

🚀 Features Implemented

Core JSON Schema Classes

  • JsonSchema: Main schema representation with support for all core validation keywords
  • JsonSchemaValidator: Comprehensive validation engine with detailed error reporting
  • JsonSchemaException: Error handling following existing patterns
  • JsonSchemaIntegration: Integration utilities for MIDI-CI system

Supported Validation Keywords

  • Type validation: null, boolean, object, array, number, string, integer
  • Object validation: properties, required, additionalProperties, minProperties, maxProperties
  • Array validation: items, minItems, maxItems, uniqueItems
  • String validation: minLength, maxLength, pattern, format
  • Numeric validation: minimum, maximum, exclusiveMinimum, exclusiveMaximum
  • Value validation: enum, const
  • Metadata: title, description, default, examples

🔧 Integration Points

Existing System Integration

  • Leverages existing Json.JsonValue for schema representation and validation
  • Integrates with MidiCIDeviceConfiguration.jsonSchemaString property
  • Compatible with existing property exchange infrastructure
  • Follows established patterns from JsonParserException and @Serializable classes

Usage Examples

// Parse schema from JSON string
val schema = JsonSchema.parse("""{
    "type": "object",
    "properties": {
        "name": {"type": "string"},
        "age": {"type": "number", "minimum": 0}
    },
    "required": ["name"]
}""")

// Validate data against schema
val validator = JsonSchemaValidator()
val data = Json.parse("""{"name": "John", "age": 30}""")
val result = validator.validate(schema, data)

if (result.isValid) {
    println("Validation passed!")
} else {
    result.errors.forEach { error ->
        println("Error: ${error.message} at ${error.instancePath}")
    }
}

// Integration with MIDI-CI device configuration
val config = MidiCIDeviceConfiguration()
config.jsonSchemaString = schemaJson
val validationResult = JsonSchemaIntegration.validateDeviceConfiguration(config, data)

🧪 Testing

Comprehensive Test Suite

  • JsonSchemaTest: Unit tests for all validation keywords and edge cases
  • JsonSchemaIntegrationTest: Integration tests with MIDI-CI system
  • Tests cover type validation, constraint validation, error handling, and schema parsing
  • All tests follow existing patterns from JsonTest.kt

Build Verification

  • ✅ Successfully compiles with ./gradlew :ktmidi-ci:compileKotlinJvm
  • ✅ No breaking changes to existing functionality
  • ✅ Follows Kotlin multiplatform conventions

📁 File Structure

ktmidi-ci/src/commonMain/kotlin/dev/atsushieno/ktmidi/ci/jsonschema/
├── JsonSchema.kt                 # Main schema representation
├── JsonSchemaValidator.kt        # Validation engine
├── JsonSchemaException.kt        # Error handling
└── JsonSchemaIntegration.kt      # MIDI-CI integration

ktmidi-ci/src/commonTest/kotlin/dev/atsushieno/ktmidi/ci/jsonschema/
├── JsonSchemaTest.kt             # Unit tests
└── JsonSchemaIntegrationTest.kt  # Integration tests

🎨 Design Principles

  • Consistency: Follows existing ktmidi-ci patterns and conventions
  • Integration: Seamless integration with existing JSON and property systems
  • Extensibility: Designed for future enhancements and additional vocabulary support
  • Performance: Efficient validation with detailed error reporting
  • Compatibility: Works with existing Json.JsonValue and serialization infrastructure

🔗 Related

  • Implements JSON Schema Draft 2020-12 specification
  • Integrates with existing property exchange system in PropertyCommonRules.kt
  • Enhances MidiCIDeviceConfiguration schema validation capabilities
  • Compatible with existing Json.kt parsing and serialization

Link to Devin run: https://app.devin.ai/sessions/cee16ac818b34fdd8a5db07189691483

Requested by: Atsushi Eno ([email protected])

- Add JsonSchema data class with support for JSON Schema Draft 2020-12
- Implement JsonSchemaValidator with comprehensive validation logic
- Support core validation keywords: type, properties, required, items, enum, const, format
- Add numeric constraints: minimum, maximum, exclusiveMinimum, exclusiveMaximum
- Add string constraints: minLength, maxLength, pattern
- Add array constraints: minItems, maxItems, uniqueItems
- Add object constraints: minProperties, maxProperties, additionalProperties
- Integrate with existing Json.JsonValue system and MidiCIDeviceConfiguration
- Include comprehensive unit tests and integration tests
- Follow existing ktmidi-ci patterns and conventions

Co-Authored-By: Atsushi Eno <[email protected]>
Copy link
Author

🤖 Devin AI Engineer

I'll be helping with this pull request! Here's what you should know:

✅ I will automatically:

  • Address comments on this PR. Add '(aside)' to your comment to have me ignore it.
  • Look at CI failures and help fix them

Note: I can only respond to comments from users who have write access to this repository.

⚙️ Control Options:

  • Disable automatic comment and CI monitoring

devin-ai-integration bot and others added 3 commits June 22, 2025 07:27
- Implement JsonSchemaClassGenerator with comprehensive class generation
- Support for nested objects, arrays, enums, and primitive types
- String-based serialization using existing Json.JsonValue infrastructure
- Generate companion object methods for fromString/fromJsonValue
- Generate instance methods for toJsonString/toJsonValue
- Handle complex MIDI schema patterns like nested objects and enums
- Add comprehensive test suite for generator functionality
- Follow existing ktmidi-ci patterns and naming conventions

Co-Authored-By: Atsushi Eno <[email protected]>
- Replace broken obj[Json.JsonValue(key)] lookups with jsonValue.getObjectValue(key)
- JsonValue lacks equals()/hashCode() methods causing Map key lookup failures
- This should resolve JSON schema type parsing returning null instead of OBJECT
- Also fixes similar issues in class generator serialization methods

Co-Authored-By: Atsushi Eno <[email protected]>
- Add objectClassCache to GenerationContext to prevent duplicate class generation
- Ensure getKotlinType() returns cached class names for same object schemas
- Resolves testNestedObjectGeneration failure (expected 2 classes, was getting 3)
- Maintains all existing JsonSchemaClassGenerator functionality

Co-Authored-By: Atsushi Eno <[email protected]>
Copy link
Author

Closing due to inactivity for more than 7 days. Configure here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

0 participants