Docs@skmtc/core

Schemas

TYPE

OasSchema

@skmtc/core 0.63.4

Union type representing all possible OpenAPI Schema objects in the SKMTC system.

OasSchema is the fundamental type for representing any OpenAPI schema definition after it has been parsed and processed by the SKMTC pipeline. It encompasses all JSON Schema types supported by OpenAPI 3.x specifications, providing type-safe access to schema properties and validation constraints.

This union type is used throughout the system for schema processing, type generation, and validation. Each variant corresponds to a specific JSON Schema type with its own set of properties and validation rules.

Supported Schema Types

  • OasArray: Array schemas with item type definitions and constraints
  • OasBoolean: Boolean schemas with optional default values
  • OasInteger: Integer schemas with numeric constraints and formats
  • OasNumber: Number schemas with numeric constraints and formats
  • OasObject: Object schemas with properties, required fields, and constraints
  • OasString: String schemas with length constraints, patterns, and formats
  • OasUnknown: Schemas with unknown or mixed types
  • OasUnion: Union schemas representing oneOf/anyOf constructs
  • OasIntersection: An allOf that keeps a reference

Signature

import type { OasSchema } from '@skmtc/core'

type OasSchema =
  | OasArray
  | OasBoolean
  | OasInteger
  | OasNumber
  | OasObject
  | OasString
  | OasUnknown
  | OasUnion
  | OasIntersection

Examples

Type checking and processing
import type { OasSchema } from '@skmtc/core';

function processSchema(schema: OasSchema): string {
  if (schema.type === 'object') {
    // TypeScript knows this is OasObject
    return `Object with ${Object.keys(schema.properties || {}).length} properties`;
  } else if (schema.type === 'array') {
    // TypeScript knows this is OasArray
    return `Array of ${schema.items.type} items`;
  } else if (schema.type === 'string') {
    // TypeScript knows this is OasString
    return `String${schema.format ? ` (${schema.format})` : ''}`;
  }
  // Handle other types...
  return `${schema.type} type`;
}
Schema validation and constraints
function validateSchemaConstraints(schema: OasSchema, value: unknown): boolean {
  switch (schema.type) {
    case 'string':
      if (typeof value !== 'string') return false;
      if (schema.minLength && value.length < schema.minLength) return false;
      if (schema.maxLength && value.length > schema.maxLength) return false;
      if (schema.pattern && !new RegExp(schema.pattern).test(value)) return false;
      return true;

    case 'integer':
    case 'number':
      if (typeof value !== 'number') return false;
      if (schema.minimum && value < schema.minimum) return false;
      if (schema.maximum && value > schema.maximum) return false;
      return true;

    case 'array':
      if (!Array.isArray(value)) return false;
      if (schema.minItems && value.length < schema.minItems) return false;
      if (schema.maxItems && value.length > schema.maxItems) return false;
      return true;

    default:
      return true;
  }
}
Code generation based on schema type
class TypeScriptGenerator {
  generateType(schema: OasSchema): string {
    switch (schema.type) {
      case 'object':
        return this.generateInterface(schema);
      case 'array':
        return `Array<${this.generateType(schema.items)}>`;
      case 'string':
        if (schema.enums) {
          return schema.enums.map(e => `'${e}'`).join(' | ');
        }
        return 'string';
      case 'integer':
      case 'number':
        return 'number';
      case 'boolean':
        return 'boolean';
      case 'union':
        return schema.variants.map(v => this.generateType(v)).join(' | ');
      default:
        return 'unknown';
    }
  }
}