-
Notifications
You must be signed in to change notification settings - Fork 0
Description
features that will require re-scoping and planning.
There's a definitive profile configuration in use for using yaml files in general, the question here is would it be a good idea to support validation of that profile... Especially in the case where profiles are used in configuration
Pros of using a validation profile for YAML:
- Data Consistency: Ensures your YAML documents adhere to a predefined structure and expected data types, reducing errors and inconsistencies.
- Early Error Detection: Catches invalid data early in your workflow, preventing issues from propagating to later stages of your application.
- Improved Code Maintainability: A clear YAML schema makes it easier to understand the structure and purpose of your data, simplifying code maintenance and updates.
- Enhanced Security: Validation can help prevent malformed or malicious YAML data from being processed, improving the security of your application.
- Documentation: A well-defined schema serves as documentation for the expected format of your YAML data.
Cons of using a validation profile for YAML:**
- Development Overhead: Creating and maintaining a validation profile, especially using custom functions, requires development time and effort.
- Potential for Rigidity: Strict schemas can sometimes be inflexible, making it harder to adapt to evolving data requirements.
- Limited Tooling (in some cases): The availability of mature YAML schema validation libraries and tools might be less extensive compared to JSON Schema.
How to achieve validation with libfyaml in C:
Since libfyaml primarily focuses on parsing and emitting YAML, you'll need to combine it with other techniques for validation:
-
Custom Validation Functions: This is the most common approach with libfyaml. After parsing the YAML data, write C functions to validate its structure, data types, and values against your requirements.
#include <yaml.h> #include <stdio.h> #include <stdbool.h> bool validate_name(yaml_document_t *document, const char *key) { yaml_node_t *root = yaml_document_get_root_node(document); if (root->type != YAML_MAPPING_NODE) { return false; // Root node should be a mapping } for (yaml_node_pair_t *pair = root->data.mapping.pairs.start; pair < root->data.mapping.pairs.top; pair++) { yaml_node_t *key_node = yaml_document_get_node(document, pair->key); if (key_node->type == YAML_SCALAR_NODE && strcmp((char *)key_node->data.scalar.value, key) == 0) { // Check if the value associated with the key is a string yaml_node_t *value_node = yaml_document_get_node(document, pair->value); if (value_node->type == YAML_SCALAR_NODE) { return true; } else { return false; // Value is not a string } } } return false; // Key not found } int main() { // Load YAML data using libfyaml // ... // Validate the YAML data if (validate_name(&document, "name")) { printf("YAML validation successful!\n"); } else { fprintf(stderr, "YAML validation failed!\n"); } // ... return 0; }
-
Third-party Libraries: Explore C libraries that might offer YAML schema validation. While less common than JSON Schema validators, some libraries could provide this functionality.
-
Schema Conversion and JSON Schema Validation: If you need interoperability with JSON Schema, you can convert your YAML data to JSON and then use
libjson-schemafor validation.
Key Considerations:
- Schema Language: If you opt for a schema-based approach, choose a schema language that suits your needs (e.g., JSON Schema, Kwalify).
- Performance: For performance-critical applications, carefully evaluate the overhead of validation. Custom functions might provide better performance if optimized well.
- Error Handling: Implement robust error handling to provide informative messages when validation fails.
Sub-issues
Metadata
Metadata
Assignees
Labels
Type
Projects
Status