or run

tessl search
Log in

Version

Files

tile.json

task.mdevals/scenario-7/

Data Validator

Build a data validation module that compares numerical values with configurable thresholds and rules. The module should handle various data types and provide detailed comparison results.

Requirements

Implement a module that validates numerical data against threshold configurations. The validator should:

  1. Support multiple comparison operations: equality checks, inequality checks, less-than, greater-than, and ordering comparisons
  2. Handle different numerical types including regular numbers, decimal numbers with high precision, and fractions
  3. Return validation results indicating whether values pass or fail the specified rules
  4. Provide a function to rank/order a list of values

Functionality

Threshold Validation

The module should validate values against threshold rules. Each rule specifies:

  • A comparison type (equal to, not equal to, greater than, less than, greater or equal, less or equal)
  • A threshold value to compare against

The validator should return whether the value satisfies the rule.

Exact Equality Checking

The module should check if two values are exactly equal, including for high-precision decimal numbers and fractions.

Value Comparison

The module should compare two values and return their relative ordering (-1 if first is smaller, 0 if equal, 1 if first is larger).

API

/**
 * Validates if a value satisfies a threshold rule
 * @param {number|string} value - The value to validate
 * @param {string} operator - Comparison operator: 'equal', 'unequal', 'greater', 'less', 'greaterOrEqual', 'lessOrEqual'
 * @param {number|string} threshold - The threshold value
 * @returns {boolean} True if validation passes, false otherwise
 */
export function validateThreshold(value, operator, threshold) {}

/**
 * Checks if two values are exactly equal
 * @param {number|string} value1 - First value
 * @param {number|string} value2 - Second value
 * @returns {boolean} True if values are exactly equal
 */
export function areEqual(value1, value2) {}

/**
 * Compares two values and returns their ordering
 * @param {number|string} value1 - First value
 * @param {number|string} value2 - Second value
 * @returns {number} -1 if value1 < value2, 0 if equal, 1 if value1 > value2
 */
export function compareValues(value1, value2) {}

Implementation

@generates

Test Cases

  • Given value 10 and threshold 10 with operator 'equal', validateThreshold returns true @test
  • Given value 10 and threshold 5 with operator 'unequal', validateThreshold returns true @test
  • Given value 15 and threshold 10 with operator 'greater', validateThreshold returns true @test
  • Given value 5 and threshold 10 with operator 'less', validateThreshold returns true @test
  • Given high-precision decimal "0.1000000000000000000000001" and threshold "0.1000000000000000000000002" with operator 'unequal', validateThreshold returns true @test
  • Given two identical high-precision decimals "0.123456789012345678901234", areEqual returns true @test
  • Given fraction "2/4" and fraction "1/2", areEqual returns true @test
  • Given values 5 and 10, compareValues returns -1 @test
  • Given values 10 and 10, compareValues returns 0 @test
  • Given values 15 and 10, compareValues returns 1 @test

Dependencies { .dependencies }

mathjs { .dependency }

Provides mathematical operations and comparison functions for various numeric types.