DEV Community

Denis
Denis

Posted on

How Our AI Agents Built a Dynamic Schema & Data Validator for Developers

Building the Dynamic Schema & Data Validator with AI Agents

In the world of modern software development, data consistency is paramount. Developers, data engineers, and analysts constantly grapple with the challenge of validating structured data, whether it's JSON payloads from an API or XML configurations. Ensuring these adhere to predefined schemas is a critical, yet often manual and tedious, task. This was the technical challenge we tasked our AI agents, Jan and Klára, with solving: building a robust, in-browser Dynamic Schema & Data Validator.

The Technical Challenge: Ensuring Data Integrity

The core problem was clear: how to provide a simple, immediate way for developers to validate their data against complex schemas (JSON Schema or XML Schema Definition - XSD) without needing to set up local environments or rely on external services. The solution needed to offer:

  • Real-time, in-browser validation.
  • Clear, actionable error messages.
  • Support for common schema types.
  • An intuitive user interface.

Jan (AI Developer) & Klára (AI Designer): A Synergistic Approach

Our AI agents embarked on this project with distinct, yet collaborative, roles.

Klára, our AI Designer, focused on the user experience. She designed an interface that was both functional and easy to navigate. The layout allows users to paste their schema and data side-by-side, with validation results displayed prominently and errors highlighted precisely where they occur in the data structure. Her design principles emphasized clarity and immediate feedback.

Jan, our AI Developer, took on the complex task of building the validation engine. He needed to interpret various schema definitions and compare them against arbitrary data inputs. This involved parsing both JSON and XML structures and implementing logic to check for missing required fields, incorrect data types, and structural inconsistencies.

'My main challenge was to create a lightweight, in-browser validation library that could mimic the core functionalities of tools like Z-Schema without the overhead,' explains Jan. 'The snippet you see handles basic type checking, required properties, and nested structures. It's designed to give quick, visual feedback directly in the browser, making the validation loop much faster for developers.'

Here's a glimpse into the core JavaScript snippet Jan developed for the JSON validation logic:

        // Start of embedded Z-Schema-like library (custom basic implementation for common JSON Schema features)
        const ZSchema = {
            validate: function(data, schema) {
                const errors = [];
                let valid = true;

                const validateType = (path, value, expectedType) => {
                    let actualType;
                    if (value === null) actualType = 'null';
                    else if (Array.isArray(value)) actualType = 'array';
                    else actualType = typeof value;

                    if (expectedType === 'integer') {
                        if (typeof value !== 'number' || !Number.isInteger(value)) {
                            errors.push({ path: path, message: `Expected type 'integer', got '${actualType}'` });
                            return false;
                        }
                    } else if (expectedType === 'number') {
                        if (typeof value !== 'number') {
                            errors.push({ path: path, message: `Expected type 'number', got '${actualType}'` });
                            return false;
                        }
                    }
                     else if (expectedType === 'string') {
                        if (typeof value !== 'string') {
                            errors.push({ path: path, message: `Expected type 'string', got '${actualType}'` });
                            return false;
                        }
                    }
                     else if (expectedType === 'boolean') {
                        if (typeof value !== 'boolean') {
                            errors.push({ path: path, message: `Expected type 'boolean', got '${actualType}'` });
                            return false;
                        }
                    } else if (expectedType === 'array') {
                        if (!Array.isArray(value)) {
                            errors.push({ path: path, message: `Expected type 'array', got '${actualType}'` });
                            return false;
                        }
                    } else if (expectedType === 'object') {
                        if (typeof value !== 'object' || value === null || Array.isArray(value)) {
                            errors.push({ path: path, message: `Expected type 'object', got '${actualType}'` });
                            return false;
                        }
                    }
                    return true;
                };

                const traverse = (currentData, currentSchema, currentPath) => {
                    if (!currentSchema) return;

                    // Type validation
                    if (currentSchema.type) {
                        if (!validateType(currentPath, currentData, currentSchema.type)) {
                            valid = false;
                        }
                    }

                    // Properties validation for objects
                    if (currentSchema.type === 'object' && typeof currentData === 'object' && currentData !== null && !Array.isArray(currentData)) {
                        // Check required properties
                        if (currentSchema.required) {
                            for (const requiredProp of currentSchema.required) {
                                if (!(requiredProp in currentData)) {
                                    errors.push({ path: `${currentPath}.${requiredProp}`, message: `Missing required property '${requiredProp}'` });
                                    valid = false;
                                }
                            }
                        }
                        // Recurse into properties
                        if (currentSchema.properties) {
                            for (const key in currentSchema.properties) {
                                traverse(currentData[key], currentSchema.properties[key], `${currentPath}.${key}`);
                            }
                        }
                    }

                    // Items validation for arrays
                    if (currentSchema.type === 'array' && Array.isArray(currentData)) {
                        if (currentSchema.items) {
                            for (let i = 0; i < currentData.length; i++) {
                                traverse(currentData[i], currentSchema.items, `${currentPath}[${i}]`);
                            }
                        }
                    }
                };

                traverse(data, schema, '$'); // Start traversal from root

                return { valid, errors };
            }
        };
        // End of embedded Z-Schema-like library
Enter fullscreen mode Exit fullscreen mode

Quality Assurance & Deployment

Once Jan and Klára completed their respective parts, Martin, our AI QA specialist, rigorously tested the validator. He designed and executed a comprehensive suite of tests, covering valid and invalid JSON/XML, edge cases, and large payloads to ensure the tool was robust and accurate. His work was crucial in refining the error reporting and overall reliability.

Finally, Tomáš, our AI Deployment Engineer, took over to deploy the Dynamic Schema & Data Validator. He configured the necessary infrastructure, ensured optimal performance, and made it accessible to the public.

Unlock Advanced Capabilities

The free version of our Dynamic Schema & Data Validator offers essential functionality. For just $1.99, you can unlock premium features including unlimited validations, support for massive datasets, schema inference (generating a basic schema from example data), and the ability to download comprehensive validation reports. These advanced capabilities are designed to integrate seamlessly into your most demanding workflows.

Try it Now!

Experience the power of instant, intelligent data validation for yourself.
Head over to the live demo and streamline your development process today!

https://pixeloffice.eu/showcase/schema-data-validator/

Top comments (0)