Function: createUnsafeFirstModel()
createUnsafeFirstModel<
S
,E
>(schema
,extend?
): { [KeyType in string | number | symbol]: ({ [Key in keyof UnsafeFirstModel<S> as Key extends keyof E ? never : Key]: UnsafeFirstModel<S>[Key] } & E)[KeyType] }
Defined in: packages/model/src/safe.ts:111
Creates a model where unsafe validation is the default behavior, with safe versions available as alternatives. The from
and cast
methods throw errors, while safeFrom
and safeCast
return Results.
Type Parameters
S
S
extends StandardSchemaV1
<unknown
, unknown
>
Type of the Standard Schema compatible validation schema
E
E
extends Record
<PropertyKey
, unknown
> = { }
Parameters
schema
S
A validation schema that follows the Standard Schema interface
extend?
E
Returns
{ [KeyType in string | number | symbol]: ({ [Key in keyof UnsafeFirstModel<S> as Key extends keyof E ? never : Key]: UnsafeFirstModel<S>[Key] } & E)[KeyType] }
A model with unsafe methods as default and safe methods as alternatives
Example
import { z } from 'zod';
const User = createUnsafeFirstModel(z.object({
name: z.string().min(1),
email: z.string().email(),
}));
const value = User.from({ name: 'John', email: '[email protected]' });
// User (may throw ModelValidationError)
const result = User.safeFrom({ name: 'John', email: '[email protected]' });
// Result<User, ModelValidationError>