Skip to content
Flare 0.1.x is pre-release. Expect breaking changes before 1.0 — see the changelog.

Lib

@flare-ts/lib is a standalone TypeScript utility library with zero third-party runtime dependencies. Use it on its own in any TypeScript project, or alongside @flare-ts/core in a Flare app — schema and model symbols are always imported from this package, never from core. The package ships one public module today: schema validation and JSON serialization under ./schema.

For HTTP contracts and Flare-specific composition, see Core. The pages below cover the schema library itself.

  • Schema: schema(), primitives, safeParse, discriminated unions, records, and serializers (compileSerializer, toJsonSchema).
  • Model: model() for named DTO classes with the same validation and serialization surface as schema().
SubpathUse for
. (default)Primitives, schema(), model(), serializers (compileSerializer, toJsonSchema), and all related types
./schemaSame symbols as the default entry; prefer when the import path should name the module

Both entries resolve to identical symbols. Prefer ./schema when you want the import path to name the module explicitly; either path works.

import { schema, str, int, compileSerializer } from "@flare-ts/lib/schema";
// equivalent:
import { schema, str, int, compileSerializer } from "@flare-ts/lib";

Everything below is exported from @flare-ts/lib and @flare-ts/lib/schema. @flare-ts/core does not re-export any of these — even in a Flare app, import them from this package.

SymbolSummary
schemaCreates a schema token. Overloads: object descriptor, top-level array [ItemSchema], top-level record [{ $record: ValueSchema }], and discriminated union (discriminant, branches).
modelCreates an extendable schema-token class. Overloads: existing SchemaToken, object descriptor, or discriminated union. For top-level array or record shapes, build schema([...]) or schema([{ $record: ... }]) first, then pass that token to model(existingToken).

Schema tokens (from schema() or model()) expose:

  • safeParse(raw: ArrayBuffer | string | JsonValue): SafeParseResult<T>: on success you get typed data; on failure you get { success: false, error: SchemaError }. The call never throws.
  • optional(): SchemaToken<T>: marks this token optional when nested in a parent descriptor (see Optional nested schemas on the Schema page).
SymbolSummary
strString coercer; .min, .max, .pattern
textString coercer with full JSON escaping on serialize (multiline / untrusted text)
intInteger coercer; .min, .max
floatNumber coercer; .min, .max
boolBoolean coercer
uuidUUID v4 string coercer
dateDate coercer; .format("ISO" | "YMD" | "DMY" | "MDY" | "TIMESTAMP")
enumsLiteral union from a readonly tuple of allowed strings
arrayComma-separated or JSON-array coercer for a nested primitive or schema inner type
optionalWraps a primitive so absent, empty, or null input becomes undefined
defaultToWraps a primitive with a fallback when absent, empty, or null; signature (fallback, primitive)

Use optional(innerPrimitive) on primitive fields. Use .optional() on a nested SchemaToken when the whole nested object may be absent. See Combinators for examples and edge cases.

SymbolSummary
compileSerializer(token: OpaqueSchemaToken) => Serializer: returns a function that turns a matching JsonValue into a JSON string
toJsonSchema(token: OpaqueSchemaToken) => object: returns a JSON Schema description of the token
SymbolSummary
ModelTokenBuilderExtendable constructor intersected with SchemaToken<T>; return type of model()
SchemaTokenParsed schema with safeParse and optional()
OpaqueSchemaTokenSchema identity without output type (serializer / JSON Schema input)
DescriptorValueField value in a descriptor: TypedPrimitive<T> or SchemaToken<T>
DiscriminantValuesUnion of discriminant literal values for discriminated schemas
BranchShapeBranch field shape with discriminant key omitted
SafeParseResult{ success: true; data: T } | { success: false; error: SchemaError }
SchemaError{ fields: FieldError[] }
FieldError{ path, message, received } per failed field
JsonValueAny JSON-serializable value
JsonObject{ [key: string]: JsonValue }
Serializer(doc: JsonValue) => string
PrimitiveOpaque primitive coercer reference
TypedPrimitiveCallable primitive with output type (v: string) => T
ArrayTypedPrimitiveArray-accepting primitive coercer
PrimitiveJsonSchemaJSON Schema fragment attached to a primitive

@flare-ts/core owns Flare-specific composition (FlareHost, FlareService, ControllerBase, flareContract, flareConfig, …) and nothing else. Anything that describes a schema or model — primitives, combinators, schema(), model(), serializers, and the related types — is imported from @flare-ts/lib/schema (or the equivalent default entry @flare-ts/lib):

import { ControllerBase, flareContract } from "@flare-ts/core";
import {
model,
str,
int,
optional,
schema,
compileSerializer,
} from "@flare-ts/lib/schema";

This stays true whether the consumer is a Flare host, a standalone CLI, or a queue worker — there is exactly one place to import schema and model symbols from.