CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-deep-eql

Improved deep equality testing for Node.js and the browser with support for complex types and circular references.

Overall
score

96%

Overview
Eval results
Files

task.mdevals/scenario-10/

RegExp Validation Tool

Build a validation utility that compares regular expression patterns to verify they match expected configurations.

Background

Configuration files often define regular expression patterns for validation rules (email formats, phone numbers, URL patterns, etc.). When these configurations are loaded from different sources or modified by different systems, you need to verify that the patterns remain equivalent to the expected values.

Requirements

Implement a function validateRegexConfig(actual, expected) that:

  1. Accepts two parameters:

    • actual: An object containing regex patterns as values
    • expected: An object containing the expected regex patterns
  2. Returns an object with:

    • isValid: Boolean indicating if all patterns match
    • mismatches: Array of keys where patterns differ (empty if all match)
  3. The comparison should:

    • Compare both the pattern and all flags (case-insensitive, global, multiline, etc.)
    • Handle cases where the regex might be defined as RegExp objects or strings
    • Return true only if patterns are identical in both pattern text and flags

Constraints

  • Both actual and expected will have the same keys
  • Values will be RegExp objects
  • Consider RegExp objects equal only if they have the same pattern and flags

Dependencies { .dependencies }

deep-eql { .dependency }

Provides deep equality comparison for complex types including regular expressions.

Test Cases

Test Case 1: Matching patterns @test

File: validate-regex.test.js

const { validateRegexConfig } = require('./validate-regex');

test('should validate matching regex patterns', () => {
  const actual = {
    email: /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/g,
    phone: /^\d{3}-\d{3}-\d{4}$/
  };

  const expected = {
    email: /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/g,
    phone: /^\d{3}-\d{3}-\d{4}$/
  };

  const result = validateRegexConfig(actual, expected);
  expect(result.isValid).toBe(true);
  expect(result.mismatches).toEqual([]);
});

Test Case 2: Mismatched flags @test

File: validate-regex.test.js

test('should detect flag differences', () => {
  const actual = {
    email: /test@example\.com/i,
    url: /https?:\/\/.+/
  };

  const expected = {
    email: /test@example\.com/,  // Missing 'i' flag
    url: /https?:\/\/.+/
  };

  const result = validateRegexConfig(actual, expected);
  expect(result.isValid).toBe(false);
  expect(result.mismatches).toEqual(['email']);
});

Test Case 3: Mismatched patterns @test

File: validate-regex.test.js

test('should detect pattern differences', () => {
  const actual = {
    zipCode: /^\d{5}$/,
    date: /^\d{4}-\d{2}-\d{2}$/
  };

  const expected = {
    zipCode: /^\d{5}-\d{4}$/,  // Different pattern
    date: /^\d{4}-\d{2}-\d{2}$/
  };

  const result = validateRegexConfig(actual, expected);
  expect(result.isValid).toBe(false);
  expect(result.mismatches).toEqual(['zipCode']);
});

Install with Tessl CLI

npx tessl i tessl/npm-deep-eql

tile.json