55
66import java .util .ArrayList ;
77import java .util .Collections ;
8- import java . util . HashSet ;
8+
99import java .util .List ;
1010import java .util .Map ;
1111import java .util .Set ;
@@ -49,12 +49,12 @@ static String enrichedError(String baseMessage, Frame frame, JsonValue contextVa
4949 }
5050
5151 /// Compiles a JTD schema and throws exceptions for invalid schemas
52+ ///
5253 /// @param schema The JTD schema as a JsonValue
53- /// @return Compiled JtdSchema
5454 /// @throws IllegalArgumentException if the schema is invalid
55- public JtdSchema compile (JsonValue schema ) {
55+ public void compile (JsonValue schema ) {
5656 definitions .clear ();
57- return compileSchema (schema );
57+ compileSchema (schema );
5858 }
5959
6060 /// Validates a JSON instance against a JTD schema
@@ -324,19 +324,13 @@ JtdSchema compileSchema(JsonValue schema, boolean isRoot) {
324324 }
325325 }
326326
327- return compileObjectSchema (obj , isRoot );
327+ return compileObjectSchema (obj );
328328 }
329-
330- /// Compiles an object schema according to RFC 8927 with strict semantics
331- JtdSchema compileObjectSchema (JsonObject obj ) {
332- return compileObjectSchema (obj , true ); // Default to root schema
333- }
334-
329+
335330 /// Compiles an object schema according to RFC 8927 with strict semantics
336331 /// @param obj The JSON object to compile
337- /// @param isRoot Whether this is a root-level schema (can contain definitions)
338332 /// @return Compiled JtdSchema
339- JtdSchema compileObjectSchema (JsonObject obj , boolean isRoot ) {
333+ JtdSchema compileObjectSchema (JsonObject obj ) {
340334 // Check for mutually-exclusive schema forms
341335 List <String > forms = new ArrayList <>();
342336 Map <String , JsonValue > members = obj .members ();
@@ -357,7 +351,7 @@ JtdSchema compileObjectSchema(JsonObject obj, boolean isRoot) {
357351
358352 // RFC 8927: Check for form-specific properties that shouldn't be mixed
359353 if (forms .size () == 1 ) {
360- String form = forms .get ( 0 );
354+ String form = forms .getFirst ( );
361355 switch (form ) {
362356 case "elements" , "values" , "enum" , "ref" , "type" -> {
363357 // These forms should not have properties-specific attributes
@@ -445,11 +439,11 @@ JtdSchema compileObjectSchema(JsonObject obj, boolean isRoot) {
445439 case "ref" -> compileRefSchema (obj );
446440 case "type" -> compileTypeSchema (obj );
447441 case "enum" -> compileEnumSchema (obj );
448- case "elements" -> compileElementsSchema (obj , isRoot );
449- case "properties" -> compilePropertiesSchema (obj , isRoot );
450- case "optionalProperties" -> compilePropertiesSchema (obj , isRoot ); // handled together
451- case "values" -> compileValuesSchema (obj , isRoot );
452- case "discriminator" -> compileDiscriminatorSchema (obj , isRoot );
442+ case "elements" -> compileElementsSchema (obj );
443+ case "properties" -> compilePropertiesSchema (obj );
444+ case "optionalProperties" -> compilePropertiesSchema (obj ); // handled together
445+ case "values" -> compileValuesSchema (obj );
446+ case "discriminator" -> compileDiscriminatorSchema (obj );
453447 default -> throw new IllegalArgumentException ("Unknown schema form: " + form );
454448 };
455449 }
@@ -542,21 +536,13 @@ JtdSchema compileEnumSchema(JsonObject obj) {
542536 }
543537
544538 JtdSchema compileElementsSchema (JsonObject obj ) {
545- return compileElementsSchema (obj , true ); // Default to root
546- }
547-
548- JtdSchema compileElementsSchema (JsonObject obj , boolean isRoot ) {
549539 Map <String , JsonValue > members = obj .members ();
550540 JsonValue elementsValue = members .get ("elements" );
551541 JtdSchema elementsSchema = compileSchema (elementsValue , false ); // Elements are nested schemas
552542 return new JtdSchema .ElementsSchema (elementsSchema );
553543 }
554-
544+
555545 JtdSchema compilePropertiesSchema (JsonObject obj ) {
556- return compilePropertiesSchema (obj , true ); // Default to root
557- }
558-
559- JtdSchema compilePropertiesSchema (JsonObject obj , boolean isRoot ) {
560546 Map <String , JtdSchema > properties = Map .of ();
561547 Map <String , JtdSchema > optionalProperties = Map .of ();
562548
@@ -568,7 +554,7 @@ JtdSchema compilePropertiesSchema(JsonObject obj, boolean isRoot) {
568554 if (!(propsValue instanceof JsonObject propsObj )) {
569555 throw new IllegalArgumentException ("properties must be an object" );
570556 }
571- properties = parsePropertySchemas (propsObj , false ); // Property schemas are nested
557+ properties = parsePropertySchemas (propsObj ); // Property schemas are nested
572558 }
573559
574560 // Parse optional properties
@@ -577,7 +563,7 @@ JtdSchema compilePropertiesSchema(JsonObject obj, boolean isRoot) {
577563 if (!(optPropsValue instanceof JsonObject optPropsObj )) {
578564 throw new IllegalArgumentException ("optionalProperties must be an object" );
579565 }
580- optionalProperties = parsePropertySchemas (optPropsObj , false ); // Property schemas are nested
566+ optionalProperties = parsePropertySchemas (optPropsObj ); // Property schemas are nested
581567 }
582568
583569 // RFC 8927: Check for key overlap between properties and optionalProperties
@@ -603,21 +589,13 @@ JtdSchema compilePropertiesSchema(JsonObject obj, boolean isRoot) {
603589 }
604590
605591 JtdSchema compileValuesSchema (JsonObject obj ) {
606- return compileValuesSchema (obj , true ); // Default to root
607- }
608-
609- JtdSchema compileValuesSchema (JsonObject obj , boolean isRoot ) {
610592 Map <String , JsonValue > members = obj .members ();
611593 JsonValue valuesValue = members .get ("values" );
612594 JtdSchema valuesSchema = compileSchema (valuesValue , false ); // Values are nested schemas
613595 return new JtdSchema .ValuesSchema (valuesSchema );
614596 }
615-
597+
616598 JtdSchema compileDiscriminatorSchema (JsonObject obj ) {
617- return compileDiscriminatorSchema (obj , true ); // Default to root
618- }
619-
620- JtdSchema compileDiscriminatorSchema (JsonObject obj , boolean isRoot ) {
621599 Map <String , JsonValue > members = obj .members ();
622600 JsonValue discriminatorValue = members .get ("discriminator" );
623601 if (!(discriminatorValue instanceof JsonString discStr )) {
@@ -704,15 +682,10 @@ void validateDiscriminatorMapping(String mappingKey, JtdSchema variantSchema, St
704682
705683 /// Extracts and stores top-level definitions for ref resolution
706684 private Map <String , JtdSchema > parsePropertySchemas (JsonObject propsObj ) {
707- return parsePropertySchemas (propsObj , true ); // Default to root
708- }
709-
710- /// Extracts and stores top-level definitions for ref resolution
711- private Map <String , JtdSchema > parsePropertySchemas (JsonObject propsObj , boolean isRoot ) {
712685 Map <String , JtdSchema > schemas = new java .util .HashMap <>();
713686 for (String key : propsObj .members ().keySet ()) {
714687 JsonValue schemaValue = propsObj .members ().get (key );
715- schemas .put (key , compileSchema (schemaValue , isRoot ));
688+ schemas .put (key , compileSchema (schemaValue , false ));
716689 }
717690 return schemas ;
718691 }
0 commit comments