Dynamic Template Validation in Vue with TypeScript
Template validation is crucial for maintaining code quality and preventing runtime errors in Vue applications. This challenge asks you to implement a function that dynamically validates a Vue component's template against a provided schema, identifying potential errors and providing helpful messages. This is useful for catching issues early in development and ensuring consistent template structure across your project.
Problem Description
You need to create a TypeScript function validateVueTemplate that takes a Vue component's template string and a validation schema as input. The schema defines the expected structure and attributes of the template. The function should parse the template, compare it against the schema, and return an array of validation errors. Each error should include a message describing the issue and the location (line number) where it occurred.
Key Requirements:
- Template Parsing: The function must parse the template string into a usable format for comparison. Consider using a simple regex-based approach or a more robust HTML parser if needed.
- Schema Matching: The schema should be a JavaScript object that describes the expected structure. This could include expected element names, attribute names, and attribute values.
- Error Reporting: The function must identify discrepancies between the template and the schema and report them as errors with clear messages and line numbers.
- TypeScript: The solution must be written in TypeScript, with appropriate type annotations.
Expected Behavior:
The validateVueTemplate function should return an array of error objects. If the template is valid according to the schema, the array should be empty. Each error object should have the following properties:
message: A string describing the error.line: The line number where the error occurred (1-based indexing).
Edge Cases to Consider:
- Invalid Template Syntax: The template might contain invalid HTML or Vue syntax.
- Complex Schemas: The schema might specify complex relationships between elements and attributes.
- Nested Components: The template might contain nested Vue components, which require recursive validation. (For simplicity, assume no nested components in this challenge).
- Dynamic Attributes: Attributes whose values are bound to data properties. (Ignore these for this challenge; focus on static attribute values).
- Whitespace: Handle whitespace variations in the template.
Examples
Example 1:
Input:
templateString: "<template><div v-if='show'>Hello</div></template>"
schema: {
template: {
div: {
attributes: {
'v-if': 'show'
},
children: ['Hello']
}
}
}
Output: []
Explanation: The template matches the schema perfectly. No errors are reported.
Example 2:
Input:
templateString: "<template><div v-if='showMessage'>Goodbye</div></template>"
schema: {
template: {
div: {
attributes: {
'v-if': 'show'
},
children: ['Hello']
}
}
}
Output: [
{ message: "Expected attribute 'v-if' to be 'show', but found 'showMessage'", line: 2 },
{ message: "Expected child text 'Hello', but found 'Goodbye'", line: 2 }
]
Explanation: The 'v-if' attribute value is incorrect, and the child text doesn't match the schema.
Example 3:
Input:
templateString: "<template><div></div></template>"
schema: {
template: {
div: {
attributes: {
'v-if': 'show'
},
children: ['Hello']
}
}
}
Output: [
{ message: "Missing attribute 'v-if'", line: 2 },
{ message: "Missing child text 'Hello'", line: 2 }
]
Explanation: The 'v-if' attribute is missing, and the child text is missing.
Constraints
- The template string will be a valid Vue template string.
- The schema will be a valid JavaScript object.
- The template string will not exceed 1000 characters.
- The function should return the result within 100ms.
- The schema will only contain simple attribute and child text validations. No complex nesting or component validation is required.
Notes
- You can use regular expressions or a simple HTML parser to parse the template string.
- Focus on the core logic of comparing the template against the schema and reporting errors.
- Consider using TypeScript's type system to ensure type safety.
- Line numbers should be calculated accurately based on the template string.
- For simplicity, assume that all attributes are strings.
- The schema is a simplified representation of a Vue template. A real-world schema would be more complex.
- Error messages should be informative and helpful for developers.