# yml/sort-keys

require mapping keys to be sorted

# 📖 Rule Details

This rule checks all pair definitions of mapping and verifies that all keys are sorted alphabetically or specified order.

Now loading...

# 🔧 Options

jsonc/sort-keys:
  - error
  # For example, a definition for eslintrc
  - pathPattern: ^$ # Hits the root properties
    order: 
      - root
      - plugins
      - extends
      - env
      - rules
      - overrides
      # ...
  - pathPattern: ^env$
    order: { type: asc }
  - pathPattern: ^rules$
    order: { type: asc }
  # ...

The option receives multiple objects with the following properties:

  • pathPattern (Required) ... Defines the regular expression pattern of paths to which you want to enforce the order. If you want to apply to the top level, define "^$".
  • hasProperties ... Defines an array of property names. Checks only objects that have the defined properties.
  • order (Required) ... Defines how to enforce the order. You can use an object or an array.
    • Array ... Defines an array of properties to enforce the order.
    • Object ... The object has the following properties:
      • type:
        • "asc" ... Enforce properties to be in ascending order. This is default.
        • "desc" ... Enforce properties to be in descending order.
      • caseSensitive ... If true, enforce properties to be in case-sensitive order. Default is true.
      • natural ... If true, enforce properties to be in natural order. Default is false.
  • minKeys ... Specifies the minimum number of keys that an object should have in order for the object's unsorted keys to produce an error. Default is 2, which means by default all objects with unsorted keys will result in lint errors.

You can also define options in the same format as the sort-keys (opens new window) rule.

jsonc/sort-keys:
  - error
  - asc
  - caseSensitive: true
    natural: false
    minKeys: 2

See here (opens new window) for details.

# 🚀 Version

This rule was introduced in eslint-plugin-yml v0.3.0

# 🔍 Implementation

Taken with ❤️ from ESLint core (opens new window)