or run

npx @tessl/cli init
Log in

Version

Files

docs

index.md
tile.json

task.mdevals/scenario-2/

JSON Schema Keyword Collector

Build a utility that analyzes JSON Schema documents to collect and categorize all the standard JSON Schema keywords found within them.

Requirements

Your task is to implement a function that traverses a JSON Schema document and returns a categorized report of all standard JSON Schema keywords present in the schema and its nested subschemas.

The function should:

  1. Accept a JSON Schema object as input
  2. Traverse the entire schema structure including all nested schemas
  3. Identify and categorize each standard JSON Schema keyword encountered
  4. Return an object containing:
    • singleSchemaKeywords: Array of keywords that contain a single schema (e.g., "not", "additionalProperties", "if", "then", "else")
    • arraySchemaKeywords: Array of keywords that contain arrays of schemas (e.g., "allOf", "anyOf", "oneOf")
    • objectSchemaKeywords: Array of keywords that contain objects of schemas (e.g., "properties", "patternProperties", "definitions")
    • totalSchemasVisited: Total count of schema objects visited during traversal

Each keyword should only appear once in its respective category, even if it appears multiple times in the schema.

Example

Given this schema:

{
  properties: {
    name: { type: 'string' },
    age: { type: 'number' }
  },
  allOf: [
    { type: 'object' },
    { required: ['name'] }
  ],
  if: { properties: { age: { minimum: 18 } } },
  then: { properties: { canVote: { const: true } } }
}

The output should be:

{
  singleSchemaKeywords: ['if', 'then'],
  arraySchemaKeywords: ['allOf'],
  objectSchemaKeywords: ['properties'],
  totalSchemasVisited: 8
}

Test Cases

  • Given a schema with properties containing two properties, the function returns objectSchemaKeywords containing "properties" and totalSchemasVisited of 3 @test

  • Given a schema with allOf, anyOf, and oneOf keywords, the function returns arraySchemaKeywords containing all three keywords @test

  • Given a schema with if, then, and else keywords, the function returns singleSchemaKeywords containing all three keywords @test

  • Given a schema with definitions and nested patternProperties, the function returns objectSchemaKeywords containing both keywords @test

  • Given an empty schema object, the function returns empty arrays for all keyword categories and totalSchemasVisited of 1 @test

Implementation

@generates

API

/**
 * Analyzes a JSON Schema and returns categorized keywords found within it.
 *
 * @param {object} schema - The JSON Schema to analyze
 * @returns {object} Analysis result with categorized keywords and visit count
 * @returns {string[]} returns.singleSchemaKeywords - Keywords containing single schemas
 * @returns {string[]} returns.arraySchemaKeywords - Keywords containing schema arrays
 * @returns {string[]} returns.objectSchemaKeywords - Keywords containing schema objects
 * @returns {number} returns.totalSchemasVisited - Total schemas visited during traversal
 */
function analyzeSchemaKeywords(schema) {
  // IMPLEMENTATION HERE
}

module.exports = { analyzeSchemaKeywords };

Dependencies { .dependencies }

json-schema-traverse { .dependency }

Provides JSON Schema traversal capabilities for visiting all schema objects.

@satisfied-by