diff --git a/.github/utils/generate_json_schema.py b/.github/utils/generate_json_schema.py index 9f21b55e3..3fd833ac0 100644 --- a/.github/utils/generate_json_schema.py +++ b/.github/utils/generate_json_schema.py @@ -69,6 +69,9 @@ class Config(BaseConfig): def get_json_schema(): + """ + Generate JSON schema for Haystack pipelines. + """ schema_definitions = {} additional_definitions = {} @@ -210,11 +213,69 @@ def get_json_schema(): return pipeline_schema +def list_indexed_versions(index): + """ + Given the schema index as a parsed JSON, + return a list of all the versions it contains. + """ + indexed_versions = [] + for version_entry in index["oneOf"]: + for property_entry in version_entry["allOf"]: + if "properties" in property_entry.keys(): + indexed_versions.append(property_entry["properties"]["version"]["const"]) + return indexed_versions + + +def cleanup_rc_versions(index): + """ + Given the schema index as a parsed JSON, + removes any existing (unstable) rc version from it. + """ + new_versions_list = [] + for version_entry in index["oneOf"]: + for property_entry in version_entry["allOf"]: + if "properties" in property_entry.keys(): + if "rc" not in property_entry["properties"]["version"]["const"]: + new_versions_list.append(version_entry) + break + index["oneOf"] = new_versions_list + return index + + +def new_version_entry(version): + """ + Returns a new entry for the version index JSON schema. + """ + return { + "allOf": [ + {"properties": {"version": {"const": version}}}, + { + "$ref": "https://raw.githubusercontent.com/deepset-ai/haystack/master/json-schemas/" + f"haystack-pipeline-{version}.schema.json" + }, + ] + } + + def generate_json_schema(): + # Create new schema file pipeline_schema = get_json_schema() destination_path.parent.mkdir(parents=True, exist_ok=True) destination_path.write_text(json.dumps(pipeline_schema, indent=2)) + # Update schema index + index = [] + index_path = Path(__file__).parent.parent.parent / "json-schemas" / "haystack-pipeline.schema.json" + with open(index_path, "r") as index_file: + index = json.load(index_file) + if index: + index = cleanup_rc_versions(index) + indexed_versions = list_indexed_versions(index) + if not any(version == schema_version for version in indexed_versions): + index["oneOf"].append(new_version_entry(schema_version)) + with open(index_path, "w") as index_file: + json.dump(index, index_file, indent=4) + def main(): from github import Github diff --git a/json-schemas/haystack-pipeline.schema.json b/json-schemas/haystack-pipeline.schema.json new file mode 100644 index 000000000..d79e7e48d --- /dev/null +++ b/json-schemas/haystack-pipeline.schema.json @@ -0,0 +1,51 @@ +{ + "$schema": "http://json-schema.org/draft-07/schema", + "$id": "https://haystack.deepset.ai/json-schemas/haystack-pipeline-1.1.0.schema.json", + "title": "Haystack Pipeline", + "description": "Haystack Pipeline YAML file describing the nodes of the pipelines. For more info read the docs at: https://haystack.deepset.ai/components/pipelines#yaml-file-definitions", + "type": "object", + "oneOf": [ + { + "allOf": [ + { + "properties": { + "version": { + "const": "0.7" + } + } + }, + { + "$ref": "https://raw.githubusercontent.com/deepset-ai/haystack/master/json-schemas/haystack-pipeline-0.7.schema.json" + } + ] + }, + { + "allOf": [ + { + "properties": { + "version": { + "const": "1.1.0" + } + } + }, + { + "$ref": "https://raw.githubusercontent.com/deepset-ai/haystack/master/json-schemas/haystack-pipeline-1.1.0.schema.json" + } + ] + }, + { + "allOf": [ + { + "properties": { + "version": { + "const": "1.2.0rc0" + } + } + }, + { + "$ref": "https://raw.githubusercontent.com/deepset-ai/haystack/master/json-schemas/haystack-pipeline-1.2.0rc0.schema.json" + } + ] + } + ] +} \ No newline at end of file