From f1a21af4153c60124cf7504b753c284da12fda5f Mon Sep 17 00:00:00 2001 From: Andrea Frittoli Date: Wed, 15 May 2024 14:59:00 +0100 Subject: [PATCH] Fix custom schema and add validation The custom schema had a few issues to be fixed: - missing chainId and links from the context - missing version in the type regex - replace "." with "\." in the type regex This PR fixes all those and adds a conformance.json example file to validate against the custom schema. Fixes #216 Signed-off-by: Andrea Frittoli --- custom/conformance.json | 56 +++++++++++++++++++++++++++++++++++++++++ custom/schema.json | 9 ++++++- tools/validate.sh | 5 +++- 3 files changed, 68 insertions(+), 2 deletions(-) create mode 100644 custom/conformance.json diff --git a/custom/conformance.json b/custom/conformance.json new file mode 100644 index 00000000..9b94234f --- /dev/null +++ b/custom/conformance.json @@ -0,0 +1,56 @@ +{ + "context": { + "version": "0.5.0-draft", + "id": "271069a8-fc18-44f1-b38f-9d70a1695819", + "source": "/event/source/123", + "type": "dev.cdeventsx.mytool-resource.created.0.1.0", + "timestamp": "2023-03-20T14:27:05.315384Z", + "schemaUri": "https://myorg.com/schema/mytool", + "chainId": "6ca3f9c5-1cef-4ce0-861c-2456a69cf137", + "links": [ + { + "linkType": "RELATION", + "linkKind": "TRIGGER", + "target": { + "contextId": "5328c37f-bb7e-4bb7-84ea-9f5f85e4a7ce" + }, + "tags": { + "foo1": "bar", + "foo2": "bar" + } + }, { + "linkType": "PATH", + "from": { + "contextId": "5328c37f-bb7e-4bb7-84ea-9f5f85e4a7ce" + }, + "tags": { + "foo1": "bar", + "foo2": "bar" + } + }, { + "linkType": "END", + "from": { + "contextId": "5328c37f-bb7e-4bb7-84ea-9f5f85e4a7ce" + }, + "tags": { + "foo1": "bar", + "foo2": "bar" + } + } + ] + }, + "subject": { + "id": "pkg:resource/name@234fd47e07d1004f0aed9c", + "source": "/event/source/123", + "type": "mytool-resource", + "content": { + "user": "mybot-myapp", + "description": "a useful resource", + "nested": { + "key": "value", + "list": ["data1", "data2"] + } + } + } +} + \ No newline at end of file diff --git a/custom/schema.json b/custom/schema.json index 3648bdb8..806ebc2c 100644 --- a/custom/schema.json +++ b/custom/schema.json @@ -19,7 +19,7 @@ }, "type": { "type": "string", - "pattern": "^dev.cdeventsx.[a-zA-Z0-9]+-[a-zA-Z]+.[a-zA-Z]+$" + "pattern": "^dev\\.cdeventsx\\.[a-zA-Z0-9]+-[a-zA-Z]+\\.[a-zA-Z]+\\.[0-9]\\.[0-9]\\.[0-9]$" }, "timestamp": { "type": "string", @@ -29,6 +29,13 @@ "type": "string", "minLength": 1, "format": "uri" + }, + "chainId": { + "type": "string", + "minLength": 1 + }, + "links": { + "$ref": "links/embeddedlinksarray" } }, "additionalProperties": false, diff --git a/tools/validate.sh b/tools/validate.sh index 8015d247..83ce8e2c 100755 --- a/tools/validate.sh +++ b/tools/validate.sh @@ -57,7 +57,7 @@ while read -r schema; do done <<<$(find "${SCHEMAS_FOLDER}/links" -type f -name 'link*json' -maxdepth 1) # Test custom schema num_schemas=$(( num_schemas + 1 )) -ajv compile ${JSON_VALIDATOR_OPTIONS} -r "${EMBEDDED_LINKS_SCHEMAS}" -s custom/schema.json || schema_failed=$(( schema_failed + 1 )) +ajv compile ${JSON_VALIDATOR_OPTIONS} -r "${EMBEDDED_LINKS_SCHEMAS}" -s "${ROOT}/custom/schema.json" || schema_failed=$(( schema_failed + 1 )) echo "$(( num_schemas - schema_failed )) out of ${num_schemas} schemas are valid" @@ -77,6 +77,9 @@ find "${EXAMPLES_FOLDER}" -type f -name '*.json' | while read -r example; do printf "%s %s: " "${SUBJECT}" "${PREDICATE}" ajv validate ${JSON_VALIDATOR_OPTIONS} -r "${EMBEDDED_LINKS_SCHEMAS}" -s "${SCHEMA_FILE}" -d "${example}" || example_failed=$(( example_failed + 1 )) done +# Test custom example +num_examples=$(( num_examples + 1 )) +ajv validate ${JSON_VALIDATOR_OPTIONS} -r "${EMBEDDED_LINKS_SCHEMAS}" -s "${ROOT}/custom/schema.json" -d "${ROOT}/custom/conformance.json" || example_failed=$(( example_failed + 1 )) # Cleanup local schemas find "${ROOT}/schemas" -name '*.local' -exec rm {} +