YAML Format

Learn YAML syntax for configuring your mocks in moclojer. Complete guide with practical examples and best practices.

YAML is the most common configuration format in moclojer. It's simple to read and write, requires no programming knowledge, and is perfect for defining mock APIs declaratively.

Why YAML?

Advantages:

  • βœ… Readable: Looks like English, easy to understand

  • βœ… Simple: Less verbose than JSON or XML

  • βœ… Structured: Maintains clear hierarchy

  • βœ… Comments: Can document inline

When to use YAML:

  • You're starting with moclojer

  • Need simple and clear configuration

  • Want to collaborate with non-programmers

  • Prefer readable configuration files

Basic Structure

Every moclojer YAML file is a list of endpoints:

- endpoint:
    method: GET
    path: /hello
    response:
      status: 200
      body: "Hello, World!"

- endpoint:
    method: POST
    path: /users
    response:
      status: 201
      body: '{"id": 1, "name": "Alice"}'

Anatomy:

  • Each endpoint starts with - endpoint:

  • Indentation with 2 spaces (not tabs!)

  • Required keys: path, response

  • Optional but recommended key: method

Essential YAML Syntax

1. Indentation

Indentation defines hierarchy:

⚠️ IMPORTANT:

  • Always use 2 spaces per level

  • Never mix spaces and tabs

  • Tools: configure your editor for "soft tabs"

2. Strings

Three ways to write strings:

For JSON in body, use >:

3. Numbers and Booleans

4. Lists

5. Objects (Maps)

6. Comments

Complete Annotated Example

Best Practices

βœ… Do

  1. Use consistent indentation (2 spaces)

  2. Add explanatory comments

  3. Use > for inline JSON

  4. Group related endpoints

  5. Order by method and path

❌ Avoid

  1. Tabs for indentation

  2. Unnecessary quotes

  3. JSON without >

  4. Endpoints without comments in large files

YAML Troubleshooting

Problem: "YAML parse error: mapping values are not allowed"

Cause: Colon without space or quotes

Problem: "YAML parse error: did not find expected key"

Cause: Incorrect indentation

Problem: Broken JSON in body

Cause: Didn't use > for multi-line

YAML Validation

Online

Editors

  • VS Code: "YAML" extension by Red Hat

  • Sublime: "YAML Nav" extension

  • Vim: "vim-yaml" plugin

Command line

Comparison with Other Formats

YAML vs JSON

YAML wins in:

  • Readability (50% fewer characters)

  • Native comments

  • Multi-line strings

JSON wins in:

  • Faster parsing

  • Universal support

YAML vs EDN

For most users, YAML is simpler. Use EDN only if you:

  • Work with Clojure

  • Need complex data structures

  • Want programmatic integration

See EDN Format Guide for details.

Next Steps

Now that you've mastered YAML, explore:

  1. Path Parameters - Dynamic parameters in URLs

  2. Template Variables - Dynamic responses

  3. Configuration Spec - Complete reference

See Also

Last updated

Was this helpful?