CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-openapi-types

TypeScript type definitions for OpenAPI documents across all specification versions.

83

0.92x
Overview
Eval results
Files

json-schema.mddocs/

JSON Schema Types

Base JSON Schema interface supporting the schema definitions used throughout OpenAPI specifications. This interface provides the foundation for schema validation and type definition across all OpenAPI versions.

Capabilities

Base JSON Schema Interface

Core JSON Schema interface providing the fundamental schema definition structure.

/**
 * Base JSON Schema interface supporting core JSON Schema specification
 */
interface IJsonSchema {
  /** Schema identifier */
  id?: string;
  /** JSON Schema version */
  $schema?: string;
  /** Schema title */
  title?: string;
  /** Schema description */
  description?: string;
  /** Multiple of constraint for numeric values */
  multipleOf?: number;
  /** Maximum value constraint */
  maximum?: number;
  /** Exclusive maximum flag */
  exclusiveMaximum?: boolean;
  /** Minimum value constraint */
  minimum?: number;
  /** Exclusive minimum flag */
  exclusiveMinimum?: boolean;
  /** Maximum string length constraint */
  maxLength?: number;
  /** Minimum string length constraint */
  minLength?: number;
  /** Regular expression pattern for string validation */
  pattern?: string;
  /** Additional items schema for arrays */
  additionalItems?: boolean | IJsonSchema;
  /** Items schema definition for arrays */
  items?: IJsonSchema | IJsonSchema[];
  /** Maximum array length constraint */
  maxItems?: number;
  /** Minimum array length constraint */
  minItems?: number;
  /** Unique items constraint for arrays */
  uniqueItems?: boolean;
  /** Maximum object properties constraint */
  maxProperties?: number;
  /** Minimum object properties constraint */
  minProperties?: number;
  /** Required properties array */
  required?: string[];
  /** Additional properties schema */
  additionalProperties?: boolean | IJsonSchema;
  /** Schema definitions collection */
  definitions?: {
    [name: string]: IJsonSchema;
  };
  /** Object properties schemas */
  properties?: {
    [name: string]: IJsonSchema;
  };
  /** Pattern-based property schemas */
  patternProperties?: {
    [name: string]: IJsonSchema;
  };
  /** Property dependencies */
  dependencies?: {
    [name: string]: IJsonSchema | string[];
  };
  /** Enumeration of valid values */
  enum?: any[];
  /** Value type or array of types */
  type?: string | string[];
  /** All of constraint - schema must match all sub-schemas */
  allOf?: IJsonSchema[];
  /** Any of constraint - schema must match at least one sub-schema */
  anyOf?: IJsonSchema[];
  /** One of constraint - schema must match exactly one sub-schema */
  oneOf?: IJsonSchema[];
  /** Not constraint - schema must not match the sub-schema */
  not?: IJsonSchema;
  /** JSON Reference to another schema */
  $ref?: string;
}

Usage Examples:

import { IJsonSchema } from "openapi-types";

// Basic string schema
const stringSchema: IJsonSchema = {
  type: "string",
  minLength: 1,
  maxLength: 100,
  pattern: "^[a-zA-Z0-9]+$"
};

// Object schema with properties
const userSchema: IJsonSchema = {
  type: "object",
  title: "User",
  description: "User account information",
  required: ["id", "email"],
  properties: {
    id: {
      type: "string",
      pattern: "^[0-9]+$"
    },
    email: {
      type: "string",
      format: "email"
    },
    name: {
      type: "string",
      minLength: 1
    },
    age: {
      type: "integer",
      minimum: 0,
      maximum: 150
    }
  },
  additionalProperties: false
};

// Array schema
const tagsSchema: IJsonSchema = {
  type: "array",
  items: {
    type: "string",
    minLength: 1
  },
  uniqueItems: true,
  maxItems: 10
};

// Schema with definitions and references
const petStoreSchema: IJsonSchema = {
  type: "object",
  definitions: {
    Pet: {
      type: "object",
      required: ["name", "photoUrls"],
      properties: {
        id: {
          type: "integer",
          format: "int64"
        },
        name: {
          type: "string"
        },
        photoUrls: {
          type: "array",
          items: {
            type: "string"
          }
        },
        status: {
          type: "string",
          enum: ["available", "pending", "sold"]
        }
      }
    }
  },
  properties: {
    pets: {
      type: "array",
      items: {
        $ref: "#/definitions/Pet"
      }
    }
  }
};

// Complex schema with constraints
const conditionalSchema: IJsonSchema = {
  type: "object",
  properties: {
    type: {
      type: "string",
      enum: ["person", "organization"]
    }
  },
  allOf: [
    {
      if: {
        properties: {
          type: { const: "person" }
        }
      },
      then: {
        properties: {
          firstName: { type: "string" },
          lastName: { type: "string" }
        },
        required: ["firstName", "lastName"]
      }
    },
    {
      if: {
        properties: {
          type: { const: "organization" }
        }
      },
      then: {
        properties: {
          organizationName: { type: "string" }
        },
        required: ["organizationName"]
      }
    }
  ]
};

// Validation helper functions
function hasRequiredFields(schema: IJsonSchema): boolean {
  return schema.required != null && schema.required.length > 0;
}

function isObjectSchema(schema: IJsonSchema): boolean {
  return schema.type === "object" || (Array.isArray(schema.type) && schema.type.includes("object"));
}

function isArraySchema(schema: IJsonSchema): boolean {
  return schema.type === "array" || (Array.isArray(schema.type) && schema.type.includes("array"));
}

function getSchemaProperties(schema: IJsonSchema): { [name: string]: IJsonSchema } | undefined {
  return schema.properties;
}

Install with Tessl CLI

npx tessl i tessl/npm-openapi-types

docs

generic-types.md

index.md

json-schema.md

openapi-v2.md

openapi-v3_1.md

openapi-v3.md

tile.json