You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

64 lines
2.1 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
  5. *
  6. * This program is free software: you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the
  8. * Free Software Foundation, either version 3 of the License, or (at your
  9. * 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. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <fstream>
  20. #include <wx/filename.h>
  21. #include <wx/log.h>
  22. #include <json_schema_validator.h>
  23. #include <locale_io.h>
  24. JSON_SCHEMA_VALIDATOR::JSON_SCHEMA_VALIDATOR( const wxFileName& aSchemaFile )
  25. {
  26. std::ifstream schema_stream( aSchemaFile.GetFullPath().fn_str() );
  27. nlohmann::json schema;
  28. try
  29. {
  30. // For some obscure reason on MINGW, using UCRT option,
  31. // m_schema_validator.set_root_schema() hangs without switching to locale "C"
  32. #if defined(__MINGW32__) && defined(_UCRT)
  33. LOCALE_IO dummy;
  34. #endif
  35. schema_stream >> schema;
  36. m_validator.set_root_schema( schema );
  37. }
  38. catch( std::exception& e )
  39. {
  40. if( !aSchemaFile.FileExists() )
  41. {
  42. wxLogError( wxString::Format( _( "schema file '%s' not found" ),
  43. aSchemaFile.GetFullPath() ) );
  44. }
  45. else
  46. {
  47. wxLogError( wxString::Format( _( "Error loading schema: %s" ), e.what() ) );
  48. }
  49. }
  50. }
  51. nlohmann::json JSON_SCHEMA_VALIDATOR::Validate( const nlohmann::json& aJson,
  52. nlohmann::json_schema::error_handler& aErrorHandler,
  53. const nlohmann::json_uri& aInitialUri ) const
  54. {
  55. return m_validator.validate( aJson, aErrorHandler, aInitialUri );
  56. }