Replies: 1 comment
-
It's possible to check the individual YAML tokens when the SLS file doesn't include any Jinja. The example below will find numbers starting with '0' that aren't enclosed in quotation marks. The would exclude the false positives discovered in #293 and #299 but requires filtering the Jinja statements first. import re
import yaml
from saltlint.linter.rule import Rule
from saltlint.utils import LANGUAGE_SLS
class YamlImplicitOctalValueRule(Rule):
id = '210'
shortdesc = "Numbers that start with '0' should always be encapsulated in quotation marks"
description = "Numbers that start with '0' should always be encapsulated in quotation marks"
severity = 'HIGH'
languages = [LANGUAGE_SLS]
tags = ['formatting']
version_added = 'develop'
regex = re.compile(r"^0\d*$")
def matchtext(self, file, text):
results = []
yaml_loader = yaml.BaseLoader(text)
try:
previous = None
current = yaml_loader.get_token()
while current is not None:
next = yaml_loader.get_token()
# Match YAML scalar (values) tokens and check the token style to verify
# the value isn't enclosed in quotes.
if isinstance(current, yaml.tokens.ScalarToken) and not current.style:
# Check if the whole value is a number starting with '0'.
if self.regex.match(current.value):
line_no = 0 # TODO: get line number, for now just return 0
line = current.value # TODO: get line, for now just return the value
results.append((line_no, line, self.shortdesc))
previous = current
current = next
except yaml.scanner.ScannerError:
pass
return results |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Due to the usage of Jinja it's not always possible to validate the YAML syntax in state files. It would be nice if the YAMLlint rules could also be used to validate the state files.
SaltStack does include the option to lint the YAML output after detecting a successful render using salt.modules.yaml (new in version 3005).
Related issues:
Beta Was this translation helpful? Give feedback.
All reactions