简体   繁体   中英

Use (scalar) variables in jsonschema to dynamically set attribute, e.g. title

I am using Google Cloud deployment manager to manage infrastructure as code (IAC) and they support providing schema files for describing IAC templates. Their support of jsonschema is a bit weird - the documentation is very brief but it suggests that they support the latest version of the schema plus they require title and description to be within an info object.

This is irritating because I use an HTML renderer for my schemas, which implements jsonschema and therefore, it requires title and description to be set as top-level properties.

To satisfy both, I need to duplicate, eg:


title: foo
description: bar

info:
  title: foo
  description: bar

I was hoping to just define title and description values once and then use some $ref: "#/$defs/title" magic but I don't think you can use this to dynamically set values like the title because this functionality is intended for schema parsers to fetch block content from elsewhere.

Is there any way I can avoid duplicating the values - beyond dynamically rendering my schema files which I do not want to do.

As far as I can tell, there is no way to use reference within json schema.
As a crude workaround you can use a script to add/replace placeholders:

#!/bin/bash
sed -i 's/\$title/title: foo/g' file.json
sed -i 's/\$desc/description: bar/g' file.json

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM