Skip to main content
Redhat Developers  Logo
  • Products

    Featured

    • Red Hat Enterprise Linux
      Red Hat Enterprise Linux Icon
    • Red Hat OpenShift AI
      Red Hat OpenShift AI
    • Red Hat Enterprise Linux AI
      Linux icon inside of a brain
    • Image mode for Red Hat Enterprise Linux
      RHEL image mode
    • Red Hat OpenShift
      Openshift icon
    • Red Hat Ansible Automation Platform
      Ansible icon
    • Red Hat Developer Hub
      Developer Hub
    • View All Red Hat Products
    • Linux

      • Red Hat Enterprise Linux
      • Image mode for Red Hat Enterprise Linux
      • Red Hat Universal Base Images (UBI)
    • Java runtimes & frameworks

      • JBoss Enterprise Application Platform
      • Red Hat build of OpenJDK
    • Kubernetes

      • Red Hat OpenShift
      • Microsoft Azure Red Hat OpenShift
      • Red Hat OpenShift Virtualization
      • Red Hat OpenShift Lightspeed
    • Integration & App Connectivity

      • Red Hat Build of Apache Camel
      • Red Hat Service Interconnect
      • Red Hat Connectivity Link
    • AI/ML

      • Red Hat OpenShift AI
      • Red Hat Enterprise Linux AI
    • Automation

      • Red Hat Ansible Automation Platform
      • Red Hat Ansible Lightspeed
    • Developer tools

      • Red Hat Trusted Software Supply Chain
      • Podman Desktop
      • Red Hat OpenShift Dev Spaces
    • Developer Sandbox

      Developer Sandbox
      Try Red Hat products and technologies without setup or configuration fees for 30 days with this shared Openshift and Kubernetes cluster.
    • Try at no cost
  • Technologies

    Featured

    • AI/ML
      AI/ML Icon
    • Linux
      Linux Icon
    • Kubernetes
      Cloud icon
    • Automation
      Automation Icon showing arrows moving in a circle around a gear
    • View All Technologies
    • Programming Languages & Frameworks

      • Java
      • Python
      • JavaScript
    • System Design & Architecture

      • Red Hat architecture and design patterns
      • Microservices
      • Event-Driven Architecture
      • Databases
    • Developer Productivity

      • Developer productivity
      • Developer Tools
      • GitOps
    • Secure Development & Architectures

      • Security
      • Secure coding
    • Platform Engineering

      • DevOps
      • DevSecOps
      • Ansible automation for applications and services
    • Automated Data Processing

      • AI/ML
      • Data Science
      • Apache Kafka on Kubernetes
      • View All Technologies
    • Start exploring in the Developer Sandbox for free

      sandbox graphic
      Try Red Hat's products and technologies without setup or configuration.
    • Try at no cost
  • Learn

    Featured

    • Kubernetes & Cloud Native
      Openshift icon
    • Linux
      Rhel icon
    • Automation
      Ansible cloud icon
    • Java
      Java icon
    • AI/ML
      AI/ML Icon
    • View All Learning Resources

    E-Books

    • GitOps Cookbook
    • Podman in Action
    • Kubernetes Operators
    • The Path to GitOps
    • View All E-books

    Cheat Sheets

    • Linux Commands
    • Bash Commands
    • Git
    • systemd Commands
    • View All Cheat Sheets

    Documentation

    • API Catalog
    • Product Documentation
    • Legacy Documentation
    • Red Hat Learning

      Learning image
      Boost your technical skills to expert-level with the help of interactive lessons offered by various Red Hat Learning programs.
    • Explore Red Hat Learning
  • Developer Sandbox

    Developer Sandbox

    • Access Red Hat’s products and technologies without setup or configuration, and start developing quicker than ever before with our new, no-cost sandbox environments.
    • Explore Developer Sandbox

    Featured Developer Sandbox activities

    • Get started with your Developer Sandbox
    • OpenShift virtualization and application modernization using the Developer Sandbox
    • Explore all Developer Sandbox activities

    Ready to start developing apps?

    • Try at no cost
  • Blog
  • Events
  • Videos

How to configure YAML schema to make editing files easier

November 25, 2020
Aurélien Pupier
Related topics:
Developer ToolsIDEs

Share:

    YAML is a friendly data serialization standard that works with all programming languages. While configuration files are often defined in YAML, it can even be used as a programming language, like the workflow language at Google, or Apache Camel K.

    It has the advantage of not having any braces, making it lightweight visually. One of the drawbacks is that editing YAML files may not always be easy. For instance, writing a tag at the wrong indentation level can be hard to detect. To help with editing, it is possible to provide a YAML schema that can be leveraged by a large set of integrated development environments (IDEs). Unfortunately, this practice is not widespread. Consequently, users waste time searching for a missing or extra space and browsing documentation.

    In this article, you will discover the benefits of providing a YAML schema and how to make it consumable for all your users, making it easier to edit YAML files.

    YAML schema

    Providing a schema describing the structure of a YAML file type is possible. You might not find the specification easily because it relies on the JSON Schema specifications. YAML schemas allow several IDEs and editors to provide code completion, documentation on hover, and validation of the file, including Eclipse Desktop IDE, Eclipse Che, VS Code, IntelliJ, Emacs, and vim.

    For instance, see the code completion example in VS Code for an Apache Camel K integration file in Figure 1.

    VS Code showing code completion in the file test.camelk.yaml.
    Figure 1: Code faster with the code completion provided by your YAML schema.

    Figure 2 shows an example of file validation.

    VS Code showing a validation error in the file test.camelk.yaml
    Figure 2: Validate your file against your YAML schema to ensure that it has the right structure.

    Figure 3 shows an example of documentation on hover.

    Documentation on hover in a kustomization.yaml file
    Figure 3: Hover over a property to see a description.

    Schema registry

    I recommend that you register the schema in the JSON Schema store which is, as far as I know, the largest registry of JSON and YAML schemas. The JSON Schema store is used directly or through the YAML Language Server in several IDEs. It allows an automatic binding of YAML files with their corresponding schema. The biggest advantage is that it is completely transparent for the end user.

    To benefit from the JSON Schema store and the built-in integration offered by IDEs, the YAML schema writer needs to set a file name pattern for the YAML files and the schema's users need to respect that pattern. For instance, you can use this kind of file pattern *.myId.yaml. Take care to avoid collision with the existing file name pattern. Colliding with the existing file name patterns will complicate things for your users.

    Schema association without a file name pattern

    If you do not want to impose a file name pattern, two possibilities remain that benefit from the schema association. Using one of these other methods will add extra effort either for the user or during your tooling maintenance.

    Possibility 1: Specific IDE schema association

    Most IDEs provide ways to associate a schema to a specific file via preferences. In this case, the burden is on your user.

    It is also possible to write a specific extension to auto-associate the schema. The drawback is that specific IDE extensions must be written for each IDE that you want to support. Hopefully, for some of them, there is a specific API to do it. For instance, vscode-yaml is one way you could accomplish this.

    Possibility 2: Using in-file declarations

    It is possible to use inline metadata to specify the schema corresponding to the file. The following modeline-like format must be used:

    # yaml-language-server: $schema=https://my.url.to/the/schema

    The main drawback is that this option places a burden on your users. This annotation is also specific to yaml-language-server and not part of the specifications. If you think it would be nice to have it directly inside the YAML specification, please cast your vote here.

    The main advantage is that it is available under the hood with all recent IDEs by using the YAML Language Server.

    Summary

    Providing a YAML schema provides a tremendous benefit to your users. Now you know multiple ways to make your YAML schema easily accessible to all of your users, and make editing these YAML files easier. From here, I hope to see a huge increase in registered schemas on the Schema store!

    Last updated: January 25, 2021

    Recent Posts

    • Integrate Red Hat AI Inference Server & LangChain in agentic workflows

    • Streamline multi-cloud operations with Ansible and ServiceNow

    • Automate dynamic application security testing with RapiDAST

    • Assessing AI for OpenShift operations: Advanced configurations

    • OpenShift Lightspeed: Assessing AI for OpenShift operations

    Red Hat Developers logo LinkedIn YouTube Twitter Facebook

    Products

    • Red Hat Enterprise Linux
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform

    Build

    • Developer Sandbox
    • Developer Tools
    • Interactive Tutorials
    • API Catalog

    Quicklinks

    • Learning Resources
    • E-books
    • Cheat Sheets
    • Blog
    • Events
    • Newsletter

    Communicate

    • About us
    • Contact sales
    • Find a partner
    • Report a website issue
    • Site Status Dashboard
    • Report a security problem

    RED HAT DEVELOPER

    Build here. Go anywhere.

    We serve the builders. The problem solvers who create careers with code.

    Join us if you’re a developer, software engineer, web designer, front-end designer, UX designer, computer scientist, architect, tester, product manager, project manager or team lead.

    Sign me up

    Red Hat legal and privacy links

    • About Red Hat
    • Jobs
    • Events
    • Locations
    • Contact Red Hat
    • Red Hat Blog
    • Inclusion at Red Hat
    • Cool Stuff Store
    • Red Hat Summit

    Red Hat legal and privacy links

    • Privacy statement
    • Terms of use
    • All policies and guidelines
    • Digital accessibility

    Report a website issue