we have subnet names as keys in subnets map. I want create schema to make subnet name length restricted conditional based on vpc_access key which is nested in the subnet.
I know that it is possible to make condition on nested property via properties.PROPERTY1.properties.PROPERTY2.const.VALUE
, but in my case I do not know the exact property name because I’m using patternProperties
.
So I’m trying to do something like this. It is JSON schema written in yaml form.
subnets:
if:
patternProperties:
^.*$:
properties:
vpc_access:
const: true
required:
- vpc_access
then:
propertyNames:
pattern: ^[a-z][a-z0-9-]{1,22}[a-z0-9]$
patternProperties:
^.*$:
$ref: /schemas/network.subnet
Can you please confirm if this is possible in schema and help how to do it, please?
gorge511 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
patternProperties
will only evaluate keys that match the pattern. Otherwise, any other property will be evaluated based on other parts of the schema, or ignored all together depending on the constraints of your schema
in this example, anything matching the pattern will expect an object with a required key vpc_access: true
. Otherwise, any other properties not matching the pattern will require whatever the network.subnet
schema is.
subnets:
patternProperties:
'^[a-z][a-z0-9-]{1,22}[a-z0-9]$':
type: object
properties:
vpc_access:
type: boolean
const: true
required:
- vpc_access
additionalProperties:
$ref: /schemas/network.subnet
if you are trying to evaluate the value of vpc_access
to enforce the key length, it’s not possible to evaluate a nested value for a parent key.
On the other hand, if your data instance is expecting vpc_access: true
and the key length to match the pattern, if the key matches the pattern and the value of vpc_access
is false
, the instance will fail validation because you are constraining the schema to only allow const: true
, effectively giving you a similar output to your request.