adrienverge / yamllint

A linter for YAML files.
GNU General Public License v3.0
2.83k stars 269 forks source link

How do I validate a key must contain something ? #543

Closed aruncjee closed 1 year ago

aruncjee commented 1 year ago

Hello guys,

I'm looking to see if yamllint has such feature.

I want to check each and every key contains or ends with something. Like an e-mail address.

For example: "@foo.com" It must end with it.

Are there any options for it?

Let me know, please.

Thanks, Arun.

andrewimeson commented 1 year ago

Check out #392

You could possibly do this with JSON Schemas instead.

andrewimeson commented 1 year ago

This wouldn't give you the failing entry, but here's a simple validation with yq and bash

---
people:
  - name: bob
    email: bob@example.com
  - name: alice
    email: alice@example.com
  - name: mallory
    email: send me a fax please
#!/usr/bin/env bash
set -euo pipefail

validate() {
    local -a _files=("$@")
    local _result
    _result=$(cat "${_files[@]}" |
        yq '.people.[].email | test("@example.com")' |
        grep -c false || [[ $? == 1 ]])

    if ((_result > 0)); then
        echo failed email validation
        return 1
    fi
}

validate "$@"
adrienverge commented 1 year ago

Hello,

@andrewimeson is right: JSON Schemas could help here :+1:

Yamllint aims to validate the form, not the content or its structure.