question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Teach ruleset yaml parser to support yaml anchors/merge keys

See original GitHub issue

Describe the bug The following yaml rule with anchor does not validate correctly

To Reproduce

  1. Go to online spectral parser
  2. paste the content of the following rules
rules:
  no-x-headers-request: &no-x-headers
    description: "All 'HTTP' headers SHOULD NOT include 'X-' headers (https://tools.ietf.org/html/rfc6648)."
    severity: warn
    given:
      - "$..parameters[?(@.in == 'header')].name"
    message: |-
      HTTP header '{{value}}' SHOULD NOT include 'X-' prefix in {{path}}
    recommended: true
    type: style
    then:
      function: pattern
      functionOptions:
        notMatch: "/^[xX]-/"
  no-x-headers-response:
    <<: *no-x-headers
    given:
      - $.[responses][*].headers.*~
    message: |-
      HTTP header '{{value}}' SHOULD NOT include 'X-' prefix in {{path}}

  1. See error
/rules/no-x-headers-response should have required property 'then'

Expected behavior No errors

Environment (remove any that are not applicable):

  • Version: spectral 5.1.0

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
P0lipcommented, Sep 28, 2020

@ioggstream I just wanted to let you know 5.6.0 is out with the support for YAML merge keys inside of rulesets.

0reactions
P0lipcommented, Sep 11, 2020

Faster: true (but on rule files I suspect the difference is minimal)

It depends, since we have these huge OAS2 and OAS3 JSON schemas referenced in our ruleset, so the impact might be a bit higher than it may seem. Moreover, the AsyncAPI ruleset references the AsyncAPI JSON schema which is not small either. Either way, shouldn’t be a big deal to use yaml everywhere.

Safer: not sure as the attacker controls the filename extension.

yeah, sure. Agreed.

Thanks again! This will DRY rule files where the same rule should be applied - with different error messages - to request parameters and response fields.

You are welcome. If there is any other spot that does not support merge keys, do let me know. We should be supporting them almost everywhere now.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Advanced YAML syntax cheatsheet
Schemas are rulesets in YAML 1.2 that tell the loader/parser what each part of your YAML file converts to in actual commands. They're ......
Read more >
YAML Anchor, Aliases and Merge Keys
Writing about YAML anchor, alias and merge-key in use for reference YAML nodes/data and YAML mixins.
Read more >
gitlab-ci.yaml syntax for merging YAML arrays (i.e., script or ...
Support merging rules arrays with !reference ... we can't read the anchors from the YAML parser, but the idea is to use some...
Read more >
Introduction to YAML Configurations
Merging maps. Anchors and aliases work for scalar values, but to save maps or sequences, use << to inject the alias.
Read more >
Is there YAML syntax for sharing part of a list or map?
The merge key type is probably what you want. It uses a special << mapping key to indicate merges, allowing an alias to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found