Coverage for lobster/common/yaml_validation.py: 100%

9 statements  

« prev     ^ index     » next       coverage.py v7.17.3, created at 2026-11-13 15:05 +0000

1#!/usr/bin/env python3 

2# 

3# LOBSTER - Lightweight Open BMW Software Traceability Evidence Report 

4# Copyright (C) 2026 Bayerische Motoren Werke Aktiengesellschaft (BMW AG) 

5# 

6# This program is free software: you can redistribute it and/or modify 

7# it under the terms of the GNU Affero General Public License as 

8# published by the Free Software Foundation, either version 3 of the 

9# License, or (at your option) any later version. 

10# 

11# This program is distributed in the hope that it will be useful, but 

12# WITHOUT ANY WARRANTY; without even the implied warranty of 

13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 

14# Affero General Public License for more details. 

15# 

16# You should have received a copy of the GNU Affero General Public 

17# License along with this program. If not, see 

18# <https://www.gnu.org/licenses/>. 

19 

20# Shared yamale-based validation for LOBSTER's YAML config files. 

21 

22from pathlib import Path 

23 

24import yamale 

25 

26 

27def validate_yaml_file(schema_file, filename: str): 

28 """Validates a YAML file against a yamale schema file.""" 

29 schema = yamale.make_schema(schema_file) 

30 # Read as UTF-8 ourselves, since yamale would use the platform's default encoding. 

31 # The resolved path is attached so that yamale prints it in validation errors. 

32 resolved = str(Path(filename).resolve()) 

33 with open(filename, "r", encoding="UTF-8") as f: 

34 content = f.read() 

35 data = [(item, resolved) for item, _ in yamale.make_data(content=content)] 

36 yamale.validate(schema, data)