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

additionalProperties: {schema} vs. annotated properties #106

Closed
TomasBahnik opened this issue Jan 13, 2025 · 2 comments
Closed

additionalProperties: {schema} vs. annotated properties #106

TomasBahnik opened this issue Jan 13, 2025 · 2 comments

Comments

@TomasBahnik
Copy link

TomasBahnik commented Jan 13, 2025

Hello, I am facing an issue how to generate schema allowing validation of collection of named sub-objects.

Example:
Let's say my annotated values.yaml file looks like this

# @schema
# required: true
# @schema
people:
  # @schema
  # required: true
  # type: integer
  # @schema
  weight: 100
  # @schema
  # required: true
  # type: integer
  # @schema
  height: 200

And I would like to generate schema that validates as correct the values

people:
  john:
    weight: 10
    height: 100
  mary:
    weight: 10
    height: 100

but that validation fails. It validates as correct single person like john.yaml

people:
 weight: 10
 height: 100

The only way is to annotate the values.yaml file with additionalProperties: {schema} like

# @schema
# additionalProperties: {
#        "properties": {
#          "height": {
#            "default": 100,
#            "description": "The height of the person in unspecified units.",
#            "title": "height",
#            "type": "integer"
#          },
#          "weight": {
#            "default": 10,
#            "description": "The weight of the person in unspecified units.",
#            "title": "weight",
#            "type": "integer"
#          }
#        },
#        "required": [
#          "weight",
#          "height"
#        ]
#      }
# required: true
# @schema
people:
 weight: 100
 height: 200

Which generates duplicated schema code.

The first question is whether I can achieve validation of collection of named sub-objects without additionalProperties: {schema} annotattion i.e. only annotating individual properties in values file.

And second question. Is the $ref supposed to work also for additionalProperties like

# additionalProperties: { $ref: people.json#/properties }

Thank you

Tomas

@dadav
Copy link
Owner

dadav commented Jan 14, 2025

Hi Thomas,

I think patternProperties is the way to go here:

# yaml-language-server: $schema=values.schema.json

# @schema
# patternProperties:
#   ".*":
#     required: [weight, height]
#     properties:
#       weight:
#         type: integer
#       height:
#         type: integer
# @schema
people:

The $ref should work like this too.

@TomasBahnik
Copy link
Author

Thank you dadav. That solution is cleaner and more powerful.

@dadav dadav closed this as completed Jan 15, 2025
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