From 9a7ae7dddb0e9d6f41e741e2b994a8d2a947ea4b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Enrique=20Garc=C3=ADa=20Cota?= Date: Mon, 8 Jun 2020 20:20:57 +0200 Subject: [PATCH] wip-schema --- kong/plugins/zipkin/schema.lua | 18 +++++++++++++++++- spec/schema_spec.lua | 20 ++++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 spec/schema_spec.lua diff --git a/kong/plugins/zipkin/schema.lua b/kong/plugins/zipkin/schema.lua index 82aefe6..bd46d2d 100644 --- a/kong/plugins/zipkin/schema.lua +++ b/kong/plugins/zipkin/schema.lua @@ -25,6 +25,21 @@ local static_tag = Schema.define { }, } +local validate_static_tags = function(tags) + if type(tags) ~= "table" then + return true + end + local found = {} + for i = 1, #tags do + local name = tags[i].name + if found[name] then + return nil, "repeating tags are not allowed: " .. name + end + found[name] = true + end + return true +end + return { name = "zipkin", fields = { @@ -40,7 +55,8 @@ return { { traceid_byte_count = { type = "integer", required = true, default = 16, one_of = { 8, 16 } } }, { header_type = { type = "string", required = true, default = "preserve", one_of = { "preserve", "b3", "b3-single", "w3c" } } }, - { static_tags = { type = "array", elements = static_tag } } + { static_tags = { type = "array", elements = static_tag, + custom_validator = validate_static_tags } } }, }, }, }, diff --git a/spec/schema_spec.lua b/spec/schema_spec.lua new file mode 100644 index 0000000..33d7e48 --- /dev/null +++ b/spec/schema_spec.lua @@ -0,0 +1,20 @@ +local schema_def = require "kong.plugins.zipkin.schema" +local v = require("spec.helpers").validate_plugin_config_schema + +describe("Plugin: Zipkin (schema)", function() + it("rejects repeated tags", function() + local ok, err = v({ + http_endpoint = "http://example.dev", + static_tags = { + { name = "foo", value = "bar" }, + { name = "foo", value = "baz" }, + }, + }, schema_def) + + assert.is_falsy(ok) + assert.same({ + static_tags = "repeated tags are not allowed: foo" + }, err) + end) +end) +