Skip to content

feat(swift_core): Establish JSON identity routing and evaluation protocols#1720

Open
pinieb wants to merge 20 commits into
a2ui-project:mainfrom
pinieb:swift-json-schema-protocols
Open

feat(swift_core): Establish JSON identity routing and evaluation protocols#1720
pinieb wants to merge 20 commits into
a2ui-project:mainfrom
pinieb:swift-json-schema-protocols

Conversation

@pinieb

@pinieb pinieb commented Jun 22, 2026

Copy link
Copy Markdown
Collaborator

Description

Overview

This PR establishes the foundational skeleton for the JSON Schema evaluation engine. It introduces the core data structures needed to deterministically identify nodes in the schema tree and defines the strict execution contracts required to safely evaluate JSON instances against those nodes.

By separating identity resolution from execution state, this PR paves the way for the SchemaCompiler and the implementation of specific validation keywords without risking tight coupling or infinite recursion loops.

Key Additions

1. Identity & Routing (RFC 6901)

  • JSONPointer: Implemented a robust path management struct fully compliant with RFC 6901, including proper escaping and unescaping of ~0 and ~1 characters.
  • SchemaIdentity: Created a deterministic identity pairing a Base URI with a JSON Pointer. Crucially, this includes the updatingBaseURI logic necessary to intercept $id keywords, allowing schemas to safely reset their resolution scope and pointer paths during compilation.

2. Execution Contracts

  • KeywordEvaluator: Defined the primary protocol that all future validators, applicators, and annotators will conform to.
  • SchemaNode: Implemented the compiled AST node representation. It holds an array of KeywordEvaluators and handles the aggregation of errors and annotations during evaluation.
  • ValidationResult: Designed to fully support the 2020-12 hierarchical/verbose output format, tracking isValid, errors, annotations, and deeply nested child results.

3. Context & State Management

  • ValidationContext: Introduced the immutable state container passed down during tree traversal.
  • ValidatorConfiguration: Added standard user configurations, including maxEvaluationDepth to defend against stack overflows from infinitely recursive malicious schemas.

Technical Notes

  • Recursion Defense: ValidationContext.passingDown(toInstanceKey:) actively tracks execution depth and will throw a hard error if the configured limit is breached.
  • Immutability: The execution lifecycle remains strictly non-mutating. Context updates and identity mutations are handled via copying value types (Swift structs) to maintain thread safety and prevent shared-state side effects.

Testing

  • Added coverage for RFC 6901 character escaping and path appending.
  • Added coverage for Base URI resets (simulating the behavior of $id).
  • Added coverage for Context max-depth throwing.
  • Verified SchemaNode successfully aggregates multiple keyword results (combining discrete annotations and errors into a single ValidationResult).

Pre-launch Checklist

For this PR:

  • I have added updates to the [CHANGELOG].
  • I updated/added relevant documentation.
  • My code changes (if any) have tests.
  • If my branch is on fork, I have verified that scripts/e2e_test.sh passes.
    • Used run_tests.sh for Swift tests

pinieb added 7 commits June 21, 2026 09:20
This change establishes the foundational infrastructure for Swift libraries in the A2UI monorepo. There is just enough code (JSONValue.swift and its tests) to standup a SwiftPM library and get CI up and running.
Every node in a compiled JSON Schema AST must have a deterministic identity to allow for $ref resolution and accurate error reporting. This identity is a combination of a Base URI and a JSON Pointer (RFC 6901).

This change introduces the mechanisms to do this.
…xt management

- Add `ValidationContext` and `ValidatorConfiguration` to safely pass
  execution state (like max depth limits) down the AST without mutation.
- Add `ValidationResult` struct designed to support the 2020-12
  hierarchical output format, tracking errors and annotations.
- Define the `KeywordEvaluator` protocol to standardize keyword execution.
- Implement the `SchemaNode` executable AST struct with logic to aggregate
  results, errors, and annotations from multiple evaluators.
- Add test coverage for recursion depth limits and result aggregation.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request sets up the A2UISwiftCore package and implements the core types for the generic JSONSchema validator target, including JSONValue, JSONPointer, SchemaIdentity, and SchemaNode, along with comprehensive unit tests and documentation. The review feedback highlights several key improvements: making the JSONPointer string initializer failable to handle malformed paths safely, conforming both JSONPointer and SchemaIdentity to Hashable for collection and dictionary compatibility, normalizing baseURI in SchemaIdentity to prevent double-fragment URIs, and ensuring compliance with the JSON Schema Draft 2020-12 specification by discarding annotations when validation fails.

Comment thread renderers/swift_core/JSONSchema/Sources/JSONPointer.swift Outdated
Comment thread renderers/swift_core/JSONSchema/Sources/JSONPointer.swift Outdated
Comment thread renderers/swift_core/JSONSchema/Sources/SchemaIdentity.swift Outdated
Comment thread renderers/swift_core/JSONSchema/Sources/SchemaIdentity.swift Outdated
Comment thread swift/core/JSONSchema/Sources/SchemaNode.swift
pinieb and others added 3 commits June 22, 2026 18:33
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Signed-off-by: Pete Biencourt <pinieb@users.noreply.github.com>
pinieb and others added 10 commits June 23, 2026 11:36
# Conflicts:
#	Package.swift
#	renderers/swift_core/AGENTS.md
#	swift/core/CODING_STANDARDS.md
#	swift/core/JSONSchema/README.md
#	swift/core/JSONSchema/Sources/JSONPointer.swift
#	swift/core/JSONSchema/Sources/JSONValue.swift
#	swift/core/JSONSchema/Sources/KeywordEvaluator.swift
#	swift/core/JSONSchema/Sources/SchemaIdentity.swift
#	swift/core/JSONSchema/Sources/SchemaNode.swift
#	swift/core/JSONSchema/Sources/SchemaRegistry.swift
#	swift/core/JSONSchema/Sources/ValidationContext.swift
#	swift/core/JSONSchema/Sources/ValidationError.swift
#	swift/core/JSONSchema/Sources/ValidationResult.swift
#	swift/core/JSONSchema/Sources/ValidatorConfiguration.swift
#	swift/core/JSONSchema/Tests/ExecutionContractsTests.swift
#	swift/core/JSONSchema/Tests/JSONValueTests.swift
#	swift/core/JSONSchema/Tests/SchemaIdentityTests.swift
#	swift/core/README.md
@pinieb pinieb marked this pull request as ready for review June 23, 2026 23:25
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.

1 participant