Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(db): add examples to metaschema fields #11731

Merged
merged 1 commit into from
Nov 6, 2023
Merged

Conversation

Guaris
Copy link
Contributor

@Guaris Guaris commented Oct 10, 2023

Summary

This pull request introduces a new field attribute called examples in the metaschema. The examples attribute is designed to provide example values for fields in schemas. examples is an array so you can specify multiple example values for a single field.

Adding examples would like this:

kong/db/schema/typedefs.lua

typedefs.certificate = Schema.define {
    type = "string",
    custom_validator = validate_certificate,
    description = "A string representing a certificate.",
    examples = {
        "-----BEGIN CERTIFICATE-----\nYourCertificateDataHere\n-----END CERTIFICATE-----",
        "-----BEGIN CERTIFICATE-----\nAnotherCertificateDataHere\n-----END CERTIFICATE-----"
    }
}

and

typedefs.auto_timestamp_s = Schema.define {
  type = "integer",
  timestamp = true,
  auto = true,
  description = "An integer representing an automatic Unix timestamp in seconds.",
  examples = { 1633924743 }
}

But also in plugin schemas, examples help illustrate what the data is supposed to look like.
For example:
kong/plugins/response-ratelimiting/schema.lua
for redis_port

{
  redis_port = typedefs.port({
    default = 6379,
    description = "When using the `redis` policy, this property specifies the port of the Redis server.",
    examples = { 6379 }
  }),
},

or redis_password

{
  redis_password = {
    description =
      "When using the `redis` policy, this property specifies the password to connect to the Redis server.",
    type = "string",
    len_min = 0,
    referenceable = true,
    examples = { "myredispassword" }
  },
},

Why

Improved automation

We've already invested in automation for generating API specs and the plugin configuration reference. The inclusion of example fields is a natural extension of this effort in my eyes. By providing concrete examples in the Lua schemas, we can generate more informative and accurate specs/docs automatically.

Communication

Examples illustrate what data should look like, this makes it easier for people to understand the intended structure and format of fields in our schemas.

Plugins

Currently, we offer descriptions, sometimes default values in our plugin configuration reference docs. We could add examples and sometimes we do within the description field, but that information grows stale. My theory is that providing accurate examples in the schemas, and subsequently in the docs, would help users onboard with plugins.

@Guaris Guaris changed the title Add examples to metaschema fields Feat: Add examples to metaschema fields Oct 11, 2023
@jschmid1 jschmid1 requested review from jschmid1 and hbagdi October 16, 2023 08:20
@kikito kikito force-pushed the feat/example-field branch from c332875 to 6365b6d Compare October 31, 2023 09:37
@kikito
Copy link
Member

kikito commented Oct 31, 2023

We will want to backport this (the field definition, not the contents of the field) to older versions of the gateway in order to facilitate backports.

@jschmid1 jschmid1 changed the title Feat: Add examples to metaschema fields feat/add examples to metaschema fields Nov 2, 2023
@jschmid1 jschmid1 changed the title feat/add examples to metaschema fields feat(db): add examples to metaschema fields Nov 2, 2023
@jschmid1 jschmid1 merged commit 1771397 into master Nov 6, 2023
27 checks passed
@jschmid1 jschmid1 deleted the feat/example-field branch November 6, 2023 10:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants