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:
- Each leaf has
field,comparator, andvalue(exceptis_null/is_not_null). - Each group node has
group_operator(and/or) and a non-emptygrouparray. comparatormust be from the allowed list.- No circular references.
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
- All the comparators from Problem 12 are valid.
is_nullandis_not_nulldo not require avalue.- Nesting depth ≤ 5.