Interface: UnsafeFirstModel<S>
Defined in: packages/model/src/types.ts:405
Type Parameters
S
S
extends StandardSchemaV1
Properties
cast()
readonly
cast: (value
) =>InferOutput
<S
>
Defined in: packages/model/src/types.ts:499
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
const User = createUnsafeFirstModel(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:470
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
const User = createUnsafeFirstModel(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:441
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
const User = createUnsafeFirstModel(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;
}
safeCast()
readonly
safeCast: (value
) =>Result
<InferOutput
<S
>,ModelValidationError
>
Defined in: packages/model/src/types.ts:551
Safely validates and converts unknown data to the model's output type. Returns a Result instead of throwing errors on validation failure.
Parameters
value
unknown
Any value that should be validated against the model schema
Returns
Result
<InferOutput
<S
>, ModelValidationError
>
Result containing either the validated data or validation error
Example
const User = createUnsafeFirstModel(z.object({
name: z.string().min(1),
email: z.string().email(),
}));
const okResult = User.safeCast({ name: 'John', email: '[email protected]' });
// Type: Result<User, ModelValidationError>
// Value: { success: true, data: { name: 'John', email: '[email protected]' } }
const errResult = User.safeCast('invalid data');
// Type: Result<User, ModelValidationError>
// Value: { success: false, error: ModelValidationError }
safeFrom()
readonly
safeFrom: (value
) =>Result
<InferOutput
<S
>,ModelValidationError
>
Defined in: packages/model/src/types.ts:524
Safely validates and converts data that matches the model's input type to the model's output type. Returns a Result instead of throwing errors on validation failure.
Parameters
value
InferInput
<S
>
Data matching the model's input type
Returns
Result
<InferOutput
<S
>, ModelValidationError
>
Result containing either the validated data or validation error
Example
const User = createUnsafeFirstModel(z.object({
name: z.string().min(1),
email: z.string().email(),
}));
const okResult = User.safeFrom({ name: 'John', email: '[email protected]' });
// Type: Result<User, ModelValidationError>
// Value: { success: true, data: { name: 'John', email: '[email protected]' } }
const errResult = User.safeFrom({ name: '', email: 'invalid' });
// Type: Result<User, ModelValidationError>
// Value: { success: false, error: ModelValidationError }
schema
readonly
schema:S
Defined in: packages/model/src/types.ts:422
The underlying validation schema used by the model.
Example
const Todo = createUnsafeFirstModel(z.object({
name: z.string(),
completed: z.boolean()
}));
const TodoList = createUnsafeFirstModel(z.object({
name: z.string(),
todos: z.array(Todo.schema)
}));