Skip to content

Home > llm-ops > TypeChatJsonValidator

TypeChatJsonValidator interface

Represents an object that can validate JSON strings according to a given TypeScript schema.

Signature:

typescript
export interface TypeChatJsonValidator<T extends object>
export interface TypeChatJsonValidator<T extends object>

Properties

PropertyModifiersTypeDescription
schemastringA string containing TypeScript source code for the validation schema.
stripNullsbooleanA boolean indicating whether to delete properties with null values from JSON objects. Some language models (e.g. gpt-3.5-turbo) have a tendency to assign null values to optional properties instead of omitting them. The default for this property is false, but an application can set the property to true for schemas that don't permit null values.
typeNamestringA string containing the JSON object target type name in the schema.

Methods

MethodDescription
createModuleTextFromJson(jsonObject)Transform JSON into TypeScript code for validation. Returns a Success<string> object if the conversion is successful, or an Error object if the JSON can't be transformed. The returned TypeScript source code is expected to be an ECMAScript module that imports one or more types from "./schema" and combines those types and a representation of the JSON object in a manner suitable for type-checking by the TypeScript compiler.
validate(jsonText)Parses and validates the given JSON string according to the associated TypeScript schema. Returns a Success<T> object containing the parsed JSON object if validation was successful. Otherwise, returns an Error object with a message property that contains the TypeScript compiler diagnostics.

Released under the MIT License.