Skip to content
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

Addressing "items" keyword in "type" : "array" declarations. #100

Open
jjgenoese opened this issue Aug 14, 2024 · 8 comments
Open

Addressing "items" keyword in "type" : "array" declarations. #100

jjgenoese opened this issue Aug 14, 2024 · 8 comments

Comments

@jjgenoese
Copy link

There does not seem to be a way to programatically access the contents of the "items" keyword. Apologies if the method for doing this is documented elsewhere.

I am working on a project that involves translating different schemas (e.g. AVRO, JSONSchema) to an internal format. To do so I need to "walk" the schema. json-sKema seemed like the right tool to do that, but I am unable to "see" the "items" tag that comes as part of an "array" declaration.

Thanks for any help.

@erosb
Copy link
Owner

erosb commented Aug 14, 2024

Hello, you may check if a Schema instance is an ItemSchema, and if yes, then read its itemsSchema member (value of the "items" keyword). Or you can create a subclass of SchemaVisitor and override its visitItemsSchema() method (then use schema.accept(yourVisitorObj)). Hope it helps.

@jjgenoese
Copy link
Author

Thank you so much for replying so quickly! I'll try this out. I was concerned because it doesn't show up when I walk the getSubschemas() and getPropertySubschemas() objects. If I start at the top CompositeSchema, I should encounter the property whose TypeSchema is "array". Can you suggest a way to navigate to "items" from there? Thanks again.

fieldName:anArray; class:com.github.erosb.jsonsKema.CompositeSchema
==> SubSchemas
==> ==> TypeSchema(type="array", location=line 26, character 6, pointer: #/properties/anArray/type)
==> PropertySchemas

How do I get to "Items". Please ignore this followup question if the answer is documented. Thanks much!!!!

@jjgenoese
Copy link
Author

(typo: I mean getPropertySchemas() -- sorry)

@erosb
Copy link
Owner

erosb commented Aug 14, 2024

Any subschemas inside a CompositeSchema correspond to a single schema keyword. So if you have a json schema like

{
  "type": "array",
  "items": {
    "type": "string"
  }
}

Then it will be parsed into a

CompositeSchema(
  subschemas = listOf(
    TypeSchema(type = "array"),
    ItemsSchema(itemsSchema = CompositeSchema(
      subschemas = listOf(TypeSchema(type = "string"))
    ))
  )
)

(Hope this kotlin notation is readable enough)

@jjgenoese
Copy link
Author

jjgenoese commented Aug 15, 2024

Thank you again for working with me on this. I want to use this library.

Whenever I iterate through the CompositeSchema's subschemas and propertySchemas, the ItemSchema doesn't show up.

For example:
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"properties": {
"anArray" : {
"type" : "array"
"items" : ["string"]
}
}
}

    I recurse through this structure and print everything out I get this 
    
    	==>	 top: CompositeSchema(subschemas=[TypeSchema(type="object", 
==>		==>	 subschema: TypeSchema(type="object", location=line 4, character 13, pointer: #/type)
==>		==>	 propertyschema:anArray: CompositeSchema(subschemas=[TypeSchema(type="array", location=line 7, character 6, pointer: #/properties/anArray/type)], ...
==>		==>		==>	 subschema: TypeSchema(type="array", location=line 7, character 6, pointer: #/properties/anArray/type)

No ItemSchema appears in the subschemas() of CompositeSchema("anArray")

Thanks for your help!!!!

@erosb
Copy link
Owner

erosb commented Aug 15, 2024

"items" : ["string"] this isn't valid according to json schema 2020-12. I'm surprised it even loads (I would expect it to throw an exception here ).

Please try with "items": { "type": "string" }

@jjgenoese
Copy link
Author

jjgenoese commented Aug 15, 2024

Oh man! It works, beautifully!!!! Thank you so much!!!

I am so grateful! Apologies for troubling you with what essentially was a syntax error. Is there any way I can "buy you a coffee" ?

@erosb
Copy link
Owner

erosb commented Aug 15, 2024

Haha, there is a green "Donate" button on the predecessor project: https://github.com/everit-org/json-schema
I appreciate =)

erosb added a commit that referenced this issue Sep 2, 2024
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

No branches or pull requests

2 participants