0
votes

I'm trying to write a jsonschema for a list of dictionaries (aka an array of objects) where I validate the keys in the dictionary. The labels in this example are what I'm interested in. I'd like to allow an arbitrary number of labels and like to validate that the name and value fields always exist in the label dictionary. Here is an example input represented as yaml.

some_field1: "value_a"
some_field2: "value_b"
labels:
- name: "bar"
  value: "foo"
- name: "baz"
  value: "blah"

Here is what I've pieced together so far, but it doesn't validate the keys in the dictionaries. I'm not sure exactly how the additionalProperites works in this case, but I found an example online.

properties:
  some_field1:
    type: string
    default: 'value_a'
  some_field2:
    type: string
    default: 'value_b'
  labels:
    type: array
    items:
      type: object
      additionalProperties:
        type: string

My use case is that I'm trying to create a Custom Resource Definition (CRD) for Kubernetes where I validate the input, and my understanding is that the CRDs use openapi3/jsonschema validation to define their fields.

I'm having trouble finding an information about how to validate a list of dictionaries with specific keys. I'd appreciate any help that you have to offer.

1

1 Answers

4
votes

Known/fixed keys of a dictionary can be defined in properties and included in the required list:

  labels:
    type: array
    items:
      type: object
      required: [name, value]
      properties:
        name:
          type: string
        value:
          type: string
      additionalProperties:
        type: string