@@ -17,38 +17,7 @@ public class Jtd {
1717
1818 /// Top-level definitions map for ref resolution
1919 private final Map <String , JtdSchema > definitions = new java .util .HashMap <>();
20-
21- /// Stack frame for iterative validation with path and offset tracking
22- record Frame (JtdSchema schema , JsonValue instance , String ptr , Crumbs crumbs , String discriminatorKey ) {
23- /// Constructor for normal validation without discriminator context
24- Frame (JtdSchema schema , JsonValue instance , String ptr , Crumbs crumbs ) {
25- this (schema , instance , ptr , crumbs , null );
26- }
27-
28- @ Override
29- public String toString () {
30- final var kind = schema .getClass ().getSimpleName ();
31- final var tag = (schema instanceof JtdSchema .RefSchema r ) ? "(ref=" + r .ref () + ")" : "" ;
32- return "Frame[schema=" + kind + tag + ", instance=" + instance + ", ptr=" + ptr +
33- ", crumbs=" + crumbs + ", discriminatorKey=" + discriminatorKey + "]" ;
34- }
35- }
36-
37- /// Lightweight breadcrumb trail for human-readable error paths
38- record Crumbs (String value ) {
39- static Crumbs root () {
40- return new Crumbs ("#" );
41- }
42-
43- Crumbs withObjectField (String name ) {
44- return new Crumbs (value + "→field:" + name );
45- }
46-
47- Crumbs withArrayIndex (int idx ) {
48- return new Crumbs (value + "→item:" + idx );
49- }
50- }
51-
20+
5221 /// Extracts offset from JsonValue implementation classes
5322 static int offsetOf (JsonValue v ) {
5423 return switch (v ) {
@@ -65,8 +34,8 @@ static int offsetOf(JsonValue v) {
6534 /// Creates an enriched error message with offset and path information
6635 static String enrichedError (String baseMessage , Frame frame , JsonValue contextValue ) {
6736 int off = offsetOf (contextValue );
68- String ptr = frame .ptr ;
69- String via = frame .crumbs .value ();
37+ String ptr = frame .ptr () ;
38+ String via = frame .crumbs () .value ();
7039 return "[off=" + off + " ptr=" + ptr + " via=" + via + "] " + baseMessage ;
7140 }
7241
@@ -106,25 +75,25 @@ Result validateWithStack(JtdSchema schema, JsonValue instance) {
10675 stack .push (rootFrame );
10776
10877 LOG .fine (() -> "Starting stack validation - schema=" +
109- rootFrame .schema .getClass ().getSimpleName () +
110- (rootFrame .schema instanceof JtdSchema .RefSchema r ? "(ref=" + r .ref () + ")" : "" ) +
78+ rootFrame .schema () .getClass ().getSimpleName () +
79+ (rootFrame .schema () instanceof JtdSchema .RefSchema r ? "(ref=" + r .ref () + ")" : "" ) +
11180 ", ptr=#" );
11281
11382 // Process frames iteratively
11483 while (!stack .isEmpty ()) {
11584 Frame frame = stack .pop ();
116- LOG .fine (() -> "Processing frame - schema: " + frame .schema .getClass ().getSimpleName () +
117- (frame .schema instanceof JtdSchema .RefSchema r ? "(ref=" + r .ref () + ")" : "" ) +
118- ", ptr: " + frame .ptr + ", off: " + offsetOf (frame .instance ));
85+ LOG .fine (() -> "Processing frame - schema: " + frame .schema () .getClass ().getSimpleName () +
86+ (frame .schema () instanceof JtdSchema .RefSchema r ? "(ref=" + r .ref () + ")" : "" ) +
87+ ", ptr: " + frame .ptr () + ", off: " + offsetOf (frame .instance () ));
11988
12089 // Validate current frame
121- if (!frame .schema .validateWithFrame (frame , errors , false )) {
122- LOG .fine (() -> "Validation failed for frame at " + frame .ptr + " with " + errors .size () + " errors" );
90+ if (!frame .schema () .validateWithFrame (frame , errors , false )) {
91+ LOG .fine (() -> "Validation failed for frame at " + frame .ptr () + " with " + errors .size () + " errors" );
12392 continue ; // Continue processing other frames even if this one failed
12493 }
12594
12695 // Handle special validations for PropertiesSchema
127- if (frame .schema instanceof JtdSchema .PropertiesSchema propsSchema ) {
96+ if (frame .schema () instanceof JtdSchema .PropertiesSchema propsSchema ) {
12897 validatePropertiesSchema (frame , propsSchema , errors );
12998 }
13099
@@ -179,8 +148,8 @@ void validatePropertiesSchema(Frame frame, JtdSchema.PropertiesSchema propsSchem
179148
180149 /// Pushes child frames for complex schema types
181150 void pushChildFrames (Frame frame , java .util .Deque <Frame > stack ) {
182- JtdSchema schema = frame .schema ;
183- JsonValue instance = frame .instance ;
151+ JtdSchema schema = frame .schema () ;
152+ JsonValue instance = frame .instance () ;
184153
185154 LOG .finer (() -> "Pushing child frames for schema type: " + schema .getClass ().getSimpleName ());
186155
@@ -189,8 +158,8 @@ void pushChildFrames(Frame frame, java.util.Deque<Frame> stack) {
189158 if (instance instanceof JsonArray arr ) {
190159 int index = 0 ;
191160 for (JsonValue element : arr .values ()) {
192- String childPtr = frame .ptr + "/" + index ;
193- Crumbs childCrumbs = frame .crumbs .withArrayIndex (index );
161+ String childPtr = frame .ptr () + "/" + index ;
162+ Crumbs childCrumbs = frame .crumbs () .withArrayIndex (index );
194163 Frame childFrame = new Frame (elementsSchema .elements (), element , childPtr , childCrumbs );
195164 stack .push (childFrame );
196165 LOG .finer (() -> "Pushed array element frame at " + childPtr );
@@ -214,8 +183,8 @@ void pushChildFrames(Frame frame, java.util.Deque<Frame> stack) {
214183 JsonValue value = obj .members ().get (key );
215184
216185 if (value != null ) {
217- String childPtr = frame .ptr + "/" + key ;
218- Crumbs childCrumbs = frame .crumbs .withObjectField (key );
186+ String childPtr = frame .ptr () + "/" + key ;
187+ Crumbs childCrumbs = frame .crumbs () .withObjectField (key );
219188 Frame childFrame = new Frame (entry .getValue (), value , childPtr , childCrumbs );
220189 stack .push (childFrame );
221190 LOG .finer (() -> "Pushed required property frame at " + childPtr );
@@ -235,8 +204,8 @@ void pushChildFrames(Frame frame, java.util.Deque<Frame> stack) {
235204 JsonValue value = obj .members ().get (key );
236205
237206 if (value != null ) {
238- String childPtr = frame .ptr + "/" + key ;
239- Crumbs childCrumbs = frame .crumbs .withObjectField (key );
207+ String childPtr = frame .ptr () + "/" + key ;
208+ Crumbs childCrumbs = frame .crumbs () .withObjectField (key );
240209 Frame childFrame = new Frame (childSchema , value , childPtr , childCrumbs );
241210 stack .push (childFrame );
242211 LOG .finer (() -> "Pushed optional property frame at " + childPtr );
@@ -250,8 +219,8 @@ void pushChildFrames(Frame frame, java.util.Deque<Frame> stack) {
250219 for (var entry : obj .members ().entrySet ()) {
251220 String key = entry .getKey ();
252221 JsonValue value = entry .getValue ();
253- String childPtr = frame .ptr + "/" + key ;
254- Crumbs childCrumbs = frame .crumbs .withObjectField (key );
222+ String childPtr = frame .ptr () + "/" + key ;
223+ Crumbs childCrumbs = frame .crumbs () .withObjectField (key );
255224 Frame childFrame = new Frame (valuesSchema .values (), value , childPtr , childCrumbs );
256225 stack .push (childFrame );
257226 LOG .finer (() -> "Pushed values schema frame at " + childPtr );
@@ -266,7 +235,7 @@ void pushChildFrames(Frame frame, java.util.Deque<Frame> stack) {
266235 JtdSchema variantSchema = discSchema .mapping ().get (discriminatorValueStr );
267236 if (variantSchema != null ) {
268237
269- Frame variantFrame = new Frame (variantSchema , instance , frame .ptr , frame .crumbs , discSchema .discriminator ());
238+ Frame variantFrame = new Frame (variantSchema , instance , frame .ptr () , frame .crumbs () , discSchema .discriminator ());
270239 stack .push (variantFrame );
271240 LOG .finer (() -> "Pushed discriminator variant frame for " + discriminatorValueStr + " with discriminator key: " + discSchema .discriminator ());
272241 }
@@ -276,8 +245,8 @@ void pushChildFrames(Frame frame, java.util.Deque<Frame> stack) {
276245 case JtdSchema .RefSchema refSchema -> {
277246 try {
278247 JtdSchema resolved = refSchema .target ();
279- Frame resolvedFrame = new Frame (resolved , instance , frame .ptr ,
280- frame .crumbs , frame .discriminatorKey ());
248+ Frame resolvedFrame = new Frame (resolved , instance , frame .ptr () ,
249+ frame .crumbs () , frame .discriminatorKey ());
281250 pushChildFrames (resolvedFrame , stack );
282251 LOG .finer (() -> "Pushed ref schema resolved to " +
283252 resolved .getClass ().getSimpleName () + " for ref: " + refSchema .ref ());
0 commit comments