23andMe / Yamale

A schema and validator for YAML.
MIT License
666 stars 88 forks source link

Alphabetical Order validator #209

Closed PatrickDuncan closed 1 year ago

PatrickDuncan commented 1 year ago

It would be nice if there was an alphabetical order validator for maps.

Success

animals:
  rabbit: "fast"
  turtle: "slow"

Fail

animals:
  turtle: "slow"
  rabbit: "fast"

Fail

vehicles: 4
animals:
  rabbit: "fast"
  turtle: "slow"
mildebrandt commented 1 year ago

Hi! I no longer maintain Yamale, but I can point out that the yaml spec says that keys in a mapping have no order: https://yaml.org/spec/1.2.2/#3221-mapping-key-order

Since Yamale doesn't actually do the deserialization of the yaml, Yamale couldn't guarantee that it gets the proper order from the library when validation starts.

There are workarounds for each library, but my feeling is that a sequence should be used if you want to maintain order in your yaml.

PatrickDuncan commented 1 year ago

@mildebrandt Thanks, that limitation makes sense. I think using an array (sequence) has too many tradeoffs with readability and the chance of duplicates with my usecase. I don't think Yamale has a validator for duplicate entries in arrays.

I could create a custom script that does some form of validation.