losisin / helm-values-schema-json

Helm plugin for generating values.schema.json from multiple values files
MIT License
75 stars 11 forks source link

Feature Request: Defining Array Object Types #90

Closed Persi closed 1 month ago

Persi commented 1 month ago

I could not find a solution to define the type of objects contained in an array. I believe the JSON Schema Spec is providing a way to do that, with the keyword items on an array.

If my default values.yaml should just provide the option to define a property as array, with an empty array as default value, I am unable to define the type of object I expect in that array.

For example:

property: [] # @schema: type: [array,null]

just results in the following schema definition:

...
    "property": {
      "type": "array"
    },
...

My suggestion would be to introduce a new keyword like itemType, with a definition for object as follows:

property: [] # @schema: type: [array,null] ; itemType: {"key":"string","key2":"integer"}

For primitive types just like that:

property: [] # @schema: type: [array,null] ; itemType: string
losisin commented 1 month ago

@Persi we already have this using item annotation. However, this will only pass item type and not it's properties. See here: https://github.com/losisin/helm-values-schema-json/pull/49/files If object is defined with properties, how should the desired JSON look like?

Persi commented 1 month ago

This MR looks good to me, I'll try it out after next release. Thank you for the fast reaction and fix :)

Persi commented 1 month ago

I tried current release 1.5.4 of your helm plugin, but unfortunately it does not seem to work.

My property looks like follows:

myprop: [] # @schema description: nice desc here;uniqueItems:true;item:object;itemProperties:{\"name\": {\"type\": \"string\"}, \"serviceType\": {\"type\": \"string\"}}

Result looks like follows:

    "myprop": {
      "description": "nice desc here",
      "items": {
        "type": "object"
      },
      "type": "array",
      "uniqueItems": true
    },

What do i miss?

losisin commented 1 month ago

I think it's the quotes escaping. this yaml:

myprop: [] # @schema description: nice desc here;uniqueItems:true;item:object;itemProperties:{"name": {"type": "string"}, "serviceType": {"type": "string"}}

produces this json:

{
    "$schema": "https://json-schema.org/draft/2020-12/schema",
    "properties": {
        "myprop": {
            "description": "nice desc here",
            "items": {
                "properties": {
                    "name": {
                        "type": "string"
                    },
                    "serviceType": {
                        "type": "string"
                    }
                },
                "type": "object"
            },
            "type": "array",
            "uniqueItems": true
        }
    },
    "type": "object"
}
Persi commented 1 month ago

Wow ... if you do the thing and you do it right ... it just works :D Thanks for your support!

losisin commented 1 month ago

glad I can help :)