Skip to content

Function: createSafeFirstModel()

createSafeFirstModel<S, E>(schema, extend?): { [KeyType in string | number | symbol]: ({ [Key in keyof SafeFirstModel<S> as Key extends keyof E ? never : Key]: SafeFirstModel<S>[Key] } & E)[KeyType] }

Defined in: packages/model/src/safe.ts:74

Creates a model where safe validation is the default behavior, with unsafe versions available as fallbacks. The from and cast methods return Results, while unsafeFrom and unsafeCast throw errors.

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 SafeFirstModel<S> as Key extends keyof E ? never : Key]: SafeFirstModel<S>[Key] } & E)[KeyType] }

A model with safe methods as default and unsafe methods as alternatives

Example

typescript
import { z } from 'zod';

const User = createSafeFirstModel(z.object({
  name: z.string().min(1),
  email: z.string().email(),
}));

const result = User.from({ name: 'John', email: '[email protected]' });
// Result<User, ModelValidationError>

const value = User.unsafeFrom({ name: 'John', email: '[email protected]' });
// User (may throw ModelValidationError)