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

Taplo Schema Support #113

Open
2 tasks
joshmedeski opened this issue May 20, 2024 · 0 comments
Open
2 tasks

Taplo Schema Support #113

joshmedeski opened this issue May 20, 2024 · 0 comments
Assignees
Labels
enhancement New feature or request

Comments

@joshmedeski
Copy link
Owner

joshmedeski commented May 20, 2024

What would you like sesh to do?

As a user, I want taplo to offer my schema definition, so I can more easily create and modify sesh configuration.

  • Create schema file
  • Integrate with Neovim

Create schema file

To create a schema definition for a TOML file using Taplo, follow these steps:

  1. Create a new file with a .toml extension for your schema, e.g., schema.toml.

  2. Open the schema.toml file and define your schema using the Taplo schema language. Here's an example schema:

[TOML schema]
title = "My Schema"
description = "A schema for my TOML configuration file"

[section1]
key1 = { type = "string", required = true }
key2 = { type = "integer", minimum = 0, maximum = 100 }

[section2]
key3 = { type = "boolean" }
key4 = { type = "array", items = { type = "string" } }

In this example:

  • The schema has a title and description.
  • section1 defines two keys: key1 (required string) and key2 (integer between 0 and 100).
  • section2 defines two keys: key3 (boolean) and key4 (array of strings).
  1. Save the schema.toml file.

  2. In your main TOML configuration file, add a reference to the schema file using the __schema key:

__schema = "schema.toml"

[section1]
key1 = "value1"
key2 = 50

[section2]
key3 = true
key4 = ["item1", "item2"]
  1. Taplo will now validate your TOML configuration file against the defined schema.

For more information on Taplo's schema language and available validation options, refer to the [Taplo documentation](https://taplo.tamasfe.dev/configuration/schema.html).

Integrate with brew

Yes, you can configure the schema file to be installed via Homebrew and automatically detected based on the file's location. Here's how you can achieve this:

  1. Create a Homebrew formula for your schema file. Let's say you have a schema file named myapp.toml.schema and you want to install it using Homebrew. Create a new file named myapp-schema.rb with the following content:
class MyappSchema < Formula
  desc "Schema for MyApp TOML configuration"
  homepage "https://example.com/myapp"
  url "https://example.com/myapp/myapp.toml.schema"
  sha256 "SCHEMA_FILE_SHA256_HASH"

  def install
    share.install "myapp.toml.schema"
  end
end

Replace SCHEMA_FILE_SHA256_HASH with the actual SHA256 hash of your schema file.

  1. Install the schema using Homebrew:
brew install ./myapp-schema.rb

This will install the schema file in the Homebrew shared directory, typically /usr/local/share/myapp.toml.schema.

  1. In your application, you can automatically detect the schema file based on its location. Taplo supports schema auto-discovery by looking for schema files in specific locations relative to the TOML file being validated.

By default, Taplo looks for schema files in the following locations (in order):

  • <filename>.schema.json
  • <filename>.schema.toml
  • <filename>.schema.yaml
  • ./<filename>.json
  • ./<filename>.toml
  • ./<filename>.yaml
  • $/schema/<filename>.json
  • $/schema/<filename>.toml
  • $/schema/<filename>.yaml

Where <filename> is the name of your TOML configuration file.

  1. To use the installed schema file, you can symlink it to one of the supported locations. For example, if your TOML configuration file is named config.toml, you can create a symlink in the same directory:
ln -s /usr/local/share/myapp.toml.schema config.toml.schema.toml

Now, when Taplo validates your config.toml file, it will automatically discover and use the schema file installed via Homebrew.

By following these steps, you can install the schema file using Homebrew and have Taplo automatically detect and use it based on the file's location.

@joshmedeski joshmedeski added the enhancement New feature or request label May 20, 2024
@joshmedeski joshmedeski self-assigned this May 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
Status: Backlog
Development

No branches or pull requests

1 participant