docs/ref/modules/utils/schema-validator/README.md
The Schema Validator is a shared module that validates JSON messages against Wazuh-indexer index template mappings. It ensures that data sent to the Wazuh indexer conforms to the expected schema, preventing indexing errors and maintaining data integrity across Wazuh components.
The validator supports Wazuh-indexer mapping syntax including all data types, nested objects, and strict validation mode. It provides detailed error messages for debugging and integrates seamlessly with multiple Wazuh modules (FIM, SCA, Syscollector).
The module follows a factory pattern with three main components:
┌─────────────────────────────────────┐
│ SchemaValidatorFactory │
│ (Singleton) │
│ │
│ + getInstance() │
│ + initialize() │
│ + getValidator(indexPattern) │
│ + isInitialized() │
└─────────────┬───────────────────────┘
│
│ manages
▼
┌─────────────────────────────────────┐
│ ISchemaValidatorEngine │
│ (Interface) │
│ │
│ + validate(message) │
│ + getSchemaName() │
└─────────────┬───────────────────────┘
│
│ implements
▼
┌─────────────────────────────────────┐
│ SchemaValidatorEngine │
│ (Concrete Implementation) │
│ │
│ + loadSchemaFromString() │
│ + validate(message) │
│ + getSchemaName() │
└─────────────────────────────────────┘
Each Wazuh module integrates with the Schema Validator independently:
┌────────────┐ ┌────────────┐ ┌──────────────┐
│ FIM │ │ SCA │ │ Syscollector │
└─────┬──────┘ └─────┬──────┘ └──────┬───────┘
│ │ │
└───────────────┴────────────────┘
│
┌───────────▼────────────┐
│ SchemaValidatorFactory│
│ (Singleton) │
└───────────┬────────────┘
│
┌───────────▼───────────┐
│ Embedded Schemas │
│ - wazuh-states-* │
└───────────────────────┘
The validator supports schemas for all Wazuh state indices:
wazuh-states-inventory-hardwarewazuh-states-inventory-systemwazuh-states-inventory-networkwazuh-states-inventory-packageswazuh-states-inventory-hotfixeswazuh-states-inventory-portswazuh-states-inventory-processeswazuh-states-scawazuh-states-fim-filewazuh-states-fim-registry#include "schemaValidator.hpp"
// 1. Initialize the factory (once during module startup)
auto& validatorFactory = SchemaValidator::SchemaValidatorFactory::getInstance();
if (validatorFactory.initialize())
{
m_logFunction(LOG_INFO, "Schema validator initialized successfully");
}
// 2. Get a validator for a specific index
auto validator = validatorFactory.getValidator("wazuh-states-inventory-packages");
if (validator)
{
// 3. Validate a JSON message
std::string jsonMessage = R"({
"agent": {"id": "001"},
"package": {"name": "nginx", "version": "1.18.0"}
})";
auto result = validator->validate(jsonMessage);
if (result.isValid)
{
// Message is valid, proceed with indexing
sendToIndexer(jsonMessage);
}
else
{
// Validation failed, log errors
for (const auto& error : result.errors)
{
m_logFunction(LOG_ERROR, "Validation error: " + error);
}
// Delete from local DB to prevent integrity loops
deleteFromLocalDatabase(data);
}
}
#include "schemaValidator_c.h"
// 1. Initialize the factory
if (schema_validator_initialize())
{
minfo("Schema validator initialized successfully");
}
// 2. Validate a message
char* errorMessage = NULL;
const char* index = "wazuh-states-fim-file";
const char* message = "{\"file\":{\"path\":\"/etc/passwd\"}}";
if (schema_validator_validate(index, message, &errorMessage))
{
// Message is valid
send_to_indexer(message);
}
else
{
// Validation failed
if (errorMessage)
{
merror("Validation failed: %s", errorMessage);
free(errorMessage);
}
delete_from_database(data);
}
initialize() once during module startupisInitialized() before getting validatorsThe module gracefully handles initialization and validation failures:
isInitialized() returns falsegetValidator() returns nullptrValidationResult.isValid is falseValidationResult.errors contains detailed error messages| Module | Integration Status | Documentation |
|---|---|---|
| Syscollector | Integrated | Architecture |
| SCA | Integrated | Architecture |
| FIM | Integrated | Architecture |
The Schema Validator is built as part of the Wazuh build system:
make TARGET=server|agent <DEBUG=1>
Schema resources are automatically embedded during the build process using CMake.
Run unit tests with:
cd src/build
ctest -L schema_validator -V