@lssm/lib.schema

A small schema dictionary to describe operation I/O once and export to Zod (runtime validation), Pothos (GraphQL type refs), and JSON Schema.

Installation

npm install @lssm/lib.schema
# or
bun add @lssm/lib.schema

Core Exports

  • SchemaModel: Compose fields into typed object models
  • ScalarTypeEnum: Common scalar types (NonEmptyString, Email, DateTime, etc.)
  • defineEnum: Create type-safe enums
  • FieldType: Wrap scalars with Zod/GraphQL/JSON Schema

Example: Basic Schema

import { SchemaModel, ScalarTypeEnum } from '@lssm/lib.schema';

export const CreateSpotInput = new SchemaModel({
  name: 'CreateSpotInput',
  fields: {
    name: { type: ScalarTypeEnum.NonEmptyString(), isOptional: false },
    latitude: { type: ScalarTypeEnum.Latitude(), isOptional: false },
    longitude: { type: ScalarTypeEnum.Longitude(), isOptional: false },
  },
});

// Get Zod schema for validation
const zodSchema = CreateSpotInput.getZod();

// Get Pothos input name for GraphQL
const pothosName = CreateSpotInput.getPothosInput();

// Get JSON Schema
const jsonSchema = CreateSpotInput.getJsonSchema();

Example: Enums

import { defineEnum, SchemaModel } from '@lssm/lib.schema';

const Weekday = defineEnum('Weekday', [
  'MO', 'TU', 'WE', 'TH', 'FR', 'SA', 'SU',
] as const);

const RecurrenceRule = new SchemaModel({
  name: 'RecurrenceRule',
  fields: {
    frequency: { 
      type: defineEnum('Frequency', ['DAILY', 'WEEKLY', 'MONTHLY'] as const),
      isOptional: false 
    },
    byWeekday: { type: Weekday, isOptional: true, isArray: true },
  },
});

Available Scalars

Strings

  • NonEmptyString()
  • Email()
  • PhoneNumber()
  • CountryCode()
  • Locale()
  • TimeZone()

Numbers

  • PositiveNumber()
  • Latitude()
  • Longitude()

Dates & Times

  • Date()
  • DateTime()

Generic

  • String()
  • JSON()