Skip to content

Interface: Model<S>

Defined in: packages/model/src/types.ts:68

Type Parameters

S

S extends StandardSchemaV1

Properties

cast()

readonly cast: (value) => InferOutput<S>

Defined in: packages/model/src/types.ts:162

Validates and converts unknown data to the model's output type.

Parameters

value

unknown

Any value that should be validated against the model schema

Returns

InferOutput<S>

The validated and potentially transformed data

Throws

When the input data doesn't match the schema

Example

typescript
const User = createModel(z.object({
  name: z.string().min(1).trim(),
  email: z.string().email().trim().toLowerCase(),
}));

User.cast({ name: 'John Doe ', email: '[email protected]' });
// ✅ Ok: returns { name: 'John Doe', email: '[email protected]' }

User.cast({ name: '', email: '' });
// ❌ Runtime Error: validation failed

User.cast({ name: 'John' });
// ❌ Runtime Error: missing properties

User.cast('string');
// ❌ Runtime Error: wrong type

from()

readonly from: (value) => InferOutput<S>

Defined in: packages/model/src/types.ts:133

Validates and converts data that matches the model's input type to the model's output type.

Parameters

value

InferInput<S>

Data matching the model's input type

Returns

InferOutput<S>

The validated and potentially transformed data

Throws

When the input data doesn't match the schema

Example

typescript
const User = createModel(z.object({
  name: z.string().min(1).trim(),
  email: z.string().email().trim().toLowerCase(),
}));

User.from({ name: 'John Doe ', email: '[email protected]' });
// ✅ Ok: returns { name: 'John Doe', email: '[email protected]' }

User.from({ name: '', email: '' });
// ❌ Runtime Error: validation failed

User.from({ name: 'John' });
// ❌ TypeScript Error: missing properties

User.from('string');
// ❌ TypeScript Error: wrong type

is()

readonly is: (value) => value is InferOutput<S>

Defined in: packages/model/src/types.ts:104

Type guard that checks if a value matches the model's schema.

Parameters

value

unknown

The value to check against the model schema

Returns

value is InferOutput<S>

true if the value is valid according to the schema, false otherwise

Example

typescript
const User = createModel(z.object({ name: z.string() }));

async function fetchUser(data: unknown) {
  const response = await fetch('/api/user').then(res => res.json());

  return User.is(response) ? response : null;
}

schema

readonly schema: S

Defined in: packages/model/src/types.ts:85

The underlying validation schema used by the model.

Example

typescript
const Todo = createModel(z.object({
  name: z.string(),
  completed: z.boolean()
}));

const TodoList = createModel(z.object({
  name: z.string(),
  todos: z.array(Todo.schema)
}));