Interface: SafeModel<S>
Defined in: packages/model/src/types.ts:165
Type Parameters
S
S
extends StandardSchemaV1
Properties
cast()
readonly
cast: (value
) =>Result
<InferOutput
<S
>,ModelValidationError
>
Defined in: packages/model/src/types.ts:253
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 = createSafeModel(z.object({
name: z.string().min(1),
email: z.string().email(),
}));
const okResult = User.cast({ name: 'John', email: '[email protected]' });
// Type: Result<User, ModelValidationError>
// Value: { success: true, data: { name: 'John', email: '[email protected]' } }
const errResult = User.cast('invalid data');
// Type: Result<User, ModelValidationError>
// Value: { success: false, error: ModelValidationError }
from()
readonly
from: (value
) =>Result
<InferOutput
<S
>,ModelValidationError
>
Defined in: packages/model/src/types.ts:226
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 = createSafeModel(z.object({
name: z.string().min(1),
email: z.string().email(),
}));
const okResult = User.from({ name: 'John', email: '[email protected]' });
// Type: Result<User, ModelValidationError>
// Value: { success: true, data: { name: 'John', email: '[email protected]' } }
const errResult = User.from({ name: '', email: 'invalid' });
// Type: Result<User, ModelValidationError>
// Value: { success: false, error: ModelValidationError }
is()
readonly
is: (value
) =>value is InferOutput<S>
Defined in: packages/model/src/types.ts:201
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 = createSafeModel(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:182
The underlying validation schema used by the model.
Example
const Todo = createSafeModel(z.object({
name: z.string(),
completed: z.boolean()
}));
const TodoList = createSafeModel(z.object({
name: z.string(),
todos: z.array(Todo.schema)
}));