or run

tessl search
Log in

Version

Files

tile.json

task.mdevals/scenario-1/

Custom Email Component Validator

Build a validation utility for custom MJML component attributes that enforces type constraints and validates attribute values.

Requirements

Your validator should support the following validation rules:

Unit Validation

Validate that attribute values contain specific units and proper formatting:

  • Support for unit patterns like unit(px,%) which requires values to have px or % units
  • Support for multiple value patterns like unit(px,%){1,4} which accepts 1-4 space-separated values with the specified units
  • Return validation errors when values don't match the required unit pattern

Enum Validation

Validate that attribute values match one of a predefined set of allowed values:

  • Support for enum(value1,value2,value3) patterns that restrict values to specific options
  • Case-sensitive matching
  • Return validation errors for values not in the enum set

Color Validation

Validate color values in various formats:

  • Support hex colors (#fff, #ffffff)
  • Support RGB/RGBA colors (rgb(255,0,0), rgba(255,0,0,0.5))
  • Support named colors (red, blue, green, etc.)
  • Return validation errors for invalid color formats

Validation Output

The validator should return validation results with:

  • A boolean indicating if validation passed
  • Error messages describing what validation failed and why
  • The invalid value that caused the failure

Test Cases

  • Validates unit(px) pattern accepts "10px" but rejects "10" and "10em" @test
  • Validates unit(px,%){1,4} pattern accepts "10px 20px" and "5% 10% 15% 20%" but rejects "10px 20em" @test
  • Validates enum(left,center,right) pattern accepts "center" but rejects "middle" @test
  • Validates color type accepts "#fff", "rgb(255,0,0)", and "blue" but rejects "notacolor" @test

Implementation

@generates

API

/**
 * Validates an attribute value against a type pattern.
 *
 * @param {string} value - The attribute value to validate
 * @param {string} typePattern - The type pattern (e.g., "unit(px,%)", "enum(a,b,c)", "color")
 * @returns {Object} Validation result with { valid: boolean, error?: string, value?: string }
 */
function validateAttribute(value, typePattern) {
  // IMPLEMENTATION HERE
}

/**
 * Parses a unit pattern and extracts allowed units and value count.
 *
 * @param {string} pattern - The unit pattern (e.g., "unit(px,%){1,4}")
 * @returns {Object} Parsed pattern with { units: string[], minCount: number, maxCount: number }
 */
function parseUnitPattern(pattern) {
  // IMPLEMENTATION HERE
}

/**
 * Parses an enum pattern and extracts allowed values.
 *
 * @param {string} pattern - The enum pattern (e.g., "enum(left,center,right)")
 * @returns {string[]} Array of allowed values
 */
function parseEnumPattern(pattern) {
  // IMPLEMENTATION HERE
}

module.exports = {
  validateAttribute,
  parseUnitPattern,
  parseEnumPattern
};

Dependencies { .dependencies }

mjml { .dependency }

Provides email template markup language support.