Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to validate yaml using yq?

Tags:

validation

yq

I know there are at least two yq, I'm using this one: https://github.com/mikefarah/yq. According to the doc, I can validate a yaml file like this:

yq -v file.yaml

The problem: it outputs a lot of debug info however according to the doc, it should outputs "nothing if [the file] valid, otherwise it will print errors". As far as I see, the file looks valid.

How should I avoid output if the file is valid then?

The file:

name: Copywrite

on:
  push:
    tags:
      - v*
    branches:
      - main
  pull_request:
  workflow_dispatch:

jobs:
  copywrite:
    name: Run Header Copyright
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@93ea575cc8 # v3.1.0

      - name: Install Copywrite
        id: install
        uses: hashicorp/[email protected]

      - name: Output Installed Copywrite Version
        run: echo "Installed Copywrite CLI ${{steps.install.outputs.version}}"

      - name: Run Copywrite Header Compliance
        run: copywrite headers --plan

The output:

yq -v ./file.yml
16:52:54 processArgs [DEBU] processed args: [./file.yml]
16:52:54 maybeFile [DEBU] checking './file.yml' is a file
16:52:54 maybeFile [DEBU] error: <nil>, dir: false
16:52:54 maybeFile [DEBU] result: true
16:52:54 FormatFromFilename [DEBU] checking file extension './file.yml' for auto format detection
16:52:54 FormatFromFilename [DEBU] detected format 'yml'
16:52:54 initCommand [DEBU] Using input format yml
16:52:54 initCommand [DEBU] Using output format yml
16:52:54 ParseExpression [DEBU] Parsing expression: []
16:52:54 ConvertToPostfix [DEBU] postfix processing currentToken )
16:52:54 ConvertToPostfix [DEBU] opstackLen: 0
16:52:54 ConvertToPostfix [DEBU] PostFix Result:
16:52:54 GetMatchingNodes [DEBU] getMatchingNodes - nothing to do
16:52:54 PrintResults [DEBU] PrintResults for 1 matches
16:52:54 PrintResults [DEBU] -- print sep logic: p.firstTimePrinting: false, previousDocIndex: 0, mappedDoc.Document: 0
16:52:54 PrintResults [DEBU] D0, P[], (doc)::name: Copywrite
on:
    push:
        tags:
            - v*
        branches:
            - main
    pull_request:
    workflow_dispatch:
jobs:
    copywrite:
        name: Run Header Copyright
        runs-on: ubuntu-latest
        steps:
            - name: Checkout
              uses: actions/checkout@93ea575cc8 # v3.1.0
            - name: Install Copywrite
              id: install
              uses: hashicorp/[email protected]
            - name: Output Installed Copywrite Version
              run: echo "Installed Copywrite CLI ${{steps.install.outputs.version}}"
            - name: Run Copywrite Header Compliance
              run: copywrite headers --plan
name: Copywrite
on:
  push:
    tags:
      - v*
    branches:
      - main
  pull_request:
  workflow_dispatch:
jobs:
  copywrite:
    name: Run Header Copyright
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@93ea575cc8 # v3.1.0
      - name: Install Copywrite
        id: install
        uses: hashicorp/[email protected]
      - name: Output Installed Copywrite Version
        run: echo "Installed Copywrite CLI ${{steps.install.outputs.version}}"
      - name: Run Copywrite Header Compliance
        run: copywrite headers --plan
16:52:54 PrintResults [DEBU] done printing results
like image 576
Putnik Avatar asked Oct 14 '25 09:10

Putnik


1 Answers

Firstly, the documentation uses v as command (no dash), you are trying to use it as flag -v. Secondly, you are referencing a documentation for version 3.x but your output suggests you are using version 4.x.

With version 3.x the following works, and gives no output if the file is valid:

yq v file.yaml
# or
yq validate file.yaml

With version 4.x (where -v and --verbose stand for verbose mode), you could simply feed the file into yq (defaulting to the idempotent filter .), and suppress stdout. Possible errors will come through on stderr.

yq file.yaml >/dev/null
like image 171
pmf Avatar answered Oct 18 '25 12:10

pmf



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!