or run

npx @tessl/cli init
Log in

Version

Files

tile.json

task.mdevals/scenario-1/

Schema Validator

Build a TypeScript utility that validates and analyzes JSON Schema definitions using type-safe structures.

Requirements

Create a module that provides functions to work with JSON Schema definitions. The module should:

  1. Schema Validation Check: Implement a function isValidSchema that accepts a schema object and returns a boolean indicating whether it represents a valid schema structure. A valid schema must have at least one of: type, properties, items, allOf, anyOf, oneOf, or $ref.

  2. Required Fields Extraction: Implement a function getRequiredFields that accepts a schema object and returns an array of required field names. If the schema has no required fields, return an empty array.

  3. Schema Complexity Analyzer: Implement a function analyzeComplexity that accepts a schema object and returns an object with complexity metrics:

    • hasNestedObjects: boolean indicating if the schema has nested object properties
    • hasArrays: boolean indicating if the schema defines arrays
    • hasComposition: boolean indicating if the schema uses composition keywords (allOf, anyOf, oneOf)
    • constraintCount: number of validation constraints applied (e.g., minLength, maxLength, pattern, minimum, maximum, enum)
  4. Property Type Checker: Implement a function getPropertyType that accepts a schema object and a property name, and returns the type of that property as a string. If the property doesn't exist, return null. If the type is an array of types, return the first type.

Test Cases

  • isValidSchema returns true for a schema with a type property set to "string" @test
  • isValidSchema returns true for a schema with properties defined but no type @test
  • isValidSchema returns false for an empty object with no schema keywords @test
  • getRequiredFields returns ["name", "email"] for a schema with required: ["name", "email"] @test
  • getRequiredFields returns an empty array for a schema with no required property @test
  • analyzeComplexity correctly identifies a schema with nested object properties @test
  • analyzeComplexity correctly counts validation constraints including minLength, pattern, and enum @test
  • getPropertyType returns "string" for a property defined with type: "string" @test
  • getPropertyType returns null for a non-existent property @test

Implementation

@generates

API

/**
 * Checks if a schema object is a valid schema structure
 */
export function isValidSchema(schema: any): boolean;

/**
 * Extracts required field names from a schema
 */
export function getRequiredFields(schema: any): string[];

/**
 * Analyzes the complexity of a schema
 */
export function analyzeComplexity(schema: any): {
  hasNestedObjects: boolean;
  hasArrays: boolean;
  hasComposition: boolean;
  constraintCount: number;
};

/**
 * Gets the type of a specific property in a schema
 */
export function getPropertyType(schema: any, propertyName: string): string | null;

Dependencies { .dependencies }

openapi-types { .dependency }

Provides TypeScript type definitions for JSON Schema structures.