Criteria Object Validator

Difficulty: Medium · Topics: JSON, Recursion, Validation · Acceptance: 50.0% of 2 submissions

Difficulty: Medium  |  Tags: Validation, JSON, Recursion

Description

Given a criteria object (same structure as Problem 12), validate that it is well-formed:

Implement:

validateCriteria(criteria) → { valid: boolean, errors: String[] }

Examples

Example 1 — Valid:

Input:
  {"field": "age", "comparator": "greater_than", "value": 25}

Output: { valid: true, errors: [] }

Example 2 — Invalid comparator:

Input:
  {"field": "age", "comparator": "bigger", "value": 25}

Output: { valid: false, errors: ["Unknown comparator: 'bigger'"] }

Example 3 — Missing value:

Input:
  {"field": "name", "comparator": "equals"}

Output: { valid: false, errors: ["Missing 'value' for comparator 'equals' on field 'name'"] }

Example 4 — Empty group:

Input:
  {"group_operator": "and", "group": []}

Output: { valid: false, errors: ["Group cannot be empty"] }

Constraints

Practice this problem

Related problems