Webthing-CPP: a modern CPP implementation of the WebThings API


LCOV - code coverage report
Current view: top level - webthing - json_validator.hpp (source / functions) Coverage Total Hit
Test: filtered_coverage.info Lines: 100.0 % 7 7
Test Date: 2025-03-15 12:45:00 Functions: 83.3 % 6 5

            Line data    Source code
       1              : // Webthing-CPP
       2              : // SPDX-FileCopyrightText: 2023-present Benno Waldhauer
       3              : // SPDX-License-Identifier: MIT
       4              : 
       5              : #pragma once
       6              : 
       7              : #include <bw/webthing/errors.hpp>
       8              : #include <bw/webthing/json.hpp>
       9              : 
      10              : #ifdef WT_USE_JSON_SCHEMA_VALIDATION
      11              : 
      12              :     #include <nlohmann/json-schema.hpp>
      13              : 
      14              : #endif
      15              : 
      16              : namespace bw::webthing {
      17              : 
      18              : template<class T>
      19           46 : void validate_value_by_scheme(const T& value, json schema)
      20              : {
      21              : #ifdef WT_USE_JSON_SCHEMA_VALIDATION
      22              : 
      23           46 :     nlohmann::json_schema::json_validator validator;
      24           46 :     validator.set_root_schema(schema);
      25              :     try
      26              :     {
      27           61 :         validator.validate(value);
      28              :     }
      29           30 :     catch(std::exception& ex)
      30              :     {
      31           45 :         throw InvalidJson(ex.what());
      32              :     }
      33              : 
      34              : #else
      35              :     // always succeed
      36              : #endif
      37           46 : }
      38              : 
      39              : } // bw::webthing
        

Generated by: LCOV version 2.0-1