Skip to content

Conversation

@hughcars
Copy link
Collaborator

@hughcars hughcars commented Jan 28, 2026

Note: this PR is part of a series broadly aimed at refactor config file to make it simpler to use, and drive towards a simpler testing experience.

This PR introduces a dependency on json-schema-validator and uses this to allow checking a config against the schema directly within c++. It avoids the need for a separate tracking of the schema by embedding it within the executable, to avoid the need for file reading. This adds ~30kB to the binary, but makes it effectively self describing. It effectively adds a set of redundant checks, given many of these constraints are checked during json reading, but this will enable a follow up PR that removes these checks whilst simplifying configfile.hpp and configfile.cpp.

The validation can be done wholesale, where a top level json is provided, or using a keyword, to test only against a subset of the json. Additionally it was necessary to downgrade the schema version we are using, to match what the validator library was capable of, luckily the schema we have is actually already compliant with that version (with 1 tiny exception), so this required minimal changes other than the tag.

The error reporting from the schema follows the documentation ["Key1"]["Key2"] style of printing, and should also print all errors it finds in the config at once, rather than failing at the first it finds and then erroring out. So a user should get a more complete description of the fixes they need to make, without retrying so much.

The check config github action will eventually become redundant and be removed, but for now I am leaving it in as a follow up PR hardens the schema to cover as much as possible.

  • Rebase on main
  • Update CHANGELOG with PR number

@hughcars hughcars force-pushed the hughcars/json-schema-validator branch 3 times, most recently from d056766 to f449749 Compare January 28, 2026 20:44
- Downgrade schema files to JSON Schema draft-07 for library compatibility
- Add nlohmann/json-schema-validator v2.4.0 as external dependency
- Integrate schema validation into IoData config file parsing
- Add ValidateConfig() function to validate JSON against schema
- Add unit tests for schema validation of example configs
- New jsonschema.hpp/cpp with ValidateConfig overloads:
  - ValidateConfig(config, schema_dir) for full config validation
  - ValidateConfig(config, schema_dir, key) for sub-schema validation by key
- DFS search through schema to find named sub-schema (e.g., "LumpedPort")
- Preserves $defs for proper $ref resolution in sub-schemas
- Add comprehensive test cases for sub-schema validation
- Replace commented-out C++ validation tests with schema-based tests
- Fix test-geodata.cpp: use attribute 1 instead of 0
Add custom error handler that formats paths in documentation style:
  At ["Boundaries"]["LumpedPort"][0]: error message

Instead of JSON pointer format:
  /Boundaries/LumpedPort/0
- Add cmake/EmbedSchema.cmake to generate embedded_schema.hpp at build time
- Convert all schema JSON files to C++ string literals
- Remove runtime file dependency - schema always available
- Remove PALACE_SCHEMA_DIR compile definition and schema install
- Update ValidateConfig() API to not require schema_dir parameter
- Add test to verify embedded schema matches source files
@hughcars hughcars force-pushed the hughcars/json-schema-validator branch from f449749 to 605138a Compare January 28, 2026 23:00
- Add schema files as CMake configure dependencies for automatic regeneration
- Log warning when $ref resolution fails in FindSchemaByKey
- Add descriptive comment to generated embedded_schema.hpp
- Add test for config file with comments (preprocessing integration)
- Extract root schema filename to constexpr variable
- Detect ambiguous schema keys and warn (return null instead of arbitrary match)
- Extract ResolveRef helper to simplify FindEnumInSchema
- Add comment noting schemas use JSON Schema draft-07
@hughcars hughcars force-pushed the hughcars/json-schema-validator branch from 605138a to 5d36b0f Compare January 29, 2026 15:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant